Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: tool/input_sdk/private/js_mirrors.dart

Issue 1989593002: Wrap and unwrap types in dart:mirrors (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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:mirrors'; 7 import 'dart:mirrors';
8 import 'dart:_foreign_helper' show JS; 8 import 'dart:_foreign_helper' show JS;
9 import 'dart:_internal' as _internal; 9 import 'dart:_internal' as _internal;
10 10
(...skipping 20 matching lines...) Expand all
31 } 31 }
32 32
33 void _dput(obj, String name, val) { 33 void _dput(obj, String name, val) {
34 JS('', '#.dput(#, #, #)', _dart, obj, name, val); 34 JS('', '#.dput(#, #, #)', _dart, obj, name, val);
35 } 35 }
36 36
37 dynamic _dsend(obj, String name, List args) { 37 dynamic _dsend(obj, String name, List args) {
38 return JS('', '#.dsend(#, #, ...#)', _dart, obj, name, args); 38 return JS('', '#.dsend(#, #, ...#)', _dart, obj, name, args);
39 } 39 }
40 40
41 // TODO(vsm): These methods need to validate whether we really have a
42 // WrappedType or a raw type that should be wrapped (as opposed to a
43 // function).
44 dynamic _unwrap(obj) => JS('', '#.unwrapped()', obj);
45
46 dynamic _wrap(obj) => JS('', '#.wrapType(#)', _dart, obj);
47
41 class JsInstanceMirror implements InstanceMirror { 48 class JsInstanceMirror implements InstanceMirror {
42 final Object reflectee; 49 final Object reflectee;
43 50
44 JsInstanceMirror._(this.reflectee); 51 JsInstanceMirror._(this.reflectee);
45 52
46 ClassMirror get type => 53 ClassMirror get type =>
47 throw new UnimplementedError("ClassMirror.type unimplemented"); 54 throw new UnimplementedError("ClassMirror.type unimplemented");
48 bool get hasReflectee => 55 bool get hasReflectee =>
49 throw new UnimplementedError("ClassMirror.hasReflectee unimplemented"); 56 throw new UnimplementedError("ClassMirror.hasReflectee unimplemented");
50 delegate(Invocation invocation) => 57 delegate(Invocation invocation) =>
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 95
89 List<InstanceMirror> _metadata; 96 List<InstanceMirror> _metadata;
90 Map<Symbol, MethodMirror> _declarations; 97 Map<Symbol, MethodMirror> _declarations;
91 98
92 // TODO(vsm):These need to be immutable when escaping from this class. 99 // TODO(vsm):These need to be immutable when escaping from this class.
93 List<InstanceMirror> get metadata => _metadata; 100 List<InstanceMirror> get metadata => _metadata;
94 Map<Symbol, MethodMirror> get declarations => _declarations; 101 Map<Symbol, MethodMirror> get declarations => _declarations;
95 102
96 JsClassMirror._(Type cls) 103 JsClassMirror._(Type cls)
97 : _cls = cls, 104 : _cls = cls,
98 simpleName = new Symbol(JS('String', '#.name', cls)) { 105 simpleName = new Symbol(JS('String', '#.name', _unwrap(cls))) {
99 // Load metadata. 106 // Load metadata.
100 var fn = JS('Function', '#[dart.metadata]', _cls); 107 var fn = JS('Function', '#[dart.metadata]', _unwrap(_cls));
101 _metadata = (fn == null) 108 _metadata = (fn == null)
102 ? <InstanceMirror>[] 109 ? <InstanceMirror>[]
103 : new List<InstanceMirror>.from( 110 : new List<InstanceMirror>.from(
104 fn().map((i) => new JsInstanceMirror._(i))); 111 fn().map((i) => new JsInstanceMirror._(i)));
105 112
106 // Load declarations. 113 // Load declarations.
107 // TODO(vsm): This is only populating the default constructor right now. 114 // TODO(vsm): This is only populating the default constructor right now.
108 _declarations = new Map<Symbol, MethodMirror>(); 115 _declarations = new Map<Symbol, MethodMirror>();
109 _declarations[simpleName] = new JsMethodMirror._(this, _cls); 116 _declarations[simpleName] = new JsMethodMirror._(this, _cls);
110 } 117 }
111 118
112 InstanceMirror newInstance(Symbol constructorName, List args, 119 InstanceMirror newInstance(Symbol constructorName, List args,
113 [Map<Symbol, dynamic> namedArgs]) { 120 [Map<Symbol, dynamic> namedArgs]) {
114 // TODO(vsm): Support named constructors and named arguments. 121 // TODO(vsm): Support named constructors and named arguments.
115 assert(getName(constructorName) == ""); 122 assert(getName(constructorName) == "");
116 assert(namedArgs == null || namedArgs.isEmpty); 123 assert(namedArgs == null || namedArgs.isEmpty);
117 var instance = JS('', 'new #(...#)', _cls, args); 124 var instance = JS('', 'new #(...#)', _unwrap(_cls), args);
118 return new JsInstanceMirror._(instance); 125 return new JsInstanceMirror._(instance);
119 } 126 }
120 127
121 List<ClassMirror> get superinterfaces { 128 List<ClassMirror> get superinterfaces {
122 var interfaceThunk = JS('Function', '#[dart.implements]', _cls); 129 var interfaceThunk = JS('Function', '#[dart.implements]', _unwrap(_cls));
123 if (interfaceThunk == null) { 130 if (interfaceThunk == null) {
124 return []; 131 return [];
125 } else { 132 } else {
126 List<Type> interfaces = interfaceThunk(); 133 List<Type> interfaces = interfaceThunk();
127 return interfaces.map((t) => new JsClassMirror._(t)).toList(); 134 return interfaces.map((t) => new JsClassMirror._(t)).toList();
128 } 135 }
129 } 136 }
130 137
131 // TODO(vsm): Implement 138 // TODO(vsm): Implement
132 InstanceMirror getField(Symbol fieldName) => 139 InstanceMirror getField(Symbol fieldName) =>
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 throw new UnimplementedError("ClassMirror.owner unimplemented"); 176 throw new UnimplementedError("ClassMirror.owner unimplemented");
170 Symbol get qualifiedName => 177 Symbol get qualifiedName =>
171 throw new UnimplementedError("ClassMirror.qualifiedName unimplemented"); 178 throw new UnimplementedError("ClassMirror.qualifiedName unimplemented");
172 Type get reflectedType { return _cls; } 179 Type get reflectedType { return _cls; }
173 Map<Symbol, MethodMirror> get staticMembers => 180 Map<Symbol, MethodMirror> get staticMembers =>
174 throw new UnimplementedError("ClassMirror.staticMembers unimplemented"); 181 throw new UnimplementedError("ClassMirror.staticMembers unimplemented");
175 ClassMirror get superclass { 182 ClassMirror get superclass {
176 if (_cls == Object) { 183 if (_cls == Object) {
177 return null; 184 return null;
178 } else { 185 } else {
179 return new JsClassMirror._(JS('Type', '#.__proto__', _cls)); 186 return new JsClassMirror._(_wrap(JS('Type', '#.__proto__', _unwrap(_cls))) );
180 } 187 }
181 } 188 }
182 List<TypeMirror> get typeArguments => 189 List<TypeMirror> get typeArguments =>
183 throw new UnimplementedError("ClassMirror.typeArguments unimplemented"); 190 throw new UnimplementedError("ClassMirror.typeArguments unimplemented");
184 List<TypeVariableMirror> get typeVariables => 191 List<TypeVariableMirror> get typeVariables =>
185 throw new UnimplementedError("ClassMirror.typeVariables unimplemented"); 192 throw new UnimplementedError("ClassMirror.typeVariables unimplemented");
186 } 193 }
187 194
188 class JsTypeMirror implements TypeMirror { 195 class JsTypeMirror implements TypeMirror {
189 // TODO(vsm): Support original declarations, etc., where there is no actual 196 // TODO(vsm): Support original declarations, etc., where there is no actual
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 throw new UnimplementedError("ParameterMirror.simpleName unimplemented"); 268 throw new UnimplementedError("ParameterMirror.simpleName unimplemented");
262 } 269 }
263 270
264 class JsMethodMirror implements MethodMirror { 271 class JsMethodMirror implements MethodMirror {
265 final String _name; 272 final String _name;
266 final dynamic _method; 273 final dynamic _method;
267 List<ParameterMirror> _params; 274 List<ParameterMirror> _params;
268 275
269 JsMethodMirror._(JsClassMirror cls, this._method) 276 JsMethodMirror._(JsClassMirror cls, this._method)
270 : _name = getName(cls.simpleName) { 277 : _name = getName(cls.simpleName) {
271 var ftype = JS('', '#.classGetConstructorType(#)', _dart, cls._cls); 278 var ftype = JS('', '#.classGetConstructorType(#)', _dart, _unwrap(cls._cls)) ;
272 _params = _createParameterMirrorList(ftype); 279 _params = _createParameterMirrorList(ftype);
273 } 280 }
274 281
275 // TODO(vsm): Support named constructors. 282 // TODO(vsm): Support named constructors.
276 Symbol get constructorName => new Symbol(''); 283 Symbol get constructorName => new Symbol('');
277 List<ParameterMirror> get parameters => _params; 284 List<ParameterMirror> get parameters => _params;
278 285
279 List<ParameterMirror> _createParameterMirrorList(ftype) { 286 List<ParameterMirror> _createParameterMirrorList(ftype) {
280 if (ftype == null) { 287 if (ftype == null) {
281 // TODO(vsm): No explicit constructor. Verify this. 288 // TODO(vsm): No explicit constructor. Verify this.
282 return []; 289 return [];
283 } 290 }
284 291
285 // TODO(vsm): Add named args. 292 // TODO(vsm): Add named args.
286 List args = ftype.args; 293 List args = ftype.args;
287 List opts = ftype.optionals; 294 List opts = ftype.optionals;
288 var params = new List<ParameterMirror>(args.length + opts.length); 295 var params = new List<ParameterMirror>(args.length + opts.length);
289 296
290 for (var i = 0; i < args.length; ++i) { 297 for (var i = 0; i < args.length; ++i) {
291 var type = args[i]; 298 var type = args[i];
292 var metadata = ftype.metadata[i]; 299 var metadata = ftype.metadata[i];
293 // TODO(vsm): Recover the param name. 300 // TODO(vsm): Recover the param name.
294 var param = new JsParameterMirror._('', type, metadata); 301 var param = new JsParameterMirror._('', _wrap(type), metadata);
295 params[i] = param; 302 params[i] = param;
296 } 303 }
297 304
298 for (var i = 0; i < opts.length; ++i) { 305 for (var i = 0; i < opts.length; ++i) {
299 var type = opts[i]; 306 var type = opts[i];
300 var metadata = ftype.metadata[args.length + i]; 307 var metadata = ftype.metadata[args.length + i];
301 // TODO(vsm): Recover the param name. 308 // TODO(vsm): Recover the param name.
302 var param = new JsParameterMirror._('', type, metadata); 309 var param = new JsParameterMirror._('', _wrap(type), metadata);
303 params[i + args.length] = param; 310 params[i + args.length] = param;
304 } 311 }
305 312
306 return params; 313 return params;
307 } 314 }
308 315
309 // TODO(vsm): Implement 316 // TODO(vsm): Implement
310 bool get isAbstract => 317 bool get isAbstract =>
311 throw new UnimplementedError("MethodMirror.isAbstract unimplemented"); 318 throw new UnimplementedError("MethodMirror.isAbstract unimplemented");
312 bool get isConstConstructor => throw new UnimplementedError( 319 bool get isConstConstructor => throw new UnimplementedError(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 throw new UnimplementedError("MethodMirror.owner unimplemented"); 352 throw new UnimplementedError("MethodMirror.owner unimplemented");
346 Symbol get qualifiedName => 353 Symbol get qualifiedName =>
347 throw new UnimplementedError("MethodMirror.qualifiedName unimplemented"); 354 throw new UnimplementedError("MethodMirror.qualifiedName unimplemented");
348 TypeMirror get returnType => 355 TypeMirror get returnType =>
349 throw new UnimplementedError("MethodMirror.returnType unimplemented"); 356 throw new UnimplementedError("MethodMirror.returnType unimplemented");
350 Symbol get simpleName => 357 Symbol get simpleName =>
351 throw new UnimplementedError("MethodMirror.simpleName unimplemented"); 358 throw new UnimplementedError("MethodMirror.simpleName unimplemented");
352 String get source => 359 String get source =>
353 throw new UnimplementedError("MethodMirror.source unimplemented"); 360 throw new UnimplementedError("MethodMirror.source unimplemented");
354 } 361 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698