Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for passing Symbol to JS and fix it for GraalJSRuntime #110

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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