Skip to content

Commit 0f4471d

Browse files
eregonbnoordhuis
authored andcommitted
Handle catching and returning a Ruby exception from a Ruby callback called from JS back to Ruby for the TruffleRuby backend
1 parent 615e7b9 commit 0f4471d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/mini_racer/truffleruby.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ def convert_js_to_ruby(value)
246246
js_map_to_hash(value)
247247
elsif map_iterator?(value)
248248
value.map { |e| convert_js_to_ruby(e) }
249+
elsif Polyglot::ForeignException === value
250+
exc = MiniRacer::ScriptError.new(value.message)
251+
exc.set_backtrace(value.backtrace)
252+
exc
249253
else
250254
object = value
251255
h = {}

test/mini_racer_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,10 +1152,16 @@ def test_termination_exception
11521152
def test_ruby_exception
11531153
context = MiniRacer::Context.new
11541154
context.attach("test", proc { raise "boom" })
1155+
line = __LINE__ - 1
11551156
actual = context.eval("try { test() } catch (e) { e }")
11561157
assert_equal(actual.class, MiniRacer::ScriptError)
11571158
assert_equal(actual.message, "boom")
1158-
assert_equal(actual.backtrace, ["JavaScript Error: boom", "JavaScript at <eval>:1:7"])
1159+
if RUBY_ENGINE == "truffleruby"
1160+
assert_includes(actual.backtrace[0], "#{__FILE__}:#{line}")
1161+
assert_includes(actual.backtrace[0], "block in MiniRacerTest#test_ruby_exception")
1162+
else
1163+
assert_equal(actual.backtrace, ["JavaScript Error: boom", "JavaScript at <eval>:1:7"])
1164+
end
11591165
end
11601166

11611167
def test_large_integer

0 commit comments

Comments
 (0)