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::RESERVED

Instance Attribute Summary collapse

Attributes inherited from Base

#compiler, #type

Instance Method Summary collapse

Methods inherited from Base

#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, #reserved?, #variable

Constructor Details

#initializeScopeNode

Returns a new instance of ScopeNode



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

def initialize(*)
  super

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

  @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



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

def catch_return
  @catch_return
end

#defsObject

true if singleton def, false otherwise



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

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



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

def methods
  @methods
end

#midObject

Returns the value of attribute mid



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

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



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

def uses_super
  @uses_super
end

#uses_zuperObject

Returns the value of attribute uses_zuper



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

def uses_zuper
  @uses_zuper
end

Instance Method Details

#add_arg(arg) ⇒ Object



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

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

#add_proto_ivar(ivar) ⇒ Object



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

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

#add_scope_ivar(ivar) ⇒ Object



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

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



168
169
170
171
172
# File 'opal/lib/opal/nodes/scope.rb', line 168

def add_scope_local(local)
  return if has_local? local

  @locals << local
end

#add_scope_temp(*tmps) ⇒ Object



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

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

#class?Boolean

Returns true if this is strictly a class scope

Returns:

  • (Boolean)


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

def class?
  @type == :class
end

#class_scope?Boolean

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

Returns:

  • (Boolean)


63
64
65
# File 'opal/lib/opal/nodes/scope.rb', line 63

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

#def?Boolean

Returns:

  • (Boolean)


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

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)


98
99
100
# File 'opal/lib/opal/nodes/scope.rb', line 98

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

#find_parent_defObject



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

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

  nil
end

#get_super_chainObject



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

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)


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

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)


185
186
187
# File 'opal/lib/opal/nodes/scope.rb', line 185

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

#identify!Object



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

def identify!
  return @identity if @identity

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

  @identity
end

#identityObject



239
240
241
# File 'opal/lib/opal/nodes/scope.rb', line 239

def identity
  @identity
end

#in_scope(&block) ⇒ Object



53
54
55
56
57
58
59
60
# File 'opal/lib/opal/nodes/scope.rb', line 53

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)


217
218
219
# File 'opal/lib/opal/nodes/scope.rb', line 217

def in_while?
  !@while_stack.empty?
end

#iter?Boolean

True if a block/iter scope

Returns:

  • (Boolean)


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

def iter?
  @type == :iter
end

#module?Boolean

True if this is a module scope

Returns:

  • (Boolean)


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

def module?
  @type == :module
end

#new_tempObject



189
190
191
192
193
194
195
# File 'opal/lib/opal/nodes/scope.rb', line 189

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

  tmp = next_temp
  @temps << tmp
  tmp
end

#next_tempObject



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

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

#pop_whileObject



213
214
215
# File 'opal/lib/opal/nodes/scope.rb', line 213

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.



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

def proto
  "def"
end

#push_whileObject



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

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

#queue_temp(name) ⇒ Object



203
204
205
# File 'opal/lib/opal/nodes/scope.rb', line 203

def queue_temp(name)
  @queue << name
end

#sclass?Boolean

Returns:

  • (Boolean)


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

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)


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

def should_donate?
  @type == :module
end

#to_donate_methodsObject

Generates code for this module to donate methods



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

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



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

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

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

  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)


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

def top?
  @type == :top
end

#uses_block!Object



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

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

#uses_block?Boolean

Returns:

  • (Boolean)


275
276
277
# File 'opal/lib/opal/nodes/scope.rb', line 275

def uses_block?
  @uses_block
end