Skip to content

[oscars-integration] Thread MutationContext through core engine and ByteCompiler - #5478

Open
shruti2522 wants to merge 3 commits into
boa-dev:dev/oscars-gcfrom
shruti2522:feat/msb-phase1-core
Open

[oscars-integration] Thread MutationContext through core engine and ByteCompiler#5478
shruti2522 wants to merge 3 commits into
boa-dev:dev/oscars-gcfrom
shruti2522:feat/msb-phase1-core

Conversation

@shruti2522

Copy link
Copy Markdown
Contributor

stacked on #5474

part of integrating the oscars::mark_sweep_branded GC backend. To make the GC safe at compile time using the <'gc> lifetime, we are removing MutationContext::global() and passing MutationContext down through the engine instead.

this PR threads the context through the Engine Core, Object/Shape Constructors, ByteCompiler and VM

It changes the following:

  • GC Context Bridge: Made gc_collector() in boa_gc (for the oscars_backend) return a dummy static MutationContext. This acts as a safe bridge to pass the context around before we fully support closure lifetimes.
  • Threaded MutationContext down from ContextBuilder::build() into Realm::create(), Intrinsics, StandardConstructors, and IntrinsicObjects.
  • added _in versions 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.
  • ByteCompiler now safely stores a reference to MutationContext (using a McWrapper to fix Debug bounds). We replaced unsafe { MutationContext::global() } in class.rs and function.rs block allocations with self.mc.0. ByteCompiler::new and its callers were updated.
  • updated Vm::new to 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 MutationContext during setup, compilation and execution. Next PRs will update the Standard Library builtins (like Array, Date, Map) to use the new _in variants and completely remove the remaining MutationContext::global() fallbacks

@github-actions github-actions Bot added the Waiting On Review Waiting on reviews from the maintainers label Aug 16, 2026
@github-actions github-actions Bot added this to the v1.0.0 milestone Aug 16, 2026
@github-actions github-actions Bot added C-Dependencies Pull requests that update a dependency file C-Benchmark Issues and PRs related to the benchmark subsystem. C-Tests Issues and PRs related to the tests. C-Builtins PRs and Issues related to builtins/intrinsics C-VM Issues and PRs related to the Boa Virtual Machine. C-Actions Pull requests that update Github Actions code C-GC Issue related to garbage collection C-Intl Changes related to the `Intl` implementation C-Runtime Issues and PRs related to Boa's runtime features labels Aug 16, 2026
@shruti2522
shruti2522 marked this pull request as ready for review August 17, 2026 20:21
@shruti2522
shruti2522 requested review from a team, jedel1043 and nekevss as code owners August 17, 2026 20:21
Copilot AI lite review requested due to automatic review settings August 17, 2026 20:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::GcContext bridge and threads GC allocation access through Context (alloc, gc_collector) and down into realm/intrinsics/setup.
  • Adds _in variants for core object/shape constructors and updates many allocations to use a passed &MutationContext instead 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. Prefer context.gc_collector() (or a local mc binding) 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. Prefer context.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 a Context is 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 thread core/gc/src/context.rs
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() });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Actions Pull requests that update Github Actions code C-Benchmark Issues and PRs related to the benchmark subsystem. C-Builtins PRs and Issues related to builtins/intrinsics C-Dependencies Pull requests that update a dependency file C-GC Issue related to garbage collection C-Intl Changes related to the `Intl` implementation C-Runtime Issues and PRs related to Boa's runtime features C-Tests Issues and PRs related to the tests. C-VM Issues and PRs related to the Boa Virtual Machine. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants