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

View file

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

View file

@ -5,13 +5,13 @@ module ExecJS
@rhino_context = ::Rhino::Context.new @rhino_context = ::Rhino::Context.new
end end
def exec(source) def exec(source, options = {})
if /\S/ =~ source if /\S/ =~ source
eval "(function(){#{source}})()" eval "(function(){#{source}})()", options
end end
end end
def eval(source) def eval(source, options = {})
if /\S/ =~ source if /\S/ =~ source
unbox @rhino_context.eval("(#{source})") unbox @rhino_context.eval("(#{source})")
end end
@ -44,12 +44,12 @@ module ExecJS
def exec(source) def exec(source)
context = Context.new context = Context.new
context.exec(source) context.exec(source, :pure => true)
end end
def eval(source) def eval(source)
context = Context.new context = Context.new
context.eval(source) context.eval(source, :pure => true)
end end
def compile(source) 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.exec("return foo()")
assert_equal "bar", context.eval("foo()") assert_equal "bar", context.eval("foo()")
end end
def test_pure_evaluation
context = ExecJS.compile("foo = function() { return \"bar\"; }")
assert_equal "bar", context.eval("foo()", :pure => true)
end
end end