Class: Opal::Nodes::WhenNode

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/nodes/case.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_falsy, #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

#body_codeObject



109
110
111
# File 'opal/lib/opal/nodes/case.rb', line 109

def body_code
  children.last || s(:nil)
end

#case_stmtObject



105
106
107
# File 'opal/lib/opal/nodes/case.rb', line 105

def case_stmt
  compiler.case_stmt
end

#compileObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'opal/lib/opal/nodes/case.rb', line 76

def compile
  push 'if ('

  when_checks.each_with_index do |check, idx|
    push ' || ' unless idx == 0

    if check.type == :splat
      push '(function($splt) { for (var i = 0, ii = $splt.length; i < ii; i++) {'
      if case_stmt[:cond]
        push "if ($splt[i]['$===']($case)) { return true; }"
      else
        push 'if (', js_truthy(check), ')) { return true; }'
      end
      push '} return false; })(', expr(check.children[0]), ')'
    elsif case_stmt[:cond]
      call = s(:send, check, :===, s(:arglist, s(:js_tmp, '$case')))
      push expr(call)
    else
      push js_truthy(check)
    end
  end

  push ') {', process(body_code, @level), '}'
end

#when_checksObject



101
102
103
# File 'opal/lib/opal/nodes/case.rb', line 101

def when_checks
  children[0..-2]
end