[oscars-integration] Thread MutationContext through core engine and ByteCompiler - #5478
Open
shruti2522 wants to merge 3 commits into
Open
[oscars-integration] Thread MutationContext through core engine and ByteCompiler#5478shruti2522 wants to merge 3 commits into
shruti2522 wants to merge 3 commits into
Conversation
shruti2522
marked this pull request as ready for review
August 17, 2026 20:21
Contributor
There was a problem hiding this comment.
Pull request overview
This PR is part of the oscars mark_sweep_branded GC integration work. It removes reliance on MutationContext::global() in core execution paths by threading a MutationContext through engine initialization, compilation (ByteCompiler), and VM/environment allocation, while temporarily keeping global fallbacks for still-unmigrated components.
Changes:
- Introduces a
boa_gc::GcContextbridge and threads GC allocation access throughContext(alloc,gc_collector) and down into realm/intrinsics/setup. - Adds
_invariants for core object/shape constructors and updates many allocations to use a passed&MutationContextinstead of the dummy/global context. - Updates ByteCompiler/Vm/opcodes to store/use the threaded GC context and adjusts code for oscars-backend API differences (e.g.,
GcRef-guard cloning patterns).
Reviewed changes
Copilot reviewed 89 out of 90 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/macros/tests/gcd_callback.rs | Test allocation updated; currently still uses unsafe global context (should use context.gc_collector()). |
| tests/fuzz/Cargo.toml | Adds patch override for boa_string path in fuzz workspace. |
| examples/src/bin/jstypedarray.rs | Example allocation updated; currently still uses unsafe global context (should use context.alloc). |
| examples/src/bin/derive.rs | Adds allow attribute to keep example compiling. |
| core/string/src/tests.rs | Minor assertion style updates. |
| core/string/src/lib.rs | Adds oscars-backend Trace/Finalize impl for JsString. |
| core/string/src/builder.rs | Formatting/lint adjustments in builder segment handling. |
| core/string/Cargo.toml | Adds optional oscars dep + oscars_backend feature. |
| core/runtime/src/test262.rs | Refactors downcast clone handling for oscars GcRef guards. |
| core/runtime/src/microtask/tests.rs | Adjusts cloning via GcRef guard deref. |
| core/runtime/src/console/tests.rs | Adjusts cloning via GcRef guard deref + loosens table string assertions. |
| core/runtime/src/abort/mod.rs | Adjusts cloning via GcRef guard deref. |
| core/macros/src/lib.rs | Updates derive_trace implementation structure/cloning behavior. |
| core/interner/src/sym.rs | Switches manual Trace impl to derive + unsafe_ignore_trace. |
| core/gc/src/trace.rs | Wraps custom trace body in unsafe {} to satisfy new macro expectations. |
| core/gc/src/test/weak.rs | Updates Gc type usage in size computation. |
| core/gc/src/pointers/weak_map.rs | Adds get_value helper but currently hides unsafe dummy() usage. |
| core/gc/src/pointers/mutation_context.rs | Makes dummy() const and adds global() polyfill. |
| core/gc/src/oscars_weak_map.rs | Adds dummy/leaky WeakMap implementation for oscars backend. |
| core/gc/src/lib.rs | Major oscars-backend wiring: re-exports, type aliases, macros, GcContext export. |
| core/gc/src/context.rs | Adds GcContext bridge; provides global collector handle via LazyLock. |
| core/gc/src/cell.rs | Assertion style updates. |
| core/gc/Cargo.toml | Updates features and oscars deps; adjusts defaults. |
| core/engine/src/vm/tests.rs | Disables a GC-sensitive test under oscars backend. |
| core/engine/src/vm/opcode/push/environment.rs | Threads collector into lexical env pushes; uses context.alloc for allocations. |
| core/engine/src/vm/opcode/function.rs | Adjusts closure typing in map_or_else to satisfy oscars types. |
| core/engine/src/vm/opcode/await/mod.rs | Uses context.alloc and fixes capture cloning semantics. |
| core/engine/src/vm/mod.rs | Vm::new now takes &MutationContext for initial allocations. |
| core/engine/src/vm/inline_cache/mod.rs | Adds explicit type binding for upgraded shape. |
| core/engine/src/vm/code_block.rs | Adds clippy allow for clone-on-copy under oscars backend. |
| core/engine/src/value/integer.rs | Assertion style updates in tests. |
| core/engine/src/value/inner/nan_boxed.rs | Adjusts raw pointer handling and imports for oscars backend constraints. |
| core/engine/src/value/inner/legacy.rs | Updates custom trace to match new macro/body patterns. |
| core/engine/src/value/equality.rs | Assertion style update. |
| core/engine/src/script.rs | Uses context.alloc and threads MutationContext into ByteCompiler::new. |
| core/engine/src/realm.rs | Threads MutationContext into realm creation and allocations. |
| core/engine/src/object/shape/unique_shape.rs | Adds _in APIs + fixes GcRefCell::borrow() cloning semantics. |
| core/engine/src/object/shape/shared_shape/template.rs | Adds _in APIs and threads MutationContext through template operations/creation. |
| core/engine/src/object/shape/shared_shape/mod.rs | Adds _in APIs for shape transitions; gates tests off for oscars backend. |
| core/engine/src/object/shape/shared_shape/forward_transition.rs | Adds _in APIs and threads MutationContext for weak transitions. |
| core/engine/src/object/shape/root_shape.rs | Adds new_in to create root shapes with a provided context. |
| core/engine/src/object/shape/mod.rs | Adds _in APIs for shape transitions, threading MutationContext. |
| core/engine/src/object/mod.rs | Documents TypeId identity differences under oscars backend. |
| core/engine/src/object/jsobject.rs | Adds _in constructors and changes raw pointer APIs to be backend-agnostic. |
| core/engine/src/object/builtins/jsweakset.rs | Uses context.gc_collector() for WeakSet allocation. |
| core/engine/src/object/builtins/jsweakmap.rs | Uses context.gc_collector() for WeakMap allocation. |
| core/engine/src/object/builtins/jstypedarray.rs | Updates docs to use context.alloc. |
| core/engine/src/object/builtins/jspromise.rs | Uses context.alloc; makes JsFuture cloneable. |
| core/engine/src/object/builtins/jsfunction.rs | Adds _in intrinsic constructor variant. |
| core/engine/src/native_function/mod.rs | Uses global context token for closure allocations; adjusts Gc::from_raw typing and guard cloning. |
| core/engine/src/native_function/continuation.rs | Same raw-pointer typing update for coroutines. |
| core/engine/src/module/synthetic.rs | Threads MutationContext into ByteCompiler; still uses global MC for push_module (should use threaded collector). |
| core/engine/src/module/source.rs | Threads MutationContext into ByteCompiler; still uses global MC for push_module (should use threaded collector). |
| core/engine/src/module/mod.rs | Uses context.alloc for module repr and test data allocation; adjusts GcRef guard cloning. |
| core/engine/src/module/loader/mod.rs | Derives Trace/Finalize for module cache key. |
| core/engine/src/lib.rs | Temporarily relaxes clippy lints expected under oscars backend (copy-like Gc). |
| core/engine/src/host_defined.rs | Adjusts Finalize impl bounds to match backend changes. |
| core/engine/src/error/mod.rs | Fixes GcRef guard cloning and custom trace match pattern. |
| core/engine/src/environments/runtime/mod.rs | Threads MutationContext into env pushes, but push_env still allocates nodes via global MC. |
| core/engine/src/context/mod.rs | Adds GcContext field and alloc/gc_collector APIs; threads MC through realm/vm init. |
| core/engine/src/context/intrinsics.rs | Threads MutationContext through intrinsics/templates/constructors initialization. |
| core/engine/src/bytecompiler/mod.rs | ByteCompiler now stores an mc wrapper and threads it into nested compilers. |
| core/engine/src/bytecompiler/function.rs | Threads MC into function compilation; allocates CodeBlocks using passed MC. |
| core/engine/src/bytecompiler/declarations.rs | Threads MC into declaration compilation helpers. |
| core/engine/src/bytecompiler/class.rs | Replaces global/dummy MC allocations with self.mc.0. |
| core/engine/src/builtins/weak/weak_ref.rs | Uses context.gc_collector() for weak allocations; gates tests off for oscars backend. |
| core/engine/src/builtins/weak_set/mod.rs | Uses threaded collector for map/set internals; gates tests off for oscars backend. |
| core/engine/src/builtins/weak_map/mod.rs | Uses new WeakMap::get_value; refactors map access for oscars backend. |
| core/engine/src/builtins/uri/mod.rs | Adds uninit_in to build URI intrinsics with MC. |
| core/engine/src/builtins/set/ordered_set.rs | Refactors trace loop variable naming. |
| core/engine/src/builtins/promise/mod.rs | Uses context.alloc for GC-managed state. |
| core/engine/src/builtins/json/mod.rs | Threads MC into ByteCompiler but currently drops in_with argument (behavioral bug). |
| core/engine/src/builtins/iterable/mod.rs | Adds uninit_in to build iterator prototypes with MC. |
| core/engine/src/builtins/intl/locale/utils.rs | Adjusts cloning via GcRef guard deref. |
| core/engine/src/builtins/intl/locale/mod.rs | Adjusts cloning via GcRef guard deref. |
| core/engine/src/builtins/intl/list_format/mod.rs | Assertion style update. |
| core/engine/src/builtins/generator/mod.rs | Updates custom trace match pattern. |
| core/engine/src/builtins/function/mod.rs | Threads MC into compilation but still uses unsafe global MC for env pushes in call/construct paths. |
| core/engine/src/builtins/function/arguments.rs | Adjusts environment access through GcRef guard deref. |
| core/engine/src/builtins/finalization_registry/tests.rs | Gates tests off for oscars backend. |
| core/engine/src/builtins/finalization_registry/mod.rs | Uses threaded collector for weak/ephemeron operations; some remaining global usage. |
| core/engine/src/builtins/eval/mod.rs | Threads MC into ByteCompiler and env pushes. |
| core/engine/Cargo.toml | Adds oscars_backend feature wiring to engine. |
| core/engine/benches/full.rs | Updates realm creation bench to pass MC. |
| Cargo.toml | Adds typeid dependency and workspace patch for boa_string. |
| Cargo.lock | Locks oscars commit and adds typeid dependency resolution. |
| .github/workflows/webassembly.yml | Adds dev/oscars-gc branch trigger. |
| .github/workflows/test262.yml | Adds dev/oscars-gc trigger and conditional skip. |
| .github/workflows/rust.yml | Adds dev/oscars-gc branch trigger. |
| .github/workflows/pull_request.yml | Adds dev/oscars-gc branch trigger. |
Suppressed comments (3)
core/engine/src/builtins/function/mod.rs:1191
- This uses
unsafe { MutationContext::global() }when pushing the binding-identifier lexical environment. Prefercontext.gc_collector()(or a localmcbinding) to avoid unsafe/global GC context usage.
let frame = context.vm.frame_mut();
let global = frame.realm.environment();
let index = frame
.environments
.push_lexical(1, &global, &unsafe { boa_gc::MutationContext::global() });
core/engine/src/builtins/function/mod.rs:1220
- This still uses
&unsafe { MutationContext::global() }when pushing the constructor function environment. Prefercontext.gc_collector()to keep allocations tied to the threaded collector and avoid unsafe/global usage.
&unsafe { boa_gc::MutationContext::global() },
core/engine/src/builtins/function/mod.rs:1099
- The function-scope environment push still passes
&unsafe { MutationContext::global() }. Since aContextis available here, use the threaded collector (context.gc_collector()) to avoid relying on a global/unsafe fallback.
scope,
FunctionSlots::new(this, function_object.clone(), None),
&global,
&unsafe { boa_gc::MutationContext::global() },
);
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
305
to
309
| context.interner_mut(), | ||
| in_with, | ||
| &gc, | ||
| false, | ||
| spanned_source_text, | ||
| SourcePath::Json, |
|
|
||
| self.push_env(Environment::Declarative(Gc::new( | ||
| &unsafe { boa_gc::MutationContext::dummy() }, | ||
| &gc, |
Comment on lines
419
to
423
| fn push_env(&mut self, env: Environment) { | ||
| self.tip = Some(Gc::new( | ||
| &unsafe { boa_gc::MutationContext::dummy() }, | ||
| &unsafe { boa_gc::MutationContext::global() }, | ||
| EnvironmentNode { | ||
| env, |
Comment on lines
346
to
348
| let mut envs = EnvironmentStack::new(); | ||
| envs.push_module(module_scope); | ||
| envs.push_module(module_scope, &unsafe { boa_gc::MutationContext::global() }); | ||
|
|
Comment on lines
1838
to
+1841
| let mut envs = EnvironmentStack::new(); | ||
| envs.push_module(source.scope().clone()); | ||
| envs.push_module(source.scope().clone(), &unsafe { | ||
| boa_gc::MutationContext::global() | ||
| }); |
Comment on lines
+16
to
+20
| struct SyncWrapper(MutationContext<'static, 'static>); | ||
| #[cfg(feature = "oscars_backend")] | ||
| unsafe impl Sync for SyncWrapper {} | ||
| #[cfg(feature = "oscars_backend")] | ||
| unsafe impl Send for SyncWrapper {} |
Comment on lines
22
to
25
| let result = Gc::new( | ||
| &unsafe { boa_gc::MutationContext::dummy() }, | ||
| &unsafe { boa_gc::MutationContext::global() }, | ||
| AtomicUsize::new(0), | ||
| ); |
Comment on lines
95
to
98
| let num_to_modify = Gc::new( | ||
| &unsafe { boa_gc::MutationContext::dummy() }, | ||
| &unsafe { boa_gc::MutationContext::global() }, | ||
| GcRefCell::new(0u8), | ||
| ); |
|
|
||
| pub(crate) kept_alive: Vec<JsObject>, | ||
|
|
||
| pub gc: boa_gc::GcContext, |
Comment on lines
1076
to
+1080
| let frame = context.vm.frame_mut(); | ||
| let global = frame.realm.environment(); | ||
| let index = frame.environments.push_lexical(1, global); | ||
| let index = frame | ||
| .environments | ||
| .push_lexical(1, &global, &unsafe { boa_gc::MutationContext::global() }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
stacked on #5474
part of integrating the
oscars::mark_sweep_brandedGC backend. To make the GC safe at compile time using the<'gc>lifetime, we are removingMutationContext::global()and passingMutationContextdown through the engine instead.this PR threads the context through the Engine Core, Object/Shape Constructors, ByteCompiler and VM
It changes the following:
gc_collector()inboa_gc(for theoscars_backend) return a dummy staticMutationContext. This acts as a safe bridge to pass the context around before we fully support closure lifetimes.MutationContextdown fromContextBuilder::build()intoRealm::create(),Intrinsics,StandardConstructors, andIntrinsicObjects._inversions for all core object and shape constructors (e.g.,JsObject::new_in). These new functions take a&MutationContext. The old constructors temporarily fall back to the global context to keep the build green for standard library components.ByteCompilernow safely stores a reference toMutationContext(using aMcWrapperto fixDebugbounds). We replacedunsafe { MutationContext::global() }inclass.rsandfunction.rsblock allocations withself.mc.0.ByteCompiler::newand its callers were updated.Vm::newto accept&MutationContext. Updated environment stack opcodes (PushScope,push_lexical, etc.) to take and use the threaded context instead of the global one.Next
The engine now correctly passes
MutationContextduring setup, compilation and execution. Next PRs will update the Standard Library builtins (likeArray,Date,Map) to use the new_invariants and completely remove the remainingMutationContext::global()fallbacks