Skip to content

Advance Marshal MRI conformance: symbols, Bignum links, exceptions, depth limit - #516

Merged
tannevaled merged 1 commit into
mainfrom
feat/agent-marshal
Aug 30, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/agent-marshal

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Extend the VM's byte-exact Marshal codec to match MRI 4.0.x on more of the format. Every expected value was checked byte-for-byte against the reference `ruby` interpreter.

rubyspec deltas

spec before after
core/marshal/dump_spec.rb 145 pass 162 pass (+17)
core/marshal/load_spec.rb 194 pass 210 pass (+16)

Total: +33 examples. No regressions across the broader core/marshal sweep.

internal/vm/marshal_codec.go

  • Symbols: a non-ASCII Symbol dumps as an `I`-wrapped symbol with the `:E=>true` encoding ivar (rbgo's UTF-8 default), sharing the E symbol across the stream; pure-ASCII symbols stay bare. Non-ASCII ivar-name symbols wrap the same way. Decode reads past an `I`-wrapped encoded symbol.
  • Bignum: a repeated Bignum object now links like any other composite (a Fixnum-range Integer stays immediate and is never linked); factored out `writeBignumBody`.
  • compare_by_identity Hash: dumped in a `C`:Hash container and the flag restored on load.
  • Exception: emitted via the `:mesg` / `:bt` pseudo-ivars (from `@message` / `@backtrace`) then the remaining ivars; load maps them back.
  • Errors: raise on a singleton object (per-object methods or singleton-class ivars), on an instance of an anonymous class through every hook (marshal_dump / _dump / Struct / builtin subclass / plain object), and word the anonymous-module message correctly. Enforce Marshal.dump's recursion limit (`exceed depth limit`).
  • Float load: trim the legacy NUL + mantissa suffix before parsing (fixes 5 examples returning 0.0).
  • Class/module refs: accept the legacy `M` reference; validate `c` names a class and `m` a module.
  • Load proc: its return value now replaces the loaded object, matching MRI.

internal/vm/marshal.go

  • Raise `instance of IO needed` for a non-writable, non-Integer second argument; call `#binmode` on the IO when defined; parse the depth limit (`marshalDumpLimit`); raise `EOFError` for an empty IO source.

Tests

  • New `internal/vm/marshal_conformance_test.go` (package `vm_test`) covering every new branch, asserting against MRI-verified output.
  • Two existing `marshal_test.go` expectations encoded pre-MRI behaviour (a non-writable dump target was silently ignored; a load proc's return was discarded) and are corrected against the reference interpreter; the internal test's `marshalDump` call is updated for the new limit parameter.

Verification

  • `go test ./...` green; `GOOS=wasip1 GOARCH=wasm go build ./...` OK; `gofmt -l` empty.
  • Merged coverage across all internal packages = 100.0%; every changed function is 100%.

Out of scope (blocked by object model / other files)

  • Instance variables, extend, and subclassing on core String/Array/Hash/Regexp — these types cannot hold ivars in this phase (object.go).
  • Regexp source-encoding round-trip (rbgo does not track source encoding for ASCII sources), Data class support, Time local-zone (Time.local reports UTC), and Symbol#encoding-dependent load specs (builtins.go).

🤖 Generated with Claude Code

…epth limit

Extend the VM's byte-exact Marshal codec (internal/vm/marshal.go and
marshal_codec.go) to match MRI 4.0.x on more of the format.

Dump:
- encode a non-ASCII Symbol as an 'I'-wrapped symbol with the :E=>true
  encoding ivar (rbgo's UTF-8 default), sharing the E symbol across the
  stream; pure-ASCII symbols stay bare. This also wraps non-ASCII ivar-name
  symbols, matching MRI.
- link a repeated Bignum object like any other composite (a Fixnum-range
  Integer stays immediate and is never linked).
- carry a compare_by_identity Hash in a 'C':Hash container.
- emit an Exception via the :mesg / :bt pseudo-ivars (from @message and
  @__backtrace__), then the remaining ivars.
- raise on a singleton object (per-object methods or singleton-class ivars),
  on an instance of an anonymous class through every hook, and word the
  anonymous-module message as "module".
- enforce Marshal.dump's recursion limit, and raise "instance of IO needed"
  for a non-writable, non-Integer second argument; call #binmode on the IO
  when defined.

Load:
- trim the legacy NUL + mantissa suffix from a Float string before parsing.
- restore compare_by_identity from a 'C':Hash container.
- accept the legacy 'M' class-or-module reference; validate that 'c' names a
  class and 'm' a module.
- read past an 'I'-wrapped encoded symbol.
- map an Exception's :mesg / :bt onto @message / @__backtrace__.
- use the load proc's return value in place of the loaded object.
- raise EOFError for an empty IO source.

Two existing marshal_test.go expectations encoded pre-MRI behaviour (a
non-writable dump target was silently ignored; a load proc's return was
discarded) and are corrected against the reference interpreter.

rubyspec: core/marshal/dump_spec 145 -> 162 pass, load_spec 194 -> 210 pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 47a6948 into main Aug 30, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-marshal branch August 30, 2026 22:12
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.

1 participant