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
  Instance Attribute Summary
  
  Attributes inherited from ScopeNode
  #block_name, #catch_return, #defs, #gvars, #ivars, #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?, #unshift, #while_loop, #with_temp, #wrap
  
  
  
  
  
  
  
  
  Methods included from Helpers
  #current_indent, #empty_line, #indent, #js_falsy, #js_truthy, #js_truthy_optimize, #line, #lvar_to_js, #mid_to_jsid, #property, #valid_name?, #variable
  
    Instance Method Details
    
      
  
  
    | 
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
   
 
  
  
    | 
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249 | # File 'opal/lib/opal/nodes/def.rb', line 228
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     aritycode + "if ($arity < #{-(arity + 1)}) { Opal.ac($arity, #{arity}, this, #{meth}); }"
  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 | 
 
    
      
  
  
    | 
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 | # File 'opal/lib/opal/nodes/def.rb', line 40
def compile
  jsid = mid_to_jsid mid.to_s
  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
    unshift "\n#{current_indent}", scope.to_vars
    line stmt_code
    unshift arity_code if arity_code
    unshift "var $zuper = $slice.call(arguments, 0);" if scope.uses_zuper
    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 uses_defn?(scope)
    wrap "Opal.defn(self, '$#{mid}', ", ')'
  elsif scope.class?
    unshift "#{scope.proto}#{jsid} = "
  elsif scope.sclass?
    unshift "self.$$proto#{jsid} = "
  elsif scope.top?
    unshift "Opal.Object.$$proto#{jsid} = "
  else
    unshift "def#{jsid} = "
  end
  wrap '(', ", nil) && '#{mid}'" if expr?
end | 
 
    
      
  
  
    #compile_block_arg  ⇒ Object 
  
  
  
  
    | 
117
118
119
120
121
122
123
124
125
126
127 | # File 'opal/lib/opal/nodes/def.rb', line 117
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 
  
  
  
  
    | 
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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 | # File 'opal/lib/opal/nodes/def.rb', line 145
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 "}"
  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 
  
  
  
  
    | 
136
137
138
139
140
141
142
143 | # File 'opal/lib/opal/nodes/def.rb', line 136
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 
  
  
  
  
    | 
129
130
131
132
133
134 | # File 'opal/lib/opal/nodes/def.rb', line 129
def compile_rest_arg
  if rest_arg and rest_arg[1]
    splat = variable(rest_arg[1].to_sym)
    line "#{splat} = $slice.call(arguments, #{argc});"
  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 | 
 
    
      
  
  
    | 
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 | 
 
    
      
  
  
    | 
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 | 
 
    
      
  
  
    #uses_defn?(scope)  ⇒ Boolean 
  
  
  
  
    Simple helper to check whether this method should be defined through
Opal.defn() runtime helper.
   
 
  
    | 
217
218
219
220
221
222
223
224
225 | # File 'opal/lib/opal/nodes/def.rb', line 217
def uses_defn?(scope)
  if scope.iter? or scope.module?
    true
  elsif scope.class? and %w(Object BasicObject).include?(scope.name)
    true
  else
    false
  end
end |