mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-22 20:19:05 -04:00
Merge pull request #1839 from mzgoddard/fix-missing-no-param
fix: initialize stack frame params for all procedures
This commit is contained in:
commit
0fa5e9181a
5 changed files with 41 additions and 21 deletions
src
test
|
@ -38,6 +38,10 @@ class Scratch3ProcedureBlocks {
|
||||||
|
|
||||||
const [paramNames, paramIds, paramDefaults] = paramNamesIdsAndDefaults;
|
const [paramNames, paramIds, paramDefaults] = paramNamesIdsAndDefaults;
|
||||||
|
|
||||||
|
// Initialize params for the current stackFrame to {}, even if the procedure does
|
||||||
|
// not take any arguments. This is so that `getParam` down the line does not look
|
||||||
|
// at earlier stack frames for the values of a given parameter (#1729)
|
||||||
|
util.initParams();
|
||||||
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]]);
|
||||||
|
|
|
@ -170,6 +170,13 @@ class BlockUtility {
|
||||||
return this.thread.target.blocks.getProcedureParamNamesIdsAndDefaults(procedureCode);
|
return this.thread.target.blocks.getProcedureParamNamesIdsAndDefaults(procedureCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize procedure parameters in the thread before pushing parameters.
|
||||||
|
*/
|
||||||
|
initParams () {
|
||||||
|
this.thread.initParams();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
|
|
@ -321,6 +321,16 @@ class Thread {
|
||||||
this.justReported = typeof value === 'undefined' ? null : value;
|
this.justReported = typeof value === 'undefined' ? null : value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize procedure parameters on this stack frame.
|
||||||
|
*/
|
||||||
|
initParams () {
|
||||||
|
const stackFrame = this.peekStackFrame();
|
||||||
|
if (stackFrame.params === null) {
|
||||||
|
stackFrame.params = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a parameter to the stack frame.
|
* Add a parameter to the stack frame.
|
||||||
* Use when calling a procedure with parameter values.
|
* Use when calling a procedure with parameter values.
|
||||||
|
@ -329,9 +339,6 @@ class Thread {
|
||||||
*/
|
*/
|
||||||
pushParam (paramName, value) {
|
pushParam (paramName, value) {
|
||||||
const stackFrame = this.peekStackFrame();
|
const stackFrame = this.peekStackFrame();
|
||||||
if (stackFrame.params === null) {
|
|
||||||
stackFrame.params = {};
|
|
||||||
}
|
|
||||||
stackFrame.params[paramName] = value;
|
stackFrame.params[paramName] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
BIN
test/fixtures/execute/procedures-nested-missing-no-param.sb2
vendored
Normal file
BIN
test/fixtures/execute/procedures-nested-missing-no-param.sb2
vendored
Normal file
Binary file not shown.
|
@ -17,6 +17,7 @@ test('spec', t => {
|
||||||
t.type(th.peekStackFrame, 'function');
|
t.type(th.peekStackFrame, 'function');
|
||||||
t.type(th.peekParentStackFrame, 'function');
|
t.type(th.peekParentStackFrame, 'function');
|
||||||
t.type(th.pushReportedValue, 'function');
|
t.type(th.pushReportedValue, 'function');
|
||||||
|
t.type(th.initParams, 'function');
|
||||||
t.type(th.pushParam, 'function');
|
t.type(th.pushParam, 'function');
|
||||||
t.type(th.peekStack, 'function');
|
t.type(th.peekStack, 'function');
|
||||||
t.type(th.getParam, 'function');
|
t.type(th.getParam, 'function');
|
||||||
|
@ -107,6 +108,7 @@ test('peekStack', t => {
|
||||||
test('PushGetParam', t => {
|
test('PushGetParam', t => {
|
||||||
const th = new Thread('arbitraryString');
|
const th = new Thread('arbitraryString');
|
||||||
th.pushStack('arbitraryString');
|
th.pushStack('arbitraryString');
|
||||||
|
th.initParams();
|
||||||
th.pushParam('testParam', 'testValue');
|
th.pushParam('testParam', 'testValue');
|
||||||
t.strictEquals(th.peekStackFrame().params.testParam, 'testValue');
|
t.strictEquals(th.peekStackFrame().params.testParam, 'testValue');
|
||||||
t.strictEquals(th.getParam('testParam'), 'testValue');
|
t.strictEquals(th.getParam('testParam'), 'testValue');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue