Class: Dommy::Js::Quickjs::Backend

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/js/quickjs/backend.rb

Overview

Binds HostBridge’s abstract backend contract to the ‘quickjs` gem.

Constant Summary collapse

DEFAULT_TIMEOUT_MSEC =

The gem’s default eval timeout is 100ms, which interrupts large synchronous bridge loops (every property crossing is a Ruby call).

60_000

Instance Method Summary collapse

Constructor Details

#initialize(**vm_opts) ⇒ Backend

Returns a new instance of Backend.



14
15
16
17
# File 'lib/dommy/js/quickjs/backend.rb', line 14

def initialize(**vm_opts)
  vm_opts = {timeout_msec: DEFAULT_TIMEOUT_MSEC}.merge(vm_opts)
  @vm = ::Quickjs::VM.new(**vm_opts)
end

Instance Method Details

#call_js(path, *args) ⇒ Object



33
34
35
# File 'lib/dommy/js/quickjs/backend.rb', line 33

def call_js(path, *args)
  @vm.call(path, *args)
end

#define_host_function(name, &block) ⇒ Object



29
30
31
# File 'lib/dommy/js/quickjs/backend.rb', line 29

def define_host_function(name, &block)
  @vm.define_function(name, &block)
end

#disposeObject



58
59
60
# File 'lib/dommy/js/quickjs/backend.rb', line 58

def dispose
  @vm.dispose!
end

#drain_microtasksObject



37
38
39
# File 'lib/dommy/js/quickjs/backend.rb', line 37

def drain_microtasks
  @vm.drain_jobs!
end

#eval(js) ⇒ Object



19
20
21
# File 'lib/dommy/js/quickjs/backend.rb', line 19

def eval(js)
  @vm.eval_code(js, async: false)
end

#eval_awaited(js) ⇒ Object

Async eval: the gem awaits the top-level result and drains the microtask queue, so JS ‘await`/Promises resolve before returning.



25
26
27
# File 'lib/dommy/js/quickjs/backend.rb', line 25

def eval_awaited(js)
  @vm.eval_code(js, async: true)
end

#on_log(&block) ⇒ Object

Register a handler for console.(log|info|debug|warn|error). The block receives a log object (#severity / #to_s / #raw).



50
51
52
# File 'lib/dommy/js/quickjs/backend.rb', line 50

def on_log(&block)
  @vm.on_log(&block)
end

#on_unhandled_rejection(&block) ⇒ Object

Register a handler for promise rejections that reach the microtask queue with no ‘.catch` — frameworks (Turbo, …) often swallow these, so surfacing them is essential for diagnosing failures.



44
45
46
# File 'lib/dommy/js/quickjs/backend.rb', line 44

def on_unhandled_rejection(&block)
  @vm.on_unhandled_rejection(&block)
end

#run_gcObject



54
55
56
# File 'lib/dommy/js/quickjs/backend.rb', line 54

def run_gc
  @vm.gc!
end