jz 0.1.1 → 0.2.1

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.
package/src/ctx.js CHANGED
@@ -7,20 +7,38 @@
7
7
  * Refactored into focused sub-contexts for better maintainability.
8
8
  */
9
9
 
10
- // === NaN-boxing pointer type codes ===
11
- // SEALED: 4-bit tag (values 0-15). Layout is hardcoded in dispatch funcs
12
- // (__length/__typeof/__to_str/__ptr_type) and the static-data strip pass
13
- // ([src/compile.js] SHIFTABLE). No plugin/extension API internal A/B
14
- // measurement goes through `ctx.features.*` flags instead (see reset()).
10
+ // === Carrier layout ===
11
+ // i64 carrier holds either:
12
+ // - raw f64 number bits (any non-NaN-shape pattern), discriminated by
13
+ // `f64.eq(f, f)` true for real numbers, false for NaN-shape pointers.
14
+ // - NaN-shape tagged pointer: [63:51]=NAN_PREFIX | [50:47]=tag | [46:32]=aux | [31:0]=offset.
15
+ //
16
+ // LAYOUT is the single source of truth. WAT templates reference
17
+ // `${LAYOUT.TAG_SHIFT}` etc. so a layout change propagates by re-evaluation.
18
+ // Hot dispatch (__ptr_type/__ptr_aux/__ptr_offset) keeps the inline expansion
19
+ // for codegen size; those sites are commented as LAYOUT-tied.
20
+ export const LAYOUT = {
21
+ TAG_SHIFT: 47,
22
+ TAG_MASK: 0xF, // 4-bit tag (16 type slots)
23
+ AUX_SHIFT: 32,
24
+ AUX_MASK: 0x7FFF, // 15-bit aux (schemaId, elemType, atomId, …)
25
+ OFFSET_MASK: 0xFFFFFFFF, // 32-bit offset (4GB heap)
26
+ NAN_PREFIX: 0x7FF8, // top 13 bits of any NaN-shape pointer
27
+ NAN_PREFIX_BITS: 0x7FF8000000000000n, // pre-shifted for i64 OR
28
+ SSO_BIT: 0x4000, // STRING aux bit 14: 1=inline (≤4 ASCII chars in offset), 0=heap
29
+ }
30
+
31
+ // === Tagged-pointer type codes ===
32
+ // 4-bit tag (LAYOUT.TAG_MASK = 16 slots).
15
33
  // To retire a type, gate emission behind a feature flag and drop its dispatch
16
- // branches; to add, renumber with care — all hardcoded branches must update.
34
+ // branches; to add, renumber with care — hardcoded branches in module/* must update.
17
35
  export const PTR = {
18
- ATOM: 0, // null, undefined, booleans
36
+ ATOM: 0, // null, undefined, booleans, symbols (aux=atomId)
19
37
  ARRAY: 1, // heap-allocated arrays
20
38
  BUFFER: 2, // ArrayBuffer: [-8:byteLen][-4:byteCap][bytes]
21
39
  TYPED: 3, // TypedArrays (Float64Array, etc.)
22
- STRING: 4, // heap-allocated strings
23
- SSO: 5, // short string optimization (≤4 ASCII chars inline)
40
+ STRING: 4, // strings (heap or inline-SSO; aux bit LAYOUT.SSO_BIT distinguishes)
41
+ // 5: free
24
42
  OBJECT: 6, // plain objects
25
43
  HASH: 7, // dynamic objects (Map-like)
26
44
  SET: 8, // Set collections
@@ -97,6 +115,9 @@ export function reset(proto, globals) {
97
115
  stdlib: {},
98
116
  stdlibDeps: {}, // populated per-module at init time (was STDLIB_DEPS in this file)
99
117
  includes: new Set(),
118
+ extImports: new Set(), // __ext_* helpers actually emitted as env imports —
119
+ // pullStdlib() removes them from `includes` after wiring,
120
+ // so post-compile auditors (host: 'wasi') read this instead.
100
121
  }
101
122
 
102
123
 
@@ -195,10 +216,15 @@ export function reset(proto, globals) {
195
216
  noTailCall: false, // when true, emit `return call` instead of `return_call` (wasm2c compat)
196
217
  strict: false, // when true, dynamic features (obj[k], for-in) error at compile time
197
218
  // instead of pulling in dynamic-dispatch stdlib. See ProgramFacts walk.
198
- runtimeExports: true, // when false, omit helper exports like _alloc/_reset from raw wasm output.
219
+ alloc: true, // when false, omit raw allocator exports like _alloc/_clear from wasm output.
199
220
  optimize: null, // resolved {watr, hoistPtrType, ...} config — set in index.js via resolveOptimize().
200
221
  // Read by optimizeModule() (compile.js) and the post-watr pass (index.js).
201
222
  // null is treated as level 2 (all on) for back-compat with internal callers.
223
+ importMetaUrl: null, // compile-time URL for import.meta.url / import.meta.resolve static lowering.
224
+ host: 'js', // 'js' (default): allow `env.__ext_*` imports to be wired by the JS host at
225
+ // instantiation time. 'wasi': error at compile time if any `__ext_*` import
226
+ // would be emitted, since wasmtime/wasmer hosts have no JS runtime to satisfy
227
+ // them and silent fallback would corrupt output.
202
228
  }
203
229
 
204
230
  // Feature flags: capabilities the compiled module may exercise at runtime.
@@ -238,7 +264,10 @@ export function err(msg) {
238
264
  const line = before.split('\n').length
239
265
  const col = ctx.error.loc - before.lastIndexOf('\n')
240
266
  const src = ctx.error.src.split('\n')[line - 1]
241
- throw Error(`${msg}\n at line ${line}:${col}\n ${src}\n ${' '.repeat(col - 1)}^`)
267
+ const detail = `${msg}\n at line ${line}:${col}\n ${src}\n ${' '.repeat(col - 1)}^`
268
+ const e = new Error(detail)
269
+ e.stack = `${e.name}: ${detail}\n${e.stack.split('\n').slice(1).join('\n')}`
270
+ throw e
242
271
  }
243
- throw Error(msg)
272
+ throw new Error(msg)
244
273
  }