Class: Opal::Nodes::IfNode

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

Constant Summary

RUBY_ENGINE_CHECK =
[:call, [:const, :RUBY_ENGINE],
:==, [:arglist, [:str, "opal"]]]
RUBY_ENGINE_CHECK_NOT =
[:call, [:const, :RUBY_ENGINE],
:!=, [:arglist, [:str, "opal"]]]
RUBY_PLATFORM_CHECK =
[:call, [:const, :RUBY_PLATFORM],
:==, [:arglist, [:str, "opal"]]]
RUBY_PLATFORM_CHECK_NOT =
[:call, [:const, :RUBY_PLATFORM],
:!=, [:arglist, [:str, "opal"]]]

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

#compileObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'opal/lib/opal/nodes/if.rb', line 22

def compile
  truthy, falsy = self.truthy, self.falsy

  if skip_check_present?
    falsy = nil
  end

  if skip_check_present_not?
    truthy = nil
  end

  push "if (", js_truthy(test), ") {"

  # skip if-body if no truthy sexp
  indent { line stmt(truthy) } if truthy

  if falsy
    if falsy.type == :if
      line "} else ", stmt(falsy)
    else
      indent do
        line "} else {"
        line stmt(falsy)
      end

      line "}"
    end
  else
    push "}"
  end

  wrap "(function() {", "; return nil; })()" if needs_wrapper?
end

#falsyObject



72
73
74
# File 'opal/lib/opal/nodes/if.rb', line 72

def falsy
  needs_wrapper? ? compiler.returns(false_body || s(:nil)) : false_body
end

#needs_wrapper?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'opal/lib/opal/nodes/if.rb', line 76

def needs_wrapper?
  expr? or recv?
end

#skip_check_present?Boolean

pre-processing only effects falsy blocks. If engine is opal, then falsy block gets generated as normal. Unless engine is opal then that code gets generated as the falsy block

Returns:

  • (Boolean)


60
61
62
# File 'opal/lib/opal/nodes/if.rb', line 60

def skip_check_present?
  test == RUBY_ENGINE_CHECK or test == RUBY_PLATFORM_CHECK
end

#skip_check_present_not?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'opal/lib/opal/nodes/if.rb', line 64

def skip_check_present_not?
  test == RUBY_ENGINE_CHECK_NOT or test == RUBY_PLATFORM_CHECK_NOT
end

#truthyObject



68
69
70
# File 'opal/lib/opal/nodes/if.rb', line 68

def truthy
  needs_wrapper? ? compiler.returns(true_body || s(:nil)) : true_body
end