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 /** The one true [World]. */ | 5 /** The one true [World]. */ |
6 World world; | 6 World world; |
7 | 7 |
8 /** | 8 /** |
9 * Experimental phase to enable await, only set when using the | 9 * Experimental phase to enable await, only set when using the |
10 * await/awaitc.dart entrypoint. | 10 * await/awaitc.dart entrypoint. |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 /** Represents a Dart "world" of code. */ | 61 /** Represents a Dart "world" of code. */ |
62 class World { | 62 class World { |
63 WorldGenerator gen; | 63 WorldGenerator gen; |
64 String legCode; // TODO(kasperl): Remove this temporary kludge. | 64 String legCode; // TODO(kasperl): Remove this temporary kludge. |
65 | 65 |
66 FileSystem files; | 66 FileSystem files; |
67 final LibraryReader reader; | 67 final LibraryReader reader; |
68 | 68 |
69 Map<String, Library> libraries; | 69 Map<String, Library> libraries; |
70 Library corelib; | 70 Library corelib; |
| 71 Library coreimpl; |
71 | 72 |
72 Library get coreimpl() => libraries['dart:coreimpl']; | 73 // TODO(jmesserly): we shouldn't be special casing DOM anywhere. |
73 Library get dom() => libraries['dart:dom']; | 74 Library dom; |
74 | 75 |
75 List<Library> _todo; | 76 List<Library> _todo; |
76 | 77 |
77 /** Internal map to track name conflicts in the generated javascript. */ | 78 /** Internal map to track name conflicts in the generated javascript. */ |
78 Map<String, Element> _topNames; | 79 Map<String, Element> _topNames; |
79 | 80 |
80 Map<String, MemberSet> _members; | 81 Map<String, MemberSet> _members; |
81 | 82 |
82 MessageHandler messageHandler; | 83 MessageHandler messageHandler; |
83 | 84 |
84 int errors = 0, warnings = 0; | 85 int errors = 0, warnings = 0; |
85 int dartBytesRead = 0, jsBytesWritten = 0; | 86 int dartBytesRead = 0, jsBytesWritten = 0; |
86 bool seenFatal = false; | 87 bool seenFatal = false; |
87 | 88 |
88 // Special types to Dart. | 89 // Special types to Dart. |
89 DefinedType varType; | 90 DefinedType varType; |
90 // TODO(jimhug): Is this ever not === varType? | 91 // TODO(jimhug): Is this ever not === varType? |
91 DefinedType dynamicType; | 92 DefinedType dynamicType; |
92 | 93 |
93 DefinedType voidType; | 94 DefinedType voidType; |
94 | |
95 DefinedType objectType; | 95 DefinedType objectType; |
96 DefinedType numType; | 96 DefinedType numType; |
97 DefinedType intType; | 97 DefinedType intType; |
98 DefinedType doubleType; | 98 DefinedType doubleType; |
99 DefinedType boolType; | 99 DefinedType boolType; |
100 DefinedType stringType; | 100 DefinedType stringType; |
101 DefinedType listType; | 101 DefinedType listType; |
102 DefinedType mapType; | 102 DefinedType mapType; |
103 DefinedType functionType; | 103 DefinedType functionType; |
| 104 DefinedType typeErrorType; |
| 105 |
| 106 // Types from dart:coreimpl that the compiler needs |
| 107 DefinedType numImplType; |
| 108 DefinedType stringImplType; |
| 109 DefinedType functionImplType; |
| 110 DefinedType listFactoryType; |
| 111 DefinedType immutableListType; |
104 | 112 |
105 NonNullableType nonNullBool; | 113 NonNullableType nonNullBool; |
106 | 114 |
107 World(this.files) | 115 World(this.files) |
108 : libraries = {}, _todo = [], _members = {}, _topNames = {}, | 116 : libraries = {}, _todo = [], _members = {}, _topNames = {}, |
109 // TODO(jmesserly): these two types don't actually back our Date and | 117 // TODO(jmesserly): these two types don't actually back our Date and |
110 // RegExp yet, so we need to add them manually. | 118 // RegExp yet, so we need to add them manually. |
111 reader = new LibraryReader() { | 119 reader = new LibraryReader() { |
112 } | 120 } |
113 | 121 |
114 void reset() { | 122 void reset() { |
115 // TODO(jimhug): Use a smaller hammer in the future. | 123 // TODO(jimhug): Use a smaller hammer in the future. |
116 libraries = {}; _todo = []; _members = {}; _topNames = {}; | 124 libraries = {}; _todo = []; _members = {}; _topNames = {}; |
117 errors = warnings = 0; | 125 errors = warnings = 0; |
118 seenFatal = false; | 126 seenFatal = false; |
119 init(); | 127 init(); |
120 } | 128 } |
121 | 129 |
122 init() { | 130 init() { |
123 // Setup well-known libraries and types. | 131 // Setup well-known libraries and types. |
124 corelib = new Library(readFile('dart:core')); | 132 corelib = new Library(readFile('dart:core')); |
125 libraries['dart:core'] = corelib; | 133 libraries['dart:core'] = corelib; |
126 _todo.add(corelib); | 134 _todo.add(corelib); |
127 | 135 |
128 voidType = _addToCoreLib('void', false); | 136 coreimpl = getOrAddLibrary('dart:coreimpl'); |
129 dynamicType = _addToCoreLib('Dynamic', false); | 137 |
| 138 voidType = corelib.addType('void', null, false); |
| 139 dynamicType = corelib.addType('Dynamic', null, false); |
130 varType = dynamicType; | 140 varType = dynamicType; |
131 | 141 |
132 objectType = _addToCoreLib('Object', true); | 142 objectType = corelib.addType('Object', null, true); |
133 numType = _addToCoreLib('num', false); | 143 numType = corelib.addType('num', null, false); |
134 intType = _addToCoreLib('int', false); | 144 intType = corelib.addType('int', null, false); |
135 doubleType = _addToCoreLib('double', false); | 145 doubleType = corelib.addType('double', null, false); |
136 boolType = _addToCoreLib('bool', false); | 146 boolType = corelib.addType('bool', null, false); |
137 stringType = _addToCoreLib('String', false); | 147 stringType = corelib.addType('String', null, false); |
138 listType = _addToCoreLib('List', false); | 148 listType = corelib.addType('List', null, false); |
139 mapType = _addToCoreLib('Map', false); | 149 mapType = corelib.addType('Map', null, false); |
140 functionType = _addToCoreLib('Function', false); | 150 functionType = corelib.addType('Function', null, false); |
| 151 typeErrorType = corelib.addType('TypeError', null, false); |
| 152 |
| 153 numImplType = coreimpl.addType('NumImplementation', null, true); |
| 154 stringImplType = coreimpl.addType('StringImplementation', null, true); |
| 155 immutableListType = coreimpl.addType('ImmutableList', null, true); |
| 156 listFactoryType = coreimpl.addType('ListFactory', null, true); |
| 157 functionImplType = coreimpl.addType('_FunctionImplementation', null, true); |
141 | 158 |
142 nonNullBool = new NonNullableType(boolType); | 159 nonNullBool = new NonNullableType(boolType); |
143 } | 160 } |
144 | 161 |
145 _addMember(Member member) { | 162 _addMember(Member member) { |
146 // Private members are only visible in their own library. | 163 // Private members are only visible in their own library. |
147 assert(!member.isPrivate); | 164 assert(!member.isPrivate); |
148 if (member.isStatic) { | 165 if (member.isStatic) { |
149 if (member.declaringType.isTop) { | 166 if (member.declaringType.isTop) { |
150 _addTopName(member); | 167 _addTopName(member); |
151 } | 168 } |
152 return; | 169 return; |
153 } | 170 } |
154 | 171 |
155 var mset = _members[member.name]; | 172 var mset = _members[member.name]; |
156 if (mset == null) { | 173 if (mset == null) { |
157 mset = new MemberSet(member, isVar:true); | 174 mset = new MemberSet(member, isVar:true); |
158 _members[mset.name] = mset; | 175 _members[mset.name] = mset; |
159 } else { | 176 } else { |
160 mset.members.add(member); | 177 mset.members.add(member); |
161 } | 178 } |
162 } | 179 } |
163 | 180 |
164 /** | 181 /** |
165 * Adds a top level named [Element] so we track which names will be used in | 182 * Adds a top level named [Element] so we track which names will be used in |
166 * the generated JS. | 183 * the generated JS. |
167 */ | 184 */ |
168 _addTopName(Element named) { | 185 _addTopName(Element named) { |
169 if (named.nativeName != null) { | 186 if (named is Type && named.isNative) { |
170 // Reserve the native name if we have one. This ensures no other type | 187 // Reserve the native name if we have one. This ensures no other type |
171 // will take our native name. | 188 // will take our native name. |
172 _addJavascriptTopName(named, named.nativeName); | 189 _addJavascriptTopName(named, named.nativeName); |
173 } | 190 } |
174 _addJavascriptTopName(named, named.jsname); | 191 _addJavascriptTopName(named, named.jsname); |
175 } | 192 } |
176 | 193 |
177 /** | 194 /** |
178 * Reserves [name] in the generated JS, or renames the lower priority | 195 * Reserves [name] in the generated JS, or renames the lower priority |
179 * [Element] if the name we wanted was already taken. | 196 * [Element] if the name we wanted was already taken. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 } | 265 } |
249 _topNames[named.jsname] = named; | 266 _topNames[named.jsname] = named; |
250 } | 267 } |
251 | 268 |
252 _addType(Type type) { | 269 _addType(Type type) { |
253 // Top types don't have a name - we will capture their members in | 270 // Top types don't have a name - we will capture their members in |
254 // [_addMember]. | 271 // [_addMember]. |
255 if (!type.isTop) _addTopName(type); | 272 if (!type.isTop) _addTopName(type); |
256 } | 273 } |
257 | 274 |
258 _addToCoreLib(String name, bool isClass) { | |
259 var ret = new DefinedType(name, corelib, null, isClass); | |
260 corelib.types[name] = ret; | |
261 return ret; | |
262 } | |
263 | |
264 // TODO(jimhug): Can this just be a const Set? | 275 // TODO(jimhug): Can this just be a const Set? |
265 Set<String> _jsKeywords; | 276 Set<String> _jsKeywords; |
266 | 277 |
267 /** Ensures that identifiers are legal in the generated JS. */ | 278 /** Ensures that identifiers are legal in the generated JS. */ |
268 String toJsIdentifier(String name) { | 279 String toJsIdentifier(String name) { |
269 if (name == null) return null; | 280 if (name == null) return null; |
270 if (_jsKeywords == null) { | 281 if (_jsKeywords == null) { |
271 // TODO(jmesserly): this doesn't work if I write "new Set<String>.from" | 282 // TODO(jmesserly): this doesn't work if I write "new Set<String>.from" |
272 // List of JS reserved words. | 283 // List of JS reserved words. |
273 _jsKeywords = new Set.from([ | 284 _jsKeywords = new Set.from([ |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 | 394 |
384 if (library == null) { | 395 if (library == null) { |
385 library = new Library(readFile(filename)); | 396 library = new Library(readFile(filename)); |
386 info('read library ${filename}'); | 397 info('read library ${filename}'); |
387 if (!library.isCore && | 398 if (!library.isCore && |
388 !library.imports.some((li) => li.library.isCore)) { | 399 !library.imports.some((li) => li.library.isCore)) { |
389 library.imports.add(new LibraryImport(corelib)); | 400 library.imports.add(new LibraryImport(corelib)); |
390 } | 401 } |
391 libraries[filename] = library; | 402 libraries[filename] = library; |
392 _todo.add(library); | 403 _todo.add(library); |
| 404 |
| 405 if (filename == 'dart:dom') { |
| 406 dom = library; |
| 407 } |
393 } | 408 } |
394 return library; | 409 return library; |
395 } | 410 } |
396 | 411 |
397 process() { | 412 process() { |
398 while (_todo.length > 0) { | 413 while (_todo.length > 0) { |
399 final todo = _todo; | 414 final todo = _todo; |
400 _todo = []; | 415 _todo = []; |
401 for (var lib in todo) { | 416 for (var lib in todo) { |
402 lib.visitSources(); | 417 lib.visitSources(); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 | 568 |
554 withTiming(String name, f()) { | 569 withTiming(String name, f()) { |
555 final sw = new Stopwatch(); | 570 final sw = new Stopwatch(); |
556 sw.start(); | 571 sw.start(); |
557 var result = f(); | 572 var result = f(); |
558 sw.stop(); | 573 sw.stop(); |
559 info('$name in ${sw.elapsedInMs()}msec'); | 574 info('$name in ${sw.elapsedInMs()}msec'); |
560 return result; | 575 return result; |
561 } | 576 } |
562 } | 577 } |
OLD | NEW |