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