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.



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

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



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



168
169
170
171
172
# File 'lib/capybara/simulated/v8_runtime.rb', line 168

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

#disposeObject



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

def dispose                          = @iso.dispose

#dynamic_import_resolver=(prc) ⇒ Object



156
157
158
# File 'lib/capybara/simulated/v8_runtime.rb', line 156

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)

#perform_microtask_checkpointObject



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

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.



142
143
144
145
146
# File 'lib/capybara/simulated/v8_runtime.rb', line 142

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

#terminateObject

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



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

def terminate                        = @iso.terminate