1
0
Fork 0
mirror of https://github.com/rails/execjs synced 2023-03-27 23:21:20 -04:00

Wrap V8 calls with lock

This commit is contained in:
Joshua Peek 2011-06-08 14:22:58 -05:00
parent 8c64ffb247
commit a48bc7c7ad

View file

@ -4,8 +4,10 @@ module ExecJS
def initialize(source = "")
source = source.encode('UTF-8') if source.respond_to?(:encode)
@v8_context = ::V8::Context.new
@v8_context.eval(source)
lock do
@v8_context = ::V8::Context.new
@v8_context.eval(source)
end
end
def exec(source, options = {})
@ -20,7 +22,9 @@ module ExecJS
source = source.encode('UTF-8') if source.respond_to?(:encode)
if /\S/ =~ source
unbox @v8_context.eval("(#{source})")
lock do
unbox @v8_context.eval("(#{source})")
end
end
rescue ::V8::JSError => e
if e.value["name"] == "SyntaxError"
@ -31,7 +35,9 @@ module ExecJS
end
def call(properties, *args)
unbox @v8_context.eval(properties).call(*args)
lock do
unbox @v8_context.eval(properties).call(*args)
end
rescue ::V8::JSError => e
if e.value["name"] == "SyntaxError"
raise RuntimeError, e.message
@ -59,6 +65,24 @@ module ExecJS
value
end
end
private
def lock
result, exception = nil, nil
V8::C::Locker() do
begin
result = yield
rescue Exception => e
exception = e
end
end
if exception
raise exception
else
result
end
end
end
def name