Skip to content

Commit df1d9c2

Browse files
committed
Set encoding of JS::Object#to_s result string to UTF-8
A byte sequence of a string given by JS is encoded in UTF-8, so we want to be encoded in UTF-8 even in Ruby.
1 parent f4958c5 commit df1d9c2

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

ext/js/js-core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ static VALUE _rb_js_obj_to_s(VALUE obj) {
341341
struct jsvalue *p = check_jsvalue(obj);
342342
rb_js_abi_host_string_t ret0;
343343
rb_js_abi_host_js_value_to_string(p->abi, &ret0);
344-
return rb_str_new(ret0.ptr, ret0.len);
344+
return rb_utf8_str_new(ret0.ptr, ret0.len);
345345
}
346346

347347
/*

packages/npm-packages/ruby-wasm-wasi/test/unit/test_object.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ def test_to_s
6464
assert_equal "undefined", JS.eval("return undefined;").to_s
6565
end
6666

67+
def test_to_s_encoding
68+
assert_equal Encoding::UTF_8, JS.eval("return 'str';").to_s.encoding
69+
assert_equal Encoding::UTF_8, JS.eval("return 'あいうえお';").to_s.encoding
70+
end
71+
6772
def test_inspect
6873
assert_equal "str", JS.eval("return 'str';").to_s
6974
assert_equal "24", JS.eval("return 24;").inspect

0 commit comments

Comments
 (0)