Chromium Code Reviews| 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__;'; | |
|
kasperl
2012/10/18 09:36:21
Missing newline at end?
floitsch
2012/10/18 11:08:11
Done.
| |
| 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; | |
|
floitsch
2012/10/17 21:15:01
It is necessary to instantiate an object. Otherwis
kasperl
2012/10/18 09:36:21
Cool. Good to know.
| |
| 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 = {'': | |
| 65 function $mangledName(self, target) { this.self = self; this.target = target; }, | |
|
kasperl
2012/10/18 09:36:21
I'd prefer to stuff the 'function $mangledName(sel
floitsch
2012/10/18 11:08:11
Done.
| |
| 66 'super': '$superName', | |
| 67 """); | |
| 68 } | |
| 69 | |
| 70 void emitClassConstructor(ClassElement classElement, CodeBuffer buffer) { | |
| 71 // Say we have a class A with fields b, c and d, where c needs a getter and | |
| 72 // d needs both a getter and a setter. Then we produce: | |
| 73 // - a constructor (directly into the given [buffer]): | |
| 74 // function A(b, c, d) { this.b = b, this.c = c, this.d = d; } | |
| 75 // - getters and setters (stored in the [explicitGettersSetters] list): | |
| 76 // get$c : function() { return this.c; } | |
| 77 // get$d : function() { return this.d; } | |
| 78 // set$d : function(x) { this.d = x; } | |
| 79 List<String> fields = <String>[]; | |
| 80 visitClassFields(classElement, (Element member, | |
| 81 String name, | |
| 82 bool needsGetter, | |
| 83 bool needsSetter, | |
| 84 bool needsCheckedSetter) { | |
| 85 fields.add(name); | |
| 86 }); | |
| 87 | |
| 88 String constructorName = namer.safeName(classElement.name.slowToString()); | |
| 89 // Generate the constructor. | |
| 90 buffer.add("'': function $constructorName("); | |
| 91 buffer.add(Strings.join(fields, ", ")); | |
| 92 buffer.add(") {"); | |
|
kasperl
2012/10/18 09:36:21
Newline after {.
floitsch
2012/10/18 11:08:11
Done.
| |
| 93 for (String field in fields) { | |
| 94 buffer.add(" this.$field = $field;"); | |
|
kasperl
2012/10/18 09:36:21
Two space indent and newline after ;.
floitsch
2012/10/18 11:08:11
Done.
| |
| 95 } | |
| 96 buffer.add(' }'); | |
| 97 } | |
| 98 | |
| 99 void emitClassFields(ClassElement classElement, CodeBuffer buffer) { | |
| 100 /* Do nothing. */ | |
| 101 } | |
| 102 | |
| 103 void emitClassGettersSetters(ClassElement classElement, CodeBuffer buffer, | |
| 104 {bool omitLeadingComma: false}) { | |
| 105 emitComma() { | |
| 106 if (!omitLeadingComma) { | |
| 107 buffer.add(",\n "); | |
| 108 } else { | |
| 109 omitLeadingComma = false; | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 visitClassFields(classElement, (Element member, | |
| 114 String name, | |
| 115 bool needsGetter, | |
| 116 bool needsSetter, | |
| 117 bool needsCheckedSetter) { | |
| 118 if (needsGetter) { | |
| 119 emitComma(); | |
| 120 generateGetter(member, name, buffer); | |
| 121 } | |
| 122 if (needsSetter) { | |
| 123 emitComma(); | |
| 124 generateSetter(member, name, buffer); | |
| 125 } | |
| 126 if (needsCheckedSetter) { | |
| 127 assert(!needsSetter); | |
| 128 emitComma(); | |
| 129 generateCheckedSetter(member, name, buffer); | |
| 130 } | |
| 131 }); | |
| 132 } | |
| 133 } | |
| OLD | NEW |