| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 class CodeEmitterNoEvalTask extends CodeEmitterTask { |
| 6 CodeEmitterNoEvalTask(Compiler compiler, |
| 7 Namer namer, |
| 8 bool generateSourceMap) |
| 9 : super(compiler, namer, generateSourceMap); |
| 10 |
| 11 String get generateGetterSetterFunction { |
| 12 return """ |
| 13 function() { |
| 14 throw 'Internal Error: no dynamic generation of getters and setters allowed'; |
| 15 }"""; |
| 16 } |
| 17 |
| 18 String get defineClassFunction { |
| 19 return """ |
| 20 function(cls, constructor, prototype) { |
| 21 constructor.prototype = prototype; |
| 22 return constructor; |
| 23 }"""; |
| 24 } |
| 25 |
| 26 String get protoSupportCheck { |
| 27 // We don't modify the prototypes in CSP mode. Therefore we can have an |
| 28 // easier prototype-check. |
| 29 return 'var $supportsProtoName = !!{}.__proto__;\n'; |
| 30 } |
| 31 |
| 32 String get finishIsolateConstructorFunction { |
| 33 // We replace the old Isolate function with a new one that initializes |
| 34 // all its field with the initial (and often final) value of all globals. |
| 35 // |
| 36 // We also copy over old values like the prototype, and the |
| 37 // isolateProperties themselves. |
| 38 return """ |
| 39 function(oldIsolate) { |
| 40 var isolateProperties = oldIsolate.${namer.ISOLATE_PROPERTIES}; |
| 41 function Isolate() { |
| 42 for (var staticName in isolateProperties) { |
| 43 if (Object.prototype.hasOwnProperty.call(isolateProperties, staticName)) { |
| 44 this[staticName] = isolateProperties[staticName]; |
| 45 } |
| 46 } |
| 47 // Use the newly created object as prototype. In Chrome this creates a |
| 48 // hidden class for the object and makes sure it is fast to access. |
| 49 function ForceEfficientMap() {} |
| 50 ForceEfficientMap.prototype = this; |
| 51 new ForceEfficientMap; |
| 52 } |
| 53 Isolate.prototype = oldIsolate.prototype; |
| 54 Isolate.prototype.constructor = Isolate; |
| 55 Isolate.${namer.ISOLATE_PROPERTIES} = isolateProperties; |
| 56 return Isolate; |
| 57 }"""; |
| 58 } |
| 59 |
| 60 void emitBoundClosureClassHeader(String mangledName, |
| 61 String superName, |
| 62 CodeBuffer buffer) { |
| 63 buffer.add(""" |
| 64 $classesCollector.$mangledName = {'': function $mangledName(self, target) { |
| 65 this.self = self; |
| 66 this.target = target; |
| 67 }, |
| 68 'super': '$superName', |
| 69 """); |
| 70 } |
| 71 |
| 72 void emitClassConstructor(ClassElement classElement, CodeBuffer buffer) { |
| 73 // Say we have a class A with fields b, c and d, where c needs a getter and |
| 74 // d needs both a getter and a setter. Then we produce: |
| 75 // - a constructor (directly into the given [buffer]): |
| 76 // function A(b, c, d) { this.b = b, this.c = c, this.d = d; } |
| 77 // - getters and setters (stored in the [explicitGettersSetters] list): |
| 78 // get$c : function() { return this.c; } |
| 79 // get$d : function() { return this.d; } |
| 80 // set$d : function(x) { this.d = x; } |
| 81 List<String> fields = <String>[]; |
| 82 visitClassFields(classElement, (Element member, |
| 83 String name, |
| 84 bool needsGetter, |
| 85 bool needsSetter, |
| 86 bool needsCheckedSetter) { |
| 87 fields.add(name); |
| 88 }); |
| 89 |
| 90 List<String> argumentNames = fields; |
| 91 if (fields.length < ($z - $a)) { |
| 92 argumentNames = new List<String>(fields.length); |
| 93 for (int i = 0; i < fields.length; i++) { |
| 94 argumentNames[i] = new String.fromCharCodes([$a + i]); |
| 95 } |
| 96 } |
| 97 String constructorName = namer.safeName(classElement.name.slowToString()); |
| 98 // Generate the constructor. |
| 99 buffer.add("'': function $constructorName("); |
| 100 buffer.add(Strings.join(argumentNames, ", ")); |
| 101 buffer.add(") {\n"); |
| 102 for (int i = 0; i < fields.length; i++) { |
| 103 buffer.add(" this.${fields[i]} = ${argumentNames[i]};\n"); |
| 104 } |
| 105 buffer.add(' }'); |
| 106 } |
| 107 |
| 108 void emitClassFields(ClassElement classElement, CodeBuffer buffer) { |
| 109 /* Do nothing. */ |
| 110 } |
| 111 |
| 112 void emitClassGettersSetters(ClassElement classElement, CodeBuffer buffer, |
| 113 {bool omitLeadingComma: false}) { |
| 114 emitComma() { |
| 115 if (!omitLeadingComma) { |
| 116 buffer.add(",\n "); |
| 117 } else { |
| 118 omitLeadingComma = false; |
| 119 } |
| 120 } |
| 121 |
| 122 visitClassFields(classElement, (Element member, |
| 123 String name, |
| 124 bool needsGetter, |
| 125 bool needsSetter, |
| 126 bool needsCheckedSetter) { |
| 127 if (needsGetter) { |
| 128 emitComma(); |
| 129 generateGetter(member, name, buffer); |
| 130 } |
| 131 if (needsSetter) { |
| 132 emitComma(); |
| 133 generateSetter(member, name, buffer); |
| 134 } |
| 135 if (needsCheckedSetter) { |
| 136 assert(!needsSetter); |
| 137 emitComma(); |
| 138 generateCheckedSetter(member, name, buffer); |
| 139 } |
| 140 }); |
| 141 } |
| 142 } |
| OLD | NEW |