| 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 '../program_builder.dart' show ProgramBuilder; | 7 import '../program_builder.dart' show ProgramBuilder; |
| 8 import '../model.dart'; | 8 import '../model.dart'; |
| 9 import 'model_emitter.dart'; | 9 import 'model_emitter.dart'; |
| 10 import '../../common.dart'; | 10 import '../../common.dart'; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 @override | 34 @override |
| 35 int emitProgram(ProgramBuilder programBuilder) { | 35 int emitProgram(ProgramBuilder programBuilder) { |
| 36 Program program = programBuilder.buildProgram(); | 36 Program program = programBuilder.buildProgram(); |
| 37 return _emitter.emitProgram(program); | 37 return _emitter.emitProgram(program); |
| 38 } | 38 } |
| 39 | 39 |
| 40 // TODO(floitsch): copied from OldEmitter. Adjust or share. | 40 // TODO(floitsch): copied from OldEmitter. Adjust or share. |
| 41 @override | 41 @override |
| 42 bool isConstantInlinedOrAlreadyEmitted(ConstantValue constant) { | 42 bool isConstantInlinedOrAlreadyEmitted(ConstantValue constant) { |
| 43 if (constant.isFunction) return true; // Already emitted. | 43 return _emitter.isConstantInlinedOrAlreadyEmitted(constant); |
| 44 if (constant.isPrimitive) return true; // Inlined. | |
| 45 if (constant.isDummy) return true; // Inlined. | |
| 46 // The name is null when the constant is already a JS constant. | |
| 47 // TODO(floitsch): every constant should be registered, so that we can | |
| 48 // share the ones that take up too much space (like some strings). | |
| 49 if (namer.constantName(constant) == null) return true; | |
| 50 return false; | |
| 51 } | 44 } |
| 52 | 45 |
| 53 // TODO(floitsch): copied from OldEmitter. Adjust or share. | 46 // TODO(floitsch): copied from OldEmitter. Adjust or share. |
| 54 @override | 47 @override |
| 55 int compareConstants(ConstantValue a, ConstantValue b) { | 48 int compareConstants(ConstantValue a, ConstantValue b) { |
| 56 // Inlined constants don't affect the order and sometimes don't even have | 49 return _emitter.compareConstants(a, b); |
| 57 // names. | |
| 58 int cmp1 = isConstantInlinedOrAlreadyEmitted(a) ? 0 : 1; | |
| 59 int cmp2 = isConstantInlinedOrAlreadyEmitted(b) ? 0 : 1; | |
| 60 if (cmp1 + cmp2 < 2) return cmp1 - cmp2; | |
| 61 | |
| 62 // Emit constant interceptors first. Constant interceptors for primitives | |
| 63 // might be used by code that builds other constants. See Issue 18173. | |
| 64 if (a.isInterceptor != b.isInterceptor) { | |
| 65 return a.isInterceptor ? -1 : 1; | |
| 66 } | |
| 67 | |
| 68 // Sorting by the long name clusters constants with the same constructor | |
| 69 // which compresses a tiny bit better. | |
| 70 int r = namer.constantLongName(a).compareTo(namer.constantLongName(b)); | |
| 71 if (r != 0) return r; | |
| 72 // Resolve collisions in the long name by using the constant name (i.e. JS | |
| 73 // name) which is unique. | |
| 74 return namer.constantName(a).compareTo(namer.constantName(b)); | |
| 75 } | 50 } |
| 76 | 51 |
| 77 @override | 52 @override |
| 53 js.Expression constantReference(ConstantValue value) { |
| 54 return _emitter.generateConstantReference(value); |
| 55 } |
| 56 |
| 57 @override |
| 78 js.Expression generateEmbeddedGlobalAccess(String global) { | 58 js.Expression generateEmbeddedGlobalAccess(String global) { |
| 79 return _emitter.generateEmbeddedGlobalAccess(global); | 59 return _emitter.generateEmbeddedGlobalAccess(global); |
| 80 } | 60 } |
| 81 | 61 |
| 82 @override | 62 @override |
| 83 // TODO(herhut): Use a single shared function. | 63 // TODO(herhut): Use a single shared function. |
| 84 js.Expression generateFunctionThatReturnsNull() { | 64 js.Expression generateFunctionThatReturnsNull() { |
| 85 return js.js('function() {}'); | 65 return js.js('function() {}'); |
| 86 } | 66 } |
| 87 | 67 |
| 88 @override | |
| 89 js.Expression constantReference(ConstantValue value) { | |
| 90 return _emitter.constantEmitter.reference(value); | |
| 91 } | |
| 92 | |
| 93 js.PropertyAccess _globalPropertyAccess(Element element) { | 68 js.PropertyAccess _globalPropertyAccess(Element element) { |
| 94 String name = namer.getNameX(element); | 69 String name = namer.getNameX(element); |
| 95 js.PropertyAccess pa = new js.PropertyAccess.field( | 70 js.PropertyAccess pa = new js.PropertyAccess.field( |
| 96 new js.VariableUse(namer.globalObjectFor(element)), | 71 new js.VariableUse(namer.globalObjectFor(element)), |
| 97 name); | 72 name); |
| 98 return pa; | 73 return pa; |
| 99 } | 74 } |
| 100 | 75 |
| 101 @override | 76 @override |
| 102 js.Expression isolateLazyInitializerAccess(FieldElement element) { | 77 js.Expression isolateLazyInitializerAccess(FieldElement element) { |
| 103 return js.js('#.#', [namer.globalObjectFor(element), | 78 return js.js('#.#', [namer.globalObjectFor(element), |
| 104 namer.getLazyInitializerName(element)]); | 79 namer.getLazyInitializerName(element)]); |
| 105 } | 80 } |
| 106 | 81 |
| 107 @override | 82 @override |
| 108 js.Expression isolateStaticClosureAccess(FunctionElement element) { | 83 js.Expression isolateStaticClosureAccess(FunctionElement element) { |
| 109 return js.js('#.#()', | 84 return _emitter.generateStaticClosureAccess(element); |
| 110 [namer.globalObjectFor(element), namer.getStaticClosureName(element)]); | |
| 111 } | 85 } |
| 112 | 86 |
| 113 @override | 87 @override |
| 114 js.PropertyAccess staticFieldAccess(FieldElement element) { | 88 js.PropertyAccess staticFieldAccess(FieldElement element) { |
| 115 return _globalPropertyAccess(element); | 89 return _globalPropertyAccess(element); |
| 116 } | 90 } |
| 117 | 91 |
| 118 @override | 92 @override |
| 119 js.PropertyAccess staticFunctionAccess(FunctionElement element) { | 93 js.PropertyAccess staticFunctionAccess(FunctionElement element) { |
| 120 return _globalPropertyAccess(element); | 94 return _globalPropertyAccess(element); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 143 @override | 117 @override |
| 144 js.Expression typeAccess(Element element) { | 118 js.Expression typeAccess(Element element) { |
| 145 // TODO(floitsch): minify 'ensureResolved'. | 119 // TODO(floitsch): minify 'ensureResolved'. |
| 146 // TODO(floitsch): don't emit `ensureResolved` for eager classes. | 120 // TODO(floitsch): don't emit `ensureResolved` for eager classes. |
| 147 return js.js('#.ensureResolved()', _globalPropertyAccess(element)); | 121 return js.js('#.ensureResolved()', _globalPropertyAccess(element)); |
| 148 } | 122 } |
| 149 | 123 |
| 150 @override | 124 @override |
| 151 void invalidateCaches() {} | 125 void invalidateCaches() {} |
| 152 } | 126 } |
| OLD | NEW |