Class: Opal::Nodes::IterNode

Inherits:
NodeWithArgs show all
Defined in:
opal/lib/opal/nodes/iter.rb

Instance Attribute Summary

Attributes inherited from NodeWithArgs

#arity, #original_args, #used_kwargs

Attributes inherited from ScopeNode

#await_encountered, #block_name, #catch_return, #defs, #gvars, #has_break, #has_retry, #identity, #ivars, #locals, #methods, #mid, #name, #parent, #rescue_else_sexp, #scope_name

Attributes inherited from Base

#compiler, #sexp, #type

Attributes included from Closure::NodeSupport

#closure

Instance Method Summary collapse

Methods inherited from NodeWithArgs

#compile_arity_check, #compile_body_or_shortcut, define_shortcut, #initialize, #parameters_code, shortcuts_for, #simple_value?

Methods inherited from ScopeNode

#accepts_using?, #add_arg, #add_proto_ivar, #add_scope_gvar, #add_scope_ivar, #add_scope_local, #add_scope_temp, #class?, #class_scope?, #collect_refinements_temps, #current_rescue, #def?, #def_in_class?, #defines_lambda, #find_parent_def, #gen_retry_id, #has_local?, #has_rescue_else?, #has_temp?, #identify!, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_scope, #in_while?, #initialize, #is_lambda!, #iter?, #lambda?, #lambda_definition?, #module?, #nesting, #new_refinements_temp, #new_temp, #next_temp, #pop_while, #prepare_block, #prepend_scope_temp, #push_while, #queue_temp, #refinements_temp, #relative_access, #sclass?, #scope_locals, #self, #super_chain, #to_vars, #top?, #uses_block!, #uses_block?

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_empty, #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 Closure::NodeSupport

#closure_is?, #compile_catcher, #generate_thrower, #generate_thrower_without_catcher, #in_closure, #pop_closure, #push_closure, #select_closure, #thrower

Methods included from Helpers

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

Constructor Details

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

Instance Method Details

#arity_check_nodeObject



135
136
137
# File 'opal/lib/opal/nodes/iter.rb', line 135

def arity_check_node
  s(:iter_arity_check, original_args)
end

#compileObject



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

def compile
  is_lambda! if scope.lambda_definition?

  compile_body_or_shortcut

  blockopts = {}
  blockopts["$$arity"] = arity if arity < 0
  blockopts["$$s"] = scope.self if @define_self
  blockopts["$$brk"] = @closure.throwers[:break] if @closure&.throwers&.key? :break
  blockopts["$$ret"] = @closure.throwers[:return] if @closure&.throwers&.key? :return

  if compiler.arity_check?
    blockopts["$$parameters"] = parameters_code
  end

  if compiler.enable_source_location?
    blockopts["$$source_location"] = source_location
  end

  # MRI expands a passed argument if the block:
  # 1. takes a single argument that is an array
  # 2. has more that one argument
  # With a few exceptions:
  # 1. mlhs arg: if a block takes |(a, b)| argument
  # 2. trailing ',' in the arg list (|a, |)
  # This flag on the method indicates that a block has a top level mlhs argument
  # which means that we have to expand passed array explicitly in runtime.
  if has_top_level_mlhs_arg?
    blockopts["$$has_top_level_mlhs_arg"] = "true"
  end

  if has_trailing_comma_in_args?
    blockopts["$$has_trailing_comma_in_args"] = "true"
  end

  if blockopts.keys == ["$$arity"]
    push ", #{arity}"
  elsif !blockopts.empty?
    push ', {', blockopts.map { |k, v| "#{k}: #{v}" }.join(', '), '}'
  end

  scope.nesting if @define_nesting
  scope.relative_access if @define_relative_access
end

#compile_block_argObject



90
91
92
93
94
# File 'opal/lib/opal/nodes/iter.rb', line 90

def compile_block_arg
  if block_arg
    scope.prepare_block
  end
end

#compile_bodyObject



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

def compile_body
  inline_params = nil

  to_vars = identity = body_code = nil

  in_scope do
    identity = scope.identify!

    inline_params = process(inline_args)

    compile_arity_check

    in_closure(Closure::JS_FUNCTION | Closure::ITER | (@is_lambda ? Closure::LAMBDA : 0)) do
      body_code = stmt(returned_body)

      add_temp "self = #{identity}.$$s == null ? this : #{identity}.$$s" if @define_self

      to_vars = scope.to_vars

      line body_code
    end
  end

  unshift to_vars

  if await_encountered
    unshift "async function #{identity}(", inline_params, '){'
  else
    unshift "function #{identity}(", inline_params, '){'
  end
  push '}'
end

#extract_underscore_argsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'opal/lib/opal/nodes/iter.rb', line 96

def extract_underscore_args
  valid_args = []
  caught_blank_argument = false

  args.children.each do |arg|
    arg_name = arg.children.first
    if arg_name == :_
      unless caught_blank_argument
        caught_blank_argument = true
        valid_args << arg
      end
    else
      valid_args << arg
    end
  end

  @sexp = @sexp.updated(
    nil, [
      args.updated(nil, valid_args),
      stmts
    ]
  )
end

#has_top_level_mlhs_arg?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'opal/lib/opal/nodes/iter.rb', line 124

def has_top_level_mlhs_arg?
  original_args.children.any? { |arg| arg.type == :mlhs }
end

#has_trailing_comma_in_args?Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
# File 'opal/lib/opal/nodes/iter.rb', line 128

def has_trailing_comma_in_args?
  if original_args.loc && original_args.loc.expression
    args_source = original_args.loc.expression.source
    args_source.match(/,\s*\|/)
  end
end

#returned_bodyObject



120
121
122
# File 'opal/lib/opal/nodes/iter.rb', line 120

def returned_body
  compiler.returns(stmts || s(:nil))
end