diff --git a/playground/playground.js b/playground/playground.js index 800894bb9..47631b201 100644 --- a/playground/playground.js +++ b/playground/playground.js @@ -78,6 +78,9 @@ window.onload = function() { vm.on('BLOCK_GLOW_OFF', function(data) { workspace.glowBlock(data.id, false); }); + vm.on('VISUAL_REPORT', function(data) { + workspace.reportValue(data.id, data.value); + }); // Run threads vm.start(); diff --git a/src/engine/execute.js b/src/engine/execute.js index 529990ca0..db111637d 100644 --- a/src/engine/execute.js +++ b/src/engine/execute.js @@ -96,6 +96,10 @@ var execute = function (sequencer, thread) { primitiveReportedValue.then(function(resolvedValue) { // Promise resolved: the primitive reported a value. thread.pushReportedValue(resolvedValue); + // Report the value visually if necessary. + if (thread.peekStack() === thread.topBlock) { + runtime.visualReport(thread.peekStack(), resolvedValue); + } thread.setStatus(Thread.STATUS_RUNNING); sequencer.proceedThread(thread); }, function(rejectionReason) { @@ -107,6 +111,10 @@ var execute = function (sequencer, thread) { }); } else if (thread.status === Thread.STATUS_RUNNING) { thread.pushReportedValue(primitiveReportedValue); + // Report the value visually if necessary. + if (thread.peekStack() === thread.topBlock) { + runtime.visualReport(thread.peekStack(), primitiveReportedValue); + } } }; diff --git a/src/engine/runtime.js b/src/engine/runtime.js index c51be795d..f92a91db7 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -69,6 +69,12 @@ Runtime.BLOCK_GLOW_ON = 'BLOCK_GLOW_ON'; */ Runtime.BLOCK_GLOW_OFF = 'BLOCK_GLOW_OFF'; +/** + * Event name for visual value report. + * @const {string} + */ +Runtime.VISUAL_REPORT = 'VISUAL_REPORT'; + /** * Inherit from EventEmitter */ @@ -218,6 +224,15 @@ Runtime.prototype.glowBlock = function (blockId, isGlowing) { } }; +/** + * Emit value for reporter to show in the blocks. + * @param {string} blockId ID for the block. + * @param {string} value Value to show associated with the block. + */ +Runtime.prototype.visualReport = function (blockId, value) { + this.emit(Runtime.VISUAL_REPORT, blockId, String(value)); +}; + /** * Return the Target for a particular thread. * @param {!Thread} thread Thread to determine target for. diff --git a/src/index.js b/src/index.js index 15b218071..965c8cd9a 100644 --- a/src/index.js +++ b/src/index.js @@ -53,6 +53,9 @@ function VirtualMachine () { instance.runtime.on(Runtime.BLOCK_GLOW_OFF, function (id) { instance.emit(Runtime.BLOCK_GLOW_OFF, {id: id}); }); + instance.runtime.on(Runtime.VISUAL_REPORT, function (id, value) { + instance.emit(Runtime.VISUAL_REPORT, {id: id, value: value}); + }); } /** @@ -158,6 +161,9 @@ if (ENV_WORKER) { self.vmInstance.runtime.on(Runtime.BLOCK_GLOW_OFF, function (id) { self.postMessage({method: Runtime.BLOCK_GLOW_OFF, id: id}); }); + self.vmInstance.runtime.on(Runtime.VISUAL_REPORT, function (id, value) { + self.postMessage({method: Runtime.VISUAL_REPORT, id: id, value: value}); + }); } /**