Skip to content
Closed
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
4 changes: 2 additions & 2 deletions src/compiler/irgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ class ScriptTreeGenerator {
const blockInfo = this.getBlockInfo(block.opcode);
if (blockInfo) {
const type = blockInfo.info.blockType;
if (type === BlockType.REPORTER || type === BlockType.BOOLEAN) {
if (type === BlockType.REPORTER || type === BlockType.BOOLEAN || type === BlockType.INLINE) {
return this.descendCompatLayer(block);
}
}
Expand Down Expand Up @@ -1416,7 +1416,7 @@ class ScriptTreeGenerator {
const blockInfo = this.getBlockInfo(block.opcode);
const blockType = (blockInfo && blockInfo.info && blockInfo.info.blockType) || BlockType.COMMAND;
const substacks = {};
if (blockType === BlockType.CONDITIONAL || blockType === BlockType.LOOP) {
if (blockType === BlockType.CONDITIONAL || blockType === BlockType.LOOP || blockType === BlockType.INLINE) {
for (const inputName in block.inputs) {
if (!inputName.startsWith('SUBSTACK')) continue;
const branchNum = inputName === 'SUBSTACK' ? 1 : +inputName.substring('SUBSTACK'.length);
Expand Down
34 changes: 32 additions & 2 deletions src/compiler/jsgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,29 @@ class JSGenerator {
return new TypedInput(`p${node.index}`, TYPE_UNKNOWN);

case 'compat':
if (node.blockType === BlockType.INLINE) {

This comment was marked as abuse.

const branchVariable = this.localVariables.next();
const returnVariable = this.localVariables.next();
let source = '(yield* (function*() {\n';
source += `let ${returnVariable} = undefined;\n`;
source += `const ${branchVariable} = createBranchInfo(false);\n`;
source += `${returnVariable} = (${this.generateCompatibilityLayerCall(node, false, branchVariable)});\n`;
source += `${branchVariable}.branch = globalState.blockUtility._startedBranch[0];\n`;
source += `switch (${branchVariable}.branch) {\n`;
for (const index in node.substacks) {
source += `case ${+index}: {\n`;
source += this.descendStackForSource(node.substacks[index], new Frame(false));
source += `break;\n`;
source += `}\n`; // close case
}
source += '}\n'; // close switch
source += `return ${returnVariable};\n`;
source += '})())'; // close function and yield
return new TypedInput(source, TYPE_UNKNOWN);
}
// Compatibility layer inputs never use flags.
return new TypedInput(`(${this.generateCompatibilityLayerCall(node, false)})`, TYPE_UNKNOWN);

case 'constant':
return this.safeConstantInput(node.value);

Expand Down Expand Up @@ -768,7 +788,7 @@ class JSGenerator {
const blockType = node.blockType;
if (blockType === BlockType.COMMAND || blockType === BlockType.HAT) {
this.source += `${this.generateCompatibilityLayerCall(node, isLastInLoop)};\n`;
} else if (blockType === BlockType.CONDITIONAL || blockType === BlockType.LOOP) {
} else if (blockType === BlockType.CONDITIONAL || blockType === BlockType.LOOP || blockType === BlockType.INLINE) {
const branchVariable = this.localVariables.next();
this.source += `const ${branchVariable} = createBranchInfo(${blockType === BlockType.LOOP});\n`;
this.source += `while (${branchVariable}.branch = +(${this.generateCompatibilityLayerCall(node, false, branchVariable)})) {\n`;
Expand Down Expand Up @@ -1192,6 +1212,16 @@ class JSGenerator {
this.variableInputs = {};
}

descendStackForSource (nodes, frame) {
// Wrapper for descendStack to get the source
const oldSource = this.source;
this.source = '';
this.descendStack(nodes, frame);
const stackSource = this.source;
this.source = oldSource;
return stackSource;
}

descendStack (nodes, frame) {
// Entering a stack -- all bets are off.
// TODO: allow if/else to inherit values
Expand Down
6 changes: 6 additions & 0 deletions src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,12 @@ class Runtime extends EventEmitter {
blockJSON.nextStatement = null; // null = available connection; undefined = terminal
}
break;
case BlockType.INLINE:
blockJSON.output = null;
blockInfo.disableMonitor = true;
blockInfo.branchCount = blockInfo.branchCount || 1;
blockJSON.outputShape = ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE;
break;
}

const blockText = Array.isArray(blockInfo.text) ? blockInfo.text : [blockInfo.text];
Expand Down
8 changes: 7 additions & 1 deletion src/extension-support/block-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ const BlockType = {
/**
* Arbitrary scratch-blocks XML.
*/
XML: 'xml'
XML: 'xml',

/**
* Specialized reporter block that allows for the insertion and evaluation
* of a substack.
*/
INLINE: 'inline'
};

module.exports = BlockType;