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

#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.



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

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.



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



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

def add_gvar(name)
  scope.add_scope_gvar name
end

#add_ivar(name) ⇒ Object



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

def add_ivar(name)
  scope.add_scope_ivar name
end

#add_local(name) ⇒ Object



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

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

#add_temp(temp) ⇒ Object



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

def add_temp(temp)
  scope.add_scope_temp temp
end

#childrenObject



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

def children
  @sexp.children
end

#class_variable_ownerObject



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

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

#class_variable_owner_nesting_levelObject



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

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



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

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

#compileObject



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

def compile
  raise 'Not Implemented'
end

#compile_to_fragmentsObject



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

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

  @fragments = []
  compile
  @fragments
end

#error(msg) ⇒ Object



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

def error(msg)
  @compiler.error msg
end

#expr(sexp) ⇒ Object



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

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

#expr?Boolean

Returns:

  • (Boolean)


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

def expr?
  @level == :expr
end

#expr_or_nil(sexp) ⇒ Object



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

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

#fragment(str, loc: true) ⇒ Object



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

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

#has_rescue_else?Boolean

Returns:

  • (Boolean)


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

def has_rescue_else?
  scope.has_rescue_else?
end

#helper(name) ⇒ Object



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

def helper(name)
  @compiler.helper name
end

#in_ensure(&block) ⇒ Object



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

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

#in_ensure?Boolean

Returns:

  • (Boolean)


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

def in_ensure?
  scope.in_ensure?
end

#in_resbody(&block) ⇒ Object



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

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

#in_resbody?Boolean

Returns:

  • (Boolean)


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

def in_resbody?
  scope.in_resbody?
end

#in_rescue(node, &block) ⇒ Object



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

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

#in_while?Boolean

Returns:

  • (Boolean)


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

def in_while?
  @compiler.in_while?
end

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



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

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

#push(*strs) ⇒ Object



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

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

#recv(sexp) ⇒ Object



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

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

#recv?Boolean

Returns:

  • (Boolean)


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

def recv?
  @level == :recv
end

#s(type, *children) ⇒ Object



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

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

#scopeObject



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

def scope
  @compiler.scope
end

#source_locationObject



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

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



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

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

#stmt?Boolean

Returns:

  • (Boolean)


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

def stmt?
  @level == :stmt
end

#top_scopeObject



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

def top_scope
  @compiler.top_scope
end

#unshift(*strs) ⇒ Object



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

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

#while_loopObject



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

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

#with_temp(&block) ⇒ Object



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

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

#wrap(pre, post) ⇒ Object



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

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