File tree 3 files changed +32
-0
lines changed
3 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,11 @@ def helpers
84
84
@helpers ||= Set . new ( [ :breaker , :slice ] )
85
85
end
86
86
87
+ # Operator helpers
88
+ def operator_helpers
89
+ @operator_helpers ||= Set . new
90
+ end
91
+
87
92
# Method calls made in this file
88
93
def method_calls
89
94
@method_calls ||= Set . new
Original file line number Diff line number Diff line change @@ -11,6 +11,9 @@ class CallNode < Base
11
11
12
12
SPECIALS = { }
13
13
14
+ # Operators that get optimized by compiler
15
+ OPERATORS = { :+ => :plus }
16
+
14
17
def self . add_special ( name , options = { } , &handler )
15
18
SPECIALS [ name ] = options
16
19
define_method ( "handle_#{ name } " , &handler )
@@ -139,6 +142,19 @@ def compile_default?
139
142
@compile_default
140
143
end
141
144
145
+ OPERATORS . each do |operator , name |
146
+ add_special ( operator . to_sym ) do
147
+ compiler . operator_helpers << operator . to_sym
148
+ lhs , rhs = expr ( recvr ) , expr ( arglist [ 1 ] )
149
+
150
+ push fragment ( "$rb_#{ name } (" )
151
+ push lhs
152
+ push fragment ( ", " )
153
+ push rhs
154
+ push fragment ( ")" )
155
+ end
156
+ end
157
+
142
158
add_special :require do
143
159
compile_default!
144
160
str = DependencyResolver . new ( compiler , arglist [ 1 ] ) . resolve
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ def compile
23
23
add_temp 'nil = $opal.nil'
24
24
25
25
add_used_helpers
26
+ add_used_operators
26
27
line scope . to_vars
27
28
28
29
compile_method_stubs
@@ -66,6 +67,16 @@ def add_used_helpers
66
67
helpers . to_a . each { |h | add_temp "$#{ h } = $opal.#{ h } " }
67
68
end
68
69
70
+ def add_used_operators
71
+ operators = compiler . operator_helpers . to_a
72
+ operators . each do |op |
73
+ name = Nodes ::CallNode ::OPERATORS [ op ]
74
+ line "function $rb_#{ name } (lhs, rhs) {"
75
+ line " return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs #{ op } rhs : lhs['$#{ op } '](rhs);"
76
+ line "}"
77
+ end
78
+ end
79
+
69
80
def compile_method_stubs
70
81
if compiler . method_missing?
71
82
calls = compiler . method_calls
You can’t perform that action at this time.
0 commit comments