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) {
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]);
}
}

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.
* @return {Array.<string>} 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.<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.
* @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.
* @return {?Array.<string>} 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.<string>} 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];
}
}

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 = {
getProcedureParamNamesAndIds: () => null,
getProcedureParamNamesIdsAndDefaults: () => null,
stackFrame: {
executed: false
}