jz 0.1.0 → 0.2.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.
package/module/schema.js CHANGED
@@ -7,7 +7,9 @@
7
7
  * @module schema
8
8
  */
9
9
 
10
- import { emit, typed, asF64, VAL, lookupValType, repOf } from '../src/compile.js'
10
+ import { typed, asF64 } from '../src/ir.js'
11
+ import { emit } from '../src/emit.js'
12
+ import { VAL, lookupValType, repOf } from '../src/analyze.js'
11
13
  import { err, inc } from '../src/ctx.js'
12
14
 
13
15
  /** Initialize schema helpers on ctx. Called once per compilation from core module. */
@@ -54,30 +56,27 @@ export function initSchema(ctx) {
54
56
  /** Emit code to load the inner value (slot 0) of a boxed variable. */
55
57
  ctx.schema.emitInner = (varName) => {
56
58
  inc('__ptr_offset')
57
- return typed(['f64.load', ['call', '$__ptr_offset', asF64(emit(varName))]], 'f64')
59
+ return typed(['f64.load', ['call', '$__ptr_offset', ['i64.reinterpret_f64', asF64(emit(varName))]]], 'f64')
58
60
  }
59
61
 
60
62
  /** Find property index by variable schema or structural subtyping.
61
- * Returns -1 to signal "use dynamic lookup" in three cases:
63
+ * Returns -1 to signal "use dynamic lookup" in four cases:
62
64
  * 1. Variable has precise schema but schema lacks the property
63
- * 2. Variable's valType is known and is not an object
64
- * 3. Structural search finds the property at inconsistent offsets across schemas
65
- * Case 3 is a real ambiguity — the caller must route to runtime dispatch.
66
- * `safe=true` disables structural subtyping when the variable's type is not
67
- * known to be VAL.OBJECT. Use for writes: a wrong slot clobbers unrelated
68
- * memory (e.g. arr.loc = ... corrupting arr[slot]). Reads only return wrong
69
- * values, which callers can tolerate. */
70
- ctx.schema.find = (varName, prop, safe = false) => {
65
+ * 2. Receiver is a string variable, but its valType is unknown or not OBJECT
66
+ * 3. Receiver is not a string variable (varName == null) — no type evidence
67
+ * 4. Structural search finds the property at inconsistent offsets across schemas
68
+ * Case 4 is a real ambiguity — the caller must route to runtime dispatch. */
69
+ ctx.schema.find = (varName, prop) => {
71
70
  // Precise: variable has known schema
72
71
  const id = ctx.schema.idOf(varName)
73
72
  if (id != null) return ctx.schema.list[id]?.indexOf(prop) ?? -1
74
- // Known non-object pointer-backed values must use dynamic property lookup,
75
- // not structural object schemas registered elsewhere in the function.
76
- if (typeof varName === 'string') {
77
- const vt = lookupValType(varName)
78
- if (vt != null && vt !== VAL.OBJECT) return -1
79
- if (safe && vt !== VAL.OBJECT) return -1
80
- }
73
+ // Structural subtyping requires positive evidence the receiver is OBJECT.
74
+ // Without it (varName is null, or its valType is unknown / not OBJECT) we
75
+ // can match HASH/ARRAY/etc. values as if they had OBJECT layout, producing
76
+ // slot reads on unrelated memory. Funnel those through dynamic dispatch.
77
+ if (typeof varName !== 'string') return -1
78
+ const vt = lookupValType(varName)
79
+ if (vt !== VAL.OBJECT) return -1
81
80
  // Structural subtyping: walk only schemas that contain this prop.
82
81
  // Consistent slot across all → return slot; any mismatch → -1 (dynamic lookup).
83
82
  const bucket = byProp.get(prop)