|
| 1 | +const {InputOpcode, InputType} = require('./enums'); |
| 2 | +const {IntermediateInput} = require('./intermediate'); |
| 3 | + |
| 4 | +class IRGeneratorStub { |
| 5 | +} |
| 6 | + |
| 7 | +class ScriptTreeGeneratorStub { |
| 8 | + constructor (real) { |
| 9 | + /** |
| 10 | + * The real script generator. |
| 11 | + * @type {import("./irgen").ScriptTreeGenerator} |
| 12 | + */ |
| 13 | + this.real = real; |
| 14 | + |
| 15 | + this.fakeThis = { |
| 16 | + descendInputOfBlock: this.descendInputOfBlockFromOldCompiler.bind(this) |
| 17 | + }; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Intended for extensions to override. |
| 22 | + * Always call from `fakeThis` context. |
| 23 | + * @param {{opcode: string}} block VM block |
| 24 | + * @returns {{kind: string}} Node object from old compiler. |
| 25 | + */ |
| 26 | + descendInput (block) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Intended for extensions to override. |
| 32 | + * Always call from `fakeThis` context. |
| 33 | + * @param {{opcode: string}} block VM block |
| 34 | + * @returns {{kind: string}} Node object from old compiler. |
| 35 | + */ |
| 36 | + descendStackedBlock (block) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Part of old compiler's public API. |
| 42 | + * @param parentBlock Parent VM block. |
| 43 | + * @param {string} inputName Name of input. |
| 44 | + */ |
| 45 | + descendInputOfBlockFromOldCompiler (parentBlock, inputName) { |
| 46 | + const node = this.real.descendInputOfBlock(parentBlock, inputName, true); |
| 47 | + return node; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * For internal use by new compiler only. |
| 52 | + * @param block VM block |
| 53 | + * @returns {IntermediateInput|null} |
| 54 | + */ |
| 55 | + descendInputFromNewCompiler (block) { |
| 56 | + const node = this.descendInput.call(this.fakeThis, block); |
| 57 | + if (node) { |
| 58 | + return new IntermediateInput(InputOpcode.OLD_COMPILER_COMPATIBILITY_LAYER, InputType.ANY, { |
| 59 | + block: block, |
| 60 | + oldNode: node |
| 61 | + }, true); |
| 62 | + } |
| 63 | + return null; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const TYPE_NUMBER = 1; |
| 68 | +const TYPE_STRING = 2; |
| 69 | +const TYPE_BOOLEAN = 3; |
| 70 | +const TYPE_UNKNOWN = 4; |
| 71 | +const TYPE_NUMBER_NAN = 5; |
| 72 | + |
| 73 | +/** |
| 74 | + * Part of the old compiler's public API. |
| 75 | + */ |
| 76 | +class TypedInput { |
| 77 | + /** |
| 78 | + * @param {string} source JavaScript |
| 79 | + * @param {number|IntermediateInput} typeOrIntermediate |
| 80 | + */ |
| 81 | + constructor (source, typeOrIntermediate) { |
| 82 | + /** |
| 83 | + * JavaScript. |
| 84 | + * @type {string} |
| 85 | + */ |
| 86 | + this.source = source; |
| 87 | + |
| 88 | + if (typeOrIntermediate instanceof IntermediateInput) { |
| 89 | + /** |
| 90 | + * @type {IntermediateInput} |
| 91 | + */ |
| 92 | + this.intermediate = typeOrIntermediate; |
| 93 | + } else { |
| 94 | + this.intermediate = null; |
| 95 | + this.type = typeOrIntermediate; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + asNumber () { |
| 100 | + throw new Error('TODO asNumber'); |
| 101 | + } |
| 102 | + |
| 103 | + asNumberOrNaN () { |
| 104 | + throw new Error('TODO asNumberOrNaN'); |
| 105 | + } |
| 106 | + |
| 107 | + asString () { |
| 108 | + throw new Error('TODO asString'); |
| 109 | + } |
| 110 | + |
| 111 | + asBoolean () { |
| 112 | + throw new Error('TODO asBoolean'); |
| 113 | + } |
| 114 | + |
| 115 | + asColor () { |
| 116 | + throw new Error('TODO asColor'); |
| 117 | + } |
| 118 | + |
| 119 | + asUnknown () { |
| 120 | + return this.source; |
| 121 | + } |
| 122 | + |
| 123 | + asSafe () { |
| 124 | + return this.source; |
| 125 | + } |
| 126 | + |
| 127 | + isAlwaysNumber () { |
| 128 | + // TODO |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + isAlwaysNumberOrNaN () { |
| 133 | + // TODO |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + isNeverNumber () { |
| 138 | + // TODO |
| 139 | + return false; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +class JSGeneratorStub { |
| 144 | + constructor (real) { |
| 145 | + /** |
| 146 | + * For internal use by new compiler only. |
| 147 | + * @type {import("./jsgen")} |
| 148 | + */ |
| 149 | + this.real = real; |
| 150 | + |
| 151 | + this._fakeThis = { |
| 152 | + descendInput: this.descendInputFromOldCompiler.bind(this) |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Intended for extensions to override. |
| 158 | + * Always call from `fakeThis` context. |
| 159 | + * @param {{kind: string}} node Old compiler AST node. |
| 160 | + * @returns {TypedInput} Old compiler TypedInput. |
| 161 | + */ |
| 162 | + descendInput (node) { |
| 163 | + throw new Error(`Unknown input: ${node.kind}`); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Part of old compiler's public API. |
| 168 | + * @param {IntermediateInput} intermediate |
| 169 | + * @returns {TypedInput} |
| 170 | + */ |
| 171 | + descendInputFromOldCompiler (intermediate) { |
| 172 | + const js = this.real.descendInput(intermediate); |
| 173 | + return new TypedInput(js, intermediate); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @param {IntermediateInput} intermediate |
| 178 | + * @returns {string} JavaScript |
| 179 | + */ |
| 180 | + descendInputFromNewCompiler (intermediate) { |
| 181 | + const oldNode = intermediate.inputs.oldNode; |
| 182 | + const typedInput = this.descendInput.call(this._fakeThis, oldNode); |
| 183 | + return typedInput.asSafe(); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * Part of old compiler's public API. |
| 189 | + */ |
| 190 | +JSGeneratorStub.unstable_exports = { |
| 191 | + TYPE_NUMBER, |
| 192 | + TYPE_STRING, |
| 193 | + TYPE_BOOLEAN, |
| 194 | + TYPE_UNKNOWN, |
| 195 | + TYPE_NUMBER_NAN, |
| 196 | + // factoryNameVariablePool, |
| 197 | + // functionNameVariablePool, |
| 198 | + // generatorNameVariablePool, |
| 199 | + // VariablePool, |
| 200 | + // PEN_EXT, |
| 201 | + // PEN_STATE, |
| 202 | + TypedInput |
| 203 | + // ConstantInput, |
| 204 | + // VariableInput, |
| 205 | + // Frame, |
| 206 | + // sanitize |
| 207 | +}; |
| 208 | + |
| 209 | +const oldCompilerCompatibility = { |
| 210 | + enabled: false, |
| 211 | + IRGeneratorStub, |
| 212 | + ScriptTreeGeneratorStub, |
| 213 | + TypedInput, |
| 214 | + JSGeneratorStub |
| 215 | +}; |
| 216 | + |
| 217 | +module.exports = oldCompilerCompatibility; |
0 commit comments