Class: Opal::Nodes::WhenNode

Inherits:
Base show all
Defined in:
opal/lib/opal/nodes/case.rb

Constant Summary

Constants included from Helpers

Helpers::RESERVED

Instance Attribute Summary

Attributes inherited from Base

#compiler, #type

Instance Method Summary collapse

Methods inherited from Base

#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, #reserved?, #variable

Constructor Details

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

Instance Method Details

#body_codeObject



91
92
93
# File 'opal/lib/opal/nodes/case.rb', line 91

def body_code
  body || s(:nil)
end

#case_stmtObject



87
88
89
# File 'opal/lib/opal/nodes/case.rb', line 87

def case_stmt
  compiler.case_stmt
end

#compileObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'opal/lib/opal/nodes/case.rb', line 60

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; i < $splt.length; i++) {"
      push "if ($splt[i]['$===']($case)) { return true; }"
      push "} return false; })(", expr(check[1]), ")"
    else
      if case_stmt[:cond]
        call = s(:call, check, :===, s(:arglist, s(:js_tmp, '$case')))
        push expr(call)
      else
        push js_truthy(check)
      end
    end
  end

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

#when_checksObject



83
84
85
# File 'opal/lib/opal/nodes/case.rb', line 83

def when_checks
  whens.children
end