mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-22 22:12:28 -05:00
Merge pull request #1642 from mzgoddard/set-procedure-param-defaults
Set procedure param defaults
This commit is contained in:
commit
ef4045aaf1
7 changed files with 29 additions and 7 deletions
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
}
|
||||
|
|
BIN
test/fixtures/execute/procedures-recursive-default-boolean.sb2
vendored
Normal file
BIN
test/fixtures/execute/procedures-recursive-default-boolean.sb2
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/execute/procedures-recursive-default-number.sb2
vendored
Normal file
BIN
test/fixtures/execute/procedures-recursive-default-number.sb2
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/execute/procedures-recursive-default-string.sb2
vendored
Normal file
BIN
test/fixtures/execute/procedures-recursive-default-string.sb2
vendored
Normal file
Binary file not shown.
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue