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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 writer.writeln(_DYNAMIC_FUNCTION); | 94 writer.writeln(_DYNAMIC_FUNCTION); |
95 } | 95 } |
96 | 96 |
97 void ensureTypeNameOf() { | 97 void ensureTypeNameOf() { |
98 if (_generatedTypeNameOf) return; | 98 if (_generatedTypeNameOf) return; |
99 _generatedTypeNameOf = true; | 99 _generatedTypeNameOf = true; |
100 writer.writeln(_TYPE_NAME_OF_FUNCTION); | 100 writer.writeln(_TYPE_NAME_OF_FUNCTION); |
101 } | 101 } |
102 | 102 |
103 /** Generates the $inherits function when it's first used. */ | 103 /** Generates the $inherits function when it's first used. */ |
104 ensureInheritsHelper() { | 104 void ensureInheritsHelper() { |
105 if (_generatedInherits) return; | 105 if (_generatedInherits) return; |
106 _generatedInherits = true; | 106 _generatedInherits = true; |
107 writer.writeln(_INHERITS_FUNCTION); | 107 writer.writeln(_INHERITS_FUNCTION); |
108 } | 108 } |
109 | 109 |
110 void generate(CodeWriter w) { | 110 void generate(CodeWriter w) { |
111 // Write any stuff we had queued up, then replace our writer with the one | 111 // Write any stuff we had queued up, then replace our writer with the one |
112 // in WorldGenerator so anything we discover that we need later on will be | 112 // in WorldGenerator so anything we discover that we need later on will be |
113 // generated on-demand. | 113 // generated on-demand. |
114 w.write(writer.text); | 114 w.write(writer.text); |
(...skipping 30 matching lines...) Expand all Loading... |
145 } | 145 } |
146 if (useWrap1) { | 146 if (useWrap1) { |
147 w.writeln(_EMPTY_WRAP_CALL1_FUNCTION); | 147 w.writeln(_EMPTY_WRAP_CALL1_FUNCTION); |
148 } | 148 } |
149 } | 149 } |
150 | 150 |
151 // Write operator helpers | 151 // Write operator helpers |
152 for (var opImpl in orderValuesByKeys(_usedOperators)) { | 152 for (var opImpl in orderValuesByKeys(_usedOperators)) { |
153 w.writeln(opImpl); | 153 w.writeln(opImpl); |
154 } | 154 } |
| 155 |
| 156 if (world.dom != null) { |
| 157 ensureTypeNameOf(); |
| 158 // TODO(jmesserly): we need to find a way to avoid conflicts with other |
| 159 // generated "typeName" fields. Ideally we wouldn't be patching 'Object' |
| 160 // here. |
| 161 w.writeln('Object.prototype.get\$typeName = ' + |
| 162 ' Object.prototype.\$typeNameOf;'); |
| 163 } |
155 } | 164 } |
156 } | 165 } |
157 | 166 |
158 | 167 |
159 /** Snippet for `$ne`. */ | 168 /** Snippet for `$ne`. */ |
160 final String _NE_FUNCTION = @""" | 169 final String _NE_FUNCTION = @""" |
161 function $ne(x, y) { | 170 function $ne(x, y) { |
162 if (x == null) return y != null; | 171 if (x == null) return y != null; |
163 return (typeof(x) == 'number' && typeof(y) == 'number') || | 172 return (typeof(x) == 'number' && typeof(y) == 'number') || |
164 (typeof(x) == 'boolean' && typeof(y) == 'boolean') || | 173 (typeof(x) == 'boolean' && typeof(y) == 'boolean') || |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 for (var j = 0; j < tagNames.length; j++) { | 300 for (var j = 0; j < tagNames.length; j++) { |
292 map[tagNames[j]] = true; | 301 map[tagNames[j]] = true; |
293 } | 302 } |
294 table.push({tag: tag, tags: tags, map: map}); | 303 table.push({tag: tag, tags: tags, map: map}); |
295 } | 304 } |
296 $dynamicMetadata = table; | 305 $dynamicMetadata = table; |
297 } | 306 } |
298 """; | 307 """; |
299 | 308 |
300 /** Snippet for `$typeNameOf`. */ | 309 /** Snippet for `$typeNameOf`. */ |
301 // TODO(sigmund): find a way to make this work on all browsers, including | |
302 // checking the typeName on prototype objects (so we can fix dynamic | |
303 // dispatching on $varMethod). | |
304 final String _TYPE_NAME_OF_FUNCTION = @""" | 310 final String _TYPE_NAME_OF_FUNCTION = @""" |
305 Object.prototype.$typeNameOf = function() { | 311 Object.prototype.$typeNameOf = function() { |
306 if ((typeof(window) != 'undefined' && window.constructor.name == 'DOMWindow') | 312 if ((typeof(window) != 'undefined' && window.constructor.name == 'DOMWindow') |
307 || typeof(process) != 'undefined') { // fast-path for Chrome and Node | 313 || typeof(process) != 'undefined') { // fast-path for Chrome and Node |
308 return this.constructor.name; | 314 return this.constructor.name; |
309 } | 315 } |
310 var str = Object.prototype.toString.call(this); | 316 var str = Object.prototype.toString.call(this); |
311 str = str.substring(8, str.length - 1); | 317 str = str.substring(8, str.length - 1); |
312 if (str == 'Window') { | 318 if (str == 'Window') { |
313 str = 'DOMWindow'; | 319 str = 'DOMWindow'; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 | 447 |
442 /** Snippet for `$wrap_call$1`, in case it was not necessary. */ | 448 /** Snippet for `$wrap_call$1`, in case it was not necessary. */ |
443 final String _EMPTY_WRAP_CALL1_FUNCTION = | 449 final String _EMPTY_WRAP_CALL1_FUNCTION = |
444 @"function $wrap_call$1(fn) { return fn; }"; | 450 @"function $wrap_call$1(fn) { return fn; }"; |
445 | 451 |
446 /** Snippet that initializes the isolates state. */ | 452 /** Snippet that initializes the isolates state. */ |
447 final String _ISOLATE_INIT_CODE = @""" | 453 final String _ISOLATE_INIT_CODE = @""" |
448 var $globalThis = this; | 454 var $globalThis = this; |
449 var $globals = null; | 455 var $globals = null; |
450 var $globalState = null;"""; | 456 var $globalState = null;"""; |
OLD | NEW |