Skip to content

Commit

Permalink
Revert "Update behavior of non-existant procedures in compiler to mat…
Browse files Browse the repository at this point in the history
…ch interpreter"

This reverts commit b04df3e.

There are still subtle issues related to execution order (JS is left-to-right while
Scratch is depth-first). As there will inherently be some issues, we may as well
do what runs faster.
  • Loading branch information
GarboMuffin committed Jul 23, 2023
1 parent f42b260 commit f0bb852
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 48 deletions.
63 changes: 34 additions & 29 deletions src/compiler/irgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -1291,43 +1291,33 @@ class ScriptTreeGenerator {
const procedureCode = block.mutation.proccode;
const paramNamesIdsAndDefaults = this.blocks.getProcedureParamNamesIdsAndDefaults(procedureCode);
if (paramNamesIdsAndDefaults === null) {
// Still need to evaluate arguments in case of side effects
const argumentList = [];
for (const inputName of Object.keys(block.inputs)) {
argumentList.push(this.descendInputOfBlock(block, inputName));
}
return {
kind: 'procedures.discard',
values: argumentList
kind: 'noop'
};
}

const [paramNames, paramIds, paramDefaults] = paramNamesIdsAndDefaults;
const argumentList = [];
for (let i = 0; i < paramIds.length; i++) {
let value;
if (block.inputs[paramIds[i]] && block.inputs[paramIds[i]].block) {
value = this.descendInputOfBlock(block, paramIds[i]);
} else {
value = {
kind: 'constant',
value: paramDefaults[i]
};
}
argumentList.push(value);
}

const addonBlock = this.runtime.getAddonBlock(procedureCode);
if (addonBlock) {
this.script.yields = true;
const argumentNameMap = {};
for (let i = 0; i < argumentList.length; i++) {
argumentNameMap[paramNames[i]] = argumentList[i];
const args = {};
for (let i = 0; i < paramIds.length; i++) {
let value;
if (block.inputs[paramIds[i]] && block.inputs[paramIds[i]].block) {
value = this.descendInputOfBlock(block, paramIds[i]);
} else {
value = {
kind: 'constant',
value: paramDefaults[i]
};
}
args[paramNames[i]] = value;
}
return {
kind: 'addons.call',
code: procedureCode,
arguments: argumentNameMap,
arguments: args,
blockId: block.id
};
}
Expand All @@ -1336,8 +1326,7 @@ class ScriptTreeGenerator {
const definitionBlock = this.blocks.getBlock(definitionId);
if (!definitionBlock) {
return {
kind: 'procedures.discard',
values: argumentList
kind: 'noop'
};
}
const innerDefinition = this.blocks.getBlock(definitionBlock.inputs.custom_block.block);
Expand All @@ -1361,15 +1350,31 @@ class ScriptTreeGenerator {
}

// Non-warp direct recursion yields.
if (!this.script.isWarp && procedureCode === this.script.procedureCode) {
this.script.yields = true;
if (!this.script.isWarp) {
if (procedureCode === this.script.procedureCode) {
this.script.yields = true;
}
}

const args = [];
for (let i = 0; i < paramIds.length; i++) {
let value;
if (block.inputs[paramIds[i]] && block.inputs[paramIds[i]].block) {
value = this.descendInputOfBlock(block, paramIds[i]);
} else {
value = {
kind: 'constant',
value: paramDefaults[i]
};
}
args.push(value);
}

return {
kind: 'procedures.call',
code: procedureCode,
variant,
arguments: argumentList
arguments: args
};
}

Expand Down
19 changes: 0 additions & 19 deletions src/compiler/jsgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,17 +653,6 @@ class JSGenerator {
}
return new TypedInput(`${procedureReference}(${joinedArgs})`, TYPE_UNKNOWN);
}
case 'procedures.discard': {
const args = [];
for (const value of node.values) {
args.push(this.descendInput(value).asUnknown());
}
if (args.length > 0) {
// We can use the comma operator to discard the values.
return new TypedInput(`(${args.join(',')},"")`, TYPE_STRING);
}
return new TypedInput('""', TYPE_STRING);
}

case 'sensing.answer':
return new TypedInput(`runtime.ext_scratch3_sensing._answer`, TYPE_STRING);
Expand Down Expand Up @@ -1079,14 +1068,6 @@ class JSGenerator {
this.resetVariableInputs();
break;
}
case 'procedures.discard': {
for (const value of node.values) {
const input = this.descendInput(value).asUnknown();
// Don't need to use void, but it makes it explicit that we do not care about the result.
this.source += `void ${input};\n`;
}
break;
}
case 'procedures.return':
this.stopScriptAndReturn(this.descendInput(node.value).asSafe());
break;
Expand Down

0 comments on commit f0bb852

Please sign in to comment.