Skip to content

Commit f8d7d1d

Browse files
committedNov 15, 2013
Add initial truthy? and falsy? inlined compiler helpers (#419)
1 parent db3fbfe commit f8d7d1d

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed
 

Diff for: ‎lib/opal/nodes/call.rb

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'set'
22
require 'opal/nodes/base'
3+
require 'opal/nodes/runtime_helpers'
34

45
module Opal
56
module Nodes
@@ -99,6 +100,9 @@ def handle_special
99100
push result
100101
return true
101102
end
103+
elsif RuntimeHelpers.compatible?(recvr, meth, arglist)
104+
push(RuntimeHelpers.new(@sexp, @level, @compiler).compile)
105+
return true
102106
end
103107
end
104108

Diff for: ‎lib/opal/nodes/runtime_helpers.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'set'
2+
require 'opal/nodes/base'
3+
4+
module Opal
5+
module Nodes
6+
class RuntimeHelpers < Base
7+
HELPERS = Set.new
8+
9+
children :recvr, :meth, :arglist
10+
11+
def self.compatible?(recvr, meth, arglist)
12+
recvr == [:const, :Opal] and HELPERS.include?(meth.to_sym)
13+
end
14+
15+
def self.helper(name, &block)
16+
HELPERS << name
17+
define_method("compile_#{name}", &block)
18+
end
19+
20+
def compile
21+
if HELPERS.include?(meth.to_sym)
22+
__send__("compile_#{meth}")
23+
else
24+
raise "Helper not supported: #{meth}"
25+
end
26+
end
27+
28+
helper :truthy? do
29+
unless sexp = arglist[1]
30+
raise "truthy? requires an object"
31+
end
32+
33+
js_truthy(sexp)
34+
end
35+
36+
helper :falsy? do
37+
unless sexp = arglist[1]
38+
raise "falsy? requires an object"
39+
end
40+
41+
js_falsy(sexp)
42+
end
43+
end
44+
end
45+
end

Diff for: ‎opal/core/helpers.rb

-16
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,6 @@ def self.fits_array!(value)
5151
end
5252
end
5353

54-
def self.truthy?(value)
55-
if value
56-
true
57-
else
58-
false
59-
end
60-
end
61-
62-
def self.falsy?(value)
63-
if value
64-
false
65-
else
66-
true
67-
end
68-
end
69-
7054
def self.destructure(args)
7155
%x{
7256
if (args.length == 1) {

0 commit comments

Comments
 (0)
Please sign in to comment.