Class: Capybara::Simulated::V8Runtime::Ctx

Inherits:
Object
  • Object
show all
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 `Context`s 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

Instance Method Summary collapse

Constructor Details

#initialize(snapshot: nil, timeout: 0) ⇒ Ctx

Returns a new instance of Ctx.



95
96
97
98
99
100
101
102
# File 'lib/capybara/simulated/v8_runtime.rb', line 95

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

#generationObject (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.



129
130
131
# File 'lib/capybara/simulated/v8_runtime.rb', line 129

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.



114
115
116
117
# File 'lib/capybara/simulated/v8_runtime.rb', line 114

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).



120
121
122
123
# File 'lib/capybara/simulated/v8_runtime.rb', line 120

def attach_many(fns)
  @attached.concat(fns.to_a)
  @ctx.attach_many(fns)
end

#call(name, *args) ⇒ Object



109
# File 'lib/capybara/simulated/v8_runtime.rb', line 109

def call(name, *args)  = @ctx.call(name, *args)

#compile(src, **kw) ⇒ Object



141
# File 'lib/capybara/simulated/v8_runtime.rb', line 141

def compile(src, **kw)        = @ctx.compile(src, **kw)

#compile_module(src, **kw) ⇒ Object



142
# File 'lib/capybara/simulated/v8_runtime.rb', line 142

def compile_module(src, **kw) = @ctx.compile_module(src, **kw)

#create_contextObject

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.



161
162
163
164
165
# File 'lib/capybara/simulated/v8_runtime.rb', line 161

def create_context
  realm = @iso.create_context
  realm.attach_many(@attached.to_h)
  realm
end

#disposeObject



146
# File 'lib/capybara/simulated/v8_runtime.rb', line 146

def dispose                          = @iso.dispose

#dynamic_import_resolver=(prc) ⇒ Object



149
150
151
# File 'lib/capybara/simulated/v8_runtime.rb', line 149

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.



108
# File 'lib/capybara/simulated/v8_runtime.rb', line 108

def eval(src)          = @ctx.eval(src)

#perform_microtask_checkpointObject



147
# File 'lib/capybara/simulated/v8_runtime.rb', line 147

def perform_microtask_checkpoint     = @iso.perform_microtask_checkpoint

#resetObject

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.



135
136
137
138
139
# File 'lib/capybara/simulated/v8_runtime.rb', line 135

def reset
  @ctx.reset
  @attached.clear
  @generation += 1
end

#terminateObject

── Isolate surface ─────────────────────────────────────────



145
# File 'lib/capybara/simulated/v8_runtime.rb', line 145

def terminate                        = @iso.terminate