OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library dart._js_mirrors; | 5 library dart._js_mirrors; |
6 | 6 |
7 import 'dart:collection'; | |
8 import 'dart:mirrors'; | 7 import 'dart:mirrors'; |
9 import 'dart:_foreign_helper' show JS; | 8 import 'dart:_foreign_helper' show JS; |
10 import 'dart:_internal' as _internal; | 9 import 'dart:_internal' as _internal; |
11 | 10 |
12 String getName(Symbol symbol) => | 11 String getName(Symbol symbol) => |
13 _internal.Symbol.getName(symbol as _internal.Symbol); | 12 _internal.Symbol.getName(symbol as _internal.Symbol); |
14 | 13 |
15 Symbol getSymbol(name, library) => | 14 Symbol getSymbol(name, library) => |
16 throw new UnimplementedError("MirrorSystem.getSymbol unimplemented"); | 15 throw new UnimplementedError("MirrorSystem.getSymbol unimplemented"); |
17 | 16 |
(...skipping 20 matching lines...) Expand all Loading... |
38 | 37 |
39 dynamic _dsend(obj, String name, List args) { | 38 dynamic _dsend(obj, String name, List args) { |
40 return JS('', '#.dsend(#, #, ...#)', _dart, obj, name, args); | 39 return JS('', '#.dsend(#, #, ...#)', _dart, obj, name, args); |
41 } | 40 } |
42 | 41 |
43 class JsInstanceMirror implements InstanceMirror { | 42 class JsInstanceMirror implements InstanceMirror { |
44 final Object reflectee; | 43 final Object reflectee; |
45 | 44 |
46 JsInstanceMirror._(this.reflectee); | 45 JsInstanceMirror._(this.reflectee); |
47 | 46 |
| 47 ClassMirror get type => |
| 48 throw new UnimplementedError("ClassMirror.type unimplemented"); |
| 49 bool get hasReflectee => |
| 50 throw new UnimplementedError("ClassMirror.hasReflectee unimplemented"); |
| 51 delegate(Invocation invocation) => |
| 52 throw new UnimplementedError("ClassMirror.delegate unimplemented"); |
| 53 |
48 InstanceMirror getField(Symbol symbol) { | 54 InstanceMirror getField(Symbol symbol) { |
49 var name = getName(symbol); | 55 var name = getName(symbol); |
50 var field = _dload(reflectee, name); | 56 var field = _dload(reflectee, name); |
51 return new JsInstanceMirror._(field); | 57 return new JsInstanceMirror._(field); |
52 } | 58 } |
53 | 59 |
54 InstanceMirror setField(Symbol symbol, Object value) { | 60 InstanceMirror setField(Symbol symbol, Object value) { |
55 var name = getName(symbol); | 61 var name = getName(symbol); |
56 var field = _dput(reflectee, name, value); | 62 _dput(reflectee, name, value); |
57 return new JsInstanceMirror._(field); | 63 return new JsInstanceMirror._(value); |
58 } | 64 } |
59 | 65 |
60 InstanceMirror invoke(Symbol symbol, List<dynamic> args, | 66 InstanceMirror invoke(Symbol symbol, List<dynamic> args, |
61 [Map<Symbol, dynamic> namedArgs]) { | 67 [Map<Symbol, dynamic> namedArgs]) { |
62 var name = getName(symbol); | 68 var name = getName(symbol); |
63 if (namedArgs != null) { | 69 if (namedArgs != null) { |
64 args = new List.from(args); | 70 args = new List.from(args); |
65 args.add(_toJsMap(namedArgs)); | 71 args.add(_toJsMap(namedArgs)); |
66 } | 72 } |
67 var result = _dsend(reflectee, name, args); | 73 var result = _dsend(reflectee, name, args); |
(...skipping 17 matching lines...) Expand all Loading... |
85 Map<Symbol, MethodMirror> _declarations; | 91 Map<Symbol, MethodMirror> _declarations; |
86 | 92 |
87 // TODO(vsm):These need to be immutable when escaping from this class. | 93 // TODO(vsm):These need to be immutable when escaping from this class. |
88 List<InstanceMirror> get metadata => _metadata; | 94 List<InstanceMirror> get metadata => _metadata; |
89 Map<Symbol, MethodMirror> get declarations => _declarations; | 95 Map<Symbol, MethodMirror> get declarations => _declarations; |
90 | 96 |
91 JsClassMirror._(Type cls) | 97 JsClassMirror._(Type cls) |
92 : _cls = cls, | 98 : _cls = cls, |
93 simpleName = new Symbol(JS('String', '#.name', cls)) { | 99 simpleName = new Symbol(JS('String', '#.name', cls)) { |
94 // Load metadata. | 100 // Load metadata. |
95 var fn = JS('List<InstanceMirror>', '#[dart.metadata]', _cls); | 101 var fn = JS('Function', '#[dart.metadata]', _cls); |
96 _metadata = (fn == null) | 102 _metadata = (fn == null) |
97 ? <InstanceMirror>[] | 103 ? <InstanceMirror>[] |
98 : new List<InstanceMirror>.from(fn().map((i) => new JsInstanceMirror._(i
))); | 104 : new List<InstanceMirror>.from( |
| 105 fn().map((i) => new JsInstanceMirror._(i))); |
99 | 106 |
100 // Load declarations. | 107 // Load declarations. |
101 // TODO(vsm): This is only populating the default constructor right now. | 108 // TODO(vsm): This is only populating the default constructor right now. |
102 _declarations = new Map<Symbol, MethodMirror>(); | 109 _declarations = new Map<Symbol, MethodMirror>(); |
103 _declarations[simpleName] = new JsMethodMirror._(this, _cls); | 110 _declarations[simpleName] = new JsMethodMirror._(this, _cls); |
104 } | 111 } |
105 | 112 |
106 InstanceMirror newInstance(Symbol constructorName, List args, | 113 InstanceMirror newInstance(Symbol constructorName, List args, |
107 [Map<Symbol, dynamic> namedArgs]) { | 114 [Map<Symbol, dynamic> namedArgs]) { |
108 // TODO(vsm): Support named constructors and named arguments. | 115 // TODO(vsm): Support named constructors and named arguments. |
109 assert(getName(constructorName) == ""); | 116 assert(getName(constructorName) == ""); |
110 assert(namedArgs == null || namedArgs.isEmpty); | 117 assert(namedArgs == null || namedArgs.isEmpty); |
111 var instance = JS('', 'new #(...#)', _cls, args); | 118 var instance = JS('', 'new #(...#)', _cls, args); |
112 return new JsInstanceMirror._(instance); | 119 return new JsInstanceMirror._(instance); |
113 } | 120 } |
| 121 |
| 122 List<ClassMirror> get superinterfaces { |
| 123 var interfaces = JS('Function', '#[dart.implements]', _cls); |
| 124 if (interfaces == null) { |
| 125 return []; |
| 126 } |
| 127 throw new UnimplementedError("ClassMirror.superinterfaces unimplemented"); |
| 128 } |
| 129 |
| 130 // TODO(vsm): Implement |
| 131 InstanceMirror getField(Symbol fieldName) => |
| 132 throw new UnimplementedError("ClassMirror.getField unimplemented"); |
| 133 InstanceMirror invoke(Symbol memberName, List positionalArguments, |
| 134 [Map<Symbol, dynamic> namedArguments]) => |
| 135 throw new UnimplementedError("ClassMirror.invoke unimplemented"); |
| 136 bool isAssignableTo(TypeMirror other) => |
| 137 throw new UnimplementedError("ClassMirror.isAssignable unimplemented"); |
| 138 bool isSubclassOf(ClassMirror other) => |
| 139 throw new UnimplementedError("ClassMirror.isSubclassOf unimplemented"); |
| 140 bool isSubtypeOf(TypeMirror other) => |
| 141 throw new UnimplementedError("ClassMirror.isSubtypeOf unimplemented"); |
| 142 InstanceMirror setField(Symbol fieldName, Object value) => |
| 143 throw new UnimplementedError("ClassMirror.setField unimplemented"); |
| 144 bool get hasReflectedType => throw new UnimplementedError( |
| 145 "ClassMirror.hasReflectedType unimplemented"); |
| 146 Map<Symbol, MethodMirror> get instanceMembers => |
| 147 throw new UnimplementedError("ClassMirror.instanceMembers unimplemented"); |
| 148 bool get isAbstract => |
| 149 throw new UnimplementedError("ClassMirror.isAbstract unimplemented"); |
| 150 bool get isEnum => |
| 151 throw new UnimplementedError("ClassMirror.isEnum unimplemented"); |
| 152 bool get isOriginalDeclaration => throw new UnimplementedError( |
| 153 "ClassMirror.isOriginalDeclaration unimplemented"); |
| 154 bool get isPrivate => |
| 155 throw new UnimplementedError("ClassMirror.isPrivate unimplemented"); |
| 156 bool get isTopLevel => |
| 157 throw new UnimplementedError("ClassMirror.isTopLevel unimplemented"); |
| 158 SourceLocation get location => |
| 159 throw new UnimplementedError("ClassMirror.location unimplemented"); |
| 160 ClassMirror get mixin => |
| 161 throw new UnimplementedError("ClassMirror.mixin unimplemented"); |
| 162 TypeMirror get originalDeclaration => throw new UnimplementedError( |
| 163 "ClassMirror.originalDeclaration unimplemented"); |
| 164 DeclarationMirror get owner => |
| 165 throw new UnimplementedError("ClassMirror.owner unimplemented"); |
| 166 Symbol get qualifiedName => |
| 167 throw new UnimplementedError("ClassMirror.qualifiedName unimplemented"); |
| 168 Type get reflectedType => |
| 169 throw new UnimplementedError("ClassMirror.reflectedType unimplemented"); |
| 170 Map<Symbol, MethodMirror> get staticMembers => |
| 171 throw new UnimplementedError("ClassMirror.staticMembers unimplemented"); |
| 172 ClassMirror get superclass => |
| 173 throw new UnimplementedError("ClassMirror.superclass unimplemented"); |
| 174 List<TypeMirror> get typeArguments => |
| 175 throw new UnimplementedError("ClassMirror.typeArguments unimplemented"); |
| 176 List<TypeVariableMirror> get typeVariables => |
| 177 throw new UnimplementedError("ClassMirror.typeVariables unimplemented"); |
114 } | 178 } |
115 | 179 |
116 class JsTypeMirror implements TypeMirror { | 180 class JsTypeMirror implements TypeMirror { |
| 181 // TODO(vsm): Support original declarations, etc., where there is no actual |
| 182 // reflected type. |
117 final Type reflectedType; | 183 final Type reflectedType; |
| 184 final bool hasReflectedType = true; |
118 | 185 |
119 JsTypeMirror._(this.reflectedType); | 186 JsTypeMirror._(this.reflectedType); |
| 187 |
| 188 // TODO(vsm): Implement |
| 189 bool isAssignableTo(TypeMirror other) => |
| 190 throw new UnimplementedError("TypeMirror.isAssignable unimplemented"); |
| 191 bool isSubtypeOf(TypeMirror other) => |
| 192 throw new UnimplementedError("TypeMirror.isSubtypeOf unimplemented"); |
| 193 bool get isOriginalDeclaration => throw new UnimplementedError( |
| 194 "TypeMirror.isOriginalDeclaration unimplemented"); |
| 195 bool get isPrivate => |
| 196 throw new UnimplementedError("TypeMirror.isPrivate unimplemented"); |
| 197 bool get isTopLevel => |
| 198 throw new UnimplementedError("TypeMirror.isTopLevel unimplemented"); |
| 199 SourceLocation get location => |
| 200 throw new UnimplementedError("TypeMirror.location unimplemented"); |
| 201 List<InstanceMirror> get metadata => |
| 202 throw new UnimplementedError("TypeMirror.metadata unimplemented"); |
| 203 TypeMirror get originalDeclaration => throw new UnimplementedError( |
| 204 "TypeMirror.originalDeclaration unimplemented"); |
| 205 DeclarationMirror get owner => |
| 206 throw new UnimplementedError("TypeMirror.owner unimplemented"); |
| 207 Symbol get qualifiedName => |
| 208 throw new UnimplementedError("TypeMirror.qualifiedName unimplemented"); |
| 209 Symbol get simpleName => |
| 210 throw new UnimplementedError("TypeMirror.simpleName unimplemented"); |
| 211 List<TypeMirror> get typeArguments => |
| 212 throw new UnimplementedError("TypeMirror.typeArguments unimplemented"); |
| 213 List<TypeVariableMirror> get typeVariables => |
| 214 throw new UnimplementedError("TypeMirror.typeVariables unimplemented"); |
120 } | 215 } |
121 | 216 |
122 class JsParameterMirror implements ParameterMirror { | 217 class JsParameterMirror implements ParameterMirror { |
123 final String _name; | 218 final String _name; |
124 final TypeMirror type; | 219 final TypeMirror type; |
125 final List<InstanceMirror> metadata = []; | 220 final List<InstanceMirror> metadata; |
126 | 221 |
127 JsParameterMirror._(this._name, Type t) : type = new JsTypeMirror._(t); | 222 JsParameterMirror._(this._name, Type t, List annotations) |
| 223 : type = new JsTypeMirror._(t), |
| 224 metadata = new List<InstanceMirror>.from( |
| 225 annotations.map((a) => new JsInstanceMirror._(a))); |
| 226 |
| 227 // TODO(vsm): Implement |
| 228 InstanceMirror get defaultValue => throw new UnimplementedError( |
| 229 "ParameterMirror.defaultValues unimplemented"); |
| 230 bool get hasDefaultValue => throw new UnimplementedError( |
| 231 "ParameterMirror.hasDefaultValue unimplemented"); |
| 232 bool get isConst => |
| 233 throw new UnimplementedError("ParameterMirror.isConst unimplemented"); |
| 234 bool get isFinal => |
| 235 throw new UnimplementedError("ParameterMirror.isFinal unimplemented"); |
| 236 bool get isNamed => |
| 237 throw new UnimplementedError("ParameterMirror.isNamed unimplemented"); |
| 238 bool get isOptional => |
| 239 throw new UnimplementedError("ParameterMirror.isOptional unimplemented"); |
| 240 bool get isPrivate => |
| 241 throw new UnimplementedError("ParameterMirror.isPrivate unimplemented"); |
| 242 bool get isStatic => |
| 243 throw new UnimplementedError("ParameterMirror.isStatic unimplemented"); |
| 244 bool get isTopLevel => |
| 245 throw new UnimplementedError("ParameterMirror.isTopLevel unimplemented"); |
| 246 SourceLocation get location => |
| 247 throw new UnimplementedError("ParameterMirror.location unimplemented"); |
| 248 DeclarationMirror get owner => |
| 249 throw new UnimplementedError("ParameterMirror.owner unimplemented"); |
| 250 Symbol get qualifiedName => throw new UnimplementedError( |
| 251 "ParameterMirror.qualifiedName unimplemented"); |
| 252 Symbol get simpleName => |
| 253 throw new UnimplementedError("ParameterMirror.simpleName unimplemented"); |
128 } | 254 } |
129 | 255 |
130 class JsMethodMirror implements MethodMirror { | 256 class JsMethodMirror implements MethodMirror { |
131 final String _name; | 257 final String _name; |
132 final dynamic _method; | 258 final dynamic _method; |
133 List<ParameterMirror> _params; | 259 List<ParameterMirror> _params; |
134 | 260 |
135 JsMethodMirror._(JsClassMirror cls, this._method) | 261 JsMethodMirror._(JsClassMirror cls, this._method) |
136 : _name = getName(cls.simpleName) { | 262 : _name = getName(cls.simpleName) { |
137 var ftype = JS('', '#.classGetConstructorType(#)', _dart, cls._cls); | 263 var ftype = JS('', '#.classGetConstructorType(#)', _dart, cls._cls); |
(...skipping 10 matching lines...) Expand all Loading... |
148 return []; | 274 return []; |
149 } | 275 } |
150 | 276 |
151 // TODO(vsm): Add named args. | 277 // TODO(vsm): Add named args. |
152 List args = ftype.args; | 278 List args = ftype.args; |
153 List opts = ftype.optionals; | 279 List opts = ftype.optionals; |
154 var params = new List<ParameterMirror>(args.length + opts.length); | 280 var params = new List<ParameterMirror>(args.length + opts.length); |
155 | 281 |
156 for (var i = 0; i < args.length; ++i) { | 282 for (var i = 0; i < args.length; ++i) { |
157 var type = args[i]; | 283 var type = args[i]; |
| 284 var metadata = ftype.metadata[i]; |
158 // TODO(vsm): Recover the param name. | 285 // TODO(vsm): Recover the param name. |
159 var param = new JsParameterMirror._('', type); | 286 var param = new JsParameterMirror._('', type, metadata); |
160 params[i] = param; | 287 params[i] = param; |
161 } | 288 } |
162 | 289 |
163 for (var i = 0; i < opts.length; ++i) { | 290 for (var i = 0; i < opts.length; ++i) { |
164 var type = opts[i]; | 291 var type = opts[i]; |
| 292 var metadata = ftype.metadata[args.length + i]; |
165 // TODO(vsm): Recover the param name. | 293 // TODO(vsm): Recover the param name. |
166 var param = new JsParameterMirror._('', type); | 294 var param = new JsParameterMirror._('', type, metadata); |
167 params[i + args.length] = param; | 295 params[i + args.length] = param; |
168 } | 296 } |
169 | 297 |
170 return params; | 298 return params; |
171 } | 299 } |
| 300 |
| 301 // TODO(vsm): Implement |
| 302 bool get isAbstract => |
| 303 throw new UnimplementedError("MethodMirror.isAbstract unimplemented"); |
| 304 bool get isConstConstructor => throw new UnimplementedError( |
| 305 "MethodMirror.isConstConstructor unimplemented"); |
| 306 bool get isConstructor => |
| 307 throw new UnimplementedError("MethodMirror.isConstructor unimplemented"); |
| 308 bool get isFactoryConstructor => throw new UnimplementedError( |
| 309 "MethodMirror.isFactoryConstructor unimplemented"); |
| 310 bool get isGenerativeConstructor => throw new UnimplementedError( |
| 311 "MethodMirror.isGenerativeConstructor unimplemented"); |
| 312 bool get isGetter => |
| 313 throw new UnimplementedError("MethodMirror.isGetter unimplemented"); |
| 314 bool get isOperator => |
| 315 throw new UnimplementedError("MethodMirror.isOperator unimplemented"); |
| 316 bool get isPrivate => |
| 317 throw new UnimplementedError("MethodMirror.isPrivate unimplemented"); |
| 318 bool get isRedirectingConstructor => throw new UnimplementedError( |
| 319 "MethodMirror.isRedirectingConstructor unimplemented"); |
| 320 bool get isRegularMethod => throw new UnimplementedError( |
| 321 "MethodMirror.isRegularMethod unimplemented"); |
| 322 bool get isSetter => |
| 323 throw new UnimplementedError("MethodMirror.isSetter unimplemented"); |
| 324 bool get isStatic => |
| 325 throw new UnimplementedError("MethodMirror.isStatic unimplemented"); |
| 326 bool get isSynthetic => |
| 327 throw new UnimplementedError("MethodMirror.isSynthetic unimplemented"); |
| 328 bool get isTopLevel => |
| 329 throw new UnimplementedError("MethodMirror.isTopLevel unimplemented"); |
| 330 SourceLocation get location => |
| 331 throw new UnimplementedError("MethodMirror.location unimplemented"); |
| 332 List<InstanceMirror> get metadata => |
| 333 throw new UnimplementedError("MethodMirror.metadata unimplemented"); |
| 334 DeclarationMirror get owner => |
| 335 throw new UnimplementedError("MethodMirror.owner unimplemented"); |
| 336 Symbol get qualifiedName => |
| 337 throw new UnimplementedError("MethodMirror.qualifiedName unimplemented"); |
| 338 TypeMirror get returnType => |
| 339 throw new UnimplementedError("MethodMirror.returnType unimplemented"); |
| 340 Symbol get simpleName => |
| 341 throw new UnimplementedError("MethodMirror.simpleName unimplemented"); |
| 342 String get source => |
| 343 throw new UnimplementedError("MethodMirror.source unimplemented"); |
172 } | 344 } |
OLD | NEW |