OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library dart2js.new_js_emitter.emitter; | 5 library dart2js.new_js_emitter.emitter; |
6 | 6 |
7 import 'model.dart'; | 7 import 'model.dart'; |
8 import 'model_emitter.dart'; | 8 import 'model_emitter.dart'; |
9 import '../common.dart'; | 9 import '../common.dart'; |
10 import '../js/js.dart' as js; | 10 import '../js/js.dart' as js; |
11 | 11 |
12 import '../constants/values.dart' show PrimitiveConstant; | 12 import '../constants/values.dart' show PrimitiveConstantValue; |
13 import '../tree/tree.dart' show DartString; | 13 import '../tree/tree.dart' show DartString; |
14 | 14 |
15 import '../js_backend/js_backend.dart' show Namer, JavaScriptBackend; | 15 import '../js_backend/js_backend.dart' show Namer, JavaScriptBackend; |
16 import '../js_emitter/js_emitter.dart' as emitterTask show | 16 import '../js_emitter/js_emitter.dart' as emitterTask show |
17 CodeEmitterTask, | 17 CodeEmitterTask, |
18 Emitter; | 18 Emitter; |
19 | 19 |
20 import '../universe/universe.dart' show Universe; | 20 import '../universe/universe.dart' show Universe; |
21 import '../deferred_load.dart' show DeferredLoadTask, OutputUnit; | 21 import '../deferred_load.dart' show DeferredLoadTask, OutputUnit; |
22 | 22 |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 193 |
194 StaticMethod _buildStaticMethodTearOff(FunctionElement element) { | 194 StaticMethod _buildStaticMethodTearOff(FunctionElement element) { |
195 String name = namer.getStaticClosureName(element); | 195 String name = namer.getStaticClosureName(element); |
196 String holder = namer.globalObjectFor(element); | 196 String holder = namer.globalObjectFor(element); |
197 // TODO(kasperl): This clearly doesn't work yet. | 197 // TODO(kasperl): This clearly doesn't work yet. |
198 js.Expression code = js.string("<<unimplemented>>"); | 198 js.Expression code = js.string("<<unimplemented>>"); |
199 return new StaticMethod(name, _registry.registerHolder(holder), code); | 199 return new StaticMethod(name, _registry.registerHolder(holder), code); |
200 } | 200 } |
201 | 201 |
202 // TODO(floitsch): copied from OldEmitter. Adjust or share. | 202 // TODO(floitsch): copied from OldEmitter. Adjust or share. |
203 bool isConstantInlinedOrAlreadyEmitted(Constant constant) { | 203 bool isConstantInlinedOrAlreadyEmitted(ConstantValue constant) { |
204 if (constant.isFunction) return true; // Already emitted. | 204 if (constant.isFunction) return true; // Already emitted. |
205 if (constant.isPrimitive) return true; // Inlined. | 205 if (constant.isPrimitive) return true; // Inlined. |
206 if (constant.isDummy) return true; // Inlined. | 206 if (constant.isDummy) return true; // Inlined. |
207 // The name is null when the constant is already a JS constant. | 207 // The name is null when the constant is already a JS constant. |
208 // TODO(floitsch): every constant should be registered, so that we can | 208 // TODO(floitsch): every constant should be registered, so that we can |
209 // share the ones that take up too much space (like some strings). | 209 // share the ones that take up too much space (like some strings). |
210 if (namer.constantName(constant) == null) return true; | 210 if (namer.constantName(constant) == null) return true; |
211 return false; | 211 return false; |
212 } | 212 } |
213 | 213 |
214 // TODO(floitsch): copied from OldEmitter. Adjust or share. | 214 // TODO(floitsch): copied from OldEmitter. Adjust or share. |
215 int compareConstants(Constant a, Constant b) { | 215 int compareConstants(ConstantValue a, ConstantValue b) { |
216 // Inlined constants don't affect the order and sometimes don't even have | 216 // Inlined constants don't affect the order and sometimes don't even have |
217 // names. | 217 // names. |
218 int cmp1 = isConstantInlinedOrAlreadyEmitted(a) ? 0 : 1; | 218 int cmp1 = isConstantInlinedOrAlreadyEmitted(a) ? 0 : 1; |
219 int cmp2 = isConstantInlinedOrAlreadyEmitted(b) ? 0 : 1; | 219 int cmp2 = isConstantInlinedOrAlreadyEmitted(b) ? 0 : 1; |
220 if (cmp1 + cmp2 < 2) return cmp1 - cmp2; | 220 if (cmp1 + cmp2 < 2) return cmp1 - cmp2; |
221 | 221 |
222 // Emit constant interceptors first. Constant interceptors for primitives | 222 // Emit constant interceptors first. Constant interceptors for primitives |
223 // might be used by code that builds other constants. See Issue 18173. | 223 // might be used by code that builds other constants. See Issue 18173. |
224 if (a.isInterceptor != b.isInterceptor) { | 224 if (a.isInterceptor != b.isInterceptor) { |
225 return a.isInterceptor ? -1 : 1; | 225 return a.isInterceptor ? -1 : 1; |
226 } | 226 } |
227 | 227 |
228 // Sorting by the long name clusters constants with the same constructor | 228 // Sorting by the long name clusters constants with the same constructor |
229 // which compresses a tiny bit better. | 229 // which compresses a tiny bit better. |
230 int r = namer.constantLongName(a).compareTo(namer.constantLongName(b)); | 230 int r = namer.constantLongName(a).compareTo(namer.constantLongName(b)); |
231 if (r != 0) return r; | 231 if (r != 0) return r; |
232 // Resolve collisions in the long name by using the constant name (i.e. JS | 232 // Resolve collisions in the long name by using the constant name (i.e. JS |
233 // name) which is unique. | 233 // name) which is unique. |
234 return namer.constantName(a).compareTo(namer.constantName(b)); | 234 return namer.constantName(a).compareTo(namer.constantName(b)); |
235 } | 235 } |
236 | 236 |
237 js.Expression generateEmbeddedGlobalAccess(String global) { | 237 js.Expression generateEmbeddedGlobalAccess(String global) { |
238 // TODO(floitsch): We should not use "init" for globals. | 238 // TODO(floitsch): We should not use "init" for globals. |
239 return js.string("init.$global"); | 239 return js.string("init.$global"); |
240 } | 240 } |
241 | 241 |
242 js.Expression constantReference(Constant value) { | 242 js.Expression constantReference(ConstantValue value) { |
243 if (!value.isPrimitive) return js.string("<<unimplemented>>"); | 243 if (!value.isPrimitive) return js.string("<<unimplemented>>"); |
244 PrimitiveConstant constant = value; | 244 PrimitiveConstantValue constant = value; |
245 if (constant.isBool) return new js.LiteralBool(constant.isTrue); | 245 if (constant.isBool) return new js.LiteralBool(constant.isTrue); |
246 if (constant.isString) { | 246 if (constant.isString) { |
247 DartString dartString = constant.value; | 247 DartString dartString = constant.primitiveValue; |
248 return js.string(dartString.slowToString()); | 248 return js.string(dartString.slowToString()); |
249 } | 249 } |
250 if (constant.isNum) return js.number(constant.value); | 250 if (constant.isNum) return js.number(constant.primitiveValue); |
251 if (constant.isNull) return new js.LiteralNull(); | 251 if (constant.isNull) return new js.LiteralNull(); |
252 return js.string("<<unimplemented>>"); | 252 return js.string("<<unimplemented>>"); |
253 } | 253 } |
254 | 254 |
255 void invalidateCaches() {} | 255 void invalidateCaches() {} |
256 } | 256 } |
OLD | NEW |