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() { return this.constructor.name; } |
kasperl
2012/02/29 05:59:58
I would put a newline after { for consistency.
nweiz
2012/02/29 19:41:45
Done.
| |
407 if (name == 'Window') { | 411 |
408 name = 'DOMWindow'; | 412 function firefox_$typeNameOf() { |
kasperl
2012/02/29 05:59:58
Why is there an underscore after firefox, chrome,
nweiz
2012/02/29 19:41:45
I was thinking of "$typeNameOf" as an atomic ident
| |
409 } else if (name == 'Document') { | 413 var name = constructorNameWithFallback(this); |
410 name = 'HTMLDocument'; | 414 if (name == 'Window') return 'DOMWindow'; |
411 } else if (name == 'XMLDocument') { | 415 if (name == 'Document') return 'HTMLDocument'; |
412 name = 'Document'; | 416 if (name == 'XMLDocument') return 'Document'; |
417 return name; | |
413 } | 418 } |
414 return name; | 419 |
415 });"""; | 420 function ie_$typeNameOf() { |
421 var name = constructorNameWithFallback(this); | |
422 if (name == 'Window') return 'DOMWindow'; | |
423 // IE calls both HTML and XML documents 'Document', so we check for the | |
424 // xmlVersion property, which is the empty string on HTML documents. | |
425 if (name == 'Document' && this.xmlVersion) return 'Document'; | |
426 if (name == 'Document') return 'HTMLDocument'; | |
427 return name; | |
428 } | |
429 | |
430 // If we're not in the browser, we're almost certainly running on v8. | |
kasperl
2012/02/29 05:59:58
Long term, it would be great to factor this platfo
nweiz
2012/02/29 19:41:45
The best practice in most instances where you want
| |
431 if (typeof(navigator) != 'object') return chrome_$typeNameOf; | |
432 | |
433 var userAgent = navigator.userAgent; | |
434 if (/Chrome/.test(userAgent)) return chrome_$typeNameOf; | |
435 if (/Firefox/.test(userAgent)) return firefox_$typeNameOf; | |
436 if (/MSIE/.test(userAgent)) return ie_$typeNameOf; | |
437 return function() { return constructorNameWithFallback(this); }; | |
438 })());"""; | |
416 | 439 |
417 /** Snippet for `$inherits`. */ | 440 /** Snippet for `$inherits`. */ |
418 final String _INHERITS_FUNCTION = @""" | 441 final String _INHERITS_FUNCTION = @""" |
419 /** Implements extends for Dart classes on JavaScript prototypes. */ | 442 /** Implements extends for Dart classes on JavaScript prototypes. */ |
420 function $inherits(child, parent) { | 443 function $inherits(child, parent) { |
421 if (child.prototype.__proto__) { | 444 if (child.prototype.__proto__) { |
422 child.prototype.__proto__ = parent.prototype; | 445 child.prototype.__proto__ = parent.prototype; |
423 } else { | 446 } else { |
424 function tmp() {}; | 447 function tmp() {}; |
425 tmp.prototype = parent.prototype; | 448 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)); | 580 bound.$length = Math.max(0, funcLength - (argsLength - 1)); |
558 return bound; | 581 return bound; |
559 } else { | 582 } else { |
560 var bound = function() { | 583 var bound = function() { |
561 return func.apply(thisObj, arguments); | 584 return func.apply(thisObj, arguments); |
562 }; | 585 }; |
563 bound.$length = funcLength; | 586 bound.$length = funcLength; |
564 return bound; | 587 return bound; |
565 } | 588 } |
566 };"""; | 589 };"""; |
OLD | NEW |