diff --git a/src/blocks/scratch3_procedures.js b/src/blocks/scratch3_procedures.js index 7fb31d28f..5caea72ee 100644 --- a/src/blocks/scratch3_procedures.js +++ b/src/blocks/scratch3_procedures.js @@ -27,20 +27,22 @@ class Scratch3ProcedureBlocks { call (args, util) { if (!util.stackFrame.executed) { const procedureCode = args.mutation.proccode; - const paramNamesAndIds = util.getProcedureParamNamesAndIds(procedureCode); + const paramNamesIdsAndDefaults = util.getProcedureParamNamesIdsAndDefaults(procedureCode); // If null, procedure could not be found, which can happen if custom // block is dragged between sprites without the definition. // Match Scratch 2.0 behavior and noop. - if (paramNamesAndIds === null) { + if (paramNamesIdsAndDefaults === null) { return; } - const [paramNames, paramIds] = paramNamesAndIds; + const [paramNames, paramIds, paramDefaults] = paramNamesIdsAndDefaults; for (let i = 0; i < paramIds.length; i++) { if (args.hasOwnProperty(paramIds[i])) { util.pushParam(paramNames[i], args[paramIds[i]]); + } else { + util.pushParam(paramNames[i], paramDefaults[i]); } } diff --git a/src/engine/block-utility.js b/src/engine/block-utility.js index b456d02fa..c55ba3bf3 100644 --- a/src/engine/block-utility.js +++ b/src/engine/block-utility.js @@ -104,7 +104,7 @@ class BlockUtility { } /** - * Get names for parameters for the given procedure. + * Get names and ids of parameters for the given procedure. * @param {string} procedureCode Procedure code for procedure to query. * @return {Array.} List of param names for a procedure. */ @@ -112,6 +112,15 @@ class BlockUtility { return this.thread.target.blocks.getProcedureParamNamesAndIds(procedureCode); } + /** + * Get names, ids, and defaults of parameters for the given procedure. + * @param {string} procedureCode Procedure code for procedure to query. + * @return {Array.} List of param names for a procedure. + */ + getProcedureParamNamesIdsAndDefaults (procedureCode) { + return this.thread.target.blocks.getProcedureParamNamesIdsAndDefaults(procedureCode); + } + /** * Store a procedure parameter value by its name. * @param {string} paramName The procedure's parameter name. diff --git a/src/engine/blocks.js b/src/engine/blocks.js index 61493f040..28ac3184f 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -230,11 +230,20 @@ class Blocks { } /** - * Get names of parameters for the given procedure. + * Get names and ids of parameters for the given procedure. * @param {?string} name Name of procedure to query. * @return {?Array.} List of param names for a procedure. */ getProcedureParamNamesAndIds (name) { + return this.getProcedureParamNamesIdsAndDefaults(name).slice(0, 2); + } + + /** + * Get names, ids, and defaults of parameters for the given procedure. + * @param {?string} name Name of procedure to query. + * @return {?Array.} List of param names for a procedure. + */ + getProcedureParamNamesIdsAndDefaults (name) { const cachedNames = this._cache.procedureParamNames[name]; if (typeof cachedNames !== 'undefined') { return cachedNames; @@ -247,7 +256,9 @@ class Blocks { block.mutation.proccode === name) { const names = JSON.parse(block.mutation.argumentnames); const ids = JSON.parse(block.mutation.argumentids); - this._cache.procedureParamNames[name] = [names, ids]; + const defaults = JSON.parse(block.mutation.argumentdefaults); + + this._cache.procedureParamNames[name] = [names, ids, defaults]; return this._cache.procedureParamNames[name]; } } diff --git a/test/fixtures/execute/procedures-recursive-default-boolean.sb2 b/test/fixtures/execute/procedures-recursive-default-boolean.sb2 new file mode 100644 index 000000000..b2873d2e1 Binary files /dev/null and b/test/fixtures/execute/procedures-recursive-default-boolean.sb2 differ diff --git a/test/fixtures/execute/procedures-recursive-default-number.sb2 b/test/fixtures/execute/procedures-recursive-default-number.sb2 new file mode 100644 index 000000000..cb7263488 Binary files /dev/null and b/test/fixtures/execute/procedures-recursive-default-number.sb2 differ diff --git a/test/fixtures/execute/procedures-recursive-default-string.sb2 b/test/fixtures/execute/procedures-recursive-default-string.sb2 new file mode 100644 index 000000000..bc1b4a99f Binary files /dev/null and b/test/fixtures/execute/procedures-recursive-default-string.sb2 differ diff --git a/test/unit/blocks_procedures.js b/test/unit/blocks_procedures.js index 0285b23d0..b87674bb7 100644 --- a/test/unit/blocks_procedures.js +++ b/test/unit/blocks_procedures.js @@ -16,7 +16,7 @@ test('calling a custom block with no definition does not throw', t => { } }; const util = { - getProcedureParamNamesAndIds: () => null, + getProcedureParamNamesIdsAndDefaults: () => null, stackFrame: { executed: false }