Skip to content
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
42 changes: 36 additions & 6 deletions ext/js/lib/js.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "js.so"
require_relative "./hash.rb"
require_relative "./array.rb"
require_relative "./nil_class.rb"
require_relative "js/hash.rb"
require_relative "js/array.rb"
require_relative "js/nil_class.rb"

# The JS module provides a way to interact with JavaScript from Ruby.
#
Expand All @@ -12,7 +12,10 @@
# JS.global[:document].write("Hello, world!")
# div = JS.global[:document].createElement("div")
# div[:innerText] = "click me"
# JS.global[:document][:body].appendChild(div)
# body = JS.global[:document][:body]
# if body[:classList].contains?("main")
# body.appendChild(div)
# end
# div.addEventListener("click") do |event|
# puts event # => # [object MouseEvent]
# puts event[:detail] # => 1
Expand All @@ -30,7 +33,9 @@
# JS.global[:document].call(:write, "Hello, world!")
# div = JS.global[:document].call(:createElement, "div")
# div[:innerText] = "click me"
# JS.global[:document][:body].call(:appendChild, div)
# if body[:classList].call(:contains, "main") == JS::True
# body.appendChild(div)
# end
# div.call(:addEventListener, "click") do |event|
# puts event # => # [object MouseEvent]
# puts event[:detail] # => 1
Expand Down Expand Up @@ -132,16 +137,41 @@ def new(*args)
JS.global[:Reflect].construct(self, args.to_js)
end

# Provide a shorthand form for JS::Object#call
#
# This method basically calls the JavaScript method with the same
# name as the Ruby method name as is using JS::Object#call.
#
# Exceptions are the following cases:
# * If the method name ends with a question mark (?), the question mark is removed
# and the method is called as a predicate method. The return value is converted to
# a Ruby boolean value automatically.
#
# This shorthand is unavailable for the following cases and you need to use
# JS::Object#call instead:
# * If the method name is invalid as a Ruby method name (e.g. contains a hyphen, reserved word, etc.)
# * If the method name is already defined as a Ruby method under JS::Object
# * If the JavaScript method name ends with a question mark (?)
def method_missing(sym, *args, &block)
if self[sym].typeof == "function"
sym_str = sym.to_s
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
elsif self[sym].typeof == "function"
self.call(sym, *args, &block)
else
super
end
end

# Check if a JavaScript method exists
#
# See JS::Object#method_missing for details.
def respond_to_missing?(sym, include_private)
return true if super
sym_str = sym.to_s
sym = sym_str[0..-2].to_sym if sym_str.end_with?("?")
self[sym].typeof == "function"
end

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 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 @@ -277,6 +277,27 @@ def test_method_missing_with_undefined_method
assert_raise(NoMethodError) { object.bar }
end

def test_method_missing_with_?
object = JS.eval(<<~JS)
return {
return_true() { return true; },
return_false() { return false; },
return_object() { return {}; }
};
JS

# Normally return JS::Object when without ?
assert_true object.return_true == JS::True
assert_true object.return_false == JS::False

# Return Ruby boolean value when 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?
end

def test_respond_to_missing?
object = JS.eval(<<~JS)
return { foo() { return true; } };
Expand Down