Skip to content

Commit

Permalink
Add test for passing Symbol to JS and fix it for GraalJSRuntime and R…
Browse files Browse the repository at this point in the history
…ubyRhinoRuntime

* Add missing require "json" for ExternalRuntime
  • Loading branch information
eregon committed Jan 27, 2022
1 parent 39118e2 commit 071dd2d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/execjs/external_runtime.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "tmpdir"
require "execjs/runtime"
require "tmpdir"
require "json"

module ExecJS
class ExternalRuntime < Runtime
Expand Down
2 changes: 2 additions & 0 deletions lib/execjs/graaljs_runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def convert_ruby_to_js(value)
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
Expand Down
7 changes: 6 additions & 1 deletion lib/execjs/ruby_rhino_runtime.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "execjs/runtime"
require "json"

module ExecJS
class RubyRhinoRuntime < Runtime
Expand Down Expand Up @@ -32,7 +33,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 = JSON.parse(JSON.generate(args), create_additions: false)

unbox @rhino_context.eval(properties).call(*converted_args)
rescue Exception => e
raise wrap_error(e)
end
Expand Down
7 changes: 7 additions & 0 deletions test/test_execjs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ 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)
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)
Expand Down

0 comments on commit 071dd2d

Please sign in to comment.