Class: Opal::Nodes::Base

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

Instance Attribute Summary collapse

Attributes included from Closure::NodeSupport

#closure

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Closure::NodeSupport

#closure_is?, #compile_catcher, #generate_thrower, #generate_thrower_without_catcher, #in_closure, #pop_closure, #push_closure, #select_closure, #thrower

Methods included from Helpers

#current_indent, #empty_line, #indent, #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.



36
37
38
39
40
41
42
# File 'lib/opal/nodes/base.rb', line 36

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

Instance Attribute Details

#compilerObject (readonly)

Returns the value of attribute compiler.



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

def compiler
  @compiler
end

#sexpObject (readonly)

Returns the value of attribute sexp.



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

def sexp
  @sexp
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.children(*names) ⇒ Object



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

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

.handle(*types) ⇒ Object



16
17
18
19
20
# File 'lib/opal/nodes/base.rb', line 16

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

.handlersObject



12
13
14
# File 'lib/opal/nodes/base.rb', line 12

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

.truthy_optimize?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/opal/nodes/base.rb', line 30

def self.truthy_optimize?
  false
end

Instance Method Details

#add_gvar(name) ⇒ Object



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

def add_gvar(name)
  scope.add_scope_gvar name
end

#add_ivar(name) ⇒ Object



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

def add_ivar(name)
  scope.add_scope_ivar name
end

#add_local(name) ⇒ Object



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

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

#add_temp(temp) ⇒ Object



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

def add_temp(temp)
  scope.add_scope_temp temp
end

#childrenObject



44
45
46
# File 'lib/opal/nodes/base.rb', line 44

def children
  @sexp.children
end

#class_variable_ownerObject



207
208
209
210
211
212
213
# File 'lib/opal/nodes/base.rb', line 207

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

#class_variable_owner_nesting_levelObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/opal/nodes/base.rb', line 191

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



215
216
217
# File 'lib/opal/nodes/base.rb', line 215

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

#compileObject



56
57
58
# File 'lib/opal/nodes/base.rb', line 56

def compile
  raise 'Not Implemented'
end

#compile_to_fragmentsObject



48
49
50
51
52
53
54
# File 'lib/opal/nodes/base.rb', line 48

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

  @fragments = []
  compile
  @fragments
end

#error(msg) ⇒ Object



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

def error(msg)
  @compiler.error msg
end

#expr(sexp) ⇒ Object



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

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

#expr?Boolean

Returns:

  • (Boolean)


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

def expr?
  @level == :expr
end

#expr_or_empty(sexp) ⇒ Object



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

def expr_or_empty(sexp)
  sexp && sexp.type != :nil ? expr(sexp) : ''
end

#expr_or_nil(sexp) ⇒ Object



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

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

#fragment(str, loc: true) ⇒ Object



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

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

#has_rescue_else?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/opal/nodes/base.rb', line 167

def has_rescue_else?
  scope.has_rescue_else?
end

#helper(name) ⇒ Object



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

def helper(name)
  @compiler.helper name
end

#in_ensure(&block) ⇒ Object



171
172
173
# File 'lib/opal/nodes/base.rb', line 171

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

#in_ensure?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/opal/nodes/base.rb', line 175

def in_ensure?
  scope.in_ensure?
end

#in_resbody(&block) ⇒ Object



179
180
181
# File 'lib/opal/nodes/base.rb', line 179

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

#in_resbody?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/opal/nodes/base.rb', line 183

def in_resbody?
  scope.in_resbody?
end

#in_rescue(node, &block) ⇒ Object



187
188
189
# File 'lib/opal/nodes/base.rb', line 187

def in_rescue(node, &block)
  scope.in_rescue(node, &block)
end

#in_while?Boolean

Returns:

  • (Boolean)


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

def in_while?
  @compiler.in_while?
end

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



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

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

#push(*strs) ⇒ Object



60
61
62
63
64
65
# File 'lib/opal/nodes/base.rb', line 60

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

#recv(sexp) ⇒ Object



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

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

#recv?Boolean

Returns:

  • (Boolean)


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

def recv?
  @level == :recv
end

#s(type, *children) ⇒ Object



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

def s(type, *children)
  ::Opal::AST::Node.new(type, children, location: @sexp.loc)
end

#scopeObject



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

def scope
  @compiler.scope
end

#source_locationObject



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/opal/nodes/base.rb', line 219

def source_location
  expr = @sexp.loc.expression
  if expr.respond_to? :source_buffer
    file = expr.source_buffer.name
    file = "<internal:#{file}>" if file.start_with?("corelib/")
    file = "<js:#{file}>" if file.end_with?(".js")
  else
    file = "(eval)"
  end
  line = @sexp.loc.line
  "['#{file}', #{line}]"
end

#stmt(sexp) ⇒ Object



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

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

#stmt?Boolean

Returns:

  • (Boolean)


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

def stmt?
  @level == :stmt
end

#top_scopeObject



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

def top_scope
  @compiler.top_scope
end

#unshift(*strs) ⇒ Object



67
68
69
70
71
72
# File 'lib/opal/nodes/base.rb', line 67

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

#while_loopObject



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

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

#with_temp(&block) ⇒ Object



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

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

#wrap(pre, post) ⇒ Object



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

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