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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 buffer.write(metadata); | 111 buffer.write(metadata); |
112 } | 112 } |
113 } else { | 113 } else { |
114 throw 'Unexpected value in metadata: ${Error.safeToString(metadata)}'; | 114 throw 'Unexpected value in metadata: ${Error.safeToString(metadata)}'; |
115 } | 115 } |
116 buffer.write(',$n'); | 116 buffer.write(',$n'); |
117 } | 117 } |
118 buffer.write('];$n'); | 118 buffer.write('];$n'); |
119 } | 119 } |
120 | 120 |
121 jsAst.Fun extendWithMetadata(FunctionElement element, jsAst.Fun code) { | 121 List<int> computeMetadata(FunctionElement element) { |
122 if (!backend.retainMetadataOf(element)) return code; | |
123 return compiler.withCurrentElement(element, () { | 122 return compiler.withCurrentElement(element, () { |
| 123 if (!backend.retainMetadataOf(element)) return const <int>[]; |
124 List<int> metadata = <int>[]; | 124 List<int> metadata = <int>[]; |
125 FunctionSignature signature = element.functionSignature; | |
126 if (element.isConstructor()) { | |
127 metadata.add(reifyType(element.getEnclosingClass().thisType)); | |
128 } else { | |
129 metadata.add(reifyType(signature.returnType)); | |
130 } | |
131 signature.forEachParameter((Element parameter) { | |
132 metadata | |
133 ..add(reifyName(parameter.name)) | |
134 ..add(reifyType(parameter.computeType(compiler))); | |
135 }); | |
136 Link link = element.metadata; | 125 Link link = element.metadata; |
137 // TODO(ahe): Why is metadata sometimes null? | 126 // TODO(ahe): Why is metadata sometimes null? |
138 if (link != null) { | 127 if (link != null) { |
139 for (; !link.isEmpty; link = link.tail) { | 128 for (; !link.isEmpty; link = link.tail) { |
140 metadata.add(reifyMetadata(link.head)); | 129 metadata.add(reifyMetadata(link.head)); |
141 } | 130 } |
142 } | 131 } |
143 code.body.statements.add(js.string(metadata.join(',')).toStatement()); | 132 return metadata; |
144 return code; | |
145 }); | 133 }); |
146 } | 134 } |
147 } | 135 } |
OLD | NEW |