Module: Opal::Nodes::Closure::NodeSupport

Included in:
Base
Defined in:
opal/lib/opal/nodes/closure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#closureObject

Returns the value of attribute closure.



79
80
81
# File 'opal/lib/opal/nodes/closure.rb', line 79

def closure
  @closure
end

Instance Method Details

#closure_is?(type) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'opal/lib/opal/nodes/closure.rb', line 202

def closure_is?(type)
  @closure.is?(type)
end

#compile_catcherObject

Generate a catcher if thrower has been used



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
237
238
239
240
# File 'opal/lib/opal/nodes/closure.rb', line 207

def compile_catcher
  catchers = @closure.catchers

  return if catchers.empty?

  helper :thrower

  push "} catch($e) {"
  indent do
    @closure.catchers.each do |type|
      case type
      when :eval_return
        line "if ($e === Opal.t_eval_return) return $e.$v;"
      else
        line "if ($e === $t_#{type}) return $e.$v;"
      end
    end
    line "throw $e;"
  end
  line "}"

  unshift "return " if closure_is? SEND

  unshift "var ", catchers.map { |type| "$t_#{type} = $thrower('#{type}')" }.join(", "), "; "
  unshift "try { "

  unless closure_is? JS_FUNCTION
    if scope.await_encountered
      wrap "(await (async function(){", "})())"
    else
      wrap "(function(){", "})()"
    end
  end
end

#generate_thrower(type, closure, value) ⇒ Object



102
103
104
105
106
107
# File 'opal/lib/opal/nodes/closure.rb', line 102

def generate_thrower(type, closure, value)
  id = closure.register_catcher(type)
  closure.register_thrower(type, id)
  push id, '.$throw(', expr_or_empty(value), ')'
  id
end

#generate_thrower_without_catcher(type, closure, value) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'opal/lib/opal/nodes/closure.rb', line 109

def generate_thrower_without_catcher(type, closure, value)
  helper :thrower

  if closure.throwers.key? type
    id = closure.throwers[type]
  else
    id = compiler.unique_temp('t_')
    scope = closure.node.scope&.parent || top_scope
    scope.add_scope_temp("#{id} = $thrower('#{type}')")
    closure.register_thrower(type, id)
  end
  push id, '.$throw(', expr_or_empty(value), ')'
  id
end

#in_closure(type = JS_FUNCTION) ⇒ Object



88
89
90
91
92
93
# File 'opal/lib/opal/nodes/closure.rb', line 88

def in_closure(type = JS_FUNCTION)
  closure = push_closure(type)
  out = yield closure
  pop_closure
  out
end

#pop_closureObject



81
82
83
84
85
86
# File 'opal/lib/opal/nodes/closure.rb', line 81

def pop_closure
  compile_catcher
  @compiler.closure_stack.pop
  last = @compiler.closure_stack.last
  @closure = last if last&.node == self
end

#push_closure(type = JS_FUNCTION) ⇒ Object



73
74
75
76
77
# File 'opal/lib/opal/nodes/closure.rb', line 73

def push_closure(type = JS_FUNCTION)
  closure = Closure.new(self, type, select_closure)
  @compiler.closure_stack << closure
  @closure = closure
end

#select_closure(type = ANY, break_after: NONE) ⇒ Object



95
96
97
98
99
100
# File 'opal/lib/opal/nodes/closure.rb', line 95

def select_closure(type = ANY, break_after: NONE)
  @compiler.closure_stack.reverse.find do |i|
    break if (i.type & break_after) != 0
    (i.type & type) != 0
  end
end

#thrower(type, value = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
# File 'opal/lib/opal/nodes/closure.rb', line 124

def thrower(type, value = nil)
  case type
  when :return
    thrower_closure = select_closure(DEF, break_after: MODULE | TOP)
    last_closure = select_closure(JS_FUNCTION)

    if !thrower_closure
      iter_closure = select_closure(ITER, break_after: DEF | MODULE | TOP)
      if iter_closure
        generate_thrower_without_catcher(:return, iter_closure, value)
      elsif compiler.eval?
        push 'Opal.t_eval_return.$throw(', expr_or_empty(value), ')'
      else
        error 'Invalid return'
      end
    elsif thrower_closure == last_closure
      push 'return ', expr_or_nil(value)
    else
      id = generate_thrower(:return, thrower_closure, value)
      # Additionally, register our thrower on the surrounding iter, if present
      iter_closure = select_closure(ITER, break_after: DEF | MODULE | TOP)
      iter_closure.register_thrower(:return, id) if iter_closure
    end
  when :eval_return
    thrower_closure = select_closure(DEF | LAMBDA, break_after: MODULE | TOP)

    if thrower_closure
      thrower_closure.register_catcher(:eval_return)
    end
  when :next, :redo
    thrower_closure = select_closure(ITER | LOOP_INSIDE, break_after: DEF | MODULE | TOP)
    last_closure = select_closure(JS_FUNCTION | JS_LOOP_INSIDE)

    if !thrower_closure
      error 'Invalid next'
    elsif thrower_closure == last_closure
      if thrower_closure.is? LOOP_INSIDE
        push 'continue'
      elsif thrower_closure.is? ITER | LAMBDA
        push 'return ', expr_or_nil(value)
      end
    else
      generate_thrower(:next, thrower_closure, value)
    end
  when :break
    thrower_closure = select_closure(SEND | LAMBDA | LOOP, break_after: DEF | MODULE | TOP)
    last_closure = select_closure(JS_FUNCTION | JS_LOOP)

    if !thrower_closure
      iter_closure = select_closure(ITER, break_after: DEF | MODULE | TOP)
      if iter_closure
        generate_thrower_without_catcher(:break, iter_closure, value)
      else
        error 'Invalid break'
      end
    elsif thrower_closure == last_closure
      if thrower_closure.is? JS_FUNCTION | LAMBDA
        push 'return ', expr_or_nil(value)
      elsif thrower_closure.is? LOOP
        push 'break'
      end
    else
      generate_thrower(:break, thrower_closure, value)
    end
  when :retry
    thrower_closure = select_closure(RESCUE_RETRIER, break_after: DEF | MODULE | TOP)
    last_closure = select_closure(JS_LOOP_INSIDE)

    if !thrower_closure
      error 'Invalid retry'
    elsif thrower_closure == last_closure
      push 'continue'
    else
      generate_thrower(:retry, thrower_closure, value)
    end
  end
end