Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Procedures: Substacks (VM) #65

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/blocks/scratch3_procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
procedures_definition: this.definition,
procedures_call: this.call,
procedures_return: this.return,
argument_statement: this.argumentStatement,
argument_reporter_string_number: this.argumentReporterStringNumber,
argument_reporter_boolean: this.argumentReporterBoolean
};
Expand Down Expand Up @@ -65,6 +66,11 @@
for (let i = 0; i < paramIds.length; i++) {
if (Object.prototype.hasOwnProperty.call(args, paramIds[i])) {
util.pushParam(paramNames[i], args[paramIds[i]]);
} else if (paramIds[i].startsWith("SUBSTACK")) {

Check failure on line 69 in src/blocks/scratch3_procedures.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote

Check failure on line 69 in src/blocks/scratch3_procedures.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
util.pushParam(paramNames[i], {
blockId: util.thread.peekStackFrame().op.id,
fieldId: paramIds[i]
});
} else {
util.pushParam(paramNames[i], paramDefaults[i]);
}
Expand Down Expand Up @@ -95,6 +101,7 @@

return (args, util) {
util.stopThisScript();

// If used outside of a custom block, there may be no stackframe.
if (util.thread.peekStackFrame()) {
util.stackFrame.returnValue = args.VALUE;
Expand Down Expand Up @@ -132,6 +139,22 @@
}
return value;
}

argumentStatement (args, util) {
const branchInfo = util.getParam(args.VALUE) || {};
if (!branchInfo.fieldId) return;

const blockId = branchInfo.blockId;
const block = util.target.blocks.getBlock(blockId);
if (!block) return;

const branch = block.inputs[branchInfo.fieldId];
if (!branch) return;
const branchId = branch.block;

// todo: should we wait for the branch to finish?
util.thread.pushStack(branchId);
}
}

module.exports = Scratch3ProcedureBlocks;
24 changes: 23 additions & 1 deletion src/compiler/irgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,20 @@ class ScriptTreeGenerator {
*/
descendStackedBlock (block) {
switch (block.opcode) {
case 'argument_statement': {
const name = block.fields.VALUE.value;
const index = this.script.arguments.lastIndexOf(name);
if (index === -1) {
return {
kind: 'noop'
};
}
this.script.yields = true;
return {
kind: 'procedures.statement',
name: name
};
}
case 'control_all_at_once':
// In Unsandboxed, attempts to run the script in 1 frame.
return {
Expand Down Expand Up @@ -1477,9 +1491,16 @@ class ScriptTreeGenerator {
}
}

const substacks = {};
const args = [];
for (let i = 0; i < paramIds.length; i++) {
let value;

if (paramIds[i].startsWith('SUBSTACK')) {
substacks[paramNames[i]] = this.descendSubstack(block, paramIds[i]);
continue;
}

if (block.inputs[paramIds[i]] && block.inputs[paramIds[i]].block) {
value = this.descendInputOfBlock(block, paramIds[i]);
} else {
Expand All @@ -1495,7 +1516,8 @@ class ScriptTreeGenerator {
kind: 'procedures.call',
code: procedureCode,
variant,
arguments: args
arguments: args,
substacks
};
}

Expand Down
12 changes: 12 additions & 0 deletions src/compiler/jsgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ class JSGenerator {
const procedureCode = node.code;
const procedureVariant = node.variant;
const procedureData = this.ir.procedures[procedureVariant];

if (procedureData.stack === null) {
// TODO still need to evaluate arguments for side effects
return new TypedInput('""', TYPE_STRING);
Expand Down Expand Up @@ -1227,11 +1228,14 @@ class JSGenerator {
const procedureCode = node.code;
const procedureVariant = node.variant;
const procedureData = this.ir.procedures[procedureVariant];
this.substacks = node.substacks;
if (procedureData.stack === null) {
// TODO still need to evaluate arguments
break;
}

this.ir.procedures[procedureVariant].substacks = node.substacks;

const yieldForRecursion = !this.isWarp && procedureCode === this.script.procedureCode;
if (yieldForRecursion) {
this.yieldNotWarp();
Expand All @@ -1254,7 +1258,15 @@ class JSGenerator {
case 'procedures.return':
this.stopScriptAndReturn(this.descendInput(node.value).asSafe());
break;
case 'procedures.statement': {
const procedureVariant = this.script.procedureVariant;
const procedureData = this.ir.procedures[procedureVariant];
const substacks = procedureData.substacks;

this.descendStack(substacks[node.name], new Frame(false));

break;
}
case 'timer.reset':
this.source += 'runtime.ioDevices.clock.resetProjectTimer();\n';
break;
Expand Down
Loading