Class: Opal::Nodes::DefinedNode

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

#compileObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'opal/lib/opal/nodes/defined.rb', line 10

def compile
  type = value.type

  case type
  when :self, :nil, :false, :true
    push type.to_s.inspect
  when :lasgn, :iasgn, :gasgn, :cvdecl, :masgn, :op_asgn_or, :op_asgn_and
    push "'assignment'"
  when :paren, :not
    push expr(s(:defined, value[1]))
  when :lvar
    push "'local-variable'"
  else
    if respond_to? "compile_#{type}"
      __send__ "compile_#{type}"
    else
      push "'expression'"
    end
  end
end

#compile_callObject



31
32
33
34
35
36
37
38
39
# File 'opal/lib/opal/nodes/defined.rb', line 31

def compile_call
  mid = mid_to_jsid value[2].to_s
  recv = value[1] ? expr(value[1]) : 'self'

  with_temp do |tmp|
    push "(((#{tmp} = ", recv, "#{mid}) && !#{tmp}.$$stub) || ", recv
    push "['$respond_to_missing?']('#{value[2].to_s}') ? 'method' : nil)"
  end
end

#compile_colon2Object



73
74
75
76
77
78
79
80
# File 'opal/lib/opal/nodes/defined.rb', line 73

def compile_colon2
  # TODO: avoid try/catch, probably a #process_colon2 alternative that
  # does not raise errors is needed
  push "(function(){ try { return (("
  push expr(value)
  push ") != null ? 'constant' : nil); } catch (err) { if (err.$$class"
  push " === Opal.NameError) { return nil; } else { throw(err); }}; })()"
end

#compile_colon3Object



82
83
84
# File 'opal/lib/opal/nodes/defined.rb', line 82

def compile_colon3
  push "(Opal.Object.$$scope.#{value[1]} == null ? nil : 'constant')"
end

#compile_constObject



69
70
71
# File 'opal/lib/opal/nodes/defined.rb', line 69

def compile_const
  push "($scope.#{value[1]} != null)"
end

#compile_cvarObject



86
87
88
# File 'opal/lib/opal/nodes/defined.rb', line 86

def compile_cvar
  push "(Opal.cvars['#{value[1]}'] != null ? 'class variable' : nil)"
end

#compile_gvarObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'opal/lib/opal/nodes/defined.rb', line 90

def compile_gvar
  name = value[1].to_s[1..-1]

  if %w[~ !].include? name
    push "'global-variable'"
  elsif %w[` ' + &].include? name
    with_temp do |tmp|
      push "((#{tmp} = $gvars['~'], #{tmp} != null && #{tmp} !== nil) ? "
      push "'global-variable' : nil)"
    end
  else
    push "($gvars[#{name.inspect}] != null ? 'global-variable' : nil)"
  end
end

#compile_ivarObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'opal/lib/opal/nodes/defined.rb', line 41

def compile_ivar
  # FIXME: this check should be positive for ivars initialized as nil too.
  # Since currently all known ivars are inialized to nil in the constructor
  # we can't tell if it was the user that put nil and made the ivar #defined?
  # or not.
  with_temp do |tmp|
    name = value[1].to_s[1..-1]

    push "((#{tmp} = self['#{name}'], #{tmp} != null && #{tmp} !== nil) ? "
    push "'instance-variable' : nil)"
  end
end

#compile_nth_refObject



105
106
107
108
109
110
# File 'opal/lib/opal/nodes/defined.rb', line 105

def compile_nth_ref
  with_temp do |tmp|
    push "((#{tmp} = $gvars['~'], #{tmp} != null && #{tmp} != nil) ? "
    push "'global-variable' : nil)"
  end
end

#compile_superObject



54
55
56
# File 'opal/lib/opal/nodes/defined.rb', line 54

def compile_super
  push expr(s(:defined_super, value))
end

#compile_xstrObject Also known as: compile_dxstr



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

def compile_xstr
  push expr(value)
  wrap '(typeof(', ') !== "undefined")'
end

#compile_yieldObject



58
59
60
61
# File 'opal/lib/opal/nodes/defined.rb', line 58

def compile_yield
  push compiler.handle_block_given_call(@sexp)
  wrap '((',  ') != null ? "yield" : nil)'
end