diff --git a/src/blocks/scratch3_event.js b/src/blocks/scratch3_event.js index 8687141cd..1bf798f42 100644 --- a/src/blocks/scratch3_event.js +++ b/src/blocks/scratch3_event.js @@ -56,14 +56,20 @@ class Scratch3EventBlocks { } broadcast (args, util) { - const broadcastOption = Cast.toString(args.BROADCAST_OPTION); + // TODO KARISHMA adding lookupOrCreate here temporarily (LLK/scratch-blocks#1258) + util.target.lookupOrCreateBroadcastMsg(args.BROADCAST_OPTION.id, + args.BROADCAST_OPTION.name); + const broadcastOption = Cast.toString(args.BROADCAST_OPTION.name); util.startHats('event_whenbroadcastreceived', { BROADCAST_OPTION: broadcastOption }); } broadcastAndWait (args, util) { - const broadcastOption = Cast.toString(args.BROADCAST_OPTION); + // TODO KARISHMA adding lookupOrCreate here temporarily (LLK/scratch-blocks#1258) + util.target.lookupOrCreateBroadcastMsg(args.BROADCAST_OPTION.id, + args.BROADCAST_OPTION.name); + const broadcastOption = Cast.toString(args.BROADCAST_OPTION.name); // Have we run before, starting threads? if (!util.stackFrame.startedThreads) { // No - start hats for this broadcast. diff --git a/src/engine/blocks.js b/src/engine/blocks.js index 8776c6f6c..9117e991a 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -365,7 +365,8 @@ class Blocks { case 'field': // Update block value if (!block.fields[args.name]) return; - if (args.name === 'VARIABLE' || args.name === 'LIST') { + if (args.name === 'VARIABLE' || args.name === 'LIST' || + args.name === 'BROADCAST_OPTION') { // Get variable name using the id in args.value. const variable = optRuntime.getEditingTarget().lookupVariableById(args.value); if (variable) { diff --git a/src/engine/execute.js b/src/engine/execute.js index f5acf909e..3d2e5aa6d 100644 --- a/src/engine/execute.js +++ b/src/engine/execute.js @@ -171,6 +171,19 @@ const execute = function (sequencer, thread) { if (!fields.hasOwnProperty(fieldName)) continue; if (fieldName === 'VARIABLE' || fieldName === 'LIST') { argValues[fieldName] = fields[fieldName].id; + } else if (fieldName === 'BROADCAST_OPTION') { + // TODO KARISHMA (scratch-blocks #1258) this will change when we + // upstream blockly issue #1395 is fixed. + + // we need both the id and the name here, because + // this object eventually gets passed to scratch3_event.broadcast + // which will create the message for the first time if one with the + // given id doesn't exist + // (e.g. default broadcast message, 'message1') + argValues[fieldName] = { + id: fields[fieldName].id, + name: fields[fieldName].value + }; } else { argValues[fieldName] = fields[fieldName].value; } diff --git a/src/engine/target.js b/src/engine/target.js index 6a5c8d129..9a6ad7286 100644 --- a/src/engine/target.js +++ b/src/engine/target.js @@ -92,6 +92,23 @@ class Target extends EventEmitter { return newVariable; } + /** + * Look up a broadcast message object, and create it if one doesn't exist. + * @param {string} id Id of the variable. + * @param {string} name Name of the variable. + * @return {!Variable} Variable object. + */ + lookupOrCreateBroadcastMsg (id, name) { + debugger; + const broadcastMsg = this.lookupVariableById(id); + if (broadcastMsg) return broadcastMsg; + // No variable with this name exists - create it locally. + const newBroadcastMsg = new Variable(id, name, + Variable.BROADCAST_MESSAGE_TYPE, false); + this.variables[id] = newBroadcastMsg; + return newBroadcastMsg; + } + /** * Look up a variable object. * Search begins for local variables; then look for globals. @@ -134,7 +151,7 @@ class Target extends EventEmitter { * dictionary of variables. * @param {string} id Id of variable * @param {string} name Name of variable. - * @param {string} type Type of variable, '' or 'list' + * @param {string} type Type of variable, '', 'broadcast_msg', or 'list' */ createVariable (id, name, type) { if (!this.variables.hasOwnProperty(id)) { diff --git a/src/engine/variable.js b/src/engine/variable.js index d75c0b826..4aad9ffc2 100644 --- a/src/engine/variable.js +++ b/src/engine/variable.js @@ -25,6 +25,9 @@ class Variable { case Variable.LIST_TYPE: this.value = []; break; + case Variable.BROADCAST_MESSAGE_TYPE: + this.value = this.name; + break; default: throw new Error(`Invalid variable type: ${this.type}`); } @@ -51,6 +54,14 @@ class Variable { static get LIST_TYPE () { return 'list'; } + + /** + * Type representation for list variables. + * @const {string} + */ + static get BROADCAST_MESSAGE_TYPE () { + return 'broadcast_msg'; + } } module.exports = Variable;