| 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 19 matching lines...) Expand all Loading... |
| 30 Constant value = | 30 Constant value = |
| 31 backend.constants.getConstantForMetadata(annotation); | 31 backend.constants.getConstantForMetadata(annotation); |
| 32 if (value == null) { | 32 if (value == null) { |
| 33 compiler.internalError(annotation, 'Annotation value is null.'); | 33 compiler.internalError(annotation, 'Annotation value is null.'); |
| 34 } else { | 34 } else { |
| 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('function() { return # }', |
| 41 [], [js.return_(new jsAst.ArrayInitializer.from(metadata))]); | 41 new jsAst.ArrayInitializer.from(metadata)); |
| 42 }); | 42 }); |
| 43 } | 43 } |
| 44 | 44 |
| 45 List<int> reifyDefaultArguments(FunctionElement function) { | 45 List<int> reifyDefaultArguments(FunctionElement function) { |
| 46 FunctionSignature signature = function.functionSignature; | 46 FunctionSignature signature = function.functionSignature; |
| 47 if (signature.optionalParameterCount == 0) return const []; | 47 if (signature.optionalParameterCount == 0) return const []; |
| 48 List<int> defaultValues = <int>[]; | 48 List<int> defaultValues = <int>[]; |
| 49 for (Element element in signature.optionalParameters) { | 49 for (Element element in signature.optionalParameters) { |
| 50 Constant value = backend.constants.getConstantForVariable(element); | 50 Constant value = backend.constants.getConstantForVariable(element); |
| 51 String stringRepresentation = (value == null) | 51 String stringRepresentation = (value == null) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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 = |
| 72 backend.rti.getTypeRepresentation(type, (variable) { | 72 backend.rti.getTypeRepresentation(type, (variable) { |
| 73 return js.toExpression( | 73 return js.number( |
| 74 task.typeVariableHandler.reifyTypeVariable(variable.element)); | 74 task.typeVariableHandler.reifyTypeVariable(variable.element)); |
| 75 }); | 75 }); |
| 76 | 76 |
| 77 return addGlobalMetadata( | 77 return addGlobalMetadata( |
| 78 jsAst.prettyPrint(representation, compiler).getText()); | 78 jsAst.prettyPrint(representation, compiler).getText()); |
| 79 } | 79 } |
| 80 | 80 |
| 81 int reifyName(String name) { | 81 int reifyName(String name) { |
| 82 return addGlobalMetadata('"$name"'); | 82 return addGlobalMetadata('"$name"'); |
| 83 } | 83 } |
| 84 | 84 |
| 85 int addGlobalMetadata(String string) { | 85 int addGlobalMetadata(String string) { |
| 86 return globalMetadataMap.putIfAbsent(string, () { | 86 return globalMetadataMap.putIfAbsent(string, () { |
| 87 globalMetadata.add(string); | 87 globalMetadata.add(string); |
| 88 return globalMetadata.length - 1; | 88 return globalMetadata.length - 1; |
| 89 }); | 89 }); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void emitMetadata(CodeBuffer buffer) { | 92 void emitMetadata(CodeBuffer buffer) { |
| 93 var literals = backend.typedefTypeLiterals.toList(); | 93 var literals = backend.typedefTypeLiterals.toList(); |
| 94 Elements.sortedByPosition(literals); | 94 Elements.sortedByPosition(literals); |
| 95 var properties = []; | 95 var properties = []; |
| 96 for (TypedefElement literal in literals) { | 96 for (TypedefElement literal in literals) { |
| 97 var key = namer.getNameX(literal); | 97 var key = namer.getNameX(literal); |
| 98 var value = js.toExpression(reifyType(literal.rawType)); | 98 var value = js.number(reifyType(literal.rawType)); |
| 99 properties.add(new jsAst.Property(js.string(key), value)); | 99 properties.add(new jsAst.Property(js.string(key), value)); |
| 100 } | 100 } |
| 101 var map = new jsAst.ObjectInitializer(properties); | 101 var map = new jsAst.ObjectInitializer(properties); |
| 102 buffer.write( | 102 buffer.write( |
| 103 jsAst.prettyPrint( | 103 jsAst.prettyPrint( |
| 104 js('init.functionAliases = #', map).toStatement(), compiler)); | 104 js.statement('init.functionAliases = #', map), compiler)); |
| 105 buffer.write('${N}init.metadata$_=$_['); | 105 buffer.write('${N}init.metadata$_=$_['); |
| 106 for (var metadata in globalMetadata) { | 106 for (var metadata in globalMetadata) { |
| 107 if (metadata is String) { | 107 if (metadata is String) { |
| 108 if (metadata != 'null') { | 108 if (metadata != 'null') { |
| 109 buffer.write(metadata); | 109 buffer.write(metadata); |
| 110 } | 110 } |
| 111 } else { | 111 } else { |
| 112 throw 'Unexpected value in metadata: ${Error.safeToString(metadata)}'; | 112 throw 'Unexpected value in metadata: ${Error.safeToString(metadata)}'; |
| 113 } | 113 } |
| 114 buffer.write(',$n'); | 114 buffer.write(',$n'); |
| 115 } | 115 } |
| 116 buffer.write('];$n'); | 116 buffer.write('];$n'); |
| 117 } | 117 } |
| 118 | 118 |
| 119 List<int> computeMetadata(FunctionElement element) { | 119 List<int> computeMetadata(FunctionElement element) { |
| 120 return compiler.withCurrentElement(element, () { | 120 return compiler.withCurrentElement(element, () { |
| 121 if (!backend.retainMetadataOf(element)) return const <int>[]; | 121 if (!backend.retainMetadataOf(element)) return const <int>[]; |
| 122 List<int> metadata = <int>[]; | 122 List<int> metadata = <int>[]; |
| 123 Link link = element.metadata; | 123 Link link = element.metadata; |
| 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 |