Merge pull request #1642 from mzgoddard/set-procedure-param-defaults

Set procedure param defaults
This commit is contained in:
Michael "Z" Goddard 2018-10-23 15:13:16 -04:00 committed by GitHub
commit ef4045aaf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 7 deletions

View file

@ -27,20 +27,22 @@ class Scratch3ProcedureBlocks {
call (args, util) { call (args, util) {
if (!util.stackFrame.executed) { if (!util.stackFrame.executed) {
const procedureCode = args.mutation.proccode; 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 // If null, procedure could not be found, which can happen if custom
// block is dragged between sprites without the definition. // block is dragged between sprites without the definition.
// Match Scratch 2.0 behavior and noop. // Match Scratch 2.0 behavior and noop.
if (paramNamesAndIds === null) { if (paramNamesIdsAndDefaults === null) {
return; return;
} }
const [paramNames, paramIds] = paramNamesAndIds; const [paramNames, paramIds, paramDefaults] = paramNamesIdsAndDefaults;
for (let i = 0; i < paramIds.length; i++) { for (let i = 0; i < paramIds.length; i++) {
if (args.hasOwnProperty(paramIds[i])) { if (args.hasOwnProperty(paramIds[i])) {
util.pushParam(paramNames[i], args[paramIds[i]]); util.pushParam(paramNames[i], args[paramIds[i]]);
} else {
util.pushParam(paramNames[i], paramDefaults[i]);
} }
} }

View file

@ -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. * @param {string} procedureCode Procedure code for procedure to query.
* @return {Array.<string>} List of param names for a procedure. * @return {Array.<string>} List of param names for a procedure.
*/ */
@ -112,6 +112,15 @@ class BlockUtility {
return this.thread.target.blocks.getProcedureParamNamesAndIds(procedureCode); 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.<string>} List of param names for a procedure.
*/
getProcedureParamNamesIdsAndDefaults (procedureCode) {
return this.thread.target.blocks.getProcedureParamNamesIdsAndDefaults(procedureCode);
}
/** /**
* Store a procedure parameter value by its name. * Store a procedure parameter value by its name.
* @param {string} paramName The procedure's parameter name. * @param {string} paramName The procedure's parameter name.

View file

@ -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. * @param {?string} name Name of procedure to query.
* @return {?Array.<string>} List of param names for a procedure. * @return {?Array.<string>} List of param names for a procedure.
*/ */
getProcedureParamNamesAndIds (name) { 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.<string>} List of param names for a procedure.
*/
getProcedureParamNamesIdsAndDefaults (name) {
const cachedNames = this._cache.procedureParamNames[name]; const cachedNames = this._cache.procedureParamNames[name];
if (typeof cachedNames !== 'undefined') { if (typeof cachedNames !== 'undefined') {
return cachedNames; return cachedNames;
@ -247,7 +256,9 @@ class Blocks {
block.mutation.proccode === name) { block.mutation.proccode === name) {
const names = JSON.parse(block.mutation.argumentnames); const names = JSON.parse(block.mutation.argumentnames);
const ids = JSON.parse(block.mutation.argumentids); 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]; return this._cache.procedureParamNames[name];
} }
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -16,7 +16,7 @@ test('calling a custom block with no definition does not throw', t => {
} }
}; };
const util = { const util = {
getProcedureParamNamesAndIds: () => null, getProcedureParamNamesIdsAndDefaults: () => null,
stackFrame: { stackFrame: {
executed: false executed: false
} }