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

Merge pull request #36 from josh/opaque-options

Allow for runtime specific option flags
This commit is contained in:
Guillermo Iguaran 2016-04-11 23:56:48 -05:00
commit 8f22894ca7
8 changed files with 45 additions and 22 deletions

View file

@ -6,15 +6,15 @@ module ExecJS
"Disabled"
end
def exec(source)
def exec(source, options = {})
raise Error, "ExecJS disabled"
end
def eval(source)
def eval(source, options = {})
raise Error, "ExecJS disabled"
end
def compile(source)
def compile(source, options = {})
raise Error, "ExecJS disabled"
end

View file

@ -4,7 +4,7 @@ require "json"
module ExecJS
class DuktapeRuntime < Runtime
class Context < Runtime::Context
def initialize(runtime, source = "")
def initialize(runtime, source = "", options = {})
@ctx = Duktape::Context.new(complex_object: nil)
@ctx.exec_string(encode(source), '(execjs)')
rescue Exception => e

View file

@ -4,7 +4,7 @@ require "execjs/runtime"
module ExecJS
class ExternalRuntime < Runtime
class Context < Runtime::Context
def initialize(runtime, source = "")
def initialize(runtime, source = "", options = {})
source = encode(source)
@runtime = runtime

View file

@ -15,16 +15,16 @@ module ExecJS
@runtime = runtime
end
def exec(source)
runtime.exec(source)
def exec(source, options = {})
runtime.exec(source, options)
end
def eval(source)
runtime.eval(source)
def eval(source, options = {})
runtime.eval(source, options)
end
def compile(source)
runtime.compile(source)
def compile(source, options = {})
runtime.compile(source, options)
end
def root

View file

@ -3,7 +3,7 @@ require "execjs/runtime"
module ExecJS
class RubyRacerRuntime < Runtime
class Context < Runtime::Context
def initialize(runtime, source = "")
def initialize(runtime, source = "", options = {})
source = encode(source)
lock do

View file

@ -3,7 +3,7 @@ require "execjs/runtime"
module ExecJS
class RubyRhinoRuntime < Runtime
class Context < Runtime::Context
def initialize(runtime, source = "")
def initialize(runtime, source = "", options = {})
source = encode(source)
@rhino_context = ::Rhino::Context.new

View file

@ -6,7 +6,7 @@ module ExecJS
class Context
include Encoding
def initialize(runtime, source = "")
def initialize(runtime, source = "", options = {})
end
def exec(source, options = {})
@ -30,18 +30,32 @@ module ExecJS
self.class::Context
end
def exec(source)
context = context_class.new(self)
context.exec(source)
def exec(source, options = {})
context = compile("", options)
if context.method(:exec).arity == 1
context.exec(source)
else
context.exec(source, options)
end
end
def eval(source)
context = context_class.new(self)
context.eval(source)
def eval(source, options = {})
context = compile("", options)
if context.method(:eval).arity == 1
context.eval(source)
else
context.eval(source, options)
end
end
def compile(source)
context_class.new(self, source)
def compile(source, options = {})
if context_class.instance_method(:initialize).arity == 2
context_class.new(self, source)
else
context_class.new(self, source, options)
end
end
def deprecated?

View file

@ -139,6 +139,15 @@ class TestExecJS < Test
end
end
def test_additional_options
assert ExecJS.eval("true", :foo => true)
assert ExecJS.exec("return true", :foo => true)
context = ExecJS.compile("foo = true", :foo => true)
assert context.eval("foo", :foo => true)
assert context.exec("return foo", :foo => true)
end
def test_eval_blank
assert_nil ExecJS.eval("")
assert_nil ExecJS.eval(" ")