Class: Opal::Nodes::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
opal/lib/opal/nodes/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#initialize(sexp, level, compiler) ⇒ Base

Returns a new instance of Base.



34
35
36
37
38
39
# File 'opal/lib/opal/nodes/base.rb', line 34

def initialize(sexp, level, compiler)
  @sexp = sexp
  @type = sexp.type
  @level = level
  @compiler = compiler
end

Instance Attribute Details

#compilerObject (readonly)

Returns the value of attribute compiler.



32
33
34
# File 'opal/lib/opal/nodes/base.rb', line 32

def compiler
  @compiler
end

#typeObject (readonly)

Returns the value of attribute type.



32
33
34
# File 'opal/lib/opal/nodes/base.rb', line 32

def type
  @type
end

Class Method Details

.children(*names) ⇒ Object



20
21
22
23
24
25
26
# File 'opal/lib/opal/nodes/base.rb', line 20

def self.children(*names)
  names.each_with_index do |name, idx|
    define_method(name) do
      @sexp.children[idx]
    end
  end
end

.handle(*types) ⇒ Object



14
15
16
17
18
# File 'opal/lib/opal/nodes/base.rb', line 14

def self.handle(*types)
  types.each do |type|
    Base.handlers[type] = self
  end
end

.handlersObject



10
11
12
# File 'opal/lib/opal/nodes/base.rb', line 10

def self.handlers
  @handlers ||= {}
end

.truthy_optimize?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'opal/lib/opal/nodes/base.rb', line 28

def self.truthy_optimize?
  false
end

Instance Method Details

#add_gvar(name) ⇒ Object



132
133
134
# File 'opal/lib/opal/nodes/base.rb', line 132

def add_gvar(name)
  scope.add_scope_gvar name
end

#add_ivar(name) ⇒ Object



128
129
130
# File 'opal/lib/opal/nodes/base.rb', line 128

def add_ivar(name)
  scope.add_scope_ivar name
end

#add_local(name) ⇒ Object



124
125
126
# File 'opal/lib/opal/nodes/base.rb', line 124

def add_local(name)
  scope.add_scope_local name.to_sym
end

#add_temp(temp) ⇒ Object



136
137
138
# File 'opal/lib/opal/nodes/base.rb', line 136

def add_temp(temp)
  scope.add_scope_temp temp
end

#childrenObject



41
42
43
# File 'opal/lib/opal/nodes/base.rb', line 41

def children
  @sexp.children
end

#class_variable_ownerObject



184
185
186
187
188
189
190
# File 'opal/lib/opal/nodes/base.rb', line 184

def class_variable_owner
  if scope
    "$nesting[#{class_variable_owner_nesting_level}]"
  else
    'Opal.Object'
  end
end

#class_variable_owner_nesting_levelObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'opal/lib/opal/nodes/base.rb', line 168

def class_variable_owner_nesting_level
  cvar_scope = scope
  nesting_level = 0

  while cvar_scope && !cvar_scope.class_scope?
    # Needs only `class << self`, `module`, and `class`
    # can increase nesting, but `class` & `module` are
    # covered by `class_scope?`.
    nesting_level += 1 if cvar_scope.sclass?

    cvar_scope = cvar_scope.parent
  end

  nesting_level
end

#commentsObject



192
193
194
# File 'opal/lib/opal/nodes/base.rb', line 192

def comments
  compiler.comments[@sexp.loc]
end

#compileObject



53
54
55
# File 'opal/lib/opal/nodes/base.rb', line 53

def compile
  raise 'Not Implemented'
end

#compile_to_fragmentsObject



45
46
47
48
49
50
51
# File 'opal/lib/opal/nodes/base.rb', line 45

def compile_to_fragments
  return @fragments if defined?(@fragments)

  @fragments = []
  compile
  @fragments
end

#error(msg) ⇒ Object



80
81
82
# File 'opal/lib/opal/nodes/base.rb', line 80

def error(msg)
  @compiler.error msg
end

#expr(sexp) ⇒ Object



108
109
110
# File 'opal/lib/opal/nodes/base.rb', line 108

def expr(sexp)
  @compiler.process sexp, :expr
end

#expr?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'opal/lib/opal/nodes/base.rb', line 92

def expr?
  @level == :expr
end

#expr_or_nil(sexp) ⇒ Object



120
121
122
# File 'opal/lib/opal/nodes/base.rb', line 120

def expr_or_nil(sexp)
  sexp ? expr(sexp) : 'nil'
end

#fragment(str) ⇒ Object



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

def fragment(str)
  Opal::Fragment.new str, scope, @sexp
end

#has_rescue_else?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'opal/lib/opal/nodes/base.rb', line 156

def has_rescue_else?
  scope.has_rescue_else?
end

#helper(name) ⇒ Object



140
141
142
# File 'opal/lib/opal/nodes/base.rb', line 140

def helper(name)
  @compiler.helper name
end

#in_ensure(&block) ⇒ Object



160
161
162
# File 'opal/lib/opal/nodes/base.rb', line 160

def in_ensure(&block)
  scope.in_ensure(&block)
end

#in_ensure?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'opal/lib/opal/nodes/base.rb', line 164

def in_ensure?
  scope.in_ensure?
end

#in_while?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'opal/lib/opal/nodes/base.rb', line 148

def in_while?
  @compiler.in_while?
end

#process(sexp, level = :expr) ⇒ Object



104
105
106
# File 'opal/lib/opal/nodes/base.rb', line 104

def process(sexp, level = :expr)
  @compiler.process sexp, level
end

#push(*strs) ⇒ Object



57
58
59
60
61
62
# File 'opal/lib/opal/nodes/base.rb', line 57

def push(*strs)
  strs.each do |str|
    str = fragment(str) if str.is_a?(String)
    @fragments << str
  end
end

#recv(sexp) ⇒ Object



112
113
114
# File 'opal/lib/opal/nodes/base.rb', line 112

def recv(sexp)
  @compiler.process sexp, :recv
end

#recv?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'opal/lib/opal/nodes/base.rb', line 96

def recv?
  @level == :recv
end

#s(*args) ⇒ Object



88
89
90
# File 'opal/lib/opal/nodes/base.rb', line 88

def s(*args)
  @compiler.s(*args)
end

#scopeObject



84
85
86
# File 'opal/lib/opal/nodes/base.rb', line 84

def scope
  @compiler.scope
end

#stmt(sexp) ⇒ Object



116
117
118
# File 'opal/lib/opal/nodes/base.rb', line 116

def stmt(sexp)
  @compiler.process sexp, :stmt
end

#stmt?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'opal/lib/opal/nodes/base.rb', line 100

def stmt?
  @level == :stmt
end

#unshift(*strs) ⇒ Object



64
65
66
67
68
69
# File 'opal/lib/opal/nodes/base.rb', line 64

def unshift(*strs)
  strs.reverse_each do |str|
    str = fragment(str) if str.is_a?(String)
    @fragments.unshift str
  end
end

#while_loopObject



152
153
154
# File 'opal/lib/opal/nodes/base.rb', line 152

def while_loop
  @compiler.instance_variable_get(:@while_loop)
end

#with_temp(&block) ⇒ Object



144
145
146
# File 'opal/lib/opal/nodes/base.rb', line 144

def with_temp(&block)
  @compiler.with_temp(&block)
end

#wrap(pre, post) ⇒ Object



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

def wrap(pre, post)
  unshift pre
  push post
end