Sha256: 4b557ad7f62d5ac0e29366ad022369add9789b0f697a51433f4bfe79fa90bbfa
Contents?: true
Size: 1.22 KB
Versions: 9
Compression:
Stored size: 1.22 KB
Contents
# Copyright (c) 2015 Sqreen. All Rights Reserved. # Please refer to our terms for more information: https://www.sqreen.com/terms.html # TODO: => Sqreen::JS:ExecJS require 'sqreen/js/executable_js' module Sqreen module Js class ThreadLocalExecJsRunnable < ExecutableJs 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 def run_js_cb(cbname, _budget, arguments) tl_exec_js_runnable.call(cbname, *arguments) end def with_runtimes_mutex @runtimes_mutex.synchronize { yield } end private def dispose_from_dead_threads with_runtimes_mutex do @runtimes.delete_if { |th, _runtime| !th.alive? } end end 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 end end end
Version data entries
9 entries across 9 versions & 1 rubygems