| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 buffer.write('init.metadata$_=$_['); |
| 94 Elements.sortedByPosition(literals); | 94 for (String metadata in globalMetadata) { |
| 95 var properties = []; | |
| 96 for (TypedefElement literal in literals) { | |
| 97 var key = namer.getNameX(literal); | |
| 98 var value = js.number(reifyType(literal.rawType)); | |
| 99 properties.add(new jsAst.Property(js.string(key), value)); | |
| 100 } | |
| 101 var map = new jsAst.ObjectInitializer(properties); | |
| 102 buffer.write( | |
| 103 jsAst.prettyPrint( | |
| 104 js.statement('init.functionAliases = #', map), compiler)); | |
| 105 buffer.write('${N}init.metadata$_=$_['); | |
| 106 for (var metadata in globalMetadata) { | |
| 107 if (metadata is String) { | 95 if (metadata is String) { |
| 108 if (metadata != 'null') { | 96 if (metadata != 'null') { |
| 109 buffer.write(metadata); | 97 buffer.write(metadata); |
| 110 } | 98 } |
| 111 } else { | 99 } else { |
| 112 throw 'Unexpected value in metadata: ${Error.safeToString(metadata)}'; | 100 throw 'Unexpected value in metadata: ${Error.safeToString(metadata)}'; |
| 113 } | 101 } |
| 114 buffer.write(',$n'); | 102 buffer.write(',$n'); |
| 115 } | 103 } |
| 116 buffer.write('];$n'); | 104 buffer.write('];$n'); |
| 117 } | 105 } |
| 118 | 106 |
| 119 List<int> computeMetadata(FunctionElement element) { | 107 List<int> computeMetadata(FunctionElement element) { |
| 120 return compiler.withCurrentElement(element, () { | 108 return compiler.withCurrentElement(element, () { |
| 121 if (!backend.retainMetadataOf(element)) return const <int>[]; | 109 if (!backend.retainMetadataOf(element)) return const <int>[]; |
| 122 List<int> metadata = <int>[]; | 110 List<int> metadata = <int>[]; |
| 123 Link link = element.metadata; | 111 Link link = element.metadata; |
| 124 // TODO(ahe): Why is metadata sometimes null? | 112 // TODO(ahe): Why is metadata sometimes null? |
| 125 if (link != null) { | 113 if (link != null) { |
| 126 for (; !link.isEmpty; link = link.tail) { | 114 for (; !link.isEmpty; link = link.tail) { |
| 127 metadata.add(reifyMetadata(link.head)); | 115 metadata.add(reifyMetadata(link.head)); |
| 128 } | 116 } |
| 129 } | 117 } |
| 130 return metadata; | 118 return metadata; |
| 131 }); | 119 }); |
| 132 } | 120 } |
| 133 } | 121 } |
| OLD | NEW |