Class: Opal::Nodes::ReturnNode

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

Instance Attribute Summary

Attributes inherited from Base

#compiler, #sexp, #type

Instance Method Summary collapse

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_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 Helpers

#conditional_send, #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::Base

Instance Method Details

#compileObject



143
144
145
146
147
148
149
150
151
152
# File 'opal/lib/opal/nodes/logic.rb', line 143

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
    error 'void value expression: cannot return as an expression'
  end
end

#return_expr_in_def?Boolean

Returns:

  • (Boolean)


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

def return_expr_in_def?
  return scope if expr? && (scope.def? || scope.lambda?)
end

#return_in_iter?Boolean

Returns:

  • (Boolean)


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

def return_in_iter?
  if (scope.iter? && !scope.lambda?) && parent_def = scope.find_parent_def
    parent_def
  end
end

#return_valObject



119
120
121
122
123
124
125
126
127
# File 'opal/lib/opal/nodes/logic.rb', line 119

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



139
140
141
# File 'opal/lib/opal/nodes/logic.rb', line 139

def scope_to_catch_return
  return_in_iter? || return_expr_in_def?
end