Skip to content

feat(json): enumerate JSON object members (hpm_json_object_len/_key) - #799

Merged
hyperpolymath merged 3 commits into
claude/gifted-lamport-k0mc5lfrom
claude/gifted-lamport-k0mc5l-json-objects
Nov 18, 2026
Merged

hyperpolymath merged 3 commits into
claude/gifted-lamport-k0mc5lfrom
claude/gifted-lamport-k0mc5l-json-objects

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

Depends on: #798

Split from #798 on the owner's ruling after review. That review found that the new object branch of to_json calls two externs with no Zig export yet, so a native build linking to_json would fail on unresolved symbols.

What

  • hpm_json_object_len and hpm_json_object_key let to_json materialise objects; on main it returns None for them, the gap json.affine documents.
  • They are lowered on Bun-ESM. Index 0 snapshots the object's own keys and later indices reuse the snapshot, so a walk does one Object.keys (review: it was one per key).
  • Integer-like keys: JavaScript lists them first, while std.json keeps document order. The externs document that callers must not rely on that relative order.

Why this waits

The matching hpm-json-rsr Zig exports (hpm_json_object_len, hpm_json_object_key) must land first, and the owner requires atoma to verify them before the consumer leaves alpha. This session could not run a downloaded Zig binary; Guix is being set up to provide Zig 0.15.2. A PR on hyperpolymath/hpm-json-rsr will be linked here, and this stays a draft until it merges.

Consumer: metadatastician/consent-aware-inspector pins this branch's head, because its manifest validation enumerates policy keys.

Tests

  • tests/codegen-bun/json_object_keys: keys in order, nested round-trip, escaped keys, own keys only, an inherited enumerable key excluded, non-finite members rejected.
  • json_escapes: objects now convert.

Local runs on this branch: dune runtest passes 594 tests; the Bun harnesses pass (7 plus negative controls); the codegen-deno corpus passes 39 harnesses and the WASM corpus 38.

Known, tracked separately: to_json accumulates with acc ++ [x] (O(n²) for very large objects). The owner chose to fix that with an affine in-place push in the stdlib, as its own PR.

Commits are unsigned. This container's signing shim is inactive.


Generated by Claude Code

Split from #798 on the owner's ruling. to_json materialises objects
instead of returning None: hpm_json_object_len / hpm_json_object_key,
lowered on Bun-ESM (one Object.keys snapshot per walk). Native targets need
the matching hpm-json-rsr Zig exports; this PR waits for them, so no native
build meets an unresolved symbol.

Tests: tests/codegen-bun/json_object_keys (keys, nesting, escaped keys,
own keys only, inherited enumerable key, non-finite values) and
json_escapes (objects now convert).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK
A Wasm module that imports json's externs (`use json::*`) now imports
hpm_json_object_len and hpm_json_object_key as well: 13 hpm_json_*
imports instead of 11, under the `json` module. A host supplying only
the v0.3 set fails with a LinkError at instantiation (checked with
Bun's WebAssembly on this commit). The spec now says so, and how a host
can stub the two until hpm-json-rsr exports them.

Answers the outside-diff finding on stdlib/json.affine in the CodeRabbit
review of #798; the externs moved to this PR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK
@sonarqubecloud

Copy link
Copy Markdown

@hyperpolymath
hyperpolymath marked this pull request as ready for review November 18, 2026 22:58
@hyperpolymath hyperpolymath assigned Copilot and unassigned Copilot Nov 18, 2026
Repository owner deleted a comment from coderabbitai Bot Nov 18, 2026
@hyperpolymath
hyperpolymath merged commit 2ed6e76 into claude/gifted-lamport-k0mc5l Nov 18, 2026
10 checks passed
@hyperpolymath
hyperpolymath deleted the claude/gifted-lamport-k0mc5l-json-objects branch November 18, 2026 23:09
hyperpolymath added a commit that referenced this pull request Nov 26, 2026
…ntime pruning, stringify escapes; add Url (#798)

Upstream fixes found while porting
`metadatastician/consent-aware-inspector`'s core to AffineScript (owner
ruling: fix upstream first).

**Split:** on the owner's ruling after review, JSON object-key
enumeration moved to #799 (`Depends on: #798`). It waits for the
`hpm-json-rsr` Zig exports, so no native build here can meet an
unresolved symbol.

## Added

- **`stdlib/Url.affine`: `url_resolve` and `url_parse`.** WHATWG
semantics through the host's `globalThis.URL`, so results agree with
browsers and Bun even when a program declares its own `URL`. Bun-ESM
only; no native lowering.

## Fixed

- **Local bindings named like a host intrinsic** (`get`, `len`, `trim`,
`show`, ...) now shadow it. A parameter `get` used to compile to the
ref-cell builtin and crash.
- Binders are scoped lexically: parameters, `let`, `for`, lambda,
match-arm and `catch`.
  - Sibling arms are generated outside an arm's scope.
- A `let`'s initializer still sees the intrinsic, so `let len = len(s)`
works.
- **Module flattening rebound calls to the wrong function.** When two
modules' different public declarations of one name reached a program
transitively, last-wins kept one; `viaA(1)` returned 100 instead of 2.
  - The flattener now raises `ImportCollision`.
- One declaration reached by two paths is still accepted, identified by
its source span: internalised copies differ structurally but are the
same declaration.
- **The whole runtime (~300 helpers) was emitted into every Bun-ESM
module**, including a `document.body.innerHTML` setter that `web-ext
lint` flags. Only the helpers a module reaches are emitted now, in their
original order. The inspector core went from 295 helpers to 24 and from
1 lint warning to 0.
- **`json::stringify` produced invalid JSON** for `"`, `\` and control
characters; `escape_char` returned the bare character.
- **`to_json` accepted non-finite numbers**, which have no JSON form; it
now rejects them.
- **Statement-position `match` guards ran before their arm's bindings
existed** (pre-existing). Arms now bind first and fall through on a
false guard.
- **`for x in e` evaluated `e` with `x` already in its temporal dead
zone** (pre-existing). The iterable now goes into a temporary first.

## Tests

Every new test fails on the commit before its fix.

- `tests/codegen-bun/local_shadows_builtin`: parameter, `let`, lambda,
loop and arm scopes, guards, and `for`.
- `url_module`: every `UrlParts` field, and a variant named `URL`.
- `json_escapes`: escaping and non-finite numbers.
- `diamond_internalised`: the same declaration reached by two paths
compiles.
- `collision/`: a negative check that must fail with `ImportCollision`.
- `test/test_runtime_prune.ml`: the runtime has the shape pruning relies
on (const declarations only, nothing reached by a computed name), and
reachability and order hold. A pruner that drops transitive dependencies
fails it and the `http_fetch` corpus harness.

Local runs on this branch:
- `dune runtest`: 594 tests pass.
- Bun-ESM harnesses: 6, plus three negative controls.
- codegen-deno corpus: 39 harnesses. WASM corpus: 38 harnesses.
- `check-no-extension-ts`, `check-doc-truthing`, `check-deed-gates`:
pass.
- `check-soundness-ledger`: passes after `git fetch --depth=1000`; it
fails on any shallow clone, the base included.

## Review

- 11 of CodeRabbit's 12 threads are fixed and resolved.
- The O(n²) `acc ++ [x]` accumulation stays open. It is the
language-wide idiom, and the owner chose an affine in-place
`array::push` (consuming its input, so an in-place JS `push` is sound)
as a follow-up PR.

## Follow-ups

- Real module namespacing, so two modules may each export `f`. This PR
only makes the collision loud.
- `docs/STDLIB-EXTERN-AUDIT.adoc` (dated 2026-05-23) already omits
`json.affine`.
- `affinescript compile -o dir/x.js` raises an uncaught `Sys_error` when
`dir` is missing, instead of creating it or reporting it.

Commits are unsigned. This container's signing shim is inactive.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: CLAUDE <355856132+HoTT-stuff@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants