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 (name && typeof(name) == 'string' && name != 'Object') return name; | |
sra1
2012/02/29 01:28:55
Nit: I'd put typeof first (assuming name && is pro
nweiz
2012/02/29 01:54:28
Done.
| |
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 // If we're not in the browser, we don't have to worry about any compatibility |
407 if (name == 'Window') { | 411 // headaches. |
408 name = 'DOMWindow'; | 412 if (typeof(navigator) != 'object') { |
sra1
2012/02/29 01:28:55
Nit: typeof is an operator and does not need paren
nweiz
2012/02/29 01:54:28
I believe the style is to use parens with typeof.
| |
409 } else if (name == 'Document') { | 413 return function() { return this.constructor.name; }; |
410 name = 'HTMLDocument'; | |
411 } else if (name == 'XMLDocument') { | |
412 name = 'Document'; | |
413 } | 414 } |
414 return name; | 415 |
415 });"""; | 416 var userAgent = navigator.userAgent; |
417 if (/Chrome/.test(userAgent)) { | |
418 return function() { return this.constructor.name; }; | |
sra1
2012/02/29 01:28:55
This function occurs twice.
Which makes me think a
nweiz
2012/02/29 01:54:28
Done.
| |
419 } else if (/Firefox/.test(userAgent)) { | |
420 return function() { | |
421 var name = constructorNameWithFallback(this); | |
422 if (name == 'Window') return 'DOMWindow'; | |
423 if (name == 'Document') return 'HTMLDocument'; | |
424 if (name == 'XMLDocument') return 'Document'; | |
425 return name; | |
426 }; | |
427 } else if (/MSIE/.test(userAgent)) { | |
428 return function() { | |
429 var name = constructorNameWithFallback(this); | |
430 if (name == 'Window') return 'DOMWindow'; | |
431 // IE calls both HTML and XML documents 'Document', so we check for the | |
432 // xmlVersion property, which is the empty string on HTML documents. | |
433 if (name == 'Document' && this.xmlVersion) return 'Document'; | |
434 if (name == 'Document') return 'HTMLDocument'; | |
435 return name; | |
436 }; | |
437 } else { | |
438 return function() { return constructorNameWithFallback(this); }; | |
439 } | |
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 |