Chromium Code Reviews| Index: frog/corejs.dart |
| diff --git a/frog/corejs.dart b/frog/corejs.dart |
| index 9df6b77b3939b993c9f658193a893c4593047e42..c7275843dbcf04da33958516585f3b5c7edeb3c6 100644 |
| --- a/frog/corejs.dart |
| +++ b/frog/corejs.dart |
| @@ -389,30 +389,53 @@ function $dynamicSetMetadata(inputTable) { |
| /** Snippet for `$typeNameOf`. */ |
| final String _TYPE_NAME_OF_FUNCTION = @""" |
| -$defProp(Object.prototype, '$typeNameOf', function() { |
| - var constructor = this.constructor; |
| - if (typeof(constructor) == 'function') { |
| - // The constructor isn't null or undefined at this point. Try |
| - // to grab hold of its name. |
| - var name = constructor.name; |
| - // If the name is a non-empty string, we use that as the type |
| - // name of this object. On Firefox, we often get 'Object' as |
| - // the constructor name even for more specialized objects so |
| - // we have to fall through to the toString() based implementation |
| - // below in that case. |
| - if (name && typeof(name) == 'string' && name != 'Object') return name; |
| +$defProp(Object.prototype, '$typeNameOf', (function() { |
| + function constructorNameWithFallback(obj) { |
| + var constructor = obj.constructor; |
| + if (typeof(constructor) == 'function') { |
| + // The constructor isn't null or undefined at this point. Try |
| + // to grab hold of its name. |
| + var name = constructor.name; |
| + // If the name is a non-empty string, we use that as the type |
| + // name of this object. On Firefox, we often get 'Object' as |
| + // the constructor name even for more specialized objects so |
| + // we have to fall through to the toString() based implementation |
| + // below in that case. |
| + if (typeof(name) == 'string' && name && name != 'Object') return name; |
| + } |
| + var string = Object.prototype.toString.call(obj); |
| + return string.substring(8, string.length - 1); |
| } |
| - var string = Object.prototype.toString.call(this); |
| - var name = string.substring(8, string.length - 1); |
| - if (name == 'Window') { |
| - name = 'DOMWindow'; |
| - } else if (name == 'Document') { |
| - name = 'HTMLDocument'; |
| - } else if (name == 'XMLDocument') { |
| - name = 'Document'; |
| + |
| + 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.
|
| + |
| + 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
|
| + var name = constructorNameWithFallback(this); |
| + if (name == 'Window') return 'DOMWindow'; |
| + if (name == 'Document') return 'HTMLDocument'; |
| + if (name == 'XMLDocument') return 'Document'; |
| + return name; |
| } |
| - return name; |
| -});"""; |
| + |
| + function ie_$typeNameOf() { |
| + var name = constructorNameWithFallback(this); |
| + if (name == 'Window') return 'DOMWindow'; |
| + // IE calls both HTML and XML documents 'Document', so we check for the |
| + // xmlVersion property, which is the empty string on HTML documents. |
| + if (name == 'Document' && this.xmlVersion) return 'Document'; |
| + if (name == 'Document') return 'HTMLDocument'; |
| + return name; |
| + } |
| + |
| + // 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
|
| + if (typeof(navigator) != 'object') return chrome_$typeNameOf; |
| + |
| + var userAgent = navigator.userAgent; |
| + if (/Chrome/.test(userAgent)) return chrome_$typeNameOf; |
| + if (/Firefox/.test(userAgent)) return firefox_$typeNameOf; |
| + if (/MSIE/.test(userAgent)) return ie_$typeNameOf; |
| + return function() { return constructorNameWithFallback(this); }; |
| +})());"""; |
| /** Snippet for `$inherits`. */ |
| final String _INHERITS_FUNCTION = @""" |