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 = "") def initialize(source = "")
source = source.encode('UTF-8') if source.respond_to?(:encode) source = source.encode('UTF-8') if source.respond_to?(:encode)
@v8_context = ::V8::Context.new lock do
@v8_context.eval(source) @v8_context = ::V8::Context.new
@v8_context.eval(source)
end
end end
def exec(source, options = {}) def exec(source, options = {})
@ -20,7 +22,9 @@ module ExecJS
source = source.encode('UTF-8') if source.respond_to?(:encode) source = source.encode('UTF-8') if source.respond_to?(:encode)
if /\S/ =~ source if /\S/ =~ source
unbox @v8_context.eval("(#{source})") lock do
unbox @v8_context.eval("(#{source})")
end
end end
rescue ::V8::JSError => e rescue ::V8::JSError => e
if e.value["name"] == "SyntaxError" if e.value["name"] == "SyntaxError"
@ -31,7 +35,9 @@ module ExecJS
end end
def call(properties, *args) 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 rescue ::V8::JSError => e
if e.value["name"] == "SyntaxError" if e.value["name"] == "SyntaxError"
raise RuntimeError, e.message raise RuntimeError, e.message
@ -59,6 +65,24 @@ module ExecJS
value value
end end
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 end
def name def name