OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // TODO(jimhug): Completeness - see tests/corelib | 5 // TODO(jimhug): Completeness - see tests/corelib |
6 | 6 |
7 /** Implements extends for dart classes on javascript prototypes. */ | 7 /** Implements extends for dart classes on javascript prototypes. */ |
8 function $inherits(child, parent) { | 8 function $inherits(child, parent) { |
9 if (child.prototype.__proto__) { | 9 if (child.prototype.__proto__) { |
10 child.prototype.__proto__ = parent.prototype; | 10 child.prototype.__proto__ = parent.prototype; |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 } | 322 } |
323 | 323 |
324 function $varMethod(name, methods) { | 324 function $varMethod(name, methods) { |
325 Object.prototype[name] = function() { | 325 Object.prototype[name] = function() { |
326 $patchMethod(this, name, methods); | 326 $patchMethod(this, name, methods); |
327 this[name].apply(this, Array.prototype.slice.call(arguments)); | 327 this[name].apply(this, Array.prototype.slice.call(arguments)); |
328 }; | 328 }; |
329 } | 329 } |
330 | 330 |
331 Object.prototype.get$typeName = function() { | 331 Object.prototype.get$typeName = function() { |
332 // TODO(vsm): how can we make this go down the fast path for Chrome? | 332 // TODO(sigmund): find a way to make this work on all browsers, including |
333 //(for Chrome: return this.constructor.name;) | 333 // checking the typeName on prototype objects (so we can fix dynamic |
| 334 // dispatching on $varMethod). |
| 335 if (window.constructor.name == 'DOMWindow') { // fast-path for Chrome |
| 336 return this.constructor.name; |
| 337 } |
334 var str = Object.prototype.toString.call(this); | 338 var str = Object.prototype.toString.call(this); |
335 return str.substring(8, str.length - 1); | 339 return str.substring(8, str.length - 1); |
336 } | 340 } |
337 | 341 |
338 function $patchMethod(obj, name, methods) { | 342 function $patchMethod(obj, name, methods) { |
339 // Get the prototype to patch. | 343 // Get the prototype to patch. |
340 // Don't overwrite an existing stub, like the one on Object.prototype | 344 // Don't overwrite an existing stub, like the one on Object.prototype |
341 var proto = Object.getPrototypeOf(obj); | 345 var proto = Object.getPrototypeOf(obj); |
342 if (!proto || proto.hasOwnProperty(name)) proto = obj; | 346 if (!proto || proto.hasOwnProperty(name)) proto = obj; |
343 var method; | 347 var method; |
344 while (obj && !(method = methods[obj.get$typeName()])) { | 348 while (obj && !(method = methods[obj.get$typeName()])) { |
345 obj = Object.getPrototypeOf(obj); | 349 obj = Object.getPrototypeOf(obj); |
346 } | 350 } |
347 Object.defineProperty(proto, name, {value: method || methods['Object']}); | 351 obj[name] = method || methods['Object']; |
348 } | 352 } |
OLD | NEW |