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

#sexpObject (readonly)

Returns the value of attribute sexp.



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

def sexp
  @sexp
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



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

def add_gvar(name)
  scope.add_scope_gvar name
end

#add_ivar(name) ⇒ Object



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

def add_ivar(name)
  scope.add_scope_ivar name
end

#add_local(name) ⇒ Object



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

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

#add_temp(temp) ⇒ Object



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

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



200
201
202
203
204
205
206
# File 'opal/lib/opal/nodes/base.rb', line 200

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

#class_variable_owner_nesting_levelObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'opal/lib/opal/nodes/base.rb', line 184

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



208
209
210
# File 'opal/lib/opal/nodes/base.rb', line 208

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



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

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

#expr?Boolean

Returns:

  • (Boolean)


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

def expr?
  @level == :expr
end

#expr_or_nil(sexp) ⇒ Object



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

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

#fragment(str, loc: true) ⇒ Object



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

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

#has_rescue_else?Boolean

Returns:

  • (Boolean)


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

def has_rescue_else?
  scope.has_rescue_else?
end

#helper(name) ⇒ Object



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

def helper(name)
  @compiler.helper name
end

#in_ensure(&block) ⇒ Object



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

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

#in_ensure?Boolean

Returns:

  • (Boolean)


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

def in_ensure?
  scope.in_ensure?
end

#in_resbody(&block) ⇒ Object



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

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

#in_resbody?Boolean

Returns:

  • (Boolean)


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

def in_resbody?
  scope.in_resbody?
end

#in_rescue(node, &block) ⇒ Object



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

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

#in_while?Boolean

Returns:

  • (Boolean)


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

def in_while?
  @compiler.in_while?
end

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



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

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



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

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

#recv?Boolean

Returns:

  • (Boolean)


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

def recv?
  @level == :recv
end

#s(type, *children) ⇒ Object



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

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

#scopeObject



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

def scope
  @compiler.scope
end

#source_locationObject



212
213
214
215
216
217
218
# File 'opal/lib/opal/nodes/base.rb', line 212

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

#stmt(sexp) ⇒ Object



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

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

#stmt?Boolean

Returns:

  • (Boolean)


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

def stmt?
  @level == :stmt
end

#top_scopeObject



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

def top_scope
  scope.top_scope
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



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

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

#with_temp(&block) ⇒ Object



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

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