mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Merge pull request #110 from eregon/symbol-to-js-fix
Add test for passing Symbol to JS and fix it for GraalJSRuntime
This commit is contained in:
commit
d8a6d4c183
4 changed files with 17 additions and 2 deletions
|
@ -1,5 +1,6 @@
|
|||
require "tmpdir"
|
||||
require "execjs/runtime"
|
||||
require "tmpdir"
|
||||
require "json"
|
||||
|
||||
module ExecJS
|
||||
class ExternalRuntime < Runtime
|
||||
|
|
|
@ -96,6 +96,8 @@ module ExecJS
|
|||
case value
|
||||
when nil, true, false, Integer, Float, String
|
||||
value
|
||||
when Symbol
|
||||
value.to_s
|
||||
when Array
|
||||
value.map { |e| convert_ruby_to_js(e) }
|
||||
when Hash
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require "execjs/runtime"
|
||||
require "json"
|
||||
|
||||
module ExecJS
|
||||
class RubyRhinoRuntime < Runtime
|
||||
|
@ -32,7 +33,11 @@ module ExecJS
|
|||
end
|
||||
|
||||
def call(properties, *args)
|
||||
unbox @rhino_context.eval(properties).call(*args)
|
||||
# Might no longer be necessary if therubyrhino handles Symbols directly:
|
||||
# https://github.com/rubyjs/therubyrhino/issues/43
|
||||
converted_args = JSON.parse(JSON.generate(args), create_additions: false)
|
||||
|
||||
unbox @rhino_context.eval(properties).call(*converted_args)
|
||||
rescue Exception => e
|
||||
raise wrap_error(e)
|
||||
end
|
||||
|
|
|
@ -167,6 +167,13 @@ class TestExecJS < Test
|
|||
end
|
||||
end
|
||||
|
||||
def test_symbol
|
||||
context = ExecJS.compile("function echo(test) { return test; }")
|
||||
assert_equal "symbol", context.call("echo", :symbol)
|
||||
assert_equal ["symbol"], context.call("echo", [:symbol])
|
||||
assert_equal({"key" => "value"}, context.call("echo", {key: :value}))
|
||||
end
|
||||
|
||||
def test_additional_options
|
||||
assert ExecJS.eval("true", :foo => true)
|
||||
assert ExecJS.exec("return true", :foo => true)
|
||||
|
|
Loading…
Reference in a new issue