Skip to content

Commit 606f488

Browse files
authored
Retain string encoding when raising exception (#372)
Fixes: #369
1 parent dc0ff20 commit 606f488

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

ext/mini_racer_extension/mini_racer_extension.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,8 +1005,8 @@ static void handle_exception(VALUE e)
10051005

10061006
if (NIL_P(e))
10071007
return;
1008-
StringValue(e);
1009-
s = RSTRING_PTR(e);
1008+
e = StringValue(e);
1009+
s = StringValueCStr(e);
10101010
switch (*s) {
10111011
case NO_ERROR:
10121012
return;
@@ -1028,7 +1028,7 @@ static void handle_exception(VALUE e)
10281028
default:
10291029
rb_raise(internal_error, "bad error class %02x", *s);
10301030
}
1031-
rb_raise(klass, "%s", s+1);
1031+
rb_enc_raise(rb_enc_get(e), klass, "%s", s+1);
10321032
}
10331033

10341034
static VALUE context_alloc(VALUE klass)

test/mini_racer_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,4 +1172,15 @@ def test_large_integer
11721172
assert_equal(types, %w[number bigint number bigint])
11731173
end
11741174
end
1175+
1176+
def test_exception_message_encoding
1177+
e = nil
1178+
begin
1179+
MiniRacer::Context.new.eval("throw Error('ä')")
1180+
rescue MiniRacer::RuntimeError => e_
1181+
e = e_
1182+
end
1183+
assert e
1184+
assert_equal(e.message.encoding.to_s, "UTF-8")
1185+
end
11751186
end

0 commit comments

Comments
 (0)