Class: Opal::Nodes::DefNode
  
  
  
Overview
  
  Constant Summary
  
  Constants included
     from Helpers
  Helpers::BASIC_IDENTIFIER_RULES, Helpers::ES3_RESERVED_WORD_EXCLUSIVE, Helpers::ES51_RESERVED_WORD, Helpers::IMMUTABLE_PROPS, Helpers::PROTO_SPECIAL_METHODS, Helpers::PROTO_SPECIAL_PROPS
  Instance Attribute Summary
  
  Attributes inherited from ScopeNode
  #block_name, #catch_return, #defs, #gvars, #ivars, #locals, #methods, #mid, #name, #parent, #scope_name, #uses_super, #uses_zuper
  
  
  Attributes inherited from Base
  #compiler, #type
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  Methods inherited from ScopeNode
  #add_arg, #add_proto_ivar, #add_scope_gvar, #add_scope_ivar, #add_scope_local, #add_scope_temp, #class?, #class_scope?, #def?, #def_in_class?, #find_parent_def, #get_super_chain, #has_local?, #has_temp?, #identify!, #identity, #in_scope, #in_while?, #initialize, #iter?, #module?, #new_temp, #next_temp, #pop_while, #proto, #push_while, #queue_temp, #sclass?, #to_vars, #top?, #uses_block!, #uses_block?
  
  
  
  
  
  
  
  
  Methods inherited from Base
  #add_gvar, #add_ivar, #add_local, #add_temp, children, #children, #compile_to_fragments, #error, #expr, #expr?, #expr_or_nil, #fragment, handle, handlers, #helper, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #stmt, #stmt?, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap
  
  
  
  
  
  
  
  
  Methods included from Helpers
  #current_indent, #empty_line, #indent, #ivar, #js_falsy, #js_truthy, #js_truthy_optimize, #line, #lvar_to_js, #mid_to_jsid, #property, #valid_ivar_name?, #valid_name?, #variable
  
    Instance Method Details
    
      
  
  
    #argc  ⇒ Object 
  
  
  
  
    | 
29
30
31
32
33
34
35
36
37
38 | # File 'opal/lib/opal/nodes/def.rb', line 29
def argc
  return @argc if @argc
  @argc = args.length - 1
  @argc -= 1 if block_arg
  @argc -= 1 if rest_arg
  @argc -= keyword_args.size
  @argc
end | 
 
    
      
  
  
    #arity_check(args, opt, splat, kwargs, block_name, mid)  ⇒ Object 
  
  
  
  
    Returns code used in debug mode to check arity of method call
   
 
  
  
    | 
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 | # File 'opal/lib/opal/nodes/def.rb', line 239
def arity_check(args, opt, splat, kwargs, block_name, mid)
  meth = mid.to_s.inspect
  arity = args.size - 1
  arity -= (opt.size)
  arity -= 1 if splat
  arity -= (kwargs.size)
  arity -= 1 if block_name
  arity = -arity - 1 if !opt.empty? or !kwargs.empty? or splat
    aritycode = "var $arity = arguments.length;"
  if arity < 0     min_arity = -(arity + 1)
    max_arity = args.size - 1
    max_arity -= 1 if block_name
    checks = []
    checks << "$arity < #{min_arity}" if min_arity > 0
    checks << "$arity > #{max_arity}" if max_arity and not(splat)
    aritycode + "if (#{checks.join(' || ')}) { Opal.ac($arity, #{arity}, this, #{meth}); }" if checks.size > 0
  else
    aritycode + "if ($arity !== #{arity}) { Opal.ac($arity, #{arity}, this, #{meth}); }"
  end
end | 
 
    
      
  
  
    #block_arg  ⇒ Object 
  
  
  
  
    | 
25
26
27 | # File 'opal/lib/opal/nodes/def.rb', line 25
def block_arg
  @block_arg ||= args[1..-1].find { |arg| arg.first == :blockarg }
end | 
 
    
      
  
  
    #compile  ⇒ Object 
  
  
  
  
    | 
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
133 | # File 'opal/lib/opal/nodes/def.rb', line 40
def compile
  params = nil
  scope_name = nil
    if block_arg
    block_name = variable(block_arg[1]).to_sym
  end
  if compiler.arity_check?
    arity_code = arity_check(args, opt_args, rest_arg, keyword_args, block_name, mid)
  end
  in_scope do
    scope.mid = mid
    scope.defs = true if recvr
    if block_name
      scope.uses_block!
      scope.add_arg block_name
    end
    scope.block_name = block_name || '$yield'
    params = process(args)
    stmt_code = stmt(compiler.returns(stmts))
    add_temp 'self = this'
    compile_rest_arg
    compile_opt_args
    compile_keyword_args
        scope_name = scope.identity
    compile_block_arg
    if rest_arg
      scope.locals.delete(rest_arg[1])
    end
    if scope.uses_zuper
      add_local '$zuper'
      add_local '$zuper_index'
      line "$zuper = [];"
      line "for($zuper_index = 0; $zuper_index < arguments.length; $zuper_index++) {"
      line "  $zuper[$zuper_index] = arguments[$zuper_index];"
      line "}"
    end
    unshift "\n#{current_indent}", scope.to_vars
    line arity_code if arity_code
    line stmt_code
    if scope.catch_return
      unshift "try {\n"
      line "} catch ($returner) { if ($returner === Opal.returner) { return $returner.$v }"
      push " throw $returner; }"
    end
  end
  unshift ") {"
  unshift(params)
  unshift "function("
  unshift "#{scope_name} = " if scope_name
  line "}"
  if recvr
    unshift 'Opal.defs(', recv(recvr), ", '$#{mid}', "
    push ')'
  elsif scope.iter?
    wrap "Opal.def(self, '$#{mid}', ", ')'
  elsif scope.module? || scope.class?
    wrap "Opal.defn(self, '$#{mid}', ", ')'
  elsif scope.sclass?
    if scope.defs
      unshift "Opal.defs(self, '$#{mid}', "
    else
      unshift "Opal.defn(self, '$#{mid}', "
    end
    push ')'
  elsif scope.top?
    unshift "Opal.defn(Opal.Object, '$#{mid}', "
    push ')'
  else
    raise "Unsupported use of `def`; please file a bug at https://github.com/opal/opal reporting this message."
  end
  wrap '(', ", nil) && '#{mid}'" if expr?
end | 
 
    
      
  
  
    #compile_block_arg  ⇒ Object 
  
  
  
  
    | 
135
136
137
138
139
140
141
142
143
144
145 | # File 'opal/lib/opal/nodes/def.rb', line 135
def compile_block_arg
  if scope.uses_block?
    scope_name  = scope.identity
    yielder     = scope.block_name
    add_temp "$iter = #{scope_name}.$$p"
    add_temp "#{yielder} = $iter || nil"
    line "#{scope_name}.$$p = null;"
  end
end | 
 
    
      
  
  
    #compile_keyword_args  ⇒ Object 
  
  
  
  
    | 
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 | # File 'opal/lib/opal/nodes/def.rb', line 169
def compile_keyword_args
  return if keyword_args.empty?
  helper :hash2
  if rest_arg
    with_temp do |tmp|
      rest_arg_name = variable(rest_arg[1].to_sym)
      line "#{tmp} = #{rest_arg_name}[#{rest_arg_name}.length - 1];"
      line "if (#{tmp} == null || !#{tmp}.$$is_hash) {"
      line "  $kwargs = $hash2([], {});"
      line "} else {"
      line "  $kwargs = #{rest_arg_name}.pop();"
      line "}"
    end
  elsif last_opt_arg = opt_args.last
    opt_arg_name = variable(last_opt_arg[1])
    line "if (#{opt_arg_name} == null) {"
    line "  $kwargs = $hash2([], {});"
    line "}"
    line "else if (#{opt_arg_name}.$$is_hash) {"
    line "  $kwargs = #{opt_arg_name};"
    line "  #{opt_arg_name} = ", expr(last_opt_arg[2]), ";"
    line "}"
    line "else if ($kwargs == null) {"
    line "  $kwargs = $hash2([], {});"
    line "}"
  else
    line "if ($kwargs == null) {"
    line "  $kwargs = $hash2([], {});"
    line "}"
  end
  line "if (!$kwargs.$$is_hash) {"
  line "  throw Opal.ArgumentError.$new('expecting keyword args');"
  line "}"
  keyword_args.each do |kwarg|
    case kwarg.first
    when :kwoptarg
      arg_name = kwarg[1]
      var_name = variable(arg_name.to_s)
      add_local var_name
      line "if ((#{var_name} = $kwargs.$$smap['#{arg_name}']) == null) {"
      line "  #{var_name} = ", expr(kwarg[2])
      line "}"
    when :kwarg
      arg_name = kwarg[1]
      var_name = variable(arg_name.to_s)
      add_local var_name
      line "if ((#{var_name} = $kwargs.$$smap['#{arg_name}']) == null) {"
      line "  throw new Error('expecting keyword arg: #{arg_name}')"
      line "}"
    when :kwrestarg
      arg_name = kwarg[1]
      var_name = variable(arg_name.to_s)
      add_local var_name
      kwarg_names = keyword_args.select do |kw|
        [:kwoptarg, :kwarg].include? kw.first
      end.map { |kw| "#{kw[1].to_s.inspect}: true" }
      used_args = "{#{kwarg_names.join ','}}"
      line "#{var_name} = Opal.kwrestargs($kwargs, #{used_args});"
    else
      raise "unknown kwarg type #{kwarg.first}"
    end
  end
end | 
 
    
      
  
  
    #compile_opt_args  ⇒ Object 
  
  
  
  
    | 
160
161
162
163
164
165
166
167 | # File 'opal/lib/opal/nodes/def.rb', line 160
def compile_opt_args
  opt_args.each do |arg|
    next if arg[2][2] == :undefined
    line "if (#{variable(arg[1])} == null) {"
    line "  #{variable(arg[1])} = ", expr(arg[2])
    line "}"
  end
end | 
 
    
      
  
  
    #compile_rest_arg  ⇒ Object 
  
  
  
  
    | 
147
148
149
150
151
152
153
154
155
156
157
158 | # File 'opal/lib/opal/nodes/def.rb', line 147
def compile_rest_arg
  if rest_arg and rest_arg[1]
    splat = variable(rest_arg[1].to_sym)
    add_local '$splat_index'
    line "var array_size = arguments.length - #{argc};"
    line "if(array_size < 0) array_size = 0;"
    line "var #{splat} = new Array(array_size);"
    line "for($splat_index = 0; $splat_index < array_size; $splat_index++) {"
    line "  #{splat}[$splat_index] = arguments[$splat_index + #{argc}];"
    line "}"
  end
end | 
 
    
      
  
  
    #keyword_args  ⇒ Object 
  
  
  
  
    | 
19
20
21
22
23 | # File 'opal/lib/opal/nodes/def.rb', line 19
def keyword_args
  @keyword_args ||= args[1..-1].select do |arg|
    [:kwarg, :kwoptarg, :kwrestarg].include? arg.first
  end
end | 
 
    
      
  
  
    #opt_args  ⇒ Object 
  
  
  
  
    | 
11
12
13 | # File 'opal/lib/opal/nodes/def.rb', line 11
def opt_args
  @opt_args ||= args[1..-1].select { |arg| arg.first == :optarg }
end | 
 
    
      
  
  
    #rest_arg  ⇒ Object 
  
  
  
  
    | 
15
16
17 | # File 'opal/lib/opal/nodes/def.rb', line 15
def rest_arg
  @rest_arg ||= args[1..-1].find { |arg| arg.first == :restarg }
end |