diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index f19ed63..cd2c784 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -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) diff --git a/lib/execjs/ruby_racer_runtime.rb b/lib/execjs/ruby_racer_runtime.rb index 3c9bc0b..c16d5ac 100644 --- a/lib/execjs/ruby_racer_runtime.rb +++ b/lib/execjs/ruby_racer_runtime.rb @@ -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) diff --git a/lib/execjs/ruby_rhino_runtime.rb b/lib/execjs/ruby_rhino_runtime.rb index 48923a3..a436fc5 100644 --- a/lib/execjs/ruby_rhino_runtime.rb +++ b/lib/execjs/ruby_rhino_runtime.rb @@ -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) diff --git a/test/test_execjs.rb b/test/test_execjs.rb index 751a544..512b865 100644 --- a/test/test_execjs.rb +++ b/test/test_execjs.rb @@ -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