From 071dd2d42b161ab7bd780d897c072205c548a5ec Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 27 Jan 2022 12:23:38 +0100 Subject: [PATCH] Add test for passing Symbol to JS and fix it for GraalJSRuntime and RubyRhinoRuntime * Add missing require "json" for ExternalRuntime --- lib/execjs/external_runtime.rb | 3 ++- lib/execjs/graaljs_runtime.rb | 2 ++ lib/execjs/ruby_rhino_runtime.rb | 7 ++++++- test/test_execjs.rb | 7 +++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index 98f7489..523bc5e 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -1,5 +1,6 @@ -require "tmpdir" require "execjs/runtime" +require "tmpdir" +require "json" module ExecJS class ExternalRuntime < Runtime diff --git a/lib/execjs/graaljs_runtime.rb b/lib/execjs/graaljs_runtime.rb index 73d22a7..01d652d 100644 --- a/lib/execjs/graaljs_runtime.rb +++ b/lib/execjs/graaljs_runtime.rb @@ -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 diff --git a/lib/execjs/ruby_rhino_runtime.rb b/lib/execjs/ruby_rhino_runtime.rb index 684d6cc..b740b72 100644 --- a/lib/execjs/ruby_rhino_runtime.rb +++ b/lib/execjs/ruby_rhino_runtime.rb @@ -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 diff --git a/test/test_execjs.rb b/test/test_execjs.rb index a860049..dbf9703 100644 --- a/test/test_execjs.rb +++ b/test/test_execjs.rb @@ -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)