1
0
Fork 0
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:
Jean Boussier 2022-01-28 11:06:41 +01:00 committed by GitHub
commit d8a6d4c183
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 2 deletions

View file

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

View file

@ -96,6 +96,8 @@ module ExecJS
case value case value
when nil, true, false, Integer, Float, String when nil, true, false, Integer, Float, String
value value
when Symbol
value.to_s
when Array when Array
value.map { |e| convert_ruby_to_js(e) } value.map { |e| convert_ruby_to_js(e) }
when Hash when Hash

View file

@ -1,4 +1,5 @@
require "execjs/runtime" require "execjs/runtime"
require "json"
module ExecJS module ExecJS
class RubyRhinoRuntime < Runtime class RubyRhinoRuntime < Runtime
@ -32,7 +33,11 @@ module ExecJS
end end
def call(properties, *args) 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 rescue Exception => e
raise wrap_error(e) raise wrap_error(e)
end end

View file

@ -167,6 +167,13 @@ class TestExecJS < Test
end end
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 def test_additional_options
assert ExecJS.eval("true", :foo => true) assert ExecJS.eval("true", :foo => true)
assert ExecJS.exec("return true", :foo => true) assert ExecJS.exec("return true", :foo => true)