Skip to content

Matches the return value of a JavaScript method with ? with a true/false decision in a JavaScript if statement #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/gems/js/lib/js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ def method_missing(sym, *args, &block)
if sym_str.end_with?("?")
# When a JS method is called with a ? suffix, it is treated as a predicate method,
# and the return value is converted to a Ruby boolean value automatically.
self.call(sym_str[0..-2].to_sym, *args, &block) == JS::True
result = self.call(sym_str[0..-2].to_sym, *args, &block)

# Type coerce the result to boolean type
# to match the true/false determination in JavaScript's if statement.
JS.global.Boolean(result) == JS::True
elsif self[sym].typeof == "function"
self.call(sym, *args, &block)
else
Expand Down
12 changes: 9 additions & 3 deletions packages/npm-packages/ruby-wasm-wasi/test/unit/test_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ def test_method_missing_with_?
return {
return_true() { return true; },
return_false() { return false; },
return_object() { return {}; }
return_object() { return {}; },
return_null() { return null; },
return_empty_string() { return ''; }
};
JS

Expand All @@ -334,8 +336,12 @@ def test_method_missing_with_?
assert_true object.return_true?
assert_false object.return_false?

# Return Ruby false when the return value is not JS::True
assert_false object.return_object?
# Return Ruby true when the return value is JavaScript true
assert_true object.return_object?

# Return Ruby false when the return value is JavaScript false
assert_false object.return_null?
assert_false object.return_empty_string?
end

def test_respond_to_missing?
Expand Down
Loading