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 /** | 5 /** |
6 * Generates JS helpers for dart:core. This used to be in a file "core.js". | 6 * Generates JS helpers for dart:core. This used to be in a file "core.js". |
7 * Having them in Dart code means we can easily control which are generated. | 7 * Having them in Dart code means we can easily control which are generated. |
8 */ | 8 */ |
9 // TODO(jmesserly): one idea to make this cleaner: put these as private "native" | 9 // TODO(jmesserly): one idea to make this cleaner: put these as private "native" |
10 // methods somewhere in a library that we import. This would be rather elegant | 10 // methods somewhere in a library that we import. This would be rather elegant |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 map[tagNames[j]] = true; | 382 map[tagNames[j]] = true; |
383 } | 383 } |
384 table.push({tag: tag, tags: tags, map: map}); | 384 table.push({tag: tag, tags: tags, map: map}); |
385 } | 385 } |
386 $dynamicMetadata = table; | 386 $dynamicMetadata = table; |
387 } | 387 } |
388 """; | 388 """; |
389 | 389 |
390 /** Snippet for `$typeNameOf`. */ | 390 /** Snippet for `$typeNameOf`. */ |
391 final String _TYPE_NAME_OF_FUNCTION = @""" | 391 final String _TYPE_NAME_OF_FUNCTION = @""" |
392 $defProp(Object.prototype, '$typeNameOf', function() { | 392 $defProp(Object.prototype, '$typeNameOf', (function() { |
393 var constructor = this.constructor; | 393 function constructorNameWithFallback(obj) { |
394 if (typeof(constructor) == 'function') { | 394 var constructor = obj.constructor; |
395 // The constructor isn't null or undefined at this point. Try | 395 if (typeof(constructor) == 'function') { |
396 // to grab hold of its name. | 396 // The constructor isn't null or undefined at this point. Try |
397 var name = constructor.name; | 397 // to grab hold of its name. |
398 // If the name is a non-empty string, we use that as the type | 398 var name = constructor.name; |
399 // name of this object. On Firefox, we often get 'Object' as | 399 // If the name is a non-empty string, we use that as the type |
400 // the constructor name even for more specialized objects so | 400 // name of this object. On Firefox, we often get 'Object' as |
401 // we have to fall through to the toString() based implementation | 401 // the constructor name even for more specialized objects so |
402 // below in that case. | 402 // we have to fall through to the toString() based implementation |
403 if (name && typeof(name) == 'string' && name != 'Object') return name; | 403 // below in that case. |
| 404 if (typeof(name) == 'string' && name && name != 'Object') return name; |
| 405 } |
| 406 var string = Object.prototype.toString.call(obj); |
| 407 return string.substring(8, string.length - 1); |
404 } | 408 } |
405 var string = Object.prototype.toString.call(this); | 409 |
406 var name = string.substring(8, string.length - 1); | 410 function chrome$typeNameOf() { |
407 if (name == 'Window') { | 411 return this.constructor.name; |
408 name = 'DOMWindow'; | |
409 } else if (name == 'Document') { | |
410 name = 'HTMLDocument'; | |
411 } else if (name == 'XMLDocument') { | |
412 name = 'Document'; | |
413 } | 412 } |
414 return name; | 413 |
415 });"""; | 414 function firefox$typeNameOf() { |
| 415 var name = constructorNameWithFallback(this); |
| 416 if (name == 'Window') return 'DOMWindow'; |
| 417 if (name == 'Document') return 'HTMLDocument'; |
| 418 if (name == 'XMLDocument') return 'Document'; |
| 419 return name; |
| 420 } |
| 421 |
| 422 function ie$typeNameOf() { |
| 423 var name = constructorNameWithFallback(this); |
| 424 if (name == 'Window') return 'DOMWindow'; |
| 425 // IE calls both HTML and XML documents 'Document', so we check for the |
| 426 // xmlVersion property, which is the empty string on HTML documents. |
| 427 if (name == 'Document' && this.xmlVersion) return 'Document'; |
| 428 if (name == 'Document') return 'HTMLDocument'; |
| 429 return name; |
| 430 } |
| 431 |
| 432 // If we're not in the browser, we're almost certainly running on v8. |
| 433 if (typeof(navigator) != 'object') return chrome$typeNameOf; |
| 434 |
| 435 var userAgent = navigator.userAgent; |
| 436 if (/Chrome/.test(userAgent)) return chrome$typeNameOf; |
| 437 if (/Firefox/.test(userAgent)) return firefox$typeNameOf; |
| 438 if (/MSIE/.test(userAgent)) return ie$typeNameOf; |
| 439 return function() { return constructorNameWithFallback(this); }; |
| 440 })());"""; |
416 | 441 |
417 /** Snippet for `$inherits`. */ | 442 /** Snippet for `$inherits`. */ |
418 final String _INHERITS_FUNCTION = @""" | 443 final String _INHERITS_FUNCTION = @""" |
419 /** Implements extends for Dart classes on JavaScript prototypes. */ | 444 /** Implements extends for Dart classes on JavaScript prototypes. */ |
420 function $inherits(child, parent) { | 445 function $inherits(child, parent) { |
421 if (child.prototype.__proto__) { | 446 if (child.prototype.__proto__) { |
422 child.prototype.__proto__ = parent.prototype; | 447 child.prototype.__proto__ = parent.prototype; |
423 } else { | 448 } else { |
424 function tmp() {}; | 449 function tmp() {}; |
425 tmp.prototype = parent.prototype; | 450 tmp.prototype = parent.prototype; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 bound.$length = Math.max(0, funcLength - (argsLength - 1)); | 582 bound.$length = Math.max(0, funcLength - (argsLength - 1)); |
558 return bound; | 583 return bound; |
559 } else { | 584 } else { |
560 var bound = function() { | 585 var bound = function() { |
561 return func.apply(thisObj, arguments); | 586 return func.apply(thisObj, arguments); |
562 }; | 587 }; |
563 bound.$length = funcLength; | 588 bound.$length = funcLength; |
564 return bound; | 589 return bound; |
565 } | 590 } |
566 };"""; | 591 };"""; |
OLD | NEW |