| 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 MetadataEmitter extends CodeEmitterHelper { | 7 class MetadataEmitter extends CodeEmitterHelper { |
| 8 /// A list of JS expressions that represent metadata, parameter names and | 8 /// A list of JS expressions that represent metadata, parameter names and |
| 9 /// type, and return types. | 9 /// type, and return types. |
| 10 final List<String> globalMetadata = []; | 10 final List<String> globalMetadata = []; |
| 11 | 11 |
| 12 /// A map used to canonicalize the entries of globalMetadata. | 12 /// A map used to canonicalize the entries of globalMetadata. |
| 13 final Map<String, int> globalMetadataMap = <String, int>{}; | 13 final Map<String, int> globalMetadataMap = <String, int>{}; |
| 14 | 14 |
| 15 /// The metadata function returns the metadata associated with | 15 /// The metadata function returns the metadata associated with |
| 16 /// [element] in generated code. The metadata needs to be wrapped | 16 /// [element] in generated code. The metadata needs to be wrapped |
| 17 /// in a function as it refers to constants that may not have been | 17 /// in a function as it refers to constants that may not have been |
| 18 /// constructed yet. For example, a class is allowed to be | 18 /// constructed yet. For example, a class is allowed to be |
| 19 /// annotated with itself. The metadata function is used by | 19 /// annotated with itself. The metadata function is used by |
| 20 /// mirrors_patch to implement DeclarationMirror.metadata. | 20 /// mirrors_patch to implement DeclarationMirror.metadata. |
| 21 jsAst.Fun buildMetadataFunction(Element element) { | 21 jsAst.Fun buildMetadataFunction(Element element) { |
| 22 if (!backend.retainMetadataOf(element)) return null; | 22 if (!backend.retainMetadataOf(element)) return null; |
| 23 return compiler.withCurrentElement(element, () { | 23 return compiler.withCurrentElement(element, () { |
| 24 var metadata = []; | 24 var metadata = []; |
| 25 Link link = element.metadata; | 25 Link link = element.metadata; |
| 26 // TODO(ahe): Why is metadata sometimes null? | 26 // TODO(ahe): Why is metadata sometimes null? |
| 27 if (link != null) { | 27 if (link != null) { |
| 28 for (; !link.isEmpty; link = link.tail) { | 28 for (; !link.isEmpty; link = link.tail) { |
| 29 MetadataAnnotation annotation = link.head; | 29 MetadataAnnotation annotation = link.head; |
| 30 Constant value = annotation.value; | 30 Constant value = |
| 31 backend.constants.getConstantForMetadata(annotation); |
| 31 if (value == null) { | 32 if (value == null) { |
| 32 compiler.internalError(annotation, 'Annotation value is null.'); | 33 compiler.internalError(annotation, 'Annotation value is null.'); |
| 33 } else { | 34 } else { |
| 34 metadata.add(task.constantReference(value)); | 35 metadata.add(task.constantReference(value)); |
| 35 } | 36 } |
| 36 } | 37 } |
| 37 } | 38 } |
| 38 if (metadata.isEmpty) return null; | 39 if (metadata.isEmpty) return null; |
| 39 return js.fun( | 40 return js.fun( |
| 40 [], [js.return_(new jsAst.ArrayInitializer.from(metadata))]); | 41 [], [js.return_(new jsAst.ArrayInitializer.from(metadata))]); |
| 41 }); | 42 }); |
| 42 } | 43 } |
| 43 | 44 |
| 44 List<int> reifyDefaultArguments(FunctionElement function) { | 45 List<int> reifyDefaultArguments(FunctionElement function) { |
| 45 FunctionSignature signature = function.functionSignature; | 46 FunctionSignature signature = function.functionSignature; |
| 46 if (signature.optionalParameterCount == 0) return const []; | 47 if (signature.optionalParameterCount == 0) return const []; |
| 47 List<int> defaultValues = <int>[]; | 48 List<int> defaultValues = <int>[]; |
| 48 for (Element element in signature.optionalParameters) { | 49 for (Element element in signature.optionalParameters) { |
| 49 Constant value = | 50 Constant value = backend.constants.getConstantForVariable(element); |
| 50 compiler.constantHandler.initialVariableValues[element]; | |
| 51 String stringRepresentation = (value == null) | 51 String stringRepresentation = (value == null) |
| 52 ? "null" | 52 ? "null" |
| 53 : jsAst.prettyPrint(task.constantReference(value), compiler) | 53 : jsAst.prettyPrint(task.constantReference(value), compiler) |
| 54 .getText(); | 54 .getText(); |
| 55 defaultValues.add(addGlobalMetadata(stringRepresentation)); | 55 defaultValues.add(addGlobalMetadata(stringRepresentation)); |
| 56 } | 56 } |
| 57 return defaultValues; | 57 return defaultValues; |
| 58 } | 58 } |
| 59 | 59 |
| 60 int reifyMetadata(MetadataAnnotation annotation) { | 60 int reifyMetadata(MetadataAnnotation annotation) { |
| 61 Constant value = annotation.value; | 61 Constant value = backend.constants.getConstantForMetadata(annotation); |
| 62 if (value == null) { | 62 if (value == null) { |
| 63 compiler.internalError(annotation, 'Annotation value is null.'); | 63 compiler.internalError(annotation, 'Annotation value is null.'); |
| 64 return -1; | 64 return -1; |
| 65 } | 65 } |
| 66 return addGlobalMetadata( | 66 return addGlobalMetadata( |
| 67 jsAst.prettyPrint(task.constantReference(value), compiler).getText()); | 67 jsAst.prettyPrint(task.constantReference(value), compiler).getText()); |
| 68 } | 68 } |
| 69 | 69 |
| 70 int reifyType(DartType type) { | 70 int reifyType(DartType type) { |
| 71 jsAst.Expression representation = | 71 jsAst.Expression representation = |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 // TODO(ahe): Why is metadata sometimes null? | 124 // TODO(ahe): Why is metadata sometimes null? |
| 125 if (link != null) { | 125 if (link != null) { |
| 126 for (; !link.isEmpty; link = link.tail) { | 126 for (; !link.isEmpty; link = link.tail) { |
| 127 metadata.add(reifyMetadata(link.head)); | 127 metadata.add(reifyMetadata(link.head)); |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 return metadata; | 130 return metadata; |
| 131 }); | 131 }); |
| 132 } | 132 } |
| 133 } | 133 } |
| OLD | NEW |