class Sqreen::Js::ThreadLocalExecJsRunnable

Public Class Methods

new(code) click to toggle source
# File lib/sqreen/js/thread_local_exec_js_runnable.rb, line 13
def initialize(code)
  @code = code
  @tl_key = "SQREEN_EXECJS_CONTEXT_#{object_id}".freeze
  @runtimes = [] # place where to keep strong references
  @runtimes_mutex = Mutex.new
end

Public Instance Methods

run_js_cb(cbname, _budget, arguments) click to toggle source
# File lib/sqreen/js/thread_local_exec_js_runnable.rb, line 20
def run_js_cb(cbname, _budget, arguments)
  tl_exec_js_runnable.call(cbname, *arguments)
end
with_runtimes_mutex() { || ... } click to toggle source
# File lib/sqreen/js/thread_local_exec_js_runnable.rb, line 24
def with_runtimes_mutex
  @runtimes_mutex.synchronize { yield }
end

Private Instance Methods

dispose_from_dead_threads() click to toggle source
# File lib/sqreen/js/thread_local_exec_js_runnable.rb, line 30
def dispose_from_dead_threads
  with_runtimes_mutex do
    @runtimes.delete_if { |th, _runtime| !th.alive? }
  end
end
tl_exec_js_runnable() click to toggle source
# File lib/sqreen/js/thread_local_exec_js_runnable.rb, line 36
def tl_exec_js_runnable
  runnable = Thread.current[@tl_key]
  return runnable if runnable && runnable.weakref_alive?

  dispose_from_dead_threads
  runtime = ExecJS.compile(@code)
  with_runtimes_mutex do
    @runtimes << [Thread.current, runtime]
  end
  Thread.current[@tl_key] = WeakRef.new(runtime)
end