Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// This library defines the operations that define and manipulate Dart | 5 /// This library defines the operations that define and manipulate Dart |
| 6 /// classes. Included in this are: | 6 /// classes. Included in this are: |
| 7 /// - Generics | 7 /// - Generics |
| 8 /// - Class metadata | 8 /// - Class metadata |
| 9 /// - Extension methods | 9 /// - Extension methods |
| 10 /// | 10 /// |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 if (sym == null) { | 272 if (sym == null) { |
| 273 sym = JS('', 'Symbol("dartx." + #.toString())', name); | 273 sym = JS('', 'Symbol("dartx." + #.toString())', name); |
| 274 JS('', 'dartx[#] = #', name, sym); | 274 JS('', 'dartx[#] = #', name, sym); |
| 275 } | 275 } |
| 276 return sym; | 276 return sym; |
| 277 } | 277 } |
| 278 | 278 |
| 279 defineExtensionNames(names) => | 279 defineExtensionNames(names) => |
| 280 JS('', '#.forEach(#)', names, getExtensionSymbol); | 280 JS('', '#.forEach(#)', names, getExtensionSymbol); |
| 281 | 281 |
| 282 | |
| 283 int _t1 = 0, _t2 = 0; | |
| 284 void tick1() { | |
| 285 _t1 = JS('int', '# + 1', _t1); | |
| 286 if (JS('bool', '# % 100 == 0', _t1)) JS('', 'console.log([#, #])', _t1, _t2); | |
| 287 } | |
| 288 void tick2() { | |
| 289 _t2 = JS('int', '# + 1', _t2); | |
| 290 if (JS('bool', '# % 100 == 0', _t2)) JS('', 'console.log([#, #])', _t1, _t2); | |
| 291 } | |
| 292 | |
| 282 // Install properties in prototype order. Properties / descriptors from | 293 // Install properties in prototype order. Properties / descriptors from |
| 283 // more specific types should overwrite ones from less specific types. | 294 // more specific types should overwrite ones from less specific types. |
| 284 _installProperties(jsProto, extProto) { | 295 _installProperties(jsProto, extProto) { |
| 296 tick1(); | |
| 285 var coreObjProto = JS('', '#.prototype', Object); | 297 var coreObjProto = JS('', '#.prototype', Object); |
| 298 _installProperties2(jsProto, extProto, coreObjProto); | |
| 299 // Mark this jsProto as being the prototype for the extension class. | |
| 300 // TODO(sra): Fix the tagging. | |
| 301 JS('', '#._xxx = #', jsProto, extProto); | |
| 302 } | |
| 303 | |
| 304 _installProperties2(jsProto, extProto, coreObjProto) { | |
| 305 tick2(); | |
| 286 if (JS('bool', '# === #', extProto, coreObjProto)) { | 306 if (JS('bool', '# === #', extProto, coreObjProto)) { |
| 287 // core.Object members need to be copied from the non-symbol name to the | 307 // core.Object members need to be copied from the non-symbol name to the |
| 288 // symbol name. | 308 // symbol name. |
| 289 var names = getOwnPropertyNames(coreObjProto); | 309 var names = getOwnPropertyNames(coreObjProto); |
| 290 for (int i = 0; i < JS('int', '#.length', names); ++i) { | 310 for (int i = 0; i < JS('int', '#.length', names); ++i) { |
| 291 var name = JS('', '#[#]', names, i); | 311 var name = JS('', '#[#]', names, i); |
| 292 var desc = getOwnPropertyDescriptor(coreObjProto, name); | 312 var desc = getOwnPropertyDescriptor(coreObjProto, name); |
| 293 defineProperty(jsProto, getExtensionSymbol(name), desc); | 313 defineProperty(jsProto, getExtensionSymbol(name), desc); |
| 294 } | 314 } |
| 295 return; | 315 return; |
| 296 } | 316 } |
| 297 if (JS('bool', '# !== #', jsProto, extProto)) { | 317 if (JS('bool', '# !== #', jsProto, extProto)) { |
| 298 _installProperties(jsProto, JS('', '#.__proto__', extProto)); | 318 var extParent = JS('', '#.__proto__', extProto); |
| 319 // If the js parent is the extension class parent, we inherit all the | |
| 320 // extension class methods via prototype inheritance. | |
| 321 if(JS('bool', '#.__proto__._xxx !== #', jsProto, extParent)) { | |
|
Leaf
2016/05/16 18:03:07
Maybe use a symbol to be more robust? Or maybe ju
| |
| 322 _installProperties2(jsProto, extParent, coreObjProto); | |
| 323 } | |
| 299 } | 324 } |
| 300 copyTheseProperties(jsProto, extProto, getOwnPropertySymbols(extProto)); | 325 copyTheseProperties(jsProto, extProto, getOwnPropertySymbols(extProto)); |
| 301 } | 326 } |
| 302 /// | 327 /// |
| 303 /// Copy symbols from the prototype of the source to destination. | 328 /// Copy symbols from the prototype of the source to destination. |
| 304 /// These are the only properties safe to copy onto an existing public | 329 /// These are the only properties safe to copy onto an existing public |
| 305 /// JavaScript class. | 330 /// JavaScript class. |
| 306 /// | 331 /// |
| 307 registerExtension(jsType, dartExtType) => JS('', '''(() => { | 332 registerExtension(jsType, dartExtType) => JS('', '''(() => { |
| 308 // TODO(vsm): Not all registered js types are real. | 333 // TODO(vsm): Not all registered js types are real. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 387 })()'''); | 412 })()'''); |
| 388 | 413 |
| 389 /// Sets the element type of a list literal. | 414 /// Sets the element type of a list literal. |
| 390 list(obj, elementType) => | 415 list(obj, elementType) => |
| 391 JS('', '$setType($obj, ${getGenericClass(JSArray)}($elementType))'); | 416 JS('', '$setType($obj, ${getGenericClass(JSArray)}($elementType))'); |
| 392 | 417 |
| 393 setBaseClass(derived, base) => JS('', '''(() => { | 418 setBaseClass(derived, base) => JS('', '''(() => { |
| 394 // Link the extension to the type it's extending as a base class. | 419 // Link the extension to the type it's extending as a base class. |
| 395 $derived.prototype.__proto__ = $base.prototype; | 420 $derived.prototype.__proto__ = $base.prototype; |
| 396 })()'''); | 421 })()'''); |
| OLD | NEW |