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

Add test for passing Symbol to JS and fix it for GraalJSRuntime and RubyRhinoRuntime

* Add missing require "json" for ExternalRuntime
This commit is contained in:
Benoit Daloze 2022-01-27 12:23:38 +01:00
parent 39118e25f9
commit 071dd2d42b
4 changed files with 17 additions and 2 deletions

View file

@ -1,5 +1,6 @@
require "tmpdir"
require "execjs/runtime"
require "tmpdir"
require "json"
module ExecJS
class ExternalRuntime < Runtime

View file

@ -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

View file

@ -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

View file

@ -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)