Class: Capybara::Simulated::V8Runtime::Ctx
- Inherits:
-
Object
- Object
- Capybara::Simulated::V8Runtime::Ctx
- Defined in:
- lib/capybara/simulated/v8_runtime.rb
Overview
One isolate + its default context, presented as a single handle — the
shape the rest of the runtime (and ScriptCache) passes around.
rusty splits the VM into an Isolate (lifecycle / realms / microtasks /
terminate) and the Contexts it hands out (eval / call / attach /
compile / reset); this class pairs them and replays recorded host-fn
attaches onto per-frame realm contexts (rusty's attach is per-context).
Instance Attribute Summary collapse
-
#generation ⇒ Object
readonly
Bumped on every realm reset: realm-bound caches (module handles) key off
[object_id, generation]so invalidation is intrinsic to reset — the Ctx OBJECT survives a warm reset, so object_id alone can't detect one.
Instance Method Summary collapse
-
#attach(name, prc) ⇒ Object
Record every attach so
create_contextcan replay them: the bridge in a per-frame realm reaches the same Ruby host fns as the main context, but rusty's attach is per-context. -
#attach_many(fns) ⇒ Object
One rendezvous for the whole host-fn table (vs one per fn).
- #call(name, *args) ⇒ Object
- #compile(src, **kw) ⇒ Object
- #compile_module(src, **kw) ⇒ Object
-
#create_context ⇒ Object
A per-iframe realm: a fresh context in the SAME isolate (shared heap, own global + intrinsics).
- #dispose ⇒ Object
- #dynamic_import_resolver=(prc) ⇒ Object
-
#eval(src) ⇒ Object
── Context surface ───────────────────────────────────────── rusty drains microtasks at call-depth zero (V8's default kAuto policy), so a returned eval/call has already run its end-of-script microtasks.
-
#heap_statistics ⇒ Object
V8 heap accounting + a forced full GC (rusty >= 0.1.9, the gem's floor).
-
#initialize(snapshot: nil, timeout: 0) ⇒ Ctx
constructor
A new instance of Ctx.
- #low_memory_notification ⇒ Object
- #perform_microtask_checkpoint ⇒ Object
-
#reset ⇒ Object
Swap the realm for a snapshot-fresh one on the warm isolate.
-
#terminate ⇒ Object
── Isolate surface ─────────────────────────────────────────.
Constructor Details
#initialize(snapshot: nil, timeout: 0) ⇒ Ctx
Returns a new instance of Ctx.
102 103 104 105 106 107 108 109 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 102 def initialize(snapshot: nil, timeout: 0) @iso = RustyRacer::Isolate.new(host_namespace: HOST_NAMESPACE_NAME, snapshot: snapshot, timeout_ms: timeout.to_i) @ctx = @iso.context @attached = [] @generation = 0 end |
Instance Attribute Details
#generation ⇒ Object (readonly)
Bumped on every realm reset: realm-bound caches (module handles)
key off [object_id, generation] so invalidation is intrinsic to
reset — the Ctx OBJECT survives a warm reset, so object_id alone
can't detect one.
136 137 138 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 136 def generation @generation end |
Instance Method Details
#attach(name, prc) ⇒ Object
Record every attach so create_context can replay them: the bridge
in a per-frame realm reaches the same Ruby host fns as the main
context, but rusty's attach is per-context.
121 122 123 124 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 121 def attach(name, prc) @attached << [name, prc] @ctx.attach(name, prc) end |
#attach_many(fns) ⇒ Object
One rendezvous for the whole host-fn table (vs one per fn).
127 128 129 130 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 127 def attach_many(fns) @attached.concat(fns.to_a) @ctx.attach_many(fns) end |
#call(name, *args) ⇒ Object
116 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 116 def call(name, *args) = @ctx.call(name, *args) |
#compile(src, **kw) ⇒ Object
148 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 148 def compile(src, **kw) = @ctx.compile(src, **kw) |
#compile_module(src, **kw) ⇒ Object
149 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 149 def compile_module(src, **kw) = @ctx.compile_module(src, **kw) |
#create_context ⇒ Object
A per-iframe realm: a fresh context in the SAME isolate (shared heap,
own global + intrinsics). Carries .id / eval / call / dispose — the
rest of the surface create_frame_realm needs.
to_h dedups re-attached names to their latest proc, matching
attach's override semantics. NOTE: context-bound fns
(__csim_runScript*, __csim_evalEsmEntry) get realm-bound
overrides in create_frame_realm after this replay.
172 173 174 175 176 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 172 def create_context realm = @iso.create_context realm.attach_many(@attached.to_h) realm end |
#dispose ⇒ Object
153 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 153 def dispose = @iso.dispose |
#dynamic_import_resolver=(prc) ⇒ Object
160 161 162 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 160 def dynamic_import_resolver=(prc) @iso.dynamic_import_resolver = prc end |
#eval(src) ⇒ Object
── Context surface ───────────────────────────────────────── rusty drains microtasks at call-depth zero (V8's default kAuto policy), so a returned eval/call has already run its end-of-script microtasks.
115 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 115 def eval(src) = @ctx.eval(src) |
#heap_statistics ⇒ Object
V8 heap accounting + a forced full GC (rusty >= 0.1.9, the gem's
floor). Used by the per-visit heap-pressure relief in rebuild_ctx.
157 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 157 def heap_statistics = @iso.heap_statistics |
#low_memory_notification ⇒ Object
158 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 158 def low_memory_notification = @iso.low_memory_notification |
#perform_microtask_checkpoint ⇒ Object
154 155 156 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 154 def perform_microtask_checkpoint = @iso.perform_microtask_checkpoint # V8 heap accounting + a forced full GC (rusty >= 0.1.9, the gem's # floor). Used by the per-visit heap-pressure relief in `rebuild_ctx`. |
#reset ⇒ Object
Swap the realm for a snapshot-fresh one on the warm isolate. Per rusty's contract the host fns die with the old context — drop the replay record so the caller's re-attach doesn't accumulate stale entries visit over visit.
142 143 144 145 146 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 142 def reset @ctx.reset @attached.clear @generation += 1 end |
#terminate ⇒ Object
── Isolate surface ─────────────────────────────────────────
152 |
# File 'lib/capybara/simulated/v8_runtime.rb', line 152 def terminate = @iso.terminate |