Class: Opal::Nodes::ScopeNode

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/nodes/scope.rb

Direct Known Subclasses

DefNode, IterNode, ModuleNode, SingletonClassNode, TopNode

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 collapse

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, #compile_to_fragments, #error, #expr, #expr?, #expr_or_nil, #fragment, handle, handlers, #helper, #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

#initializeScopeNode

Returns a new instance of ScopeNode



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'opal/lib/opal/nodes/scope.rb', line 35

def initialize(*)
  super

  @locals   = []
  @temps    = []
  @args     = []
  @ivars    = []
  @gvars    = []
  @parent   = nil
  @queue    = []
  @unique   = 'a'
  @while_stack = []
  @identity = nil
  @defs     = nil

  @methods = []

  @uses_block = false

  # used by classes to store all ivars used in direct def methods
  @proto_ivars = []
end

Instance Attribute Details

#block_nameObject

The given block name for a def scope



14
15
16
# File 'opal/lib/opal/nodes/scope.rb', line 14

def block_name
  @block_name
end

#catch_returnObject

Returns the value of attribute catch_return



33
34
35
# File 'opal/lib/opal/nodes/scope.rb', line 33

def catch_return
  @catch_return
end

#defsObject

true if singleton def, false otherwise



24
25
26
# File 'opal/lib/opal/nodes/scope.rb', line 24

def defs
  @defs
end

#gvarsObject (readonly)

Returns the value of attribute gvars



19
20
21
# File 'opal/lib/opal/nodes/scope.rb', line 19

def gvars
  @gvars
end

#ivarsObject (readonly)

Returns the value of attribute ivars



18
19
20
# File 'opal/lib/opal/nodes/scope.rb', line 18

def ivars
  @ivars
end

#localsObject (readonly)

Returns the value of attribute locals



17
18
19
# File 'opal/lib/opal/nodes/scope.rb', line 17

def locals
  @locals
end

#methodsObject (readonly)

used by modules to know what methods to donate to includees



27
28
29
# File 'opal/lib/opal/nodes/scope.rb', line 27

def methods
  @methods
end

#midObject

Returns the value of attribute mid



21
22
23
# File 'opal/lib/opal/nodes/scope.rb', line 21

def mid
  @mid
end

#nameObject

The class or module name if this scope is a class scope



11
12
13
# File 'opal/lib/opal/nodes/scope.rb', line 11

def name
  @name
end

#parentObject

Every scope can have a parent scope



8
9
10
# File 'opal/lib/opal/nodes/scope.rb', line 8

def parent
  @parent
end

#scope_nameObject (readonly)

Returns the value of attribute scope_name



16
17
18
# File 'opal/lib/opal/nodes/scope.rb', line 16

def scope_name
  @scope_name
end

#uses_superObject

uses parents super method



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

def uses_super
  @uses_super
end

#uses_zuperObject

Returns the value of attribute uses_zuper



31
32
33
# File 'opal/lib/opal/nodes/scope.rb', line 31

def uses_zuper
  @uses_zuper
end

Instance Method Details

#add_arg(arg) ⇒ Object



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

def add_arg(arg)
  @args << arg unless @args.include? arg
  arg
end

#add_proto_ivar(ivar) ⇒ Object



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

def add_proto_ivar(ivar)
  @proto_ivars << ivar unless @proto_ivars.include? ivar
end

#add_scope_gvar(gvar) ⇒ Object



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

def add_scope_gvar(gvar)
  @gvars << gvar unless @gvars.include? gvar
end

#add_scope_ivar(ivar) ⇒ Object



144
145
146
147
148
149
150
# File 'opal/lib/opal/nodes/scope.rb', line 144

def add_scope_ivar(ivar)
  if def_in_class?
    @parent.add_proto_ivar ivar
  else
    @ivars << ivar unless @ivars.include? ivar
  end
end

#add_scope_local(local) ⇒ Object



165
166
167
168
169
# File 'opal/lib/opal/nodes/scope.rb', line 165

def add_scope_local(local)
  return if has_local? local

  @locals << local
end

#add_scope_temp(*tmps) ⇒ Object



178
179
180
# File 'opal/lib/opal/nodes/scope.rb', line 178

def add_scope_temp(*tmps)
  @temps.push(*tmps)
end

#class?Boolean

Returns true if this is strictly a class scope

Returns:

  • (Boolean)


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

def class?
  @type == :class
end

#class_scope?Boolean

Returns true if this scope is a class/module body scope

Returns:

  • (Boolean)


68
69
70
# File 'opal/lib/opal/nodes/scope.rb', line 68

def class_scope?
  @type == :class or @type == :module
end

#def?Boolean

Returns:

  • (Boolean)


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

def def?
  @type == :def
end

#def_in_class?Boolean

Is this a normal def method directly inside a class? This is used for optimizing ivars as we can set them to nil in the class body

Returns:

  • (Boolean)


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

def def_in_class?
  !@defs && @type == :def && @parent && @parent.class?
end

#find_parent_defObject



245
246
247
248
249
250
251
252
253
254
# File 'opal/lib/opal/nodes/scope.rb', line 245

def find_parent_def
  scope = self
  while scope = scope.parent
    if scope.def?
      return scope
    end
  end

  nil
end

#get_super_chainObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'opal/lib/opal/nodes/scope.rb', line 256

def get_super_chain
  chain, scope, defn, mid = [], self, 'null', 'null'

  while scope
    if scope.type == :iter
      chain << scope.identify!
      scope = scope.parent if scope.parent

    elsif scope.type == :def
      defn = scope.identify!
      mid  = "'#{scope.mid}'"
      break

    else
      break
    end
  end

  [chain, defn, mid]
end

#has_local?(local) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
174
175
176
# File 'opal/lib/opal/nodes/scope.rb', line 171

def has_local?(local)
  return true if @locals.include? local or @args.include? local
  return @parent.has_local?(local) if @parent and @type == :iter

  false
end

#has_temp?(tmp) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
# File 'opal/lib/opal/nodes/scope.rb', line 182

def has_temp?(tmp)
  @temps.include? tmp
end

#identify!Object



232
233
234
235
236
237
238
239
# File 'opal/lib/opal/nodes/scope.rb', line 232

def identify!
  return @identity if @identity

  @identity = @compiler.unique_temp
  @parent.add_scope_temp @identity if @parent

  @identity
end

#identityObject



241
242
243
# File 'opal/lib/opal/nodes/scope.rb', line 241

def identity
  @identity
end

#in_scope(&block) ⇒ Object



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

def in_scope(&block)
  indent do
    @parent = compiler.scope
    compiler.scope = self
    block.call self
    compiler.scope = @parent
  end
end

#in_while?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'opal/lib/opal/nodes/scope.rb', line 219

def in_while?
  !@while_stack.empty?
end

#iter?Boolean

True if a block/iter scope

Returns:

  • (Boolean)


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

def iter?
  @type == :iter
end

#module?Boolean

True if this is a module scope

Returns:

  • (Boolean)


78
79
80
# File 'opal/lib/opal/nodes/scope.rb', line 78

def module?
  @type == :module
end

#new_tempObject



186
187
188
189
190
191
192
# File 'opal/lib/opal/nodes/scope.rb', line 186

def new_temp
  return @queue.pop unless @queue.empty?

  tmp = next_temp
  @temps << tmp
  tmp
end

#next_tempObject



194
195
196
197
198
199
200
201
202
203
# File 'opal/lib/opal/nodes/scope.rb', line 194

def next_temp
  while true
    tmp = "$#{@unique}"
    @unique = @unique.succ
    unless has_local?(tmp)
      break
    end
  end
  tmp
end

#pop_whileObject



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

def pop_while
  @while_stack.pop
end

#protoObject

Inside a class or module scope, the javascript variable name returned by this function points to the classes' prototype. This is the target to where methods are actually added inside a class body.



110
111
112
# File 'opal/lib/opal/nodes/scope.rb', line 110

def proto
  "def"
end

#push_whileObject



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

def push_while
  info = {}
  @while_stack.push info
  info
end

#queue_temp(name) ⇒ Object



205
206
207
# File 'opal/lib/opal/nodes/scope.rb', line 205

def queue_temp(name)
  @queue << name
end

#sclass?Boolean

Returns:

  • (Boolean)


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

def sclass?
  @type == :sclass
end

#to_varsObject

Vars to use inside each scope



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'opal/lib/opal/nodes/scope.rb', line 116

def to_vars
  vars = @temps.dup
  vars.push(*@locals.map { |l| "#{l} = nil" })

  iv = ivars.map do |ivar|
    "if (self#{ivar} == null) self#{ivar} = nil;\n"
  end

  gv = gvars.map do |gvar|
    "if ($gvars#{gvar} == null) $gvars#{gvar} = nil;\n"
  end

  indent = @compiler.parser_indent
  str  = vars.empty? ? '' : "var #{vars.join ', '};\n"
  str += "#{indent}#{iv.join indent}" unless ivars.empty?
  str += "#{indent}#{gv.join indent}" unless gvars.empty?

  if class? and !@proto_ivars.empty?
    #raise "FIXME to_vars"
    pvars = @proto_ivars.map { |i| "#{proto}#{i}"}.join(' = ')
    result = "%s\n%s%s = nil;" % [str, indent, pvars]
  else
    result = str
  end

  fragment(result)
end

#top?Boolean

Returns true if this is a top scope (main file body)

Returns:

  • (Boolean)


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

def top?
  @type == :top
end

#uses_block!Object



223
224
225
226
227
228
229
230
# File 'opal/lib/opal/nodes/scope.rb', line 223

def uses_block!
  if @type == :iter && @parent
    @parent.uses_block!
  else
    @uses_block = true
    identify!
  end
end

#uses_block?Boolean

Returns:

  • (Boolean)


277
278
279
# File 'opal/lib/opal/nodes/scope.rb', line 277

def uses_block?
  @uses_block
end