Skip to content
2 changes: 2 additions & 0 deletions src/compiler/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const StackOpcode = {
DEBUGGER: 'tw.debugger',
VISUAL_REPORT: 'visualReport',
COMPATIBILITY_LAYER: 'compat',
OLD_COMPILER_COMPATIBILITY_LAYER: 'oldCompiler',

HAT_EDGE: 'hat.edge',
HAT_PREDICATE: 'hat.predicate',
Expand Down Expand Up @@ -201,6 +202,7 @@ const InputOpcode = {
CAST_COLOR: 'cast.toColor',

COMPATIBILITY_LAYER: 'compat',
OLD_COMPILER_COMPATIBILITY_LAYER: 'oldCompiler',

LOOKS_BACKDROP_NUMBER: 'looks.backdropNumber',
LOOKS_BACKDROP_NAME: 'looks.backdropName',
Expand Down
21 changes: 21 additions & 0 deletions src/compiler/irgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
IntermediateScript,
IntermediateRepresentation
} = require('./intermediate');
const oldCompilerCompatiblity = require('./old-compiler-compatibility.js');

/**
* @fileoverview Generate intermediate representations from Scratch blocks.
Expand Down Expand Up @@ -96,6 +97,12 @@ class ScriptTreeGenerator {
}
}
}

this.oldCompilerStub = (
oldCompilerCompatiblity.enabled ?
new oldCompilerCompatiblity.ScriptTreeGeneratorStub(this) :
null
);
}

setProcedureVariant (procedureVariant) {
Expand Down Expand Up @@ -199,6 +206,13 @@ class ScriptTreeGenerator {
* @returns {IntermediateInput} Compiled input node for this input.
*/
descendInput (block, preserveStrings = false) {
if (this.oldCompilerStub) {
const oldCompilerResult = this.oldCompilerStub.descendInputFromNewCompiler(block);
if (oldCompilerResult) {
return oldCompilerResult;
}
}

switch (block.opcode) {
case 'colour_picker':
return this.createConstantInput(block.fields.COLOUR.value, true);
Expand Down Expand Up @@ -593,6 +607,13 @@ class ScriptTreeGenerator {
* @returns {IntermediateStackBlock} Compiled node for this block.
*/
descendStackedBlock (block) {
if (this.oldCompilerStub) {
const oldCompilerResult = this.oldCompilerStub.descendStackedBlockFromNewCompiler(block);
if (oldCompilerResult) {
return oldCompilerResult;
}
}

switch (block.opcode) {
case 'control_all_at_once':
// In Scratch 3, this block behaves like "if 1 = 1"
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/jsgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const VariablePool = require('./variable-pool');
const jsexecute = require('./jsexecute');
const environment = require('./environment');
const {StackOpcode, InputOpcode, InputType} = require('./enums.js');
const oldCompilerCompatibility = require('./old-compiler-compatibility.js');

// These imports are used by jsdoc comments but eslint doesn't know that
/* eslint-disable no-unused-vars */
Expand Down Expand Up @@ -124,6 +125,8 @@ class JSGenerator {
this.isInHat = false;

this.debug = this.target.runtime.debug;

this.oldCompilerStub = new oldCompilerCompatibility.JSGeneratorStub(this);
}

/**
Expand Down Expand Up @@ -198,6 +201,9 @@ class JSGenerator {
// Compatibility layer inputs never use flags.
return `(${this.generateCompatibilityLayerCall(node, false)})`;

case InputOpcode.OLD_COMPILER_COMPATIBILITY_LAYER:
return this.oldCompilerStub.descendInputFromNewCompiler(block);

case InputOpcode.CONSTANT:
if (block.isAlwaysType(InputType.NUMBER)) {
if (typeof node.value !== 'number') throw new Error(`JS: '${block.type}' type constant had ${typeof node.value} type value. Expected number.`);
Expand Down Expand Up @@ -540,6 +546,9 @@ class JSGenerator {
break;
}

case InputOpcode.OLD_COMPILER_COMPATIBILITY_LAYER:
return this.oldCompilerStub.descendStackedBlockFromNewCompiler(block);

case StackOpcode.HAT_EDGE:
this.isInHat = true;
this.source += '{\n';
Expand Down
Loading