Class: Opal::Nodes::DefinedNode
- Inherits:
-
Base
- Object
- Base
- Opal::Nodes::DefinedNode
show all
- Defined in:
- opal/lib/opal/nodes/defined.rb
Instance Attribute Summary
Attributes inherited from Base
#compiler, #sexp, #type
Instance Method Summary
collapse
Methods inherited from Base
#add_gvar, #add_ivar, #add_local, #add_temp, #children, children, #class_variable_owner, #class_variable_owner_nesting_level, #comments, #compile_to_fragments, #error, #expr, #expr?, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #source_location, #stmt, #stmt?, #top_scope, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap
Methods included from Helpers
#current_indent, #empty_line, #indent, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?
Instance Method Details
#compile ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'opal/lib/opal/nodes/defined.rb', line 12
def compile
case value.type
when :self, :nil, :false, :true
push value.type.to_s.inspect
when :lvasgn, :ivasgn, :gvasgn, :cvasgn, :casgn, :op_asgn, :or_asgn, :and_asgn
push "'assignment'"
when :lvar
push "'local-variable'"
when :begin
if value.children.size == 1 && value.children[0].type == :masgn
push "'assignment'"
else
push "'expression'"
end
when :send
compile_defined_send(value)
wrap '(', " ? 'method' : nil)"
when :ivar
compile_defined_ivar(value)
wrap '(', " ? 'instance-variable' : nil)"
when :zsuper, :super
compile_defined_super
when :yield
compile_defined_yield
wrap '(', " ? 'yield' : nil)"
when :xstr
compile_defined_xstr(value)
when :const
compile_defined_const(value)
wrap '(', " ? 'constant' : nil)"
when :cvar
compile_defined_cvar(value)
wrap '(', " ? 'class variable' : nil)"
when :gvar
compile_defined_gvar(value)
wrap '(', " ? 'global-variable' : nil)"
when :back_ref
compile_defined_back_ref
wrap '(', " ? 'global-variable' : nil)"
when :nth_ref
compile_defined_nth_ref
wrap '(', " ? 'global-variable' : nil)"
when :array
compile_defined_array(value)
wrap '(', " ? 'expression' : nil)"
else
push "'expression'"
end
end
|
#compile_defined(node) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
|
# File 'opal/lib/opal/nodes/defined.rb', line 62
def compile_defined(node)
type = node.type
if respond_to? "compile_defined_#{type}"
__send__("compile_defined_#{type}", node)
else
node_tmp = scope.new_temp
push "(#{node_tmp} = ", expr(node), ')'
node_tmp
end
end
|
#compile_defined_array(node) ⇒ Object
215
216
217
218
219
220
|
# File 'opal/lib/opal/nodes/defined.rb', line 215
def compile_defined_array(node)
node.children.each_with_index do |child, idx|
push ' && ' unless idx == 0
compile_defined(child)
end
end
|
#compile_defined_back_ref ⇒ Object
200
201
202
203
204
205
|
# File 'opal/lib/opal/nodes/defined.rb', line 200
def compile_defined_back_ref
helper :gvars
back_ref_temp = scope.new_temp
push "(#{back_ref_temp} = $gvars['~'], #{back_ref_temp} != null && #{back_ref_temp} !== nil)"
back_ref_temp
end
|
#compile_defined_const(node) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'opal/lib/opal/nodes/defined.rb', line 162
def compile_defined_const(node)
const_scope, const_name = *node
const_tmp = scope.new_temp
if const_scope.nil?
push "(#{const_tmp} = #{scope.relative_access}('#{const_name}', 'skip_raise'))"
elsif const_scope == s(:cbase)
push "(#{const_tmp} = #{top_scope.absolute_const}('::', '#{const_name}', 'skip_raise'))"
else
const_scope_tmp = compile_defined(const_scope)
push " && (#{const_tmp} = #{top_scope.absolute_const}(#{const_scope_tmp}, '#{const_name}', 'skip_raise'))"
end
const_tmp
end
|
#compile_defined_cvar(node) ⇒ Object
178
179
180
181
182
183
|
# File 'opal/lib/opal/nodes/defined.rb', line 178
def compile_defined_cvar(node)
cvar_name, _ = *node
cvar_tmp = scope.new_temp
push "(#{cvar_tmp} = #{class_variable_owner}.$$cvars['#{cvar_name}'], #{cvar_tmp} != null)"
cvar_tmp
end
|
#compile_defined_gvar(node) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'opal/lib/opal/nodes/defined.rb', line 185
def compile_defined_gvar(node)
helper :gvars
name = node.children[0].to_s[1..-1]
gvar_temp = scope.new_temp
if %w[~ !].include? name
push "(#{gvar_temp} = ", expr(node), ' || true)'
else
push "(#{gvar_temp} = $gvars[#{name.inspect}], #{gvar_temp} != null)"
end
gvar_temp
end
|
#compile_defined_ivar(node) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
|
# File 'opal/lib/opal/nodes/defined.rb', line 135
def compile_defined_ivar(node)
name = node.children[0].to_s[1..-1]
tmp = scope.new_temp
push "(#{tmp} = #{scope.self}['#{name}'], #{tmp} != null && #{tmp} !== nil)"
tmp
end
|
#compile_defined_nth_ref ⇒ Object
207
208
209
210
211
212
213
|
# File 'opal/lib/opal/nodes/defined.rb', line 207
def compile_defined_nth_ref
helper :gvars
nth_ref_tmp = scope.new_temp
push "(#{nth_ref_tmp} = $gvars['~'], #{nth_ref_tmp} != null && #{nth_ref_tmp} != nil)"
nth_ref_tmp
end
|
#compile_defined_send(node) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'opal/lib/opal/nodes/defined.rb', line 94
def compile_defined_send(node)
recv, method_name, *args = *node
mid = mid_to_jsid(method_name.to_s)
if recv
recv_code = compile_defined(recv)
push ' && '
if recv.type == :send
recv_code = compile_send_recv_doesnt_raise(recv_code)
push ' && '
end
recv_tmp = scope.new_temp
push "(#{recv_tmp} = ", recv_code, ", #{recv_tmp}) && "
else
recv_tmp = scope.self
end
recv_value_tmp = scope.new_temp
push "(#{recv_value_tmp} = #{recv_tmp}) && "
meth_tmp = scope.new_temp
push "(((#{meth_tmp} = #{recv_value_tmp}#{mid}) && !#{meth_tmp}.$$stub)"
push " || #{recv_value_tmp}['$respond_to_missing?']('#{method_name}'))"
args.each do |arg|
case arg.type
when :block_pass
else
push ' && '
compile_defined(arg)
end
end
wrap '(', ')'
"#{meth_tmp}()"
end
|
#compile_defined_super ⇒ Object
147
148
149
|
# File 'opal/lib/opal/nodes/defined.rb', line 147
def compile_defined_super
push expr s(:defined_super)
end
|
#compile_defined_xstr(node) ⇒ Object
158
159
160
|
# File 'opal/lib/opal/nodes/defined.rb', line 158
def compile_defined_xstr(node)
push '(typeof(', expr(node), ') !== "undefined")'
end
|
#compile_defined_yield ⇒ Object
151
152
153
154
155
156
|
# File 'opal/lib/opal/nodes/defined.rb', line 151
def compile_defined_yield
scope.uses_block!
block_name = scope.block_name || scope.find_parent_def.block_name
push "(#{block_name} != null && #{block_name} !== nil)"
block_name
end
|
#compile_send_recv_doesnt_raise(recv_code) ⇒ Object
90
91
92
|
# File 'opal/lib/opal/nodes/defined.rb', line 90
def compile_send_recv_doesnt_raise(recv_code)
wrap_with_try_catch(recv_code)
end
|
#wrap_with_try_catch(code) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'opal/lib/opal/nodes/defined.rb', line 74
def wrap_with_try_catch(code)
returning_tmp = scope.new_temp
push "(#{returning_tmp} = (function() { try {"
push " return #{code};"
push '} catch ($err) {'
push ' if (Opal.rescue($err, [Opal.Exception])) {'
push ' try {'
push ' return false;'
push ' } finally { Opal.pop_exception() }'
push ' } else { throw $err; }'
push '}})())'
returning_tmp
end
|