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

Allow for runtime specific option flags

This commit is contained in:
Joshua Peek 2016-04-11 20:57:37 -07:00
parent b4fdd58cea
commit 0539b906aa
No known key found for this signature in database
GPG key ID: 2852ABCC729F67DA
8 changed files with 29 additions and 20 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -139,6 +139,15 @@ class TestExecJS < Test
end end
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 def test_eval_blank
assert_nil ExecJS.eval("") assert_nil ExecJS.eval("")
assert_nil ExecJS.eval(" ") assert_nil ExecJS.eval(" ")