Skip to content

Commit d8a6d4c

Browse files
authored
Merge pull request #110 from eregon/symbol-to-js-fix
Add test for passing Symbol to JS and fix it for GraalJSRuntime
2 parents 39118e2 + 071dd2d commit d8a6d4c

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

lib/execjs/external_runtime.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
require "tmpdir"
21
require "execjs/runtime"
2+
require "tmpdir"
3+
require "json"
34

45
module ExecJS
56
class ExternalRuntime < Runtime

lib/execjs/graaljs_runtime.rb

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ def convert_ruby_to_js(value)
9696
case value
9797
when nil, true, false, Integer, Float, String
9898
value
99+
when Symbol
100+
value.to_s
99101
when Array
100102
value.map { |e| convert_ruby_to_js(e) }
101103
when Hash

lib/execjs/ruby_rhino_runtime.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "execjs/runtime"
2+
require "json"
23

34
module ExecJS
45
class RubyRhinoRuntime < Runtime
@@ -32,7 +33,11 @@ def eval(source, options = {})
3233
end
3334

3435
def call(properties, *args)
35-
unbox @rhino_context.eval(properties).call(*args)
36+
# Might no longer be necessary if therubyrhino handles Symbols directly:
37+
# https://github.com/rubyjs/therubyrhino/issues/43
38+
converted_args = JSON.parse(JSON.generate(args), create_additions: false)
39+
40+
unbox @rhino_context.eval(properties).call(*converted_args)
3641
rescue Exception => e
3742
raise wrap_error(e)
3843
end

test/test_execjs.rb

+7
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ def test_context_call_missing_function
167167
end
168168
end
169169

170+
def test_symbol
171+
context = ExecJS.compile("function echo(test) { return test; }")
172+
assert_equal "symbol", context.call("echo", :symbol)
173+
assert_equal ["symbol"], context.call("echo", [:symbol])
174+
assert_equal({"key" => "value"}, context.call("echo", {key: :value}))
175+
end
176+
170177
def test_additional_options
171178
assert ExecJS.eval("true", :foo => true)
172179
assert ExecJS.exec("return true", :foo => true)

0 commit comments

Comments
 (0)