@loopdive/js2 0.57.0

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 (233) hide show
  1. package/CHANGELOG.md +1425 -0
  2. package/LICENSE +189 -0
  3. package/README.md +451 -0
  4. package/dist/checker/index.d.ts +117 -0
  5. package/dist/checker/language-service.d.ts +39 -0
  6. package/dist/checker/node-capability-map.d.ts +63 -0
  7. package/dist/checker/type-mapper.d.ts +84 -0
  8. package/dist/cjs-rewrite.d.ts +19 -0
  9. package/dist/cli.d.ts +2 -0
  10. package/dist/cli.js +363 -0
  11. package/dist/codegen/accessor-driver.d.ts +97 -0
  12. package/dist/codegen/any-helpers.d.ts +72 -0
  13. package/dist/codegen/array-element-typing.d.ts +46 -0
  14. package/dist/codegen/array-holes.d.ts +69 -0
  15. package/dist/codegen/array-methods.d.ts +68 -0
  16. package/dist/codegen/array-object-proto.d.ts +64 -0
  17. package/dist/codegen/array-reduce-fusion.d.ts +31 -0
  18. package/dist/codegen/array-to-primitive.d.ts +28 -0
  19. package/dist/codegen/async-cps.d.ts +239 -0
  20. package/dist/codegen/async-scheduler.d.ts +349 -0
  21. package/dist/codegen/binary-ops.d.ts +78 -0
  22. package/dist/codegen/binding-info.d.ts +31 -0
  23. package/dist/codegen/builtin-scaffold.d.ts +98 -0
  24. package/dist/codegen/builtin-static-globals.d.ts +8 -0
  25. package/dist/codegen/builtin-tags.d.ts +189 -0
  26. package/dist/codegen/case-convert-native.d.ts +12 -0
  27. package/dist/codegen/case-tables.d.ts +4 -0
  28. package/dist/codegen/class-bodies.d.ts +41 -0
  29. package/dist/codegen/class-member-keys.d.ts +33 -0
  30. package/dist/codegen/class-to-primitive.d.ts +39 -0
  31. package/dist/codegen/closed-method-dispatch.d.ts +42 -0
  32. package/dist/codegen/closures.d.ts +285 -0
  33. package/dist/codegen/coercion-engine.d.ts +154 -0
  34. package/dist/codegen/coercion-plan.d.ts +29 -0
  35. package/dist/codegen/context/bodies.d.ts +4 -0
  36. package/dist/codegen/context/create-context.d.ts +4 -0
  37. package/dist/codegen/context/errors.d.ts +39 -0
  38. package/dist/codegen/context/locals.d.ts +69 -0
  39. package/dist/codegen/context/source-pos.d.ts +5 -0
  40. package/dist/codegen/context/speculative.d.ts +95 -0
  41. package/dist/codegen/context/types.d.ts +1936 -0
  42. package/dist/codegen/custom-iterable.d.ts +34 -0
  43. package/dist/codegen/dataview-native.d.ts +51 -0
  44. package/dist/codegen/date-parse-native.d.ts +13 -0
  45. package/dist/codegen/dead-elimination.d.ts +26 -0
  46. package/dist/codegen/declarations.d.ts +147 -0
  47. package/dist/codegen/deno-api.d.ts +11 -0
  48. package/dist/codegen/destructuring-params.d.ts +102 -0
  49. package/dist/codegen/dyn-read.d.ts +26 -0
  50. package/dist/codegen/eval-tiering.d.ts +19 -0
  51. package/dist/codegen/expressions/assignment.d.ts +61 -0
  52. package/dist/codegen/expressions/builtins.d.ts +26 -0
  53. package/dist/codegen/expressions/calls-closures.d.ts +54 -0
  54. package/dist/codegen/expressions/calls-guards.d.ts +49 -0
  55. package/dist/codegen/expressions/calls-optional.d.ts +4 -0
  56. package/dist/codegen/expressions/calls.d.ts +83 -0
  57. package/dist/codegen/expressions/eval-inline.d.ts +24 -0
  58. package/dist/codegen/expressions/extern.d.ts +67 -0
  59. package/dist/codegen/expressions/fnctor-prototype.d.ts +52 -0
  60. package/dist/codegen/expressions/helpers.d.ts +212 -0
  61. package/dist/codegen/expressions/identifiers.d.ts +57 -0
  62. package/dist/codegen/expressions/late-imports.d.ts +81 -0
  63. package/dist/codegen/expressions/logical-ops.d.ts +18 -0
  64. package/dist/codegen/expressions/misc.d.ts +27 -0
  65. package/dist/codegen/expressions/new-super.d.ts +25 -0
  66. package/dist/codegen/expressions/promise-subclass.d.ts +38 -0
  67. package/dist/codegen/expressions/proto-override.d.ts +63 -0
  68. package/dist/codegen/expressions/unary-updates.d.ts +21 -0
  69. package/dist/codegen/expressions/unary.d.ts +6 -0
  70. package/dist/codegen/expressions.d.ts +31 -0
  71. package/dist/codegen/fallback-telemetry.d.ts +53 -0
  72. package/dist/codegen/fixups.d.ts +80 -0
  73. package/dist/codegen/fmod.d.ts +10 -0
  74. package/dist/codegen/fnctor-escape-gate.d.ts +92 -0
  75. package/dist/codegen/function-body.d.ts +52 -0
  76. package/dist/codegen/generators-native.d.ts +92 -0
  77. package/dist/codegen/helpers/body-references-own-this.d.ts +22 -0
  78. package/dist/codegen/helpers/body-uses-arguments.d.ts +12 -0
  79. package/dist/codegen/helpers/is-strict-function.d.ts +52 -0
  80. package/dist/codegen/host-import-allowlist.d.ts +140 -0
  81. package/dist/codegen/index.d.ts +500 -0
  82. package/dist/codegen/ir-tail-call.d.ts +8 -0
  83. package/dist/codegen/iterator-native.d.ts +44 -0
  84. package/dist/codegen/json-codec-native.d.ts +78 -0
  85. package/dist/codegen/json-runtime.d.ts +35 -0
  86. package/dist/codegen/json-standalone.d.ts +25 -0
  87. package/dist/codegen/linear-uint8-analysis.d.ts +46 -0
  88. package/dist/codegen/linear-uint8-arena.d.ts +7 -0
  89. package/dist/codegen/linear-uint8-codegen.d.ts +103 -0
  90. package/dist/codegen/linear-uint8-signatures.d.ts +26 -0
  91. package/dist/codegen/literals.d.ts +115 -0
  92. package/dist/codegen/map-runtime.d.ts +142 -0
  93. package/dist/codegen/math-helpers.d.ts +7 -0
  94. package/dist/codegen/member-get-dispatch.d.ts +42 -0
  95. package/dist/codegen/member-set-dispatch.d.ts +28 -0
  96. package/dist/codegen/native-proto.d.ts +98 -0
  97. package/dist/codegen/native-regex.d.ts +158 -0
  98. package/dist/codegen/native-strings.d.ts +146 -0
  99. package/dist/codegen/new-target.d.ts +30 -0
  100. package/dist/codegen/node-fs-api.d.ts +47 -0
  101. package/dist/codegen/number-format-native.d.ts +9 -0
  102. package/dist/codegen/number-ryu.d.ts +27 -0
  103. package/dist/codegen/object-ops.d.ts +94 -0
  104. package/dist/codegen/object-runtime.d.ts +171 -0
  105. package/dist/codegen/parse-number-native.d.ts +10 -0
  106. package/dist/codegen/peephole.d.ts +6 -0
  107. package/dist/codegen/property-access.d.ts +294 -0
  108. package/dist/codegen/raw-wasi-api.d.ts +13 -0
  109. package/dist/codegen/regex/bytecode.d.ts +140 -0
  110. package/dist/codegen/regex/casefold.d.ts +41 -0
  111. package/dist/codegen/regex/compile.d.ts +51 -0
  112. package/dist/codegen/regex/parse.d.ts +76 -0
  113. package/dist/codegen/regex/unicode.d.ts +42 -0
  114. package/dist/codegen/regex/vm.d.ts +24 -0
  115. package/dist/codegen/regexp-standalone.d.ts +350 -0
  116. package/dist/codegen/registry/error-types.d.ts +38 -0
  117. package/dist/codegen/registry/imports.d.ts +46 -0
  118. package/dist/codegen/registry/types.d.ts +59 -0
  119. package/dist/codegen/set-algebra.d.ts +17 -0
  120. package/dist/codegen/set-runtime.d.ts +74 -0
  121. package/dist/codegen/shared.d.ts +111 -0
  122. package/dist/codegen/stack-balance.d.ts +43 -0
  123. package/dist/codegen/statements/control-flow.d.ts +25 -0
  124. package/dist/codegen/statements/destructuring.d.ts +177 -0
  125. package/dist/codegen/statements/exceptions.d.ts +11 -0
  126. package/dist/codegen/statements/functions.d.ts +1 -0
  127. package/dist/codegen/statements/index.d.ts +1 -0
  128. package/dist/codegen/statements/loops.d.ts +7 -0
  129. package/dist/codegen/statements/nested-declarations.d.ts +78 -0
  130. package/dist/codegen/statements/shared.d.ts +38 -0
  131. package/dist/codegen/statements/tdz.d.ts +43 -0
  132. package/dist/codegen/statements/variables.d.ts +3 -0
  133. package/dist/codegen/statements.d.ts +9 -0
  134. package/dist/codegen/string-builder.d.ts +131 -0
  135. package/dist/codegen/string-ops.d.ts +87 -0
  136. package/dist/codegen/struct-accessor-closure.d.ts +36 -0
  137. package/dist/codegen/symbol-native.d.ts +55 -0
  138. package/dist/codegen/temporal-native.d.ts +8 -0
  139. package/dist/codegen/timsort.d.ts +2 -0
  140. package/dist/codegen/type-coercion.d.ts +123 -0
  141. package/dist/codegen/typeof-delete.d.ts +38 -0
  142. package/dist/codegen/uri-encoding-native.d.ts +33 -0
  143. package/dist/codegen/value-tags.d.ts +74 -0
  144. package/dist/codegen/walk-instructions.d.ts +20 -0
  145. package/dist/codegen/weak-collections-runtime.d.ts +16 -0
  146. package/dist/codegen/with-scope.d.ts +106 -0
  147. package/dist/codegen-linear/c-abi.d.ts +74 -0
  148. package/dist/codegen-linear/context.d.ts +86 -0
  149. package/dist/codegen-linear/index.d.ts +28 -0
  150. package/dist/codegen-linear/layout.d.ts +39 -0
  151. package/dist/codegen-linear/runtime.d.ts +161 -0
  152. package/dist/codegen-linear/simd.d.ts +7 -0
  153. package/dist/compiler/define-substitution.d.ts +27 -0
  154. package/dist/compiler/early-errors/assignment.d.ts +26 -0
  155. package/dist/compiler/early-errors/context.d.ts +17 -0
  156. package/dist/compiler/early-errors/duplicates.d.ts +20 -0
  157. package/dist/compiler/early-errors/index.d.ts +11 -0
  158. package/dist/compiler/early-errors/labels.d.ts +13 -0
  159. package/dist/compiler/early-errors/module-rules.d.ts +36 -0
  160. package/dist/compiler/early-errors/node-checks.d.ts +7 -0
  161. package/dist/compiler/early-errors/predicates.d.ts +140 -0
  162. package/dist/compiler/early-errors/tdz.d.ts +17 -0
  163. package/dist/compiler/import-manifest.d.ts +18 -0
  164. package/dist/compiler/output.d.ts +46 -0
  165. package/dist/compiler/validation.d.ts +45 -0
  166. package/dist/compiler.d.ts +48 -0
  167. package/dist/define-substitution-BcUeKC2A.js +109 -0
  168. package/dist/emit/binary.d.ts +50 -0
  169. package/dist/emit/c-header.d.ts +23 -0
  170. package/dist/emit/canonical-recgroup.d.ts +86 -0
  171. package/dist/emit/encoder.d.ts +28 -0
  172. package/dist/emit/object.d.ts +14 -0
  173. package/dist/emit/opcodes.d.ts +464 -0
  174. package/dist/emit/sourcemap.d.ts +33 -0
  175. package/dist/emit/wat.d.ts +6 -0
  176. package/dist/env.d.ts +46 -0
  177. package/dist/import-resolver.d.ts +68 -0
  178. package/dist/index.d.ts +486 -0
  179. package/dist/index.js +755 -0
  180. package/dist/ir/alloc-registry.d.ts +75 -0
  181. package/dist/ir/analysis/encoding.d.ts +38 -0
  182. package/dist/ir/analysis/escape.d.ts +32 -0
  183. package/dist/ir/analysis/lattice.d.ts +72 -0
  184. package/dist/ir/analysis/ownership.d.ts +31 -0
  185. package/dist/ir/analysis/stack-alloc.d.ts +20 -0
  186. package/dist/ir/backend/bytecode-emitter.d.ts +237 -0
  187. package/dist/ir/backend/bytecode-vm.d.ts +74 -0
  188. package/dist/ir/backend/emitter.d.ts +121 -0
  189. package/dist/ir/backend/handles.d.ts +133 -0
  190. package/dist/ir/backend/legality.d.ts +9 -0
  191. package/dist/ir/backend/linear-emitter.d.ts +41 -0
  192. package/dist/ir/backend/wasmgc-emitter.d.ts +43 -0
  193. package/dist/ir/builder.d.ts +401 -0
  194. package/dist/ir/from-ast.d.ts +192 -0
  195. package/dist/ir/index.d.ts +16 -0
  196. package/dist/ir/integration.d.ts +27 -0
  197. package/dist/ir/lower.d.ts +203 -0
  198. package/dist/ir/nodes.d.ts +1452 -0
  199. package/dist/ir/passes/alloc-discipline.d.ts +19 -0
  200. package/dist/ir/passes/constant-fold.d.ts +7 -0
  201. package/dist/ir/passes/dead-code.d.ts +18 -0
  202. package/dist/ir/passes/inline-small.d.ts +7 -0
  203. package/dist/ir/passes/monomorphize.d.ts +21 -0
  204. package/dist/ir/passes/simplify-cfg.d.ts +19 -0
  205. package/dist/ir/passes/tagged-union-types.d.ts +45 -0
  206. package/dist/ir/passes/tagged-unions.d.ts +22 -0
  207. package/dist/ir/propagate.d.ts +135 -0
  208. package/dist/ir/select.d.ts +81 -0
  209. package/dist/ir/types.d.ts +832 -0
  210. package/dist/ir/verify-alloc.d.ts +18 -0
  211. package/dist/ir/verify.d.ts +7 -0
  212. package/dist/link/index.d.ts +11 -0
  213. package/dist/link/isolation.d.ts +24 -0
  214. package/dist/link/linker.d.ts +37 -0
  215. package/dist/link/reader.d.ts +158 -0
  216. package/dist/link/resolver.d.ts +19 -0
  217. package/dist/optimize.d.ts +54 -0
  218. package/dist/optimize.js +262 -0
  219. package/dist/position-map.d.ts +64 -0
  220. package/dist/process-stdin-prelude.d.ts +16 -0
  221. package/dist/resolve.d.ts +82 -0
  222. package/dist/runtime/builtins.d.ts +1 -0
  223. package/dist/runtime-C-4q_KwU.js +164438 -0
  224. package/dist/runtime-containment.d.ts +6 -0
  225. package/dist/runtime-eval.d.ts +132 -0
  226. package/dist/runtime-instantiate.d.ts +16 -0
  227. package/dist/runtime.d.ts +128 -0
  228. package/dist/runtime.js +12 -0
  229. package/dist/shape-inference.d.ts +20 -0
  230. package/dist/treeshake.d.ts +17 -0
  231. package/dist/ts-api.d.ts +30 -0
  232. package/dist/wit-generator.d.ts +18 -0
  233. package/package.json +187 -0
@@ -0,0 +1,154 @@
1
+ import { Instr, ValType } from '../ir/types.js';
2
+ import { ts } from '../ts-api.js';
3
+ import { CodegenContext, FunctionContext } from './context/types.js';
4
+ /**
5
+ * The three coercion modes the backend dispatches over. Derived once from the
6
+ * three ad-hoc spellings that exist today so callers stop re-testing them.
7
+ *
8
+ * - `"standalone"` — `noJsHost(ctx)` (--target wasi / standalone): pure
9
+ * Wasm, native `$AnyString` strings, no JS host bridge.
10
+ * - `"native-strings-host"` — `ctx.nativeStrings` with a JS host present:
11
+ * native `$AnyString` strings but the host `__extern_*` bridge is available.
12
+ * - `"js-host"` — classic externref `wasm:js-string` strings.
13
+ */
14
+ export type CoercionMode = "js-host" | "native-strings-host" | "standalone";
15
+ export declare function coercionMode(ctx: CodegenContext): CoercionMode;
16
+ /**
17
+ * The ToString hint, distinguishing the genuinely-different per-context spec
18
+ * operations the matrices encode for a struct/ref operand:
19
+ * - `"string"` — ToString proper (§7.1.17): a template span (`` `${obj}` ``)
20
+ * and `String(obj)` walk @@toPrimitive("string")/toString.
21
+ * - `"default"` — the `+` operator's ToPrimitive(default) (valueOf-first,
22
+ * §7.1.1.1 / #2022): `obj + ""` must use `valueOf`. Only differs from
23
+ * `"string"` on the struct/ref arm.
24
+ */
25
+ export type ToStringHint = "string" | "default";
26
+ /**
27
+ * Emit `ToString(operand)` for an operand that has ALREADY been compiled to the
28
+ * top of the value stack with ValType `valType` and static TS type `tsType`.
29
+ *
30
+ * This is the consolidated cascade the expression-based ToString copies shared:
31
+ * void → "undefined"
32
+ * i32 boolean → "true"/"false" (emitBoolToString)
33
+ * f64/i32/i64 → number_toString (numeric stringify)
34
+ * externref null → "null"
35
+ * externref undef → "undefined"
36
+ * externref str → passthrough (already a string)
37
+ * externref other → __extern_toString / __extern_to_string_default (by hint)
38
+ * ref/ref_null → tryStructToString → $__any_to_string (native)
39
+ * coerceType(hint) / __extern_to_string_default (host)
40
+ *
41
+ * Returns the ValType left on the stack (externref in js-host mode, a native
42
+ * `ref $AnyString` in native/standalone mode), or `null` for the void arm where
43
+ * a string literal was pushed (callers treat both as "a string is on the stack").
44
+ *
45
+ * Each caller passes its own `hint` so the per-context policy (template "string"
46
+ * vs `+` "default") is preserved byte-for-byte.
47
+ *
48
+ * NOTE on Symbol: §7.1.17 ToString(Symbol) throws TypeError. The callers guard
49
+ * that BEFORE compiling the operand (`tryThrowOnSymbolStringCoercion`), because
50
+ * the throw must short-circuit operand evaluation; the engine assumes a
51
+ * non-symbol operand on the stack.
52
+ */
53
+ export declare function emitToString(ctx: CodegenContext, fctx: FunctionContext, valType: ValType | null, tsType: ts.Type, hint: ToStringHint): ValType;
54
+ /**
55
+ * Compile `operand` then emit `ToString(operand)`. The expression-based
56
+ * convenience wrapper for the `+`-concat / String() callers. Symbol guarding is
57
+ * the caller's responsibility (must short-circuit before operand evaluation).
58
+ */
59
+ export declare function compileAndEmitToString(ctx: CodegenContext, fctx: FunctionContext, operand: ts.Expression, tsType: ts.Type, hint: ToStringHint): ValType;
60
+ /**
61
+ * Emit `ToNumber(operand)` (→ f64) for an operand ALREADY compiled to the top of
62
+ * the value stack with ValType `valType`. The consolidation of the `Number(x)`
63
+ * matrix (N2) and the unary `+`/`-`/`~` coercion arms (N1):
64
+ *
65
+ * i64 (BigInt) → f64.convert_i64_s
66
+ * externref → __unbox_number (js-host) | coerceType(f64,"number") (standalone)
67
+ * ref/ref_null → coerceType(f64,"number") (object @@toPrimitive("number")/valueOf)
68
+ * i32 (number/bool) → f64.convert_i32_s
69
+ * f64 → no-op
70
+ *
71
+ * Returns the ValType left on the stack (`{kind:"f64"}` for every coercing arm;
72
+ * the original `valType` for the already-f64 no-op).
73
+ *
74
+ * NOTE — what stays in the caller (NOT folded here, because each is a *source*
75
+ * special-case that must run BEFORE the operand is on the stack as a plain
76
+ * value, or is policy that differs per caller):
77
+ * - ToNumber(Symbol) throws TypeError (§7.1.4) — guarded before operand eval.
78
+ * - the #2160 `Number(arr)` array→ToString→StringToNumber pre-check.
79
+ * - the native-string-ref (`$AnyString`/`$NativeString`) → `__str_to_number`
80
+ * (§7.1.4.1) arm — it dispatches on the ref's *typeIdx* (string-struct vs a
81
+ * generic object struct), which the caller already resolves; a generic ref
82
+ * falls to `coerceType(f64,"number")` here.
83
+ * The caller handles those, then calls `emitToNumber` for the remaining cascade.
84
+ */
85
+ export declare function emitToNumber(ctx: CodegenContext, fctx: FunctionContext, valType: ValType | null): ValType;
86
+ /**
87
+ * Append `ToBoolean(value)` (§7.1.2 → i32, 1 = truthy) for a value of ValType
88
+ * `valType` already on the stack into `sink`. The consolidation of the two
89
+ * hand-rolled truthiness sites that #2085 already aligned:
90
+ * - `ensureI32Condition` (index.ts, B1 — the canonical, pushes to `fctx.body`)
91
+ * - `buildToBooleanInstrs` (array-methods.ts, B2 — returns an `Instr[]`)
92
+ *
93
+ * The `sink` parameter unifies those two emission styles: B1 passes `fctx.body`,
94
+ * B2 passes a fresh array it then returns. Both produce the SAME sequence — this
95
+ * is behaviour-neutral (the #2085 fix already made B2 use `|x|>0` like B1, so
96
+ * there is no longer a NaN-truthy divergence to surface).
97
+ *
98
+ * null valType → i32.const 0 (compile failed upstream → keep Wasm valid: falsy)
99
+ * f64 → |x| > 0 (NaN, +0, -0 all falsy)
100
+ * externref → __is_truthy (0/NaN/null/undefined/"" → falsy); ref.is_null fallback
101
+ * any-boxed ref→ __any_unbox_bool (proper JS truthiness on the boxed value)
102
+ * native str ref→ flatten → len > 0 (empty string falsy)
103
+ * other ref → non-null (ref.is_null; i32.eqz)
104
+ * i64 → nonzero
105
+ * i32 → as-is (already 0/1-valued)
106
+ */
107
+ export declare function emitToBoolean(ctx: CodegenContext, valType: ValType | null, sink: Instr[]): Instr[];
108
+ /**
109
+ * #1917 Step E3 — `emitStrictEq` / `emitLooseEq`: the **dispatch layer** for
110
+ * `any`-operand equality. These decide WHICH helper to call (`__any_strict_eq`
111
+ * for `===`/`!==`; `__any_eq` for `==`/`!=`), marshal both operands into the
112
+ * boxed `$AnyValue` shape, emit the `call`, and apply the `!=`/`!==` negation.
113
+ *
114
+ * They are a WRAPPER, not a re-derivation: the tag-5 field-4 3-way classifier
115
+ * (boxed-number vs proto-identity vs content equality, #2040/#2585 — the
116
+ * `tag5StringEqThen` machinery) lives in the `__any_eq`/`__any_strict_eq` helper
117
+ * *bodies* in any-helpers.ts. The engine never copies that logic; it only
118
+ * selects the helper and boxes the operands. This is a byte-neutral extraction
119
+ * of the equality arms of `compileAnyBinaryDispatch`.
120
+ *
121
+ * @param negate when true, the loose/strict-equal result is i32.eqz'd (the
122
+ * `!=` / `!==` half). The helper itself always computes `==`/`===`.
123
+ * @returns the i32 result ValType, or `null` if the helper is unavailable or an
124
+ * operand failed to compile (caller falls back).
125
+ */
126
+ export declare function emitStrictEq(ctx: CodegenContext, fctx: FunctionContext, expr: ts.BinaryExpression, negate: boolean): ValType | null;
127
+ export declare function emitLooseEq(ctx: CodegenContext, fctx: FunctionContext, expr: ts.BinaryExpression, negate: boolean): ValType | null;
128
+ /**
129
+ * #1917 equality finale, slice E6 — the standalone externref-vs-externref
130
+ * loose-equality tail.
131
+ *
132
+ * For two opaque `any` externref operands that were NOT eqref-identical, the
133
+ * standalone/WASI lane (no JS host) routes through the NATIVE §7.2.15
134
+ * IsLooselyEqual instead of the unsatisfiable `__host_loose_eq` import (#2081):
135
+ * box both externrefs to `$AnyValue` via `__any_from_extern` (tag5 string / tag3
136
+ * number / tag4 bool / tag1 null) and call the keystone `__any_eq` helper (whose
137
+ * tag-5 field-4 classifier owns the String⇄Number / proto-identity arms). This
138
+ * is the SAME tag-5-sensitive boxing E3 does for the any/any case, just sourced
139
+ * from two pre-computed externref temps instead of freshly-compiled operands.
140
+ *
141
+ * Returns the instruction SEQUENCE (this caller builds an `Instr[]` for an
142
+ * `if`-arm, it does not emit live), or `null` when the helpers are unavailable so
143
+ * the caller can fall through to its host-import path exactly as before. WRAPPER,
144
+ * not a re-derivation: the classifier stays in `__any_eq`'s body (any-helpers.ts).
145
+ *
146
+ * @param tmpLeft local index holding the left externref operand.
147
+ * @param tmpRight local index holding the right externref operand.
148
+ * @param negate append `i32.eqz` for the `!=` form.
149
+ */
150
+ export declare function emitAnyEqFromExternTemps(ctx: CodegenContext, tmpLeft: number, tmpRight: number, negate: boolean): Instr[] | null;
151
+ export declare function registerStringHelperEmitters(emitters: {
152
+ boolToString: (ctx: CodegenContext, fctx: FunctionContext) => void;
153
+ nativeStringRefFromExternref: (ctx: CodegenContext, fctx: FunctionContext) => void;
154
+ }): void;
@@ -0,0 +1,29 @@
1
+ import { Instr, ValType } from '../ir/types.js';
2
+ /** Box/unbox helper funcIdxs the numeric box-unbox rows need (null = unavailable). */
3
+ export interface CoercionHelpers {
4
+ boxNumberIdx: number | null;
5
+ unboxNumberIdx: number | null;
6
+ }
7
+ export interface CoercionPlan {
8
+ instrs: Instr[];
9
+ /**
10
+ * True when the sequence cannot preserve the value (drops it and pushes a
11
+ * placeholder). A lossy plan in a branch/local fixup masks an upstream
12
+ * emitter bug; #1918 will surface these as diagnostics. Today only the
13
+ * genuine "no representation bridge exists" rows are lossy.
14
+ */
15
+ lossy?: boolean;
16
+ }
17
+ /**
18
+ * The canonical scalar / numeric / box-unbox coercion table.
19
+ *
20
+ * Returns the instruction sequence to convert a value of ValType `from` (top of
21
+ * stack) to ValType `to`, or `null` when this pair is **not** a row this table
22
+ * owns — i.e. it needs an `fctx` temporary (guarded ref.cast: externref/eqref/
23
+ * anyref → ref/ref_null) or `ctx` (AnyValue→externref helper). Callers fall
24
+ * back to their own handling for `null`.
25
+ *
26
+ * Same-kind pairs return `{ instrs: [] }` (no-op) so callers can treat a
27
+ * non-null result as "handled".
28
+ */
29
+ export declare function coercionPlan(from: ValType, to: ValType, helpers: CoercionHelpers): CoercionPlan | null;
@@ -0,0 +1,4 @@
1
+ import { Instr } from '../../ir/types.js';
2
+ import { FunctionContext } from './types.js';
3
+ export declare function pushBody(fctx: FunctionContext): Instr[];
4
+ export declare function popBody(fctx: FunctionContext, saved: Instr[]): void;
@@ -0,0 +1,4 @@
1
+ import { ts } from '../../ts-api.js';
2
+ import { WasmModule } from '../../ir/types.js';
3
+ import { CodegenContext, CodegenOptions } from './types.js';
4
+ export declare function createCodegenContext(mod: WasmModule, checker: ts.TypeChecker, options?: CodegenOptions): CodegenContext;
@@ -0,0 +1,39 @@
1
+ import { ts } from '../../ts-api.js';
2
+ import { CodegenContext, CodegenError } from './types.js';
3
+ /**
4
+ * #1921 — diagnostic severity for the helpers below. Omitting it defaults to
5
+ * `"error"`, which fails the build. Pass `"degrade"` only for a *deliberate*
6
+ * compile-with-fallback-value site (and reference the tracking issue in the
7
+ * message), or `"warning"` for a purely informational diagnostic.
8
+ */
9
+ type ReportSeverity = NonNullable<CodegenError["severity"]>;
10
+ /**
11
+ * #1921 — does this codegen diagnostic fail the build?
12
+ *
13
+ * The compile-failure gate keys on {@link CodegenError.severity}, not on a
14
+ * `"Codegen error:"` message prefix. An omitted severity is treated as
15
+ * `"error"` so a forgotten classification fails loudly instead of silently
16
+ * degrading the binary with a stack-balancer placeholder (#1918). `"warning"`
17
+ * (IR-fallback channel) and `"degrade"` (deliberate compile-with-fallback-value)
18
+ * are non-fatal.
19
+ */
20
+ export declare function isFatalCodegenDiagnostic(err: {
21
+ severity?: CodegenError["severity"];
22
+ }): boolean;
23
+ /**
24
+ * Report a compile error with source location extracted from the given AST node.
25
+ * Falls back to ctx.lastKnownNode when the node lacks source file context.
26
+ *
27
+ * #1921 — the diagnostic carries an explicit `severity: "error"` by default so
28
+ * the compile-failure gate keys on severity rather than a `"Codegen error:"`
29
+ * message prefix. Pass `"degrade"` for a deliberate compile-with-fallback site.
30
+ */
31
+ export declare function reportError(ctx: CodegenContext, node: ts.Node, message: string, severity?: ReportSeverity): void;
32
+ /**
33
+ * Report a compile error when no AST node is available.
34
+ * Uses ctx.lastKnownNode for location if possible.
35
+ *
36
+ * #1921 — defaults to `severity: "error"` (see {@link reportError}).
37
+ */
38
+ export declare function reportErrorNoNode(ctx: CodegenContext, message: string, severity?: ReportSeverity): void;
39
+ export {};
@@ -0,0 +1,69 @@
1
+ import { ValType } from '../../ir/types.js';
2
+ import { FunctionContext } from './types.js';
3
+ export declare function allocLocal(fctx: FunctionContext, name: string, type: ValType): number;
4
+ /**
5
+ * #1847 — snapshot of the local-allocation state, for tentative compilation
6
+ * that may be rolled back. Captures the locals-vector length so callers can
7
+ * truncate, plus the `localMap` names present at snapshot time so we can drop
8
+ * any name `allocLocal` added afterwards (whose slot the truncation removes).
9
+ *
10
+ * Tentative-compile sites previously truncated `fctx.locals.length` (and
11
+ * `fctx.body.length`) but left `fctx.localMap` pointing at slots past the
12
+ * truncated vector — an unbalanced state. Snapshot/restore keeps the three in
13
+ * sync.
14
+ */
15
+ export interface LocalsSnapshot {
16
+ readonly localsLen: number;
17
+ /**
18
+ * Full `localMap` entries (name → slot index) at snapshot time. Restoring the
19
+ * complete map — not just the key SET — is required because a speculative
20
+ * compile may **re-point an EXISTING name** to a freshly-allocated slot, not
21
+ * only add new names: closure-capture boxing
22
+ * (`fctx.localMap.set(cap.name, boxedLocalIdx)` in closures.ts) re-aims an
23
+ * outer variable at its boxed ref-cell. A key-set-only snapshot can delete the
24
+ * newly-added box slot but leaves the outer name pointing at the (now
25
+ * truncated) box index, so a post-rollback read of that variable emits a
26
+ * `local.get` past the function's local count — `local index out of range`
27
+ * at emit time. #2029 (tagged-template tag = a closure capturing an outer
28
+ * local; the tagged-template probe boxed `calls`→slot N, rolled back, and
29
+ * `return calls` then read the stale slot N).
30
+ */
31
+ readonly mapEntries: ReadonlyArray<readonly [string, number]>;
32
+ /** `boxedCaptures` names present at snapshot time (to drop added ones). */
33
+ readonly boxedNames: ReadonlySet<string>;
34
+ }
35
+ export declare function snapshotLocals(fctx: FunctionContext): LocalsSnapshot;
36
+ /**
37
+ * #1847 — undo allocations made since `snap`: truncate the locals vector and
38
+ * restore `localMap` to its EXACT snapshot state (drop added names AND reset
39
+ * re-pointed existing names to their snapshot slot — see {@link LocalsSnapshot}
40
+ * `mapEntries` for why the value, not just the key, must be restored). Does NOT
41
+ * touch `fctx.body` — callers truncate that themselves (the body length to
42
+ * roll back to is site-specific and often captured separately).
43
+ *
44
+ * `tempFreeList` is left as-is on purpose: it only ever holds indices that were
45
+ * valid at allocation time, and the temp-local reuse path keys buckets by type;
46
+ * a rolled-back tentative compile that released a temp would have pushed a slot
47
+ * index that is now beyond `locals.length`. To keep the free-list from handing
48
+ * out a truncated slot, we prune any bucket entry that points past the new
49
+ * locals length.
50
+ */
51
+ export declare function restoreLocals(fctx: FunctionContext, snap: LocalsSnapshot): void;
52
+ export declare function allocTempLocal(fctx: FunctionContext, type: ValType): number;
53
+ export declare function releaseTempLocal(fctx: FunctionContext, index: number): void;
54
+ export declare function getLocalType(fctx: FunctionContext, index: number): ValType | undefined;
55
+ /**
56
+ * Post-processing pass: eliminate duplicate local declarations.
57
+ *
58
+ * When the same variable name appears more than once in fctx.locals (due to
59
+ * sibling block scopes, try/catch blocks, or for-loops with the same counter
60
+ * name), this merges the duplicates by:
61
+ * 1. Keeping the first occurrence of each name (lowest index)
62
+ * 2. Rewriting all local.get/set/tee instructions that reference duplicate
63
+ * slots to use the canonical (first) slot instead
64
+ * 3. Compacting fctx.locals to remove the now-unreferenced duplicate entries
65
+ *
66
+ * This handles ALL remaining duplicate local patterns uniformly, regardless
67
+ * of how they were generated (sibling for-loops, try/catch, for-of, etc.).
68
+ */
69
+ export declare function deduplicateLocals(fctx: FunctionContext): void;
@@ -0,0 +1,5 @@
1
+ import { ts } from '../../ts-api.js';
2
+ import { Instr } from '../../ir/types.js';
3
+ import { CodegenContext, SourcePos } from './types.js';
4
+ export declare function getSourcePos(ctx: CodegenContext, node: ts.Node): SourcePos | undefined;
5
+ export declare function attachSourcePos(instr: Instr, sourcePos: SourcePos | undefined): Instr;
@@ -0,0 +1,95 @@
1
+ import { CodegenContext, FunctionContext } from './types.js';
2
+ import { LocalsSnapshot } from './locals.js';
3
+ /**
4
+ * A transactional snapshot of the codegen state a speculative compile can
5
+ * mutate. Capture with {@link snapshotSpeculative}; undo with
6
+ * {@link rollbackSpeculative}. Discard (no-op) to commit.
7
+ *
8
+ * The snapshot is near-O(1): every field is an integer read or a reference copy
9
+ * (`snapshotLocals` copies the localMap ENTRIES — name→slot — so a re-pointed
10
+ * existing name can be restored, not just dropped; locals are typically tiny;
11
+ * crucially the funcMap is NOT copied — rollback derives the names to delete
12
+ * from the popped import descriptors, which each carry their `name`). This keeps
13
+ * the helper cheap enough to wrap even the hot `compileExpression` path.
14
+ */
15
+ export interface SpeculativeSnapshot {
16
+ /** `fctx.body.length` at snapshot time — the rollback truncation target. */
17
+ readonly bodyLen: number;
18
+ /** Locals / localMap / temp-free-list snapshot (#1847). */
19
+ readonly locals: LocalsSnapshot;
20
+ /** `ctx.errors.length` — diagnostics pushed during the probe are discarded. */
21
+ readonly errorsLen: number;
22
+ /** `ctx.mod.imports.length` — imports appended during the probe are popped. */
23
+ readonly importsLen: number;
24
+ /** `ctx.numImportFuncs` — restored so the func index space is rewound. */
25
+ readonly numImportFuncs: number;
26
+ /** `ctx.numImportGlobals` — restored if the probe added an import global. */
27
+ readonly numImportGlobals: number;
28
+ /**
29
+ * `ctx.pendingLateImportShift` reference at snapshot time. A probe that armed
30
+ * the deferred shift (called `ensureLateImport` while `pendingLateImportShift`
31
+ * was null) is rewound by restoring this exact reference (usually `null`).
32
+ */
33
+ readonly pendingLateImportShift: CodegenContext["pendingLateImportShift"];
34
+ /** `mod.types.length` — see {@link rollbackSpeculative} for why this is advisory. */
35
+ readonly typesLen: number;
36
+ }
37
+ /**
38
+ * Capture a transactional snapshot of all state a speculative compile may
39
+ * mutate. O(localMap.size) for the locals key-set copy; everything else is a
40
+ * handful of integer / reference reads (no funcMap copy — see
41
+ * {@link rollbackSpeculative}).
42
+ */
43
+ export declare function snapshotSpeculative(ctx: CodegenContext, fctx: FunctionContext): SpeculativeSnapshot;
44
+ /**
45
+ * Undo every mutation made since {@link snapshotSpeculative} produced `snap`.
46
+ *
47
+ * - Truncate `fctx.body` to the snapshot length (the classic rollback).
48
+ * - Restore locals / localMap / temp-free-list (#1847 `restoreLocals`).
49
+ * - Drop diagnostics pushed during the probe (`ctx.errors`).
50
+ * - Pop late imports registered during the probe off `ctx.mod.imports`, rewind
51
+ * `numImportFuncs` / `numImportGlobals`, delete their `funcMap` entries, and
52
+ * restore the deferred-shift latch. This is the leak the #1919 helper exists
53
+ * to close: because the probe never flushed the shift (the documented
54
+ * invariant), the appended imports never shifted any already-emitted index, so
55
+ * popping them and resetting the counters returns the import space exactly to
56
+ * the pre-probe state with no body re-walk required.
57
+ *
58
+ * Registered Wasm TYPES (`ctx.mod.types` / `funcTypeCache` / struct maps) are
59
+ * deliberately NOT truncated. Type registration is content-addressed and
60
+ * idempotent (`addFuncType` dedups by signature; `getOrRegisterVecType` &c. cache
61
+ * by element kind), and type indices — unlike function indices — are never
62
+ * shifted by later import additions. A type registered during a rolled-back
63
+ * probe is therefore inert: it is either reused verbatim by the committed
64
+ * re-compile or pruned by dead-type elimination if truly unreferenced. Truncating
65
+ * it would risk desyncing a struct type registered earlier and still referenced
66
+ * (see `project_type_index_shift_and_deadelim`). `typesLen` is kept on the
67
+ * snapshot only for diagnostics / a future tightening, not acted on here.
68
+ */
69
+ export declare function rollbackSpeculative(ctx: CodegenContext, fctx: FunctionContext, snap: SpeculativeSnapshot): void;
70
+ /**
71
+ * Run `fn` as a speculative compile. `fn` returns a result and decides whether
72
+ * to keep ({@link SpeculativeOutcome.commit} `true`) or discard the emitted
73
+ * state. On `commit: false` (or if `fn` throws) the entire transaction is rolled
74
+ * back via {@link rollbackSpeculative} and `value` is still returned to the
75
+ * caller (so a probe can inspect the produced type after rollback).
76
+ *
77
+ * Use this for "try to lower; keep the body iff the shape matched" sites. For
78
+ * the pure "what ValType does this compile to, then always discard" probe use
79
+ * {@link probeCompiledType}, which is a thin wrapper that always rolls back.
80
+ */
81
+ export interface SpeculativeOutcome<T> {
82
+ commit: boolean;
83
+ value: T;
84
+ }
85
+ export declare function withSpeculativeCompile<T>(ctx: CodegenContext, fctx: FunctionContext, fn: () => SpeculativeOutcome<T>): T;
86
+ /**
87
+ * The dominant probe shape: compile `fn` purely to learn the ValType it
88
+ * produces, then ALWAYS roll back every side effect (body, locals, imports,
89
+ * errors). Returns whatever `fn` returns (typically the probed `ValType | null`).
90
+ *
91
+ * This replaces the raw `const savedLen = fctx.body.length; … ;
92
+ * fctx.body.length = savedLen;` idiom — which restored only the body — with a
93
+ * full transactional rollback.
94
+ */
95
+ export declare function probeCompiledType<T>(ctx: CodegenContext, fctx: FunctionContext, fn: () => T): T;