| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
| 6 | 6 |
| 7 class ClassEmitter extends CodeEmitterHelper { | 7 class ClassEmitter extends CodeEmitterHelper { |
| 8 | 8 |
| 9 ClassStubGenerator get _stubGenerator => | 9 ClassStubGenerator get _stubGenerator => |
| 10 new ClassStubGenerator(compiler, namer, backend); | 10 new ClassStubGenerator(compiler, namer, backend); |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * Documentation wanted -- johnniwinther | 13 * Documentation wanted -- johnniwinther |
| 14 */ | 14 */ |
| 15 void emitClass(Class cls, ClassBuilder enclosingBuilder) { | 15 void emitClass(Class cls, ClassBuilder enclosingBuilder, Fragment fragment) { |
| 16 ClassElement classElement = cls.element; | 16 ClassElement classElement = cls.element; |
| 17 | 17 |
| 18 assert(invariant(classElement, classElement.isDeclaration)); | 18 assert(invariant(classElement, classElement.isDeclaration)); |
| 19 | 19 |
| 20 emitter.needsClassSupport = true; | 20 emitter.needsClassSupport = true; |
| 21 | 21 |
| 22 ClassElement superclass = classElement.superclass; | 22 ClassElement superclass = classElement.superclass; |
| 23 String superName = ""; | 23 String superName = ""; |
| 24 if (superclass != null) { | 24 if (superclass != null) { |
| 25 superName = namer.className(superclass); | 25 superName = namer.className(superclass); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 45 emitNativeInfo(cls, builder); | 45 emitNativeInfo(cls, builder); |
| 46 | 46 |
| 47 if (classElement == backend.closureClass) { | 47 if (classElement == backend.closureClass) { |
| 48 // We add a special getter here to allow for tearing off a closure from | 48 // We add a special getter here to allow for tearing off a closure from |
| 49 // itself. | 49 // itself. |
| 50 jsAst.Fun function = js('function() { return this; }'); | 50 jsAst.Fun function = js('function() { return this; }'); |
| 51 String name = namer.getterForMember(Selector.CALL_NAME); | 51 String name = namer.getterForMember(Selector.CALL_NAME); |
| 52 builder.addProperty(name, function); | 52 builder.addProperty(name, function); |
| 53 } | 53 } |
| 54 | 54 |
| 55 emitClassBuilderWithReflectionData(cls, builder, enclosingBuilder); | 55 emitClassBuilderWithReflectionData(cls, builder, enclosingBuilder, |
| 56 fragment); |
| 56 } | 57 } |
| 57 /** | 58 /** |
| 58 * Emits the precompiled constructor when in CSP mode. | 59 * Emits the precompiled constructor when in CSP mode. |
| 59 */ | 60 */ |
| 60 void emitConstructorsForCSP(Class cls) { | 61 void emitConstructorsForCSP(Class cls) { |
| 61 List<String> fieldNames = <String>[]; | 62 List<String> fieldNames = <String>[]; |
| 62 | 63 |
| 63 if (!compiler.useContentSecurityPolicy) return; | 64 if (!compiler.useContentSecurityPolicy) return; |
| 64 | 65 |
| 65 if (!cls.onlyForRti && !cls.isNative) { | 66 if (!cls.onlyForRti && !cls.isNative) { |
| 66 fieldNames = cls.fields.map((Field field) => field.name).toList(); | 67 fieldNames = cls.fields.map((Field field) => field.name).toList(); |
| 67 } | 68 } |
| 68 | 69 |
| 69 ClassElement classElement = cls.element; | 70 ClassElement classElement = cls.element; |
| 70 | 71 |
| 71 jsAst.Expression constructorAst = | 72 jsAst.Expression constructorAst = |
| 72 _stubGenerator.generateClassConstructor(classElement, fieldNames); | 73 _stubGenerator.generateClassConstructor(classElement, fieldNames); |
| 73 | 74 |
| 74 String constructorName = namer.className(classElement); | 75 String constructorName = namer.className(classElement); |
| 75 OutputUnit outputUnit = | 76 OutputUnit outputUnit = |
| 76 compiler.deferredLoadTask.outputUnitForElement(classElement); | 77 compiler.deferredLoadTask.outputUnitForElement(classElement); |
| 77 emitter.emitPrecompiledConstructor( | 78 emitter.assemblePrecompiledConstructor( |
| 78 outputUnit, constructorName, constructorAst, fieldNames); | 79 outputUnit, constructorName, constructorAst, fieldNames); |
| 79 } | 80 } |
| 80 | 81 |
| 81 /// Returns `true` if fields added. | 82 /// Returns `true` if fields added. |
| 82 bool emitFields(FieldContainer container, | 83 bool emitFields(FieldContainer container, |
| 83 ClassBuilder builder, | 84 ClassBuilder builder, |
| 84 { bool classIsNative: false, | 85 { bool classIsNative: false, |
| 85 bool emitStatics: false }) { | 86 bool emitStatics: false }) { |
| 86 Iterable<Field> fields; | 87 Iterable<Field> fields; |
| 87 if (container is Class) { | 88 if (container is Class) { |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 } | 266 } |
| 266 | 267 |
| 267 void emitNativeInfo(Class cls, ClassBuilder builder) { | 268 void emitNativeInfo(Class cls, ClassBuilder builder) { |
| 268 if (cls.nativeInfo != null) { | 269 if (cls.nativeInfo != null) { |
| 269 builder.addProperty(namer.nativeSpecProperty, js.string(cls.nativeInfo)); | 270 builder.addProperty(namer.nativeSpecProperty, js.string(cls.nativeInfo)); |
| 270 } | 271 } |
| 271 } | 272 } |
| 272 | 273 |
| 273 void emitClassBuilderWithReflectionData(Class cls, | 274 void emitClassBuilderWithReflectionData(Class cls, |
| 274 ClassBuilder classBuilder, | 275 ClassBuilder classBuilder, |
| 275 ClassBuilder enclosingBuilder) { | 276 ClassBuilder enclosingBuilder, |
| 277 Fragment fragment) { |
| 276 ClassElement classElement = cls.element; | 278 ClassElement classElement = cls.element; |
| 277 String className = cls.name; | 279 String className = cls.name; |
| 278 | 280 |
| 279 var metadata = task.metadataCollector.buildMetadataFunction(classElement); | 281 var metadata = task.metadataCollector.buildMetadataFunction(classElement); |
| 280 if (metadata != null) { | 282 if (metadata != null) { |
| 281 classBuilder.addProperty("@", metadata); | 283 classBuilder.addProperty("@", metadata); |
| 282 } | 284 } |
| 283 | 285 |
| 284 if (backend.isAccessibleByReflection(classElement)) { | 286 if (backend.isAccessibleByReflection(classElement)) { |
| 285 List<DartType> typeVars = classElement.typeVariables; | 287 List<DartType> typeVars = classElement.typeVariables; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 300 if (emitFields(cls, staticsBuilder, emitStatics: true)) { | 302 if (emitFields(cls, staticsBuilder, emitStatics: true)) { |
| 301 jsAst.ObjectInitializer initializer = | 303 jsAst.ObjectInitializer initializer = |
| 302 staticsBuilder.toObjectInitializer(); | 304 staticsBuilder.toObjectInitializer(); |
| 303 compiler.dumpInfoTask.registerElementAst(classElement, | 305 compiler.dumpInfoTask.registerElementAst(classElement, |
| 304 initializer); | 306 initializer); |
| 305 jsAst.Node property = initializer.properties.single; | 307 jsAst.Node property = initializer.properties.single; |
| 306 compiler.dumpInfoTask.registerElementAst(classElement, property); | 308 compiler.dumpInfoTask.registerElementAst(classElement, property); |
| 307 statics.add(property); | 309 statics.add(property); |
| 308 } | 310 } |
| 309 | 311 |
| 312 // TODO(herhut): Do not grab statics out of the properties. |
| 310 ClassBuilder classProperties = | 313 ClassBuilder classProperties = |
| 311 emitter.elementDescriptors.remove(classElement); | 314 emitter.elementDescriptors[fragment].remove(classElement); |
| 312 if (classProperties != null) { | 315 if (classProperties != null) { |
| 313 statics.addAll(classProperties.properties); | 316 statics.addAll(classProperties.properties); |
| 314 } | 317 } |
| 315 | 318 |
| 316 if (!statics.isEmpty) { | 319 if (!statics.isEmpty) { |
| 317 classBuilder.addProperty('static', new jsAst.ObjectInitializer(statics)); | 320 classBuilder.addProperty('static', new jsAst.ObjectInitializer(statics)); |
| 318 } | 321 } |
| 319 | 322 |
| 320 // TODO(ahe): This method (generateClass) should return a jsAst.Expression. | 323 // TODO(ahe): This method (generateClass) should return a jsAst.Expression. |
| 321 jsAst.ObjectInitializer propertyValue = classBuilder.toObjectInitializer(); | 324 jsAst.ObjectInitializer propertyValue = classBuilder.toObjectInitializer(); |
| 322 compiler.dumpInfoTask.registerElementAst(classBuilder.element, propertyValue
); | 325 compiler.dumpInfoTask.registerElementAst(classBuilder.element, propertyValue
); |
| 323 enclosingBuilder.addProperty(className, propertyValue); | 326 enclosingBuilder.addProperty(className, propertyValue); |
| 324 | 327 |
| 325 String reflectionName = emitter.getReflectionName(classElement, className); | 328 String reflectionName = emitter.getReflectionName(classElement, className); |
| 326 if (reflectionName != null) { | 329 if (reflectionName != null) { |
| 327 if (!backend.isAccessibleByReflection(classElement)) { | 330 if (!backend.isAccessibleByReflection(classElement)) { |
| 328 enclosingBuilder.addProperty("+$reflectionName", js.number(0)); | 331 enclosingBuilder.addProperty("+$reflectionName", js.number(0)); |
| 329 } else { | 332 } else { |
| 330 List<int> types = <int>[]; | 333 List<int> types = <int>[]; |
| 331 if (classElement.supertype != null) { | 334 if (classElement.supertype != null) { |
| 332 types.add(task.metadataCollector.reifyType(classElement.supertype)); | 335 types.add(task.metadataCollector.reifyType(classElement.supertype)); |
| 333 } | 336 } |
| 334 for (DartType interface in classElement.interfaces) { | 337 for (DartType interface in classElement.interfaces) { |
| 335 types.add(task.metadataCollector.reifyType(interface)); | 338 types.add(task.metadataCollector.reifyType(interface)); |
| 336 } | 339 } |
| 337 enclosingBuilder.addProperty("+$reflectionName", | 340 enclosingBuilder.addProperty("+$reflectionName", js.numArray(types)); |
| 338 new jsAst.ArrayInitializer(types.map(js.number).toList())); | |
| 339 } | 341 } |
| 340 } | 342 } |
| 341 } | 343 } |
| 342 | 344 |
| 343 /** | 345 /** |
| 344 * Invokes [f] for each of the fields of [element]. | 346 * Invokes [f] for each of the fields of [element]. |
| 345 * | 347 * |
| 346 * [element] must be a [ClassElement] or a [LibraryElement]. | 348 * [element] must be a [ClassElement] or a [LibraryElement]. |
| 347 * | 349 * |
| 348 * If [element] is a [ClassElement], the static fields of the class are | 350 * If [element] is a [ClassElement], the static fields of the class are |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 ? new Selector.getter(member.name, member.library) | 546 ? new Selector.getter(member.name, member.library) |
| 545 : new Selector.setter(member.name, member.library); | 547 : new Selector.setter(member.name, member.library); |
| 546 String reflectionName = emitter.getReflectionName(selector, name); | 548 String reflectionName = emitter.getReflectionName(selector, name); |
| 547 if (reflectionName != null) { | 549 if (reflectionName != null) { |
| 548 var reflectable = | 550 var reflectable = |
| 549 js(backend.isAccessibleByReflection(member) ? '1' : '0'); | 551 js(backend.isAccessibleByReflection(member) ? '1' : '0'); |
| 550 builder.addProperty('+$reflectionName', reflectable); | 552 builder.addProperty('+$reflectionName', reflectable); |
| 551 } | 553 } |
| 552 } | 554 } |
| 553 } | 555 } |
| OLD | NEW |