Class: Opal::Nodes::WhenNode

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

Constant Summary

Constants included from Helpers

Helpers::BASIC_IDENTIFIER_RULES, Helpers::ES3_RESERVED_WORD_EXCLUSIVE, Helpers::ES51_RESERVED_WORD, Helpers::IMMUTABLE_PROPS, Helpers::PROTO_SPECIAL_METHODS, Helpers::PROTO_SPECIAL_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, #ivar, #js_falsy, #js_truthy, #js_truthy_optimize, #line, #lvar_to_js, #mid_to_jsid, #property, #valid_ivar_name?, #valid_name?, #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