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, #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_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #stmt, #stmt?, 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



103
104
105
# File 'opal/lib/opal/nodes/case.rb', line 103

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

#case_stmtObject



99
100
101
# File 'opal/lib/opal/nodes/case.rb', line 99

def case_stmt
  compiler.case_stmt
end

#compileObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'opal/lib/opal/nodes/case.rb', line 70

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



95
96
97
# File 'opal/lib/opal/nodes/case.rb', line 95

def when_checks
  children[0..-2]
end