Skip to content

Commit

Permalink
Accept JS::Object like object in JS::Object#==
Browse files Browse the repository at this point in the history
Fixes #395
  • Loading branch information
kateinoigakukun committed Mar 16, 2024
1 parent 84dfd76 commit f54bf38
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/gems/js/ext/js/js-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,14 @@ static VALUE _rb_js_obj_strictly_eql(VALUE obj, VALUE other) {
* Performs "==" comparison, a.k.a the "Abstract Equality Comparison"
* algorithm defined in the ECMAScript.
* https://262.ecma-international.org/11.0/#sec-abstract-equality-comparison
* If the given other object is not a JS::Object, try to convert it to a
* JS::Object using JS.try_convert. If the conversion fails, returns false.
*/
static VALUE _rb_js_obj_eql(VALUE obj, VALUE other) {
other = _rb_js_try_convert(rb_mJS, other);
if (other == Qnil) {
return Qfalse;
}
struct jsvalue *lhs = check_jsvalue(obj);
struct jsvalue *rhs = check_jsvalue(other);
bool result = rb_js_abi_host_js_value_equal(lhs->abi, rhs->abi);
Expand Down
7 changes: 7 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 @@ -31,6 +31,13 @@ def test_eql?

assert_object_eql? false, JS.eval("return 24;"), JS.eval("return 42;")
assert_object_eql? false, JS.eval("return NaN;"), JS.eval("return NaN;")

# Compare with JS::Object like object
assert_equal true, JS.eval("return 42;") == 42
assert_equal true, JS.eval("return 42;").eql?(42)
assert_equal false, JS.eval("return 42;") != 42
# Compare with non JS::Object like object
assert_equal false, JS.eval("return 42;") != Object.new
end

def assert_object_strictly_eql?(result, a, b)
Expand Down

0 comments on commit f54bf38

Please sign in to comment.