OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 class NativeEmitter { | 5 class NativeEmitter { |
6 | 6 |
7 Compiler compiler; | 7 Compiler compiler; |
8 bool addedDynamicFunction = false; | 8 bool addedDynamicFunction = false; |
9 bool addedTypeNameOfFunction = false; | 9 bool addedTypeNameOfFunction = false; |
10 bool addedDefPropFunction = false; | 10 bool addedDefPropFunction = false; |
(...skipping 10 matching lines...) Expand all Loading... |
21 Map<ClassElement, List<ClassElement>> subclasses; | 21 Map<ClassElement, List<ClassElement>> subclasses; |
22 | 22 |
23 NativeEmitter(this.compiler) | 23 NativeEmitter(this.compiler) |
24 : classesWithDynamicDispatch = new Set<ClassElement>(), | 24 : classesWithDynamicDispatch = new Set<ClassElement>(), |
25 nativeClasses = new Set<ClassElement>(), | 25 nativeClasses = new Set<ClassElement>(), |
26 subclasses = new Map<ClassElement, List<ClassElement>>(); | 26 subclasses = new Map<ClassElement, List<ClassElement>>(); |
27 | 27 |
28 /** | 28 /** |
29 * Code for finding the type name of a JavaScript object. | 29 * Code for finding the type name of a JavaScript object. |
30 */ | 30 */ |
31 static final String TYPE_NAME_OF_FUNCTION = ''' | 31 static final String TYPE_NAME_OF_FUNCTION = @""" |
32 function(obj) { | 32 (function() { |
33 var constructor = obj.constructor; | 33 function constructorNameWithFallback(obj) { |
34 if (typeof(constructor) == 'function') { | 34 var constructor = obj.constructor; |
35 // The constructor isn't null or undefined at this point. Try | 35 if (typeof(constructor) == 'function') { |
36 // to grab hold of its name. | 36 // The constructor isn't null or undefined at this point. Try |
37 var name = constructor.name; | 37 // to grab hold of its name. |
38 // If the name is a non-empty string, we use that as the type | 38 var name = constructor.name; |
39 // name of this object. On Firefox, we often get 'Object' as | 39 // If the name is a non-empty string, we use that as the type |
40 // the constructor name even for more specialized objects so | 40 // name of this object. On Firefox, we often get 'Object' as |
41 // we have to fall through to the toString() based implementation | 41 // the constructor name even for more specialized objects so |
42 // below in that case. | 42 // we have to fall through to the toString() based implementation |
43 if (name && typeof(name) == 'string' && name != 'Object') return name; | 43 // below in that case. |
| 44 if (typeof(name) == 'string' && name && name != 'Object') return name; |
| 45 } |
| 46 var string = Object.prototype.toString.call(obj); |
| 47 return string.substring(8, string.length - 1); |
44 } | 48 } |
45 var string = Object.prototype.toString.call(obj); | 49 |
46 var name = string.substring(8, string.length - 1); | 50 function chrome$typeNameOf() { |
47 if (name == 'Window') { | 51 var name = this.constructor.name; |
48 name = 'DOMWindow'; | 52 if (name == 'Window') return 'DOMWindow'; |
49 } else if (name == 'Document') { | 53 return name; |
50 name = 'HTMLDocument'; | |
51 } | 54 } |
52 return name; | 55 |
53 }'''; | 56 function firefox$typeNameOf() { |
| 57 var name = constructorNameWithFallback(this); |
| 58 if (name == 'Window') return 'DOMWindow'; |
| 59 if (name == 'Document') return 'HTMLDocument'; |
| 60 if (name == 'XMLDocument') return 'Document'; |
| 61 return name; |
| 62 } |
| 63 |
| 64 function ie$typeNameOf() { |
| 65 var name = constructorNameWithFallback(this); |
| 66 if (name == 'Window') return 'DOMWindow'; |
| 67 // IE calls both HTML and XML documents 'Document', so we check for the |
| 68 // xmlVersion property, which is the empty string on HTML documents. |
| 69 if (name == 'Document' && this.xmlVersion) return 'Document'; |
| 70 if (name == 'Document') return 'HTMLDocument'; |
| 71 return name; |
| 72 } |
| 73 |
| 74 // If we're not in the browser, we're almost certainly running on v8. |
| 75 if (typeof(navigator) != 'object') return chrome$typeNameOf; |
| 76 |
| 77 var userAgent = navigator.userAgent; |
| 78 if (/Chrome/.test(userAgent)) return chrome$typeNameOf; |
| 79 if (/Firefox/.test(userAgent)) return firefox$typeNameOf; |
| 80 if (/MSIE/.test(userAgent)) return ie$typeNameOf; |
| 81 return function() { return constructorNameWithFallback(this); }; |
| 82 })()"""; |
54 | 83 |
55 /** | 84 /** |
56 * Code for defining a property in a JavaScript object that will not | 85 * Code for defining a property in a JavaScript object that will not |
57 * be visible through for-in (aka enumerable is false). | 86 * be visible through for-in (aka enumerable is false). |
58 */ | 87 */ |
59 static final String DEF_PROP_FUNCTION = ''' | 88 static final String DEF_PROP_FUNCTION = ''' |
60 function(obj, prop, value) { | 89 function(obj, prop, value) { |
61 Object.defineProperty(obj, prop, | 90 Object.defineProperty(obj, prop, |
62 {value: value, enumerable: false, writable: true, configurable: true}); | 91 {value: value, enumerable: false, writable: true, configurable: true}); |
63 }'''; | 92 }'''; |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 entries.add("\n ['$clsName', ${tagDefns[cls]}]"); | 522 entries.add("\n ['$clsName', ${tagDefns[cls]}]"); |
494 } | 523 } |
495 buffer.add(Strings.join(entries, ',')); | 524 buffer.add(Strings.join(entries, ',')); |
496 buffer.add('];\n'); | 525 buffer.add('];\n'); |
497 buffer.add('$dynamicSetMetadataName(table);\n'); | 526 buffer.add('$dynamicSetMetadataName(table);\n'); |
498 | 527 |
499 buffer.add('})();\n'); | 528 buffer.add('})();\n'); |
500 } | 529 } |
501 } | 530 } |
502 } | 531 } |
OLD | NEW |