@opencode/codemode 0.0.0-dev-884 → 0.0.0-dev-900

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/README.md +3 -0
  2. package/dist/codemode.d.ts +2 -0
  3. package/dist/codemode.js +10 -4
  4. package/dist/vm/async.d.ts +78 -0
  5. package/dist/vm/async.js +335 -0
  6. package/dist/vm/code.d.ts +122 -0
  7. package/dist/vm/code.js +407 -0
  8. package/dist/vm/compiler/compile.d.ts +13 -0
  9. package/dist/vm/compiler/compile.js +1477 -0
  10. package/dist/vm/compiler/scope.d.ts +74 -0
  11. package/dist/vm/compiler/scope.js +515 -0
  12. package/dist/vm/data.d.ts +7 -0
  13. package/dist/vm/data.js +13 -0
  14. package/dist/vm/generators.d.ts +42 -0
  15. package/dist/vm/generators.js +413 -0
  16. package/dist/vm/host.d.ts +103 -0
  17. package/dist/vm/host.js +226 -0
  18. package/dist/vm/index.d.ts +3 -0
  19. package/dist/vm/index.js +3 -0
  20. package/dist/vm/internal-prelude.d.ts +45 -0
  21. package/dist/vm/internal-prelude.js +534 -0
  22. package/dist/vm/opcodes.d.ts +222 -0
  23. package/dist/vm/opcodes.js +235 -0
  24. package/dist/vm/prelude/bytecode.d.ts +18 -0
  25. package/dist/vm/prelude/bytecode.js +31 -0
  26. package/dist/vm/prelude/bytecode.json +1 -0
  27. package/dist/vm/prelude/helpers.d.ts +8 -0
  28. package/dist/vm/prelude/helpers.js +258 -0
  29. package/dist/vm/prelude/prelude.d.ts +21 -0
  30. package/dist/vm/prelude/prelude.js +53 -0
  31. package/dist/vm/prelude/source.d.ts +1 -0
  32. package/dist/vm/prelude/source.js +652 -0
  33. package/dist/vm/runtime/callback.d.ts +25 -0
  34. package/dist/vm/runtime/callback.js +51 -0
  35. package/dist/vm/runtime/driver.d.ts +41 -0
  36. package/dist/vm/runtime/driver.js +142 -0
  37. package/dist/vm/runtime/errors.d.ts +23 -0
  38. package/dist/vm/runtime/errors.js +169 -0
  39. package/dist/vm/runtime/execute.d.ts +22 -0
  40. package/dist/vm/runtime/execute.js +278 -0
  41. package/dist/vm/runtime/extensions.d.ts +16 -0
  42. package/dist/vm/runtime/extensions.js +270 -0
  43. package/dist/vm/runtime/generators.d.ts +6 -0
  44. package/dist/vm/runtime/generators.js +44 -0
  45. package/dist/vm/runtime/globals.d.ts +6 -0
  46. package/dist/vm/runtime/globals.js +137 -0
  47. package/dist/vm/runtime/interpreter.d.ts +50 -0
  48. package/dist/vm/runtime/interpreter.js +94 -0
  49. package/dist/vm/runtime/intrinsic-names.d.ts +56 -0
  50. package/dist/vm/runtime/intrinsic-names.js +166 -0
  51. package/dist/vm/runtime/intrinsics.d.ts +15 -0
  52. package/dist/vm/runtime/intrinsics.js +111 -0
  53. package/dist/vm/runtime/limits.d.ts +10 -0
  54. package/dist/vm/runtime/limits.js +19 -0
  55. package/dist/vm/runtime/model.d.ts +58 -0
  56. package/dist/vm/runtime/model.js +57 -0
  57. package/dist/vm/runtime/modules.d.ts +4 -0
  58. package/dist/vm/runtime/modules.js +33 -0
  59. package/dist/vm/runtime/native.d.ts +19 -0
  60. package/dist/vm/runtime/native.js +41 -0
  61. package/dist/vm/runtime/objects.d.ts +403 -0
  62. package/dist/vm/runtime/objects.js +784 -0
  63. package/dist/vm/runtime/operations.d.ts +49 -0
  64. package/dist/vm/runtime/operations.js +288 -0
  65. package/dist/vm/runtime/promises.d.ts +55 -0
  66. package/dist/vm/runtime/promises.js +232 -0
  67. package/dist/vm/runtime/references.d.ts +8 -0
  68. package/dist/vm/runtime/references.js +51 -0
  69. package/dist/vm/runtime/signals.d.ts +69 -0
  70. package/dist/vm/runtime/signals.js +68 -0
  71. package/dist/vm/runtime/source.d.ts +29 -0
  72. package/dist/vm/runtime/source.js +63 -0
  73. package/dist/vm/runtime/table.d.ts +43 -0
  74. package/dist/vm/runtime/table.js +132 -0
  75. package/dist/vm/scheduler.d.ts +178 -0
  76. package/dist/vm/scheduler.js +63 -0
  77. package/dist/vm/snapshot/codec.d.ts +88 -0
  78. package/dist/vm/snapshot/codec.js +752 -0
  79. package/dist/vm/snapshot/snapshot.d.ts +39 -0
  80. package/dist/vm/snapshot/snapshot.js +69 -0
  81. package/dist/vm/snapshot.d.ts +29 -0
  82. package/dist/vm/snapshot.js +304 -0
  83. package/dist/vm/stdlib/array.d.ts +16 -0
  84. package/dist/vm/stdlib/array.js +311 -0
  85. package/dist/vm/stdlib/bytes.d.ts +11 -0
  86. package/dist/vm/stdlib/bytes.js +222 -0
  87. package/dist/vm/stdlib/collections.d.ts +7 -0
  88. package/dist/vm/stdlib/collections.js +188 -0
  89. package/dist/vm/stdlib/console.d.ts +6 -0
  90. package/dist/vm/stdlib/console.js +93 -0
  91. package/dist/vm/stdlib/date.d.ts +2 -0
  92. package/dist/vm/stdlib/date.js +123 -0
  93. package/dist/vm/stdlib/headers.d.ts +2 -0
  94. package/dist/vm/stdlib/headers.js +92 -0
  95. package/dist/vm/stdlib/iterator.d.ts +2 -0
  96. package/dist/vm/stdlib/iterator.js +30 -0
  97. package/dist/vm/stdlib/json.d.ts +3 -0
  98. package/dist/vm/stdlib/json.js +42 -0
  99. package/dist/vm/stdlib/math.d.ts +3 -0
  100. package/dist/vm/stdlib/math.js +73 -0
  101. package/dist/vm/stdlib/number.d.ts +3 -0
  102. package/dist/vm/stdlib/number.js +72 -0
  103. package/dist/vm/stdlib/object.d.ts +6 -0
  104. package/dist/vm/stdlib/object.js +201 -0
  105. package/dist/vm/stdlib/regexp.d.ts +8 -0
  106. package/dist/vm/stdlib/regexp.js +119 -0
  107. package/dist/vm/stdlib/string.d.ts +2 -0
  108. package/dist/vm/stdlib/string.js +272 -0
  109. package/dist/vm/stdlib/url.d.ts +25 -0
  110. package/dist/vm/stdlib/url.js +255 -0
  111. package/dist/vm/stdlib/value.d.ts +6 -0
  112. package/dist/vm/stdlib/value.js +32 -0
  113. package/dist/vm/stdlib/web.d.ts +5 -0
  114. package/dist/vm/stdlib/web.js +97 -0
  115. package/dist/vm/vm.d.ts +224 -0
  116. package/dist/vm/vm.js +2921 -0
  117. package/package.json +5 -2
@@ -0,0 +1,222 @@
1
+ import type { DiagnosticKind } from "../codemode.js";
2
+ import type { ErrorType } from "./runtime/intrinsics.js";
3
+ /** Uninitialized lexical binding sentinel (temporal dead zone). */
4
+ export declare const HOLE: unique symbol;
5
+ /** Mutable box for a binding captured by a closure. */
6
+ export declare class Cell {
7
+ v: unknown;
8
+ constructor(v: unknown);
9
+ }
10
+ export declare const EMPTY_CELLS: ReadonlyArray<Cell>;
11
+ /** Raised when the VM compiler or runtime encounters a feature scheduled for a later phase. */
12
+ export declare class NotYetImplemented extends Error {
13
+ readonly feature: string;
14
+ constructor(feature: string);
15
+ }
16
+ /**
17
+ * Bytecode opcodes (1 word per instruction: `op:8 | a:8 | bx:16`, widened by `EXTRA` prefixes for `a > 0xff` or `bx > 0xffff`).
18
+ */
19
+ export declare const Op: {
20
+ readonly PUSH_UNDEF: 0;
21
+ readonly PUSH_NULL: 1;
22
+ readonly PUSH_BOOL: 2;
23
+ readonly PUSH_INT: 3;
24
+ readonly PUSH_CONST: 4;
25
+ readonly POP: 5;
26
+ readonly POPN: 6;
27
+ readonly DUP: 7;
28
+ readonly PICK: 8;
29
+ readonly ROT: 9;
30
+ readonly GET_LOCAL: 10;
31
+ readonly PUT_LOCAL: 11;
32
+ readonly HOLE_LOCAL: 12;
33
+ readonly NEW_CELL: 13;
34
+ readonly COPY_CELL: 14;
35
+ readonly GET_CELL: 15;
36
+ readonly PUT_CELL: 16;
37
+ readonly GET_UPVAL: 17;
38
+ readonly PUT_UPVAL: 18;
39
+ readonly GET_GLOBAL: 19;
40
+ readonly GET_INTRINSIC: 20;
41
+ readonly ARGS: 21;
42
+ readonly CLOSURE: 22;
43
+ readonly SET_NAME: 23;
44
+ readonly IMPORT: 24;
45
+ readonly GET_PROP: 25;
46
+ readonly GET_ELEM: 26;
47
+ readonly PUT_PROP: 27;
48
+ readonly PUT_ELEM: 28;
49
+ readonly DELETE_PROP: 29;
50
+ readonly DELETE_ELEM: 30;
51
+ readonly CHECK_BASE: 31;
52
+ readonly TO_KEY: 32;
53
+ readonly IN: 33;
54
+ readonly INSTANCEOF: 34;
55
+ readonly NEW_OBJECT: 35;
56
+ readonly NEW_ARRAY: 36;
57
+ readonly ARRAY_APPEND: 37;
58
+ readonly DEFINE_FIELD: 38;
59
+ readonly COPY_DATA_PROPS: 39;
60
+ readonly COPY_REST: 40;
61
+ readonly TEMPLATE: 41;
62
+ readonly REGEXP: 42;
63
+ readonly ADD: 43;
64
+ readonly SUB: 44;
65
+ readonly MUL: 45;
66
+ readonly LT: 46;
67
+ readonly LE: 47;
68
+ readonly GT: 48;
69
+ readonly GE: 49;
70
+ readonly SEQ: 50;
71
+ readonly SNE: 51;
72
+ readonly BINOP: 52;
73
+ readonly UNARY: 53;
74
+ readonly NOT: 54;
75
+ readonly TO_NUMERIC: 55;
76
+ readonly INC: 56;
77
+ readonly TO_STRING: 57;
78
+ readonly CONCAT: 58;
79
+ readonly JUMP: 59;
80
+ readonly LOOP: 60;
81
+ readonly JUMP_IF_FALSE: 61;
82
+ readonly JUMP_IF_TRUE: 62;
83
+ readonly JUMP_COND: 63;
84
+ readonly THROW: 64;
85
+ readonly THROW_ERROR: 65;
86
+ readonly END_FINALLY: 66;
87
+ readonly RETURN: 67;
88
+ readonly NOP: 68;
89
+ readonly EXTRA: 69;
90
+ readonly CALL: 70;
91
+ readonly CALL_SPREAD: 71;
92
+ readonly NEW: 72;
93
+ readonly NEW_SPREAD: 73;
94
+ readonly ITER_OPEN: 74;
95
+ readonly ITER_INIT: 75;
96
+ readonly ITER_STEP: 76;
97
+ readonly ITER_RESULT: 77;
98
+ readonly ITER_CLOSE: 78;
99
+ readonly ITER_APPEND_ALL: 79;
100
+ readonly FOR_IN: 80;
101
+ readonly GENERATOR_START: 81;
102
+ readonly YIELD: 82;
103
+ readonly RESUME: 83;
104
+ readonly AWAIT: 84;
105
+ readonly YSTAR: 85;
106
+ readonly YSTAR_RESULT: 86;
107
+ readonly END_PROGRAM: 87;
108
+ };
109
+ export type OpCode = (typeof Op)[keyof typeof Op];
110
+ export declare const OpNames: ReadonlyArray<string>;
111
+ /** Sub-ops for `Op.BINOP` (`a` operand). */
112
+ export declare const BinOp: {
113
+ readonly DIV: 0;
114
+ readonly MOD: 1;
115
+ readonly EXP: 2;
116
+ readonly BAND: 3;
117
+ readonly BOR: 4;
118
+ readonly BXOR: 5;
119
+ readonly SHL: 6;
120
+ readonly SAR: 7;
121
+ readonly SHR: 8;
122
+ readonly EQ: 9;
123
+ readonly NE: 10;
124
+ };
125
+ export declare const BinOpSymbols: ReadonlyArray<string>;
126
+ /** Sub-ops for `Op.UNARY` (`a` operand). */
127
+ export declare const UnaryOp: {
128
+ readonly NEG: 0;
129
+ readonly PLUS: 1;
130
+ readonly BNOT: 2;
131
+ readonly TYPEOF: 3;
132
+ readonly VOID: 4;
133
+ };
134
+ export declare const UnaryOpSymbols: ReadonlyArray<string>;
135
+ /** Sub-ops for `Op.JUMP_COND` (`a` operand). */
136
+ export declare const CondKind: {
137
+ /** If top is truthy, keep it and jump; else pop and fall through (`||`, `||=`). */
138
+ readonly TRUTHY_KEEP: 0;
139
+ /** If top is falsy, keep it and jump; else pop and fall through (`&&`, `&&=`). */
140
+ readonly FALSY_KEEP: 1;
141
+ /** If top is not null/undefined, keep it and jump; else pop and fall through (`??`, `??=`). */
142
+ readonly NOT_NULLISH_KEEP: 2;
143
+ /** If top !== undefined, keep it and jump; else pop and fall through (defaults). */
144
+ readonly DEFINED: 3;
145
+ /** If top == null, replace top with undefined and jump; else keep top and fall through (`?.`). */
146
+ readonly NULLISH: 4;
147
+ /** For `[thisValue, callable]`: if callable == null, pop callable, set top to undefined, and jump (`o.m?.()`). */
148
+ readonly NULLISH_METHOD: 5;
149
+ };
150
+ /** Sub-ops for `Op.PUT_LOCAL`, `Op.PUT_CELL`, `Op.PUT_UPVAL` (`a` operand). */
151
+ export declare const PutKind: {
152
+ /** Normal assignment: checks TDZ (`HOLE`). */
153
+ readonly ASSIGN: 0;
154
+ /** Declaration initialization or internal slot write: skips TDZ check. */
155
+ readonly INIT: 1;
156
+ /** Assignment to a `const` or read-only binding: checks TDZ first, then throws `TypeError`. */
157
+ readonly CONST: 2;
158
+ };
159
+ /** Sub-ops for `Op.CHECK_BASE` (`a` operand). */
160
+ export declare const CheckBaseKind: {
161
+ /** `o[k]`: checks `o` is not null/undefined before converting `k` (`[o, k] -> [o, k]`). */
162
+ readonly READ: 0;
163
+ /** `o.x = ...`: checks `o.x` is a writable reference (`[o] -> [o]`, `bx = k`). */
164
+ readonly WRITE_PROP: 1;
165
+ /** `o[k] = ...`: checks `o[k]` is a writable reference after `TO_KEY` (`[o, key] -> [o, key]`). */
166
+ readonly WRITE_ELEM: 2;
167
+ /** Destructuring target check: throws if top is null or undefined (`[v] -> [v]`). */
168
+ readonly DESTRUCTURE: 3;
169
+ };
170
+ /** Sub-ops for `Op.ITER_OPEN` and `Op.ITER_INIT` (`a` operand). */
171
+ export declare const IterContext: {
172
+ readonly FOR_OF: 0;
173
+ readonly DESTRUCTURE: 1;
174
+ readonly SPREAD: 2;
175
+ readonly DELEGATE: 3;
176
+ readonly FOR_AWAIT: 4;
177
+ readonly ASYNC_DELEGATE: 5;
178
+ readonly CHECK_OBJECT: 6;
179
+ };
180
+ /** Sub-ops for `Op.IMPORT` (`a` operand). */
181
+ export declare const ImportKind: {
182
+ readonly DEFAULT: 0;
183
+ readonly NAMESPACE: 1;
184
+ readonly NAMED: 2;
185
+ readonly SIDE_EFFECT: 3;
186
+ readonly DYNAMIC: 4;
187
+ };
188
+ /** Bit flags on `Code.flags`. */
189
+ export declare const CodeFlags: {
190
+ readonly ARROW: number;
191
+ readonly ASYNC: number;
192
+ readonly GENERATOR: number;
193
+ readonly SIMPLE_PARAMS: number;
194
+ readonly USES_THIS: number;
195
+ readonly USES_ARGUMENTS: number;
196
+ readonly INTERNAL: number;
197
+ readonly PROGRAM: number;
198
+ readonly NEEDS_ARGS: number;
199
+ };
200
+ /** Constant pool descriptors. */
201
+ export type TemplateSite = {
202
+ readonly cooked: ReadonlyArray<string | undefined>;
203
+ readonly raw: ReadonlyArray<string>;
204
+ readonly nodeIndex: number;
205
+ };
206
+ export type RegExpSite = {
207
+ readonly pattern: string;
208
+ readonly flags: string;
209
+ };
210
+ export type ErrorSite = {
211
+ readonly type: ErrorType;
212
+ readonly message: string;
213
+ readonly kind?: DiagnosticKind;
214
+ readonly suggestions?: ReadonlyArray<string>;
215
+ };
216
+ export type ImportSite = {
217
+ readonly specifier: string;
218
+ readonly imported?: string;
219
+ };
220
+ export type ConstValue = string | number | TemplateSite | RegExpSite | ErrorSite | ImportSite;
221
+ /** Packs one instruction word: `op:8 | a:8 | bx:16`. Throws if any field does not fit its width. */
222
+ export declare const packWord: (op: OpCode, a?: number, bx?: number) => number;
@@ -0,0 +1,235 @@
1
+ /** Uninitialized lexical binding sentinel (temporal dead zone). */
2
+ export const HOLE = Symbol("codemode.hole");
3
+ /** Mutable box for a binding captured by a closure. */
4
+ export class Cell {
5
+ v;
6
+ constructor(v) {
7
+ this.v = v;
8
+ }
9
+ }
10
+ const initCell = new Cell(0);
11
+ initCell.v = 1.5;
12
+ initCell.v = "";
13
+ initCell.v = true;
14
+ initCell.v = null;
15
+ initCell.v = undefined;
16
+ initCell.v = HOLE;
17
+ const makeEmptyCells = () => {
18
+ const arr = [];
19
+ arr.push(initCell);
20
+ arr.length = 0;
21
+ return arr;
22
+ };
23
+ export const EMPTY_CELLS = makeEmptyCells();
24
+ /** Raised when the VM compiler or runtime encounters a feature scheduled for a later phase. */
25
+ export class NotYetImplemented extends Error {
26
+ feature;
27
+ constructor(feature) {
28
+ super(`Not yet implemented in VM: ${feature}`);
29
+ this.feature = feature;
30
+ this.name = "NotYetImplemented";
31
+ }
32
+ }
33
+ /**
34
+ * Bytecode opcodes (1 word per instruction: `op:8 | a:8 | bx:16`, widened by `EXTRA` prefixes for `a > 0xff` or `bx > 0xffff`).
35
+ */
36
+ export const Op = {
37
+ // Stack and constants (10)
38
+ PUSH_UNDEF: 0,
39
+ PUSH_NULL: 1,
40
+ PUSH_BOOL: 2,
41
+ PUSH_INT: 3,
42
+ PUSH_CONST: 4,
43
+ POP: 5,
44
+ POPN: 6,
45
+ DUP: 7,
46
+ PICK: 8,
47
+ ROT: 9,
48
+ // Variables (15)
49
+ GET_LOCAL: 10,
50
+ PUT_LOCAL: 11,
51
+ HOLE_LOCAL: 12,
52
+ NEW_CELL: 13,
53
+ COPY_CELL: 14,
54
+ GET_CELL: 15,
55
+ PUT_CELL: 16,
56
+ GET_UPVAL: 17,
57
+ PUT_UPVAL: 18,
58
+ GET_GLOBAL: 19,
59
+ GET_INTRINSIC: 20,
60
+ ARGS: 21,
61
+ CLOSURE: 22,
62
+ SET_NAME: 23,
63
+ IMPORT: 24,
64
+ // Properties (10)
65
+ GET_PROP: 25,
66
+ GET_ELEM: 26,
67
+ PUT_PROP: 27,
68
+ PUT_ELEM: 28,
69
+ DELETE_PROP: 29,
70
+ DELETE_ELEM: 30,
71
+ CHECK_BASE: 31,
72
+ TO_KEY: 32,
73
+ IN: 33,
74
+ INSTANCEOF: 34,
75
+ // Literals (8)
76
+ NEW_OBJECT: 35,
77
+ NEW_ARRAY: 36,
78
+ ARRAY_APPEND: 37,
79
+ DEFINE_FIELD: 38,
80
+ COPY_DATA_PROPS: 39,
81
+ COPY_REST: 40,
82
+ TEMPLATE: 41,
83
+ REGEXP: 42,
84
+ // Operators (16)
85
+ ADD: 43,
86
+ SUB: 44,
87
+ MUL: 45,
88
+ LT: 46,
89
+ LE: 47,
90
+ GT: 48,
91
+ GE: 49,
92
+ SEQ: 50,
93
+ SNE: 51,
94
+ BINOP: 52,
95
+ UNARY: 53,
96
+ NOT: 54,
97
+ TO_NUMERIC: 55,
98
+ INC: 56,
99
+ TO_STRING: 57,
100
+ CONCAT: 58,
101
+ // Control (11)
102
+ JUMP: 59,
103
+ LOOP: 60,
104
+ JUMP_IF_FALSE: 61,
105
+ JUMP_IF_TRUE: 62,
106
+ JUMP_COND: 63,
107
+ THROW: 64,
108
+ THROW_ERROR: 65,
109
+ END_FINALLY: 66,
110
+ RETURN: 67,
111
+ NOP: 68,
112
+ EXTRA: 69,
113
+ // Calls (4)
114
+ CALL: 70,
115
+ CALL_SPREAD: 71,
116
+ NEW: 72,
117
+ NEW_SPREAD: 73,
118
+ // Iteration (7)
119
+ ITER_OPEN: 74,
120
+ ITER_INIT: 75,
121
+ ITER_STEP: 76,
122
+ ITER_RESULT: 77,
123
+ ITER_CLOSE: 78,
124
+ ITER_APPEND_ALL: 79,
125
+ FOR_IN: 80,
126
+ // Generators and async (6)
127
+ GENERATOR_START: 81,
128
+ YIELD: 82,
129
+ RESUME: 83,
130
+ AWAIT: 84,
131
+ YSTAR: 85,
132
+ YSTAR_RESULT: 86,
133
+ // Program (1)
134
+ END_PROGRAM: 87,
135
+ };
136
+ export const OpNames = Object.entries(Op).reduce((acc, [name, code]) => {
137
+ acc[code] = name;
138
+ return acc;
139
+ }, []);
140
+ /** Sub-ops for `Op.BINOP` (`a` operand). */
141
+ export const BinOp = {
142
+ DIV: 0,
143
+ MOD: 1,
144
+ EXP: 2,
145
+ BAND: 3,
146
+ BOR: 4,
147
+ BXOR: 5,
148
+ SHL: 6,
149
+ SAR: 7,
150
+ SHR: 8,
151
+ EQ: 9,
152
+ NE: 10,
153
+ };
154
+ export const BinOpSymbols = ["/", "%", "**", "&", "|", "^", "<<", ">>", ">>>", "==", "!="];
155
+ /** Sub-ops for `Op.UNARY` (`a` operand). */
156
+ export const UnaryOp = {
157
+ NEG: 0,
158
+ PLUS: 1,
159
+ BNOT: 2,
160
+ TYPEOF: 3,
161
+ VOID: 4,
162
+ };
163
+ export const UnaryOpSymbols = ["-", "+", "~", "typeof", "void"];
164
+ /** Sub-ops for `Op.JUMP_COND` (`a` operand). */
165
+ export const CondKind = {
166
+ /** If top is truthy, keep it and jump; else pop and fall through (`||`, `||=`). */
167
+ TRUTHY_KEEP: 0,
168
+ /** If top is falsy, keep it and jump; else pop and fall through (`&&`, `&&=`). */
169
+ FALSY_KEEP: 1,
170
+ /** If top is not null/undefined, keep it and jump; else pop and fall through (`??`, `??=`). */
171
+ NOT_NULLISH_KEEP: 2,
172
+ /** If top !== undefined, keep it and jump; else pop and fall through (defaults). */
173
+ DEFINED: 3,
174
+ /** If top == null, replace top with undefined and jump; else keep top and fall through (`?.`). */
175
+ NULLISH: 4,
176
+ /** For `[thisValue, callable]`: if callable == null, pop callable, set top to undefined, and jump (`o.m?.()`). */
177
+ NULLISH_METHOD: 5,
178
+ };
179
+ /** Sub-ops for `Op.PUT_LOCAL`, `Op.PUT_CELL`, `Op.PUT_UPVAL` (`a` operand). */
180
+ export const PutKind = {
181
+ /** Normal assignment: checks TDZ (`HOLE`). */
182
+ ASSIGN: 0,
183
+ /** Declaration initialization or internal slot write: skips TDZ check. */
184
+ INIT: 1,
185
+ /** Assignment to a `const` or read-only binding: checks TDZ first, then throws `TypeError`. */
186
+ CONST: 2,
187
+ };
188
+ /** Sub-ops for `Op.CHECK_BASE` (`a` operand). */
189
+ export const CheckBaseKind = {
190
+ /** `o[k]`: checks `o` is not null/undefined before converting `k` (`[o, k] -> [o, k]`). */
191
+ READ: 0,
192
+ /** `o.x = ...`: checks `o.x` is a writable reference (`[o] -> [o]`, `bx = k`). */
193
+ WRITE_PROP: 1,
194
+ /** `o[k] = ...`: checks `o[k]` is a writable reference after `TO_KEY` (`[o, key] -> [o, key]`). */
195
+ WRITE_ELEM: 2,
196
+ /** Destructuring target check: throws if top is null or undefined (`[v] -> [v]`). */
197
+ DESTRUCTURE: 3,
198
+ };
199
+ /** Sub-ops for `Op.ITER_OPEN` and `Op.ITER_INIT` (`a` operand). */
200
+ export const IterContext = {
201
+ FOR_OF: 0,
202
+ DESTRUCTURE: 1,
203
+ SPREAD: 2,
204
+ DELEGATE: 3,
205
+ FOR_AWAIT: 4,
206
+ ASYNC_DELEGATE: 5,
207
+ CHECK_OBJECT: 6,
208
+ };
209
+ /** Sub-ops for `Op.IMPORT` (`a` operand). */
210
+ export const ImportKind = {
211
+ DEFAULT: 0,
212
+ NAMESPACE: 1,
213
+ NAMED: 2,
214
+ SIDE_EFFECT: 3,
215
+ DYNAMIC: 4,
216
+ };
217
+ /** Bit flags on `Code.flags`. */
218
+ export const CodeFlags = {
219
+ ARROW: 1 << 0,
220
+ ASYNC: 1 << 1,
221
+ GENERATOR: 1 << 2,
222
+ SIMPLE_PARAMS: 1 << 3,
223
+ USES_THIS: 1 << 4,
224
+ USES_ARGUMENTS: 1 << 5,
225
+ INTERNAL: 1 << 6,
226
+ PROGRAM: 1 << 7,
227
+ NEEDS_ARGS: 1 << 9,
228
+ };
229
+ /** Packs one instruction word: `op:8 | a:8 | bx:16`. Throws if any field does not fit its width. */
230
+ export const packWord = (op, a = 0, bx = 0) => {
231
+ if ((op & 0xff) !== op || (a & 0xff) !== a || (bx & 0xffff) !== bx) {
232
+ throw new RangeError(`Instruction operand out of single-word range: op=${op} a=${a} bx=${bx}`);
233
+ }
234
+ return op | (a << 8) | (bx << 16);
235
+ };
@@ -0,0 +1,18 @@
1
+ import { type Code, type CodeJson } from "../code.js";
2
+ /** A built-in source's bytecode as `script/prelude.ts` saves it, with a hash of the text it was compiled from. */
3
+ export type SavedCodes = {
4
+ readonly text: string;
5
+ readonly codes: ReadonlyArray<CodeJson>;
6
+ };
7
+ /**
8
+ * A built-in source's bytecode (the prelude's, the VM's internal helpers'), made once per process: from the bytecode
9
+ * compiled ahead of time when it was compiled from this text, so the process neither parses nor compiles it until
10
+ * something asks for its nodes (an error's site, a saved state's); compiled now otherwise. A test checks the saved
11
+ * bytecode is what the compiler makes. Its top-level functions start at a line's start; they are found by name.
12
+ */
13
+ export declare const builtinCodes: (id: string, text: string) => {
14
+ codes: readonly Code[];
15
+ byName: Map<string, Code>;
16
+ };
17
+ /** What `script/prelude.ts` saves for a built-in source. */
18
+ export declare const saveCodes: (text: string, codes: ReadonlyArray<Code>) => SavedCodes;
@@ -0,0 +1,31 @@
1
+ import { parse } from "acorn";
2
+ import { Source } from "../runtime/source.js";
3
+ import { hash } from "../snapshot/snapshot.js";
4
+ import { codeFromJson, codeToJson } from "../code.js";
5
+ import { compileSource } from "../compiler/compile.js";
6
+ import saved from "./bytecode.json" with { type: "json" };
7
+ /**
8
+ * A built-in source's bytecode (the prelude's, the VM's internal helpers'), made once per process: from the bytecode
9
+ * compiled ahead of time when it was compiled from this text, so the process neither parses nor compiles it until
10
+ * something asks for its nodes (an error's site, a saved state's); compiled now otherwise. A test checks the saved
11
+ * bytecode is what the compiler makes. Its top-level functions start at a line's start; they are found by name.
12
+ */
13
+ export const builtinCodes = (id, text) => {
14
+ const tree = () => parse(text, { ecmaVersion: "latest", sourceType: "script" });
15
+ const ahead = saved[id];
16
+ const lazy = new Source(id, text, tree);
17
+ const codes = ahead?.text === hash(text)
18
+ ? ahead.codes.map((code) => codeFromJson(code, lazy))
19
+ : compileSource(new Source(id, text, tree()), { internal: true });
20
+ const byName = new Map();
21
+ // A top-level function comes before the functions nested in it, in pre-order.
22
+ for (const [, name] of text.matchAll(/^(?:async )?function\*? ?([\w$]+)/gm)) {
23
+ byName.set(name, codes.find((made) => made.index > 0 && made.name === name));
24
+ }
25
+ return { codes, byName };
26
+ };
27
+ /** What `script/prelude.ts` saves for a built-in source. */
28
+ export const saveCodes = (text, codes) => ({
29
+ text: hash(text),
30
+ codes: codes.map(codeToJson),
31
+ });