Skip to content

Commit

Permalink
Improve error message on assignment <non-js-like> to JS (ruby#373)
Browse files Browse the repository at this point in the history
* Improve error message on assignment <non-js-like> to JS

* change error message

* format
  • Loading branch information
krmbn0576 authored Jan 30, 2024
1 parent 6124b81 commit 5161477
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/gems/js/ext/js/js-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ static VALUE _rb_js_obj_aref(VALUE obj, VALUE key) {
static VALUE _rb_js_obj_aset(VALUE obj, VALUE key, VALUE val) {
struct jsvalue *p = check_jsvalue(obj);
VALUE rv = _rb_js_try_convert(rb_mJS, val);
if (rv == Qnil) {
rb_raise(rb_eTypeError,
"wrong argument type %s (expected JS::Object like object)",
rb_class2name(rb_obj_class(val)));
}
struct jsvalue *v = check_jsvalue(rv);
rb_js_abi_host_string_t key_abi_str;
key = rb_obj_as_string(key);
Expand Down
6 changes: 6 additions & 0 deletions packages/npm-packages/ruby-wasm-wasi/test/unit/test_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ def test_member_set
assert_equal 41.to_s, object["bar"].to_s
end

def test_member_set_with_non_js_object
assert_raise_message("wrong argument type Object (expected JS::Object like object)") do
JS.global[:tmp] = Object.new
end
end

def test_member_set_with_stress_gc
GC.stress = true
JS.global[:tmp] = "1"
Expand Down

0 comments on commit 5161477

Please sign in to comment.