Class: Opal::Nodes::ScopeNode

Inherits:
Base 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

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, #js_falsy, #js_truthy, #js_truthy_optimize, #line, #lvar_to_js, #mid_to_jsid, #property, #valid_name?, #variable

Constructor Details

#initializeScopeNode

Returns a new instance of ScopeNode



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

def initialize(*)
  super

  @locals   = []
  @temps    = []
  @args     = []
  @ivars    = []
  @gvars    = []
  @parent   = nil
  @queue    = []
  @unique   = 'a'
  @while_stack = []
  @identity = 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



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

def catch_return
  @catch_return
end

#defsObject

true if singleton def, false otherwise



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

def defs
  @defs
end

#gvarsObject (readonly)

Returns the value of attribute gvars



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

def gvars
  @gvars
end

#ivarsObject (readonly)

Returns the value of attribute ivars



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

def ivars
  @ivars
end

#methodsObject (readonly)

used by modules to know what methods to donate to includees



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

def methods
  @methods
end

#midObject

Returns the value of attribute mid



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

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



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

def uses_super
  @uses_super
end

#uses_zuperObject

Returns the value of attribute uses_zuper



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

def uses_zuper
  @uses_zuper
end

Instance Method Details

#add_arg(arg) ⇒ Object



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

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

#add_proto_ivar(ivar) ⇒ Object



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

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

#add_scope_gvar(gvar) ⇒ Object



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

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

#add_scope_ivar(ivar) ⇒ Object



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

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



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

def add_scope_local(local)
  return if has_local? local

  @locals << local
end

#add_scope_temp(*tmps) ⇒ Object



193
194
195
# File 'opal/lib/opal/nodes/scope.rb', line 193

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

#class?Boolean

Returns true if this is strictly a class scope

Returns:

  • (Boolean)


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

def class?
  @type == :class
end

#class_scope?Boolean

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

Returns:

  • (Boolean)


66
67
68
# File 'opal/lib/opal/nodes/scope.rb', line 66

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

#def?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'opal/lib/opal/nodes/scope.rb', line 94

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)


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

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

#find_parent_defObject



255
256
257
258
259
260
261
262
263
264
# File 'opal/lib/opal/nodes/scope.rb', line 255

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

  nil
end

#get_super_chainObject



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'opal/lib/opal/nodes/scope.rb', line 266

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)


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

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)


197
198
199
# File 'opal/lib/opal/nodes/scope.rb', line 197

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

#identify!Object



242
243
244
245
246
247
248
249
# File 'opal/lib/opal/nodes/scope.rb', line 242

def identify!
  return @identity if @identity

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

  @identity
end

#identityObject



251
252
253
# File 'opal/lib/opal/nodes/scope.rb', line 251

def identity
  @identity
end

#in_scope(&block) ⇒ Object



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

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)


229
230
231
# File 'opal/lib/opal/nodes/scope.rb', line 229

def in_while?
  !@while_stack.empty?
end

#iter?Boolean

True if a block/iter scope

Returns:

  • (Boolean)


90
91
92
# File 'opal/lib/opal/nodes/scope.rb', line 90

def iter?
  @type == :iter
end

#module?Boolean

True if this is a module scope

Returns:

  • (Boolean)


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

def module?
  @type == :module
end

#new_tempObject



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

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

  tmp = next_temp
  @temps << tmp
  tmp
end

#next_tempObject



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

def next_temp
  tmp = "$#{@unique}"
  @unique = @unique.succ
  tmp
end

#pop_whileObject



225
226
227
# File 'opal/lib/opal/nodes/scope.rb', line 225

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.



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

def proto
  "def"
end

#push_whileObject



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

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

#queue_temp(name) ⇒ Object



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

def queue_temp(name)
  @queue << name
end

#sclass?Boolean

Returns:

  • (Boolean)


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

def sclass?
  @type == :sclass
end

#should_donate?Boolean

A scope donates its methods if it is a module, or the core Object class. Modules donate their methods to classes or objects they are included in. Object donates methods to bridged classes whose native prototypes do not actually inherit from Opal.Object.prototype.

Returns:

  • (Boolean)


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

def should_donate?
  @type == :module
end

#to_donate_methodsObject

Generates code for this module to donate methods



151
152
153
154
155
156
157
# File 'opal/lib/opal/nodes/scope.rb', line 151

def to_donate_methods
  if should_donate? and !@methods.empty?
    fragment("%s;$opal.donate(self, [%s]);" % [@compiler.parser_indent, @methods.map(&:inspect).join(', ')])
  else
    fragment("")
  end
end

#to_varsObject

Vars to use inside each scope



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'opal/lib/opal/nodes/scope.rb', line 122

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)


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

def top?
  @type == :top
end

#uses_block!Object



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

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

#uses_block?Boolean

Returns:

  • (Boolean)


287
288
289
# File 'opal/lib/opal/nodes/scope.rb', line 287

def uses_block?
  @uses_block
end