|
3 | 3 | module Opal
|
4 | 4 | module Nodes
|
5 | 5 | class BaseScopeNode < Base
|
| 6 | + |
| 7 | + # Every scope can have a parent scope |
| 8 | + attr_accessor :parent |
| 9 | + |
| 10 | + # The class or module name if this scope is a class scope |
| 11 | + attr_accessor :name |
| 12 | + |
| 13 | + # The given block name for a def scope |
| 14 | + attr_accessor :block_name |
| 15 | + |
| 16 | + attr_reader :scope_name |
| 17 | + attr_reader :ivars |
| 18 | + |
| 19 | + attr_accessor :mid |
| 20 | + |
| 21 | + # true if singleton def, false otherwise |
| 22 | + attr_accessor :defs |
| 23 | + |
| 24 | + # used by modules to know what methods to donate to includees |
| 25 | + attr_reader :methods |
| 26 | + |
| 27 | + # uses parents super method |
| 28 | + attr_accessor :uses_super |
| 29 | + attr_accessor :uses_zuper |
| 30 | + |
| 31 | + attr_accessor :catch_return |
| 32 | + |
| 33 | + def initialize(*) |
| 34 | + super |
| 35 | + |
| 36 | + @locals = [] |
| 37 | + @temps = [] |
| 38 | + @args = [] |
| 39 | + @ivars = [] |
| 40 | + @parent = nil |
| 41 | + @queue = [] |
| 42 | + @unique = 'a' |
| 43 | + @while_stack = [] |
| 44 | + |
| 45 | + @methods = [] |
| 46 | + |
| 47 | + @uses_block = false |
| 48 | + |
| 49 | + # used by classes to store all ivars used in direct def methods |
| 50 | + @proto_ivars = [] |
| 51 | + end |
| 52 | + |
6 | 53 | def in_scope(type, &block)
|
7 |
| - indent { compiler.in_scope(type, &block) } |
| 54 | + indent do |
| 55 | + @parent = compiler.scope |
| 56 | + compiler.scope = self |
| 57 | + block.call self |
| 58 | + compiler.scope = @parent |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + # Returns true if this scope is a class/module body scope |
| 63 | + def class_scope? |
| 64 | + @type == :class or @type == :module |
| 65 | + end |
| 66 | + |
| 67 | + # Returns true if this is strictly a class scope |
| 68 | + def class? |
| 69 | + @type == :class |
| 70 | + end |
| 71 | + |
| 72 | + # True if this is a module scope |
| 73 | + def module? |
| 74 | + @type == :module |
| 75 | + end |
| 76 | + |
| 77 | + def sclass? |
| 78 | + @type == :sclass |
| 79 | + end |
| 80 | + |
| 81 | + # Returns true if this is a top scope (main file body) |
| 82 | + def top? |
| 83 | + @type == :top |
| 84 | + end |
| 85 | + |
| 86 | + # True if a block/iter scope |
| 87 | + def iter? |
| 88 | + @type == :iter |
| 89 | + end |
| 90 | + |
| 91 | + def def? |
| 92 | + @type == :def |
| 93 | + end |
| 94 | + |
| 95 | + # Is this a normal def method directly inside a class? This is |
| 96 | + # used for optimizing ivars as we can set them to nil in the |
| 97 | + # class body |
| 98 | + def def_in_class? |
| 99 | + !@defs && @type == :def && @parent && @parent.class? |
| 100 | + end |
| 101 | + |
| 102 | + # Inside a class or module scope, the javascript variable name returned |
| 103 | + # by this function points to the classes' prototype. This is the target |
| 104 | + # to where methods are actually added inside a class body. |
| 105 | + def proto |
| 106 | + "def" |
| 107 | + end |
| 108 | + |
| 109 | + # A scope donates its methods if it is a module, or the core Object |
| 110 | + # class. Modules donate their methods to classes or objects they are |
| 111 | + # included in. Object donates methods to bridged classes whose native |
| 112 | + # prototypes do not actually inherit from Opal.Object.prototype. |
| 113 | + def should_donate? |
| 114 | + @type == :module |
| 115 | + end |
| 116 | + |
| 117 | + ## |
| 118 | + # Vars to use inside each scope |
| 119 | + def to_vars |
| 120 | + vars = @temps.dup |
| 121 | + vars.push(*@locals.map { |l| "#{l} = nil" }) |
| 122 | + |
| 123 | + iv = ivars.map do |ivar| |
| 124 | + "if (self#{ivar} == null) self#{ivar} = nil;\n" |
| 125 | + end |
| 126 | + |
| 127 | + indent = @compiler.parser_indent |
| 128 | + res = vars.empty? ? '' : "var #{vars.join ', '};" |
| 129 | + str = ivars.empty? ? res : "#{res}\n#{indent}#{iv.join indent}" |
| 130 | + |
| 131 | + if class? and !@proto_ivars.empty? |
| 132 | + #raise "FIXME to_vars" |
| 133 | + pvars = @proto_ivars.map { |i| "#{proto}#{i}"}.join(' = ') |
| 134 | + result = "%s\n%s%s = nil;" % [str, indent, pvars] |
| 135 | + else |
| 136 | + result = str |
| 137 | + end |
| 138 | + |
| 139 | + fragment(result) |
| 140 | + end |
| 141 | + |
| 142 | + # Generates code for this module to donate methods |
| 143 | + def to_donate_methods |
| 144 | + if should_donate? and !@methods.empty? |
| 145 | + fragment("%s;$opal.donate(self, [%s]);" % [@compiler.parser_indent, @methods.map(&:inspect).join(', ')]) |
| 146 | + else |
| 147 | + fragment("") |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + def add_scope_ivar(ivar) |
| 152 | + if def_in_class? |
| 153 | + @parent.add_proto_ivar ivar |
| 154 | + else |
| 155 | + @ivars << ivar unless @ivars.include? ivar |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + def add_proto_ivar(ivar) |
| 160 | + @proto_ivars << ivar unless @proto_ivars.include? ivar |
| 161 | + end |
| 162 | + |
| 163 | + def add_arg(arg) |
| 164 | + @args << arg unless @args.include? arg |
| 165 | + arg |
| 166 | + end |
| 167 | + |
| 168 | + def add_scope_local(local) |
| 169 | + return if has_local? local |
| 170 | + |
| 171 | + @locals << local |
| 172 | + end |
| 173 | + |
| 174 | + def has_local?(local) |
| 175 | + return true if @locals.include? local or @args.include? local |
| 176 | + return @parent.has_local?(local) if @parent and @type == :iter |
| 177 | + |
| 178 | + false |
| 179 | + end |
| 180 | + |
| 181 | + def add_scope_temp(*tmps) |
| 182 | + @temps.push(*tmps) |
| 183 | + end |
| 184 | + |
| 185 | + def has_temp?(tmp) |
| 186 | + @temps.include? tmp |
| 187 | + end |
| 188 | + |
| 189 | + def new_temp |
| 190 | + return @queue.pop unless @queue.empty? |
| 191 | + |
| 192 | + tmp = next_temp |
| 193 | + @temps << tmp |
| 194 | + tmp |
| 195 | + end |
| 196 | + |
| 197 | + def next_temp |
| 198 | + tmp = "$#{@unique}" |
| 199 | + @unique = @unique.succ |
| 200 | + tmp |
| 201 | + end |
| 202 | + |
| 203 | + def queue_temp(name) |
| 204 | + @queue << name |
| 205 | + end |
| 206 | + |
| 207 | + def push_while |
| 208 | + info = {} |
| 209 | + @while_stack.push info |
| 210 | + info |
| 211 | + end |
| 212 | + |
| 213 | + def pop_while |
| 214 | + @while_stack.pop |
| 215 | + end |
| 216 | + |
| 217 | + def in_while? |
| 218 | + !@while_stack.empty? |
| 219 | + end |
| 220 | + |
| 221 | + def uses_block! |
| 222 | + if @type == :iter && @parent |
| 223 | + @parent.uses_block! |
| 224 | + else |
| 225 | + @uses_block = true |
| 226 | + identify! |
| 227 | + end |
| 228 | + end |
| 229 | + |
| 230 | + def identify! |
| 231 | + return @identity if @identity |
| 232 | + |
| 233 | + @identity = @compiler.unique_temp |
| 234 | + @parent.add_scope_temp @identity if @parent |
| 235 | + |
| 236 | + @identity |
| 237 | + end |
| 238 | + |
| 239 | + def identity |
| 240 | + @identity |
| 241 | + end |
| 242 | + |
| 243 | + def find_parent_def |
| 244 | + scope = self |
| 245 | + while scope = scope.parent |
| 246 | + if scope.def? |
| 247 | + return scope |
| 248 | + end |
| 249 | + end |
| 250 | + |
| 251 | + nil |
| 252 | + end |
| 253 | + |
| 254 | + def get_super_chain |
| 255 | + chain, scope, defn, mid = [], self, 'null', 'null' |
| 256 | + |
| 257 | + while scope |
| 258 | + if scope.type == :iter |
| 259 | + chain << scope.identify! |
| 260 | + scope = scope.parent if scope.parent |
| 261 | + |
| 262 | + elsif scope.type == :def |
| 263 | + defn = scope.identify! |
| 264 | + mid = "'#{scope.mid}'" |
| 265 | + break |
| 266 | + |
| 267 | + else |
| 268 | + break |
| 269 | + end |
| 270 | + end |
| 271 | + |
| 272 | + [chain, defn, mid] |
| 273 | + end |
| 274 | + |
| 275 | + def uses_block? |
| 276 | + @uses_block |
8 | 277 | end
|
9 | 278 | end
|
10 | 279 | end
|
|
0 commit comments