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

Pure function hint

This commit is contained in:
Joshua Peek 2011-02-11 11:51:59 -06:00
parent 82773bfbde
commit 4bc602f41d
4 changed files with 25 additions and 17 deletions

View file

@ -9,17 +9,20 @@ module ExecJS
@script = ""
end
def eval(source)
def eval(source, options = {})
if /\S/ =~ source
exec("return eval(#{"(#{source})".to_json})")
end
end
def exec(source)
@script << source
@script << "\n"
def exec(source, options = {})
if !options[:pure]
@script << source
@script << "\n"
source = @script
end
compile_to_tempfile(@script) do |file|
compile_to_tempfile(source) do |file|
extract_result(@runtime.exec_runtime(file.path))
end
end
@ -67,12 +70,12 @@ module ExecJS
def exec(source)
context = Context.new(self)
context.exec(source)
context.exec(source, :pure => true)
end
def eval(source)
context = Context.new(self)
context.eval(source)
context.eval(source, :pure => true)
end
def compile(source)

View file

@ -5,13 +5,13 @@ module ExecJS
@v8_context = ::V8::Context.new
end
def exec(source)
def exec(source, options = {})
if /\S/ =~ source
eval "(function(){#{source}})()"
eval "(function(){#{source}})()", options
end
end
def eval(source)
def eval(source, options = {})
if /\S/ =~ source
unbox @v8_context.eval("(#{source})")
end
@ -46,12 +46,12 @@ module ExecJS
def exec(source)
context = Context.new
context.exec(source)
context.exec(source, :pure => true)
end
def eval(source)
context = Context.new
context.eval(source)
context.eval(source, :pure => true)
end
def compile(source)

View file

@ -5,13 +5,13 @@ module ExecJS
@rhino_context = ::Rhino::Context.new
end
def exec(source)
def exec(source, options = {})
if /\S/ =~ source
eval "(function(){#{source}})()"
eval "(function(){#{source}})()", options
end
end
def eval(source)
def eval(source, options = {})
if /\S/ =~ source
unbox @rhino_context.eval("(#{source})")
end
@ -44,12 +44,12 @@ module ExecJS
def exec(source)
context = Context.new
context.exec(source)
context.exec(source, :pure => true)
end
def eval(source)
context = Context.new
context.eval(source)
context.eval(source, :pure => true)
end
def compile(source)

View file

@ -23,4 +23,9 @@ class TestExecJS < Test::Unit::TestCase
assert_equal "bar", context.exec("return foo()")
assert_equal "bar", context.eval("foo()")
end
def test_pure_evaluation
context = ExecJS.compile("foo = function() { return \"bar\"; }")
assert_equal "bar", context.eval("foo()", :pure => true)
end
end