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 = []; |
(...skipping 24 matching lines...) Expand all Loading... |
35 metadata.add(task.constantReference(value)); | 35 metadata.add(task.constantReference(value)); |
36 } | 36 } |
37 } | 37 } |
38 } | 38 } |
39 if (metadata.isEmpty) return null; | 39 if (metadata.isEmpty) return null; |
40 return js.fun( | 40 return js.fun( |
41 [], [js.return_(new jsAst.ArrayInitializer.from(metadata))]); | 41 [], [js.return_(new jsAst.ArrayInitializer.from(metadata))]); |
42 }); | 42 }); |
43 } | 43 } |
44 | 44 |
45 jsAst.Node reifyDefaultArguments(FunctionElement function) { | 45 List<int> reifyDefaultArguments(FunctionElement function) { |
46 FunctionSignature signature = function.computeSignature(compiler); | 46 FunctionSignature signature = function.computeSignature(compiler); |
47 if (signature.optionalParameterCount == 0) return null; | 47 if (signature.optionalParameterCount == 0) return const []; |
48 List<int> defaultValues = <int>[]; | 48 List<int> defaultValues = <int>[]; |
49 for (Element element in signature.orderedOptionalParameters) { | 49 for (Element element in signature.orderedOptionalParameters) { |
50 Constant value = | 50 Constant value = |
51 compiler.constantHandler.initialVariableValues[element]; | 51 compiler.constantHandler.initialVariableValues[element]; |
52 String stringRepresentation = (value == null) | 52 String stringRepresentation = (value == null) |
53 ? "null" | 53 ? "null" |
54 : jsAst.prettyPrint(task.constantReference(value), compiler) | 54 : jsAst.prettyPrint(task.constantReference(value), compiler) |
55 .getText(); | 55 .getText(); |
56 defaultValues.add(addGlobalMetadata(stringRepresentation)); | 56 defaultValues.add(addGlobalMetadata(stringRepresentation)); |
57 } | 57 } |
58 return js.toExpression(defaultValues); | 58 return defaultValues; |
59 } | 59 } |
60 | 60 |
61 int reifyMetadata(MetadataAnnotation annotation) { | 61 int reifyMetadata(MetadataAnnotation annotation) { |
62 Constant value = annotation.value; | 62 Constant value = annotation.value; |
63 if (value == null) { | 63 if (value == null) { |
64 compiler.reportInternalError( | 64 compiler.reportInternalError( |
65 annotation, 'Internal error: value is null'); | 65 annotation, 'Internal error: value is null'); |
66 return -1; | 66 return -1; |
67 } | 67 } |
68 return addGlobalMetadata( | 68 return addGlobalMetadata( |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 buffer.write(metadata); | 106 buffer.write(metadata); |
107 } | 107 } |
108 } else { | 108 } else { |
109 throw 'Unexpected value in metadata: ${Error.safeToString(metadata)}'; | 109 throw 'Unexpected value in metadata: ${Error.safeToString(metadata)}'; |
110 } | 110 } |
111 buffer.write(',$n'); | 111 buffer.write(',$n'); |
112 } | 112 } |
113 buffer.write('];$n'); | 113 buffer.write('];$n'); |
114 } | 114 } |
115 | 115 |
| 116 // TODO(ahe): Delete this method. |
116 jsAst.Fun extendWithMetadata(FunctionElement element, jsAst.Fun code) { | 117 jsAst.Fun extendWithMetadata(FunctionElement element, jsAst.Fun code) { |
117 if (!backend.retainMetadataOf(element)) return code; | 118 if (!backend.retainMetadataOf(element)) return code; |
| 119 List<int> metadata = computeMetadata(element); |
| 120 code.body.statements.add(js.string(metadata.join(',')).toStatement()); |
| 121 return code; |
| 122 } |
| 123 |
| 124 List<int> computeMetadata(FunctionElement element) { |
118 return compiler.withCurrentElement(element, () { | 125 return compiler.withCurrentElement(element, () { |
| 126 if (!backend.retainMetadataOf(element)) return const <int>[]; |
119 List<int> metadata = <int>[]; | 127 List<int> metadata = <int>[]; |
120 FunctionSignature signature = element.functionSignature; | 128 FunctionSignature signature = element.functionSignature; |
121 if (element.isConstructor()) { | 129 if (element.isConstructor()) { |
122 metadata.add(reifyType(element.getEnclosingClass().thisType)); | 130 metadata.add(reifyType(element.getEnclosingClass().thisType)); |
123 } else { | 131 } else { |
124 metadata.add(reifyType(signature.returnType)); | 132 metadata.add(reifyType(signature.returnType)); |
125 } | 133 } |
126 signature.forEachParameter((Element parameter) { | 134 signature.forEachParameter((Element parameter) { |
127 metadata | 135 metadata |
128 ..add(reifyName(parameter.name)) | 136 ..add(reifyName(parameter.name)) |
129 ..add(reifyType(parameter.computeType(compiler))); | 137 ..add(reifyType(parameter.computeType(compiler))); |
130 }); | 138 }); |
131 Link link = element.metadata; | 139 Link link = element.metadata; |
132 // TODO(ahe): Why is metadata sometimes null? | 140 // TODO(ahe): Why is metadata sometimes null? |
133 if (link != null) { | 141 if (link != null) { |
134 for (; !link.isEmpty; link = link.tail) { | 142 for (; !link.isEmpty; link = link.tail) { |
135 metadata.add(reifyMetadata(link.head)); | 143 metadata.add(reifyMetadata(link.head)); |
136 } | 144 } |
137 } | 145 } |
138 code.body.statements.add(js.string(metadata.join(',')).toStatement()); | 146 return metadata; |
139 return code; | |
140 }); | 147 }); |
141 } | 148 } |
142 } | 149 } |
OLD | NEW |