File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed
packages/npm-packages/ruby-wasm-wasi/test/unit Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,30 @@ module JS
41
41
Undefined = JS . eval ( "return undefined" )
42
42
Null = JS . eval ( "return null" )
43
43
44
+ # A boolean value in JavaScript is always a JS::Object instance from Ruby's point of view.
45
+ # If we use the boolean value returned by a JavaScript function as the condition for an if expression in Ruby,
46
+ # the if expression will always be true.
47
+ #
48
+ # == Bad Example
49
+ #
50
+ # searchParams = JS.global[:URLSearchParams].new(JS.global[:location][:search])
51
+ # if searchParams.has('phrase')
52
+ # # Always pass through here.
53
+ # ...
54
+ # else
55
+ # ...
56
+ # end
57
+ #
58
+ # Therefore, the JS::True constant is used to determine if the JavaScript function return value is true or false.
59
+ #
60
+ # == Good Example
61
+ #
62
+ # if searchParams.has('phrase') == JS::True
63
+ # ...
64
+ # end
65
+ True = JS . eval ( "return true;" )
66
+ False = JS . eval ( "return false;" )
67
+
44
68
class PromiseScheduler
45
69
def initialize ( loop )
46
70
@loop = loop
Original file line number Diff line number Diff line change @@ -27,4 +27,11 @@ def test_eval
27
27
def test_try_convert
28
28
assert_nil JS . try_convert ( Object . new )
29
29
end
30
+
31
+ def test_constasts
32
+ assert_equal 'null' , JS ::Null . to_s
33
+ assert_equal 'undefined' , JS ::Undefined . to_s
34
+ assert_equal 'true' , JS ::True . to_s
35
+ assert_equal 'false' , JS ::False . to_s
36
+ end
30
37
end
You can’t perform that action at this time.
0 commit comments