Class: Opal::Nodes::ReturnNode

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

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 Base

#compiler, #type

Instance Method Summary collapse

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

Constructor Details

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

Instance Method Details

#compileObject


174
175
176
177
178
179
180
181
182
183
# File 'lib/opal/nodes/logic.rb', line 174

def compile
  if def_scope = scope_to_catch_return
    def_scope.catch_return = true
    push 'Opal.ret(', return_val, ')'
  elsif stmt?
    push 'return ', return_val
  else
    raise SyntaxError, "void value expression: cannot return as an expression"
  end
end

#return_expr_in_def?Boolean


166
167
168
# File 'lib/opal/nodes/logic.rb', line 166

def return_expr_in_def?
  return scope if expr? and scope.def?
end

#return_in_iter?Boolean


160
161
162
163
164
# File 'lib/opal/nodes/logic.rb', line 160

def return_in_iter?
  if scope.iter? and parent_def = scope.find_parent_def
    parent_def
  end
end

#return_valObject


150
151
152
153
154
155
156
157
158
# File 'lib/opal/nodes/logic.rb', line 150

def return_val
  if value.nil?
    expr(s(:nil))
  elsif children.size > 1
    expr(s(:array, *children))
  else
    expr(value)
  end
end

#scope_to_catch_returnObject


170
171
172
# File 'lib/opal/nodes/logic.rb', line 170

def scope_to_catch_return
  return_in_iter? or return_expr_in_def?
end