Class: Opal::Nodes::DefinedNode

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/nodes/defined.rb

Instance Attribute Summary

Attributes inherited from Base

#compiler, #type

Instance Method Summary collapse

Methods inherited from Base

#add_gvar, #add_ivar, #add_local, #add_temp, #children, children, #class_variable_owner, #closest_module_node, #comments, #compile_to_fragments, #error, #expr, #expr?, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #stmt, #stmt?, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Helpers

#conditional_send, #current_indent, #empty_line, #indent, #js_falsy, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?

Constructor Details

This class inherits a constructor from Opal::Nodes::Base

Instance Method Details

#compileObject



11
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
# File 'opal/lib/opal/nodes/defined.rb', line 11

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(value)
  when :yield
    compile_defined_yield(value)
    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(value)
    wrap "(", " ? 'global-variable' : nil)"
  when :nth_ref
    compile_defined_nth_ref(value)
    wrap "(", " ? 'global-variable' : nil)"
  when :array
    compile_defined_array(value)
    wrap "(", " ? 'expression' : nil)"
  else
    push "'expression'"
  end
end

#compile_defined(node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'opal/lib/opal/nodes/defined.rb', line 61

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(node) ⇒ Object



200
201
202
203
204
205
# File 'opal/lib/opal/nodes/defined.rb', line 200

def compile_defined_back_ref(node)
  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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'opal/lib/opal/nodes/defined.rb', line 161

def compile_defined_const(node)
  const_scope, const_name = *node

  const_tmp = scope.new_temp

  if const_scope.nil?
    push "(#{const_tmp} = Opal.const_get_relative($nesting, '#{const_name}', 'skip_raise'))"
  elsif const_scope == s(:cbase)
    push "(#{const_tmp} = Opal.const_get_qualified('::', '#{const_name}', 'skip_raise'))"
  else
    const_scope_tmp = compile_defined(const_scope)
    push " && (#{const_tmp} = Opal.const_get_qualified(#{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



134
135
136
137
138
139
140
141
142
143
144
# File 'opal/lib/opal/nodes/defined.rb', line 134

def compile_defined_ivar(node)
  name = node.children[0].to_s[1..-1]
  # FIXME: this check should be positive for ivars initialized as nil too.
  # Since currently all known ivars are inialized to nil in the constructor
  # we can't tell if it was the user that put nil and made the ivar #defined?
  # or not.
  tmp = scope.new_temp
  push "(#{tmp} = self['#{name}'], #{tmp} != null && #{tmp} !== nil)"

  tmp
end

#compile_defined_nth_ref(node) ⇒ Object



207
208
209
210
211
212
213
# File 'opal/lib/opal/nodes/defined.rb', line 207

def compile_defined_nth_ref(node)
  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



93
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
# File 'opal/lib/opal/nodes/defined.rb', line 93

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 = "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
      # ignoring
    else
      push " && "
      compile_defined(arg)
    end
  end

  wrap '(', ')'
  "#{meth_tmp}()"
end

#compile_defined_super(node) ⇒ Object



146
147
148
# File 'opal/lib/opal/nodes/defined.rb', line 146

def compile_defined_super(node)
  push expr s(:defined_super)
end

#compile_defined_xstr(node) ⇒ Object



157
158
159
# File 'opal/lib/opal/nodes/defined.rb', line 157

def compile_defined_xstr(node)
  push '(typeof(', expr(node), ') !== "undefined")'
end

#compile_defined_yield(node) ⇒ Object



150
151
152
153
154
155
# File 'opal/lib/opal/nodes/defined.rb', line 150

def compile_defined_yield(node)
  scope.uses_block!
  block_name = scope.block_name || (parent = scope.find_parent_def && parent.block_name)
  push "(#{block_name} != null && #{block_name} !== nil)"
  block_name
end

#compile_send_recv_doesnt_raise(recv_code) ⇒ Object



89
90
91
# File 'opal/lib/opal/nodes/defined.rb', line 89

def compile_send_recv_doesnt_raise(recv_code)
  wrap_with_try_catch(recv_code)
end

#wrap_with_try_catch(code) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'opal/lib/opal/nodes/defined.rb', line 73

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