diff --git a/src/engine/runtime.js b/src/engine/runtime.js index e3072a9ed..317cf2bd9 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -23,6 +23,12 @@ function Runtime () { */ this.blocks = {}; + /** + * Primitive-accessible execution metadata for each block. + * @type {Object.} + */ + this.blockExecutionData = {}; + /** * All stacks in the workspace. * A list of block IDs that represent stacks (first block in stack). @@ -90,6 +96,7 @@ Runtime.THREAD_STEP_INTERVAL = 1000 / 60; Runtime.prototype.createBlock = function (block, opt_isFlyoutBlock) { // Create new block this.blocks[block.id] = block; + this.blockExecutionData[block.id] = {}; // Walk each field and add any shadow blocks // @todo Expand this to cover vertical / nested blocks @@ -98,6 +105,7 @@ Runtime.prototype.createBlock = function (block, opt_isFlyoutBlock) { for (var y in shadows) { var shadow = shadows[y]; this.blocks[shadow.id] = shadow; + this.blockExecutionData[shadow.id] = {}; } } @@ -194,6 +202,7 @@ Runtime.prototype.deleteBlock = function (e) { // Delete block delete this.blocks[e.id]; + delete this.blockExecutionData[e.id]; }; // ----------------------------------------------------------------------------- @@ -377,4 +386,24 @@ Runtime.prototype._getOpcode = function (id) { return this.blocks[id].opcode; }; +/** + * Set block execution data + * @param {!string} id Block ID + * @param {!Any} key Data key + * @param {?Any} value Data value + */ +Runtime.prototype.setBlockExecutionData = function (id, key, value) { + this.blockExecutionData[id][key] = value; +}; + +/** + * Get block execution data + * @param {!string} id Block ID + * @param {!Any} key Data key + * @return {?Any} Data value + */ +Runtime.prototype.getBlockExecutionData = function (id, key) { + return this.blockExecutionData[id][key]; +}; + module.exports = Runtime; diff --git a/src/engine/sequencer.js b/src/engine/sequencer.js index f7d78352d..3e4147d89 100644 --- a/src/engine/sequencer.js +++ b/src/engine/sequencer.js @@ -110,6 +110,22 @@ Sequencer.prototype.stepThread = function (thread) { instance.runtime.glowBlock(currentBlock, false); }; + /** + * A callback for the primitive to set data on the block level. + * @type {Function} + */ + var blockDataSetCallback = function (key, value) { + instance.runtime.setBlockExecutionData(currentBlock, key, value); + }; + + /** + * A callback for the primitive to get data on the block level. + * @type {Function} + */ + var blockDataGetCallback = function (key) { + return instance.runtime.getBlockExecutionData(currentBlock, key); + }; + // @todo var argValues = []; @@ -128,7 +144,9 @@ Sequencer.prototype.stepThread = function (thread) { blockFunction(argValues, { yield: threadYieldCallback, done: threadDoneCallback, - timeout: YieldTimers.timeout + timeout: YieldTimers.timeout, + setData: blockDataSetCallback, + getData: blockDataGetCallback }); } catch(e) {