Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c93ea95

Browse files
committedAug 15, 2014
Initial operator commit
1 parent ddc4190 commit c93ea95

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed
 

Diff for: ‎lib/opal/compiler.rb

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ def helpers
8484
@helpers ||= Set.new([:breaker, :slice])
8585
end
8686

87+
# Operator helpers
88+
def operator_helpers
89+
@operator_helpers ||= Set.new
90+
end
91+
8792
# Method calls made in this file
8893
def method_calls
8994
@method_calls ||= Set.new

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

+16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class CallNode < Base
1111

1212
SPECIALS = {}
1313

14+
# Operators that get optimized by compiler
15+
OPERATORS = { :+ => :plus }
16+
1417
def self.add_special(name, options = {}, &handler)
1518
SPECIALS[name] = options
1619
define_method("handle_#{name}", &handler)
@@ -139,6 +142,19 @@ def compile_default?
139142
@compile_default
140143
end
141144

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+
142158
add_special :require do
143159
compile_default!
144160
str = DependencyResolver.new(compiler, arglist[1]).resolve

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

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def compile
2323
add_temp 'nil = $opal.nil'
2424

2525
add_used_helpers
26+
add_used_operators
2627
line scope.to_vars
2728

2829
compile_method_stubs
@@ -66,6 +67,16 @@ def add_used_helpers
6667
helpers.to_a.each { |h| add_temp "$#{h} = $opal.#{h}" }
6768
end
6869

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+
6980
def compile_method_stubs
7081
if compiler.method_missing?
7182
calls = compiler.method_calls

0 commit comments

Comments
 (0)
Please sign in to comment.