From 7a4f7de7062c7d627ebac1d9a423dce0f3796ced 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 --- lib/execjs/graaljs_runtime.rb | 2 +- lib/execjs/ruby_rhino_runtime.rb | 6 +++++- test/test_execjs.rb | 5 +++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/execjs/graaljs_runtime.rb b/lib/execjs/graaljs_runtime.rb index 73d22a7..bbf70db 100644 --- a/lib/execjs/graaljs_runtime.rb +++ b/lib/execjs/graaljs_runtime.rb @@ -94,7 +94,7 @@ def convert_js_to_ruby(value) def convert_ruby_to_js(value) case value - when nil, true, false, Integer, Float, String + when nil, true, false, Integer, Float, String, Symbol value when Array value.map { |e| convert_ruby_to_js(e) } diff --git a/lib/execjs/ruby_rhino_runtime.rb b/lib/execjs/ruby_rhino_runtime.rb index 684d6cc..5c0a037 100644 --- a/lib/execjs/ruby_rhino_runtime.rb +++ b/lib/execjs/ruby_rhino_runtime.rb @@ -32,7 +32,11 @@ def eval(source, options = {}) 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 = args.map { |arg| Symbol === arg ? arg.to_s : arg } + + 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..faea186 100644 --- a/test/test_execjs.rb +++ b/test/test_execjs.rb @@ -167,6 +167,11 @@ def test_context_call_missing_function end end + def test_symbol + context = ExecJS.compile("function echo(test) { return test; }") + assert_equal "symbol", context.call("echo", :symbol) + end + def test_additional_options assert ExecJS.eval("true", :foo => true) assert ExecJS.exec("return true", :foo => true)