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 // TODO(vsm): This a workaround for: | 11 // TODO(vsm): This a workaround for: |
13 // https://github.com/dart-lang/dev_compiler/issues/219 | 12 // https://github.com/dart-lang/dev_compiler/issues/219 |
14 import 'dart:js'; | 13 import 'dart:js'; |
15 | 14 |
16 String getName(Symbol symbol) => | 15 String getName(Symbol symbol) => |
17 _internal.Symbol.getName(symbol as _internal.Symbol); | 16 _internal.Symbol.getName(symbol as _internal.Symbol); |
(...skipping 24 matching lines...) Expand all Loading... | |
42 | 41 |
43 dynamic _dsend(obj, String name, List args) { | 42 dynamic _dsend(obj, String name, List args) { |
44 return JS('', '#.dsend(#, #, ...#)', _dart, obj, name, args); | 43 return JS('', '#.dsend(#, #, ...#)', _dart, obj, name, args); |
45 } | 44 } |
46 | 45 |
47 class JsInstanceMirror implements InstanceMirror { | 46 class JsInstanceMirror implements InstanceMirror { |
48 final Object reflectee; | 47 final Object reflectee; |
49 | 48 |
50 JsInstanceMirror._(this.reflectee); | 49 JsInstanceMirror._(this.reflectee); |
51 | 50 |
51 ClassMirror get type => throw new UnimplementedError("ClassMirror.type unimple mented"); | |
Jennifer Messerly
2015/07/24 16:28:28
long lines
vsm
2015/08/07 17:07:32
Done.
| |
52 bool get hasReflectee => throw new UnimplementedError("ClassMirror.hasReflecte e unimplemented"); | |
53 delegate(Invocation invocation) => throw new UnimplementedError("ClassMirror.d elegate unimplemented"); | |
54 | |
52 InstanceMirror getField(Symbol symbol) { | 55 InstanceMirror getField(Symbol symbol) { |
53 var name = getName(symbol); | 56 var name = getName(symbol); |
54 var field = _dload(reflectee, name); | 57 var field = _dload(reflectee, name); |
55 return new JsInstanceMirror._(field); | 58 return new JsInstanceMirror._(field); |
56 } | 59 } |
57 | 60 |
58 InstanceMirror setField(Symbol symbol, Object value) { | 61 InstanceMirror setField(Symbol symbol, Object value) { |
59 var name = getName(symbol); | 62 var name = getName(symbol); |
60 var field = _dput(reflectee, name, value); | 63 var field = _dput(reflectee, name, value); |
61 return new JsInstanceMirror._(field); | 64 return new JsInstanceMirror._(field); |
(...skipping 27 matching lines...) Expand all Loading... | |
89 Map<Symbol, MethodMirror> _declarations; | 92 Map<Symbol, MethodMirror> _declarations; |
90 | 93 |
91 // TODO(vsm):These need to be immutable when escaping from this class. | 94 // TODO(vsm):These need to be immutable when escaping from this class. |
92 List<InstanceMirror> get metadata => _metadata; | 95 List<InstanceMirror> get metadata => _metadata; |
93 Map<Symbol, MethodMirror> get declarations => _declarations; | 96 Map<Symbol, MethodMirror> get declarations => _declarations; |
94 | 97 |
95 JsClassMirror._(Type cls) | 98 JsClassMirror._(Type cls) |
96 : _cls = cls, | 99 : _cls = cls, |
97 simpleName = new Symbol(JS('String', '#.name', cls)) { | 100 simpleName = new Symbol(JS('String', '#.name', cls)) { |
98 // Load metadata. | 101 // Load metadata. |
99 var fn = JS('List<InstanceMirror>', '#[dart.metadata]', _cls); | 102 var fn = JS('List', '#[dart.metadata]', _cls); |
Jennifer Messerly
2015/07/24 16:28:28
fwiw, we could fix our "type parser" for "JS" to u
vsm
2015/08/07 17:07:33
Up to you. We don't really use it so much I guess
| |
100 _metadata = (fn == null) | 103 _metadata = (fn == null) |
101 ? <InstanceMirror>[] | 104 ? <InstanceMirror>[] |
102 : new List<InstanceMirror>.from(fn().map((i) => new JsInstanceMirror._(i ))); | 105 : new List<InstanceMirror>.from(fn().map((i) => new JsInstanceMirror._(i ))); |
103 | 106 |
104 // Load declarations. | 107 // Load declarations. |
105 // TODO(vsm): This is only populating the default constructor right now. | 108 // TODO(vsm): This is only populating the default constructor right now. |
106 _declarations = new Map<Symbol, MethodMirror>(); | 109 _declarations = new Map<Symbol, MethodMirror>(); |
107 _declarations[simpleName] = new JsMethodMirror._(this, _cls); | 110 _declarations[simpleName] = new JsMethodMirror._(this, _cls); |
108 } | 111 } |
109 | 112 |
110 InstanceMirror newInstance(Symbol constructorName, List args, | 113 InstanceMirror newInstance(Symbol constructorName, List args, |
111 [Map<Symbol, dynamic> namedArgs]) { | 114 [Map<Symbol, dynamic> namedArgs]) { |
112 // TODO(vsm): Support named constructors and named arguments. | 115 // TODO(vsm): Support named constructors and named arguments. |
113 assert(getName(constructorName) == ""); | 116 assert(getName(constructorName) == ""); |
114 assert(namedArgs == null || namedArgs.isEmpty); | 117 assert(namedArgs == null || namedArgs.isEmpty); |
115 var instance = JS('', '#.instantiate(#, #)', _dart, _cls, args); | 118 var instance = JS('', '#.instantiate(#, #)', _dart, _cls, args); |
116 return new JsInstanceMirror._(instance); | 119 return new JsInstanceMirror._(instance); |
117 } | 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) => throw new UnimplementedError("Cla ssMirror.getField unimplemented"); | |
Jennifer Messerly
2015/07/24 16:28:28
here too
vsm
2015/08/07 17:07:33
Done.
| |
132 InstanceMirror invoke(Symbol memberName, List positionalArguments, [Map<Symbol , dynamic> namedArguments]) => throw new UnimplementedError("ClassMirror.invoke unimplemented"); | |
133 bool isAssignableTo(TypeMirror other) => throw new UnimplementedError("ClassMi rror.isAssignable unimplemented"); | |
134 bool isSubclassOf(ClassMirror other) => throw new UnimplementedError("ClassMir ror.isSubclassOf unimplemented"); | |
135 bool isSubtypeOf(TypeMirror other) => throw new UnimplementedError("ClassMirro r.isSubtypeOf unimplemented"); | |
136 InstanceMirror setField(Symbol fieldName, Object value) => throw new Unimpleme ntedError("ClassMirror.setField unimplemented"); | |
137 bool get hasReflectedType => throw new UnimplementedError("ClassMirror.hasRefl ectedType unimplemented"); | |
138 Map<Symbol, MethodMirror> get instanceMembers => throw new UnimplementedError( "ClassMirror.instanceMembers unimplemented"); | |
139 bool get isAbstract => throw new UnimplementedError("ClassMirror.isAbstract un implemented"); | |
140 bool get isEnum => throw new UnimplementedError("ClassMirror.isEnum unimplemen ted"); | |
141 bool get isOriginalDeclaration => throw new UnimplementedError("ClassMirror.is OriginalDeclaration unimplemented"); | |
142 bool get isPrivate => throw new UnimplementedError("ClassMirror.isPrivate unim plemented"); | |
143 bool get isTopLevel => throw new UnimplementedError("ClassMirror.isTopLevel un implemented"); | |
144 SourceLocation get location => throw new UnimplementedError("ClassMirror.locat ion unimplemented"); | |
145 ClassMirror get mixin => throw new UnimplementedError("ClassMirror.mixin unimp lemented"); | |
146 TypeMirror get originalDeclaration => throw new UnimplementedError("ClassMirro r.originalDeclaration unimplemented"); | |
147 DeclarationMirror get owner => throw new UnimplementedError("ClassMirror.owner unimplemented"); | |
148 Symbol get qualifiedName => throw new UnimplementedError("ClassMirror.qualifie dName unimplemented"); | |
149 Type get reflectedType => throw new UnimplementedError("ClassMirror.reflectedT ype unimplemented"); | |
150 Map<Symbol, MethodMirror> get staticMembers => throw new UnimplementedError("C lassMirror.staticMembers unimplemented"); | |
151 TypeMirror get superclass => throw new UnimplementedError("ClassMirror.superc lass unimplemented"); | |
152 List<TypeMirror> get typeArguments => throw new UnimplementedError("ClassMirro r.typeArguments unimplemented"); | |
153 List<TypeVariableMirror> get typeVariables => throw new UnimplementedError("Cl assMirror.typeVariables unimplemented"); | |
118 } | 154 } |
119 | 155 |
120 class JsTypeMirror implements TypeMirror { | 156 class JsTypeMirror implements TypeMirror { |
157 // TODO(vsm): Support original declarations, etc., where there is no actual | |
158 // reflected type. | |
121 final Type reflectedType; | 159 final Type reflectedType; |
160 final bool hasReflectedType = true; | |
Jennifer Messerly
2015/07/24 16:28:28
make this a getter? (less storage needed on the in
vsm
2015/08/07 17:07:33
Could be ... but won't a constant for long ... so
| |
122 | 161 |
123 JsTypeMirror._(this.reflectedType); | 162 JsTypeMirror._(this.reflectedType); |
163 | |
164 // TODO(vsm): Implement | |
165 bool isAssignableTo(TypeMirror other) => throw new UnimplementedError("TypeMir ror.isAssignable unimplemented"); | |
166 bool isSubtypeOf(TypeMirror other) => throw new UnimplementedError("TypeMirror .isSubtypeOf unimplemented"); | |
167 bool get isOriginalDeclaration => throw new UnimplementedError("TypeMirror.isO riginalDeclaration unimplemented"); | |
168 bool get isPrivate => throw new UnimplementedError("TypeMirror.isPrivate unimp lemented"); | |
169 bool get isTopLevel => throw new UnimplementedError("TypeMirror.isTopLevel uni mplemented"); | |
170 SourceLocation get location => throw new UnimplementedError("TypeMirror.locati on unimplemented"); | |
171 List<InstanceMirror> get metadata => throw new UnimplementedError("TypeMirror. metadata unimplemented"); | |
172 TypeMirror get originalDeclaration => throw new UnimplementedError("TypeMirror .originalDeclaration unimplemented"); | |
173 DeclarationMirror get owner => throw new UnimplementedError("TypeMirror.owner unimplemented"); | |
174 Symbol get qualifiedName => throw new UnimplementedError("TypeMirror.qualified Name unimplemented"); | |
175 Symbol get simpleName => throw new UnimplementedError("TypeMirror.simpleName u nimplemented"); | |
176 List<TypeMirror> get typeArguments => throw new UnimplementedError("TypeMirror .typeArguments unimplemented"); | |
177 List<TypeVariableMirror> get typeVariables => throw new UnimplementedError("Ty peMirror.typeVariables unimplemented"); | |
124 } | 178 } |
125 | 179 |
126 class JsParameterMirror implements ParameterMirror { | 180 class JsParameterMirror implements ParameterMirror { |
127 final String _name; | 181 final String _name; |
128 final TypeMirror type; | 182 final TypeMirror type; |
129 final List<InstanceMirror> metadata = []; | 183 final List<InstanceMirror> metadata; |
130 | 184 |
131 JsParameterMirror._(this._name, Type t) : type = new JsTypeMirror._(t); | 185 JsParameterMirror._(this._name, Type t, List annotations) : |
186 type = new JsTypeMirror._(t), | |
187 metadata = new List<InstanceMirror>.from(annotations.map((a) => new JsInstan ceMirror._(a))); | |
188 | |
189 // TODO(vsm): Implement | |
190 InstanceMirror get defaultValue => throw new UnimplementedError("ParameterMirr or.defaultValues unimplemented"); | |
191 bool get hasDefaultValue => throw new UnimplementedError("ParameterMirror.hasD efaultValue unimplemented"); | |
192 bool get isConst => throw new UnimplementedError("ParameterMirror.isConst unim plemented"); | |
193 bool get isFinal => throw new UnimplementedError("ParameterMirror.isFinal unim plemented"); | |
194 bool get isNamed => throw new UnimplementedError("ParameterMirror.isNamed unim plemented"); | |
195 bool get isOptional => throw new UnimplementedError("ParameterMirror.isOptiona l unimplemented"); | |
196 bool get isPrivate => throw new UnimplementedError("ParameterMirror.isPrivate unimplemented"); | |
197 bool get isStatic => throw new UnimplementedError("ParameterMirror.isStatic un implemented"); | |
198 bool get isTopLevel => throw new UnimplementedError("ParameterMirror.isTopLeve l unimplemented"); | |
199 SourceLocation get location => throw new UnimplementedError("ParameterMirror.l ocation unimplemented"); | |
200 DeclarationMirror get owner => throw new UnimplementedError("ParameterMirror.o wner unimplemented"); | |
201 Symbol get qualifiedName => throw new UnimplementedError("ParameterMirror.qual ifiedName unimplemented"); | |
202 Symbol get simpleName => throw new UnimplementedError("ParameterMirror.simpleN ame unimplemented"); | |
132 } | 203 } |
133 | 204 |
134 class JsMethodMirror implements MethodMirror { | 205 class JsMethodMirror implements MethodMirror { |
135 final String _name; | 206 final String _name; |
136 final dynamic _method; | 207 final dynamic _method; |
137 List<ParameterMirror> _params; | 208 List<ParameterMirror> _params; |
138 | 209 |
139 JsMethodMirror._(JsClassMirror cls, this._method) | 210 JsMethodMirror._(JsClassMirror cls, this._method) |
140 : _name = getName(cls.simpleName) { | 211 : _name = getName(cls.simpleName) { |
141 var ftype = JS('', '#.classGetConstructorType(#)', _dart, cls._cls); | 212 var ftype = JS('', '#.classGetConstructorType(#)', _dart, cls._cls); |
(...skipping 10 matching lines...) Expand all Loading... | |
152 return []; | 223 return []; |
153 } | 224 } |
154 | 225 |
155 // TODO(vsm): Add named args. | 226 // TODO(vsm): Add named args. |
156 List args = ftype.args; | 227 List args = ftype.args; |
157 List opts = ftype.optionals; | 228 List opts = ftype.optionals; |
158 var params = new List<ParameterMirror>(args.length + opts.length); | 229 var params = new List<ParameterMirror>(args.length + opts.length); |
159 | 230 |
160 for (var i = 0; i < args.length; ++i) { | 231 for (var i = 0; i < args.length; ++i) { |
161 var type = args[i]; | 232 var type = args[i]; |
233 var metadata = ftype.metadata[i]; | |
162 // TODO(vsm): Recover the param name. | 234 // TODO(vsm): Recover the param name. |
163 var param = new JsParameterMirror._('', type); | 235 var param = new JsParameterMirror._('', type, metadata); |
164 params[i] = param; | 236 params[i] = param; |
165 } | 237 } |
166 | 238 |
167 for (var i = 0; i < opts.length; ++i) { | 239 for (var i = 0; i < opts.length; ++i) { |
168 var type = opts[i]; | 240 var type = opts[i]; |
241 var metadata = ftype.metadata[args.length + i]; | |
169 // TODO(vsm): Recover the param name. | 242 // TODO(vsm): Recover the param name. |
170 var param = new JsParameterMirror._('', type); | 243 var param = new JsParameterMirror._('', type, metadata); |
171 params[i + args.length] = param; | 244 params[i + args.length] = param; |
172 } | 245 } |
173 | 246 |
174 return params; | 247 return params; |
175 } | 248 } |
249 | |
250 // TODO(vsm): Implement | |
251 bool get isAbstract => throw new UnimplementedError("MethodMirror.isAbstract u nimplemented"); | |
252 bool get isConstConstructor => throw new UnimplementedError("MethodMirror.isCo nstConstructor unimplemented"); | |
253 bool get isConstructor => throw new UnimplementedError("MethodMirror.isConstru ctor unimplemented"); | |
254 bool get isFactoryConstructor => throw new UnimplementedError("MethodMirror.is FactoryConstructor unimplemented"); | |
255 bool get isGenerativeConstructor => throw new UnimplementedError("MethodMirror .isGenerativeConstructor unimplemented"); | |
256 bool get isGetter => throw new UnimplementedError("MethodMirror.isGetter unimp lemented"); | |
257 bool get isOperator => throw new UnimplementedError("MethodMirror.isOperator u nimplemented"); | |
258 bool get isPrivate => throw new UnimplementedError("MethodMirror.isPrivate uni mplemented"); | |
259 bool get isRedirectingConstructor => throw new UnimplementedError("MethodMirro r.isRedirectingConstructor unimplemented"); | |
260 bool get isRegularMethod => throw new UnimplementedError("MethodMirror.isRegul arMethod unimplemented"); | |
261 bool get isSetter => throw new UnimplementedError("MethodMirror.isSetter unimp lemented"); | |
262 bool get isStatic => throw new UnimplementedError("MethodMirror.isStatic unimp lemented"); | |
263 bool get isSynthetic => throw new UnimplementedError("MethodMirror.isSynthetic unimplemented"); | |
264 bool get isTopLevel => throw new UnimplementedError("MethodMirror.isTopLevel u nimplemented"); | |
265 SourceLocation get location => throw new UnimplementedError("MethodMirror.loca tion unimplemented"); | |
266 List<InstanceMirror> get metadata => throw new UnimplementedError("MethodMirro r.metadata unimplemented"); | |
267 DeclarationMirror get owner => throw new UnimplementedError("MethodMirror.owne r unimplemented"); | |
268 Symbol get qualifiedName => throw new UnimplementedError("MethodMirror.qualifi edName unimplemented"); | |
269 TypeMirror get returnType => throw new UnimplementedError("MethodMirror.return Type unimplemented"); | |
270 Symbol get simpleName => throw new UnimplementedError("MethodMirror.simpleName unimplemented"); | |
271 String get source => throw new UnimplementedError("MethodMirror.source unimple mented"); | |
272 | |
176 } | 273 } |
OLD | NEW |