Chromium Code Reviews| 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 |
|
Jennifer Messerly
2015/06/15 16:42:01
2015?
vsm
2015/06/15 20:59:47
Done.
| |
| 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 getName(symbol) { | 7 import 'dart:collection'; |
| 8 throw new UnimplementedError("MirrorSystem.getName unimplemented"); | 8 import 'dart:mirrors'; |
| 9 import 'dart:_foreign_helper' show JS; | |
| 10 | |
| 11 // TODO(vsm): This a workaround for: | |
| 12 // https://github.com/dart-lang/dev_compiler/issues/219 | |
| 13 import 'dart:js'; | |
| 14 | |
| 15 String getName(Symbol symbol) { | |
| 16 return JS('String', '#.toString().substring(8, #.toString().length - 2)', symb ol, symbol); | |
|
Jennifer Messerly
2015/06/15 16:42:02
Not sure toString the best way to implement this?
vsm
2015/06/15 20:59:47
Done - thanks!
| |
| 9 } | 17 } |
| 10 | 18 |
| 11 getSymbol(name, library) { | 19 Symbol getSymbol(name, library) { |
| 12 throw new UnimplementedError("MirrorSystem.getSymbol unimplemented"); | 20 throw new UnimplementedError("MirrorSystem.getSymbol unimplemented"); |
| 13 } | 21 } |
| 14 | 22 |
| 15 final currentJsMirrorSystem = | 23 final currentJsMirrorSystem = |
| 16 throw new UnimplementedError( | 24 throw new UnimplementedError( |
| 17 "MirrorSystem.currentJsMirrorSystem unimplemented"); | 25 "MirrorSystem.currentJsMirrorSystem unimplemented"); |
| 18 | 26 |
| 19 | 27 |
| 20 reflect(reflectee) { | 28 InstanceMirror reflect(reflectee) { |
| 21 throw new UnimplementedError("MirrorSystem.reflect unimplemented"); | 29 return new JsInstanceMirror._(reflectee); |
|
Jennifer Messerly
2015/06/15 16:42:02
silly: could be => ?
vsm
2015/06/15 20:59:47
Done.
| |
| 22 } | 30 } |
| 23 | 31 |
| 24 reflectType(Type key) { | 32 TypeMirror reflectType(Type key) { |
| 25 throw new UnimplementedError("MirrorSystem.reflectType unimplemented"); | 33 // FIXME(vsm): Might not be a class. |
|
Jennifer Messerly
2015/06/15 16:42:02
TODO? or did you mean to fix before landing?
vsm
2015/06/15 20:59:46
I'll update the mirrors bug.
| |
| 34 return new JsClassMirror._(key); | |
| 26 } | 35 } |
| 36 | |
| 37 final dynamic _dart = JS('', 'dart'); | |
|
Jennifer Messerly
2015/06/15 16:42:02
Not sure this field is worth it? if we're hardcodi
vsm
2015/06/15 20:59:47
Leaf had been moving / hiding some of our runtime
Jennifer Messerly
2015/06/15 21:40:16
eventually, yeah, things will move ... but we'll h
| |
| 38 final _metadata = JS('', '#.metadata', _dart); | |
| 39 | |
| 40 dynamic _dload(obj, String name) { | |
|
Jennifer Messerly
2015/06/15 16:42:02
btw, if we wanted to make these APIs available sta
vsm
2015/06/15 20:59:47
thanks for filing!
| |
| 41 return JS('', '#.dload(#, #)', _dart, obj, name); | |
| 42 } | |
| 43 | |
| 44 void _dput(obj, String name, val) { | |
| 45 JS('', '#.dput(#, #, #)', _dart, obj, name, val); | |
| 46 } | |
| 47 | |
| 48 dynamic _dsend(obj, String name, List args) { | |
| 49 return JS('', '#.dsendArray(#, #, #)', _dart, obj, name, args); | |
| 50 } | |
| 51 | |
| 52 class JsInstanceMirror implements InstanceMirror { | |
| 53 Object _instance; | |
|
Jennifer Messerly
2015/06/15 16:42:01
final?
vsm
2015/06/15 20:59:46
Done.
| |
| 54 | |
| 55 JsInstanceMirror._(this._instance); | |
| 56 | |
| 57 Object get reflectee { | |
|
Jennifer Messerly
2015/06/15 16:42:02
This could just make `reflectee` be the final fiel
vsm
2015/06/15 20:59:47
Done.
| |
| 58 return this._instance; | |
| 59 } | |
| 60 | |
| 61 InstanceMirror getField(Symbol symbol) { | |
| 62 var name = getName(symbol); | |
| 63 var field = _dload(_instance, name); | |
| 64 return new JsInstanceMirror._(field); | |
| 65 } | |
| 66 | |
| 67 InstanceMirror setField(Symbol symbol, Object value) { | |
| 68 var name = getName(symbol); | |
| 69 var field = _dput(_instance, name, value); | |
| 70 return new JsInstanceMirror._(field); | |
| 71 } | |
| 72 | |
| 73 InstanceMirror invoke(Symbol symbol, List<dynamic> args, [Map<Symbol, dynamic> namedArgs]) { | |
|
Jennifer Messerly
2015/06/15 16:42:02
long line
vsm
2015/06/15 20:59:47
Done.
| |
| 74 var name = getName(symbol); | |
| 75 var result = _dsend(_instance, name, args); | |
|
Jennifer Messerly
2015/06/15 16:42:01
TODO: named args?
or maybe implement it:
if
vsm
2015/06/15 20:59:47
Code stolen! :-)
| |
| 76 return new JsInstanceMirror._(result); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 class JsClassMirror implements ClassMirror { | |
| 81 final Type _cls; | |
| 82 Symbol _simpleName; | |
|
Jennifer Messerly
2015/06/15 16:42:02
final Symbol simpleName?
and init like:
JsClassM
vsm
2015/06/15 20:59:47
Done.
| |
| 83 | |
| 84 List<InstanceMirror> _metadata; | |
|
Jennifer Messerly
2015/06/15 16:42:01
these could probably be final too, but would need
vsm
2015/06/15 20:59:47
Yes, I also need to make this immutable (see comme
| |
| 85 Map<Symbol, MethodMirror> _declarations; | |
| 86 | |
| 87 Symbol get simpleName => _simpleName; | |
| 88 | |
| 89 // These need to be immutable when escaping from this class. | |
| 90 List<InstanceMirror> get metadata => _metadata; | |
| 91 Map<Symbol, MethodMirror> get declarations => _declarations; | |
| 92 | |
| 93 JsClassMirror._(this._cls) { | |
| 94 _simpleName = new Symbol(JS('String', '#.name', _cls)); | |
| 95 | |
| 96 // Load metadata. | |
| 97 var fn = JS('List<InstanceMirror>', '#[dart.metadata]', _cls); | |
|
Jennifer Messerly
2015/06/15 16:42:02
fyi, I don't think we parse this. (but we should)
| |
| 98 _metadata = (fn == null) ? [] : fn().map((i) => new JsInstanceMirror._(i)); | |
|
Jennifer Messerly
2015/06/15 16:42:02
this looks like it's assigning Iterable<InstanceMi
vsm
2015/06/15 20:59:47
Good catch! - this is the problem with just emitti
| |
| 99 | |
| 100 // Load declarations. | |
| 101 // FIXME(vsm): This is only populating the default constructor right now. | |
| 102 _declarations = new Map<Symbol, MethodMirror>(); | |
| 103 _declarations[_simpleName] = new JsMethodMirror._(this, _cls); | |
| 104 } | |
| 105 | |
| 106 InstanceMirror newInstance(Symbol constructorName, List args, [Map<Symbol, dyn amic> namedArgs]) { | |
|
Jennifer Messerly
2015/06/15 16:42:01
long line
vsm
2015/06/15 20:59:47
Done.
| |
| 107 // FIXME(vsm): Support named constructors and named arguments. | |
| 108 assert(getName(constructorName) == ""); | |
| 109 assert(namedArgs == null || namedArgs.isEmpty); | |
| 110 var instance = JS('', '#.instantiate(#, #)', _dart, _cls, args); | |
| 111 return new JsInstanceMirror._(instance); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 class JsTypeMirror implements TypeMirror { | |
| 116 final Type reflectedType; | |
| 117 | |
| 118 JsTypeMirror._(this.reflectedType); | |
| 119 } | |
| 120 | |
| 121 class JsParameterMirror implements ParameterMirror { | |
| 122 final String _name; | |
| 123 final TypeMirror type; | |
| 124 final List<InstanceMirror> metadata = []; | |
| 125 | |
| 126 JsParameterMirror._(this._name, Type t) : type = new JsTypeMirror._(t); | |
| 127 } | |
| 128 | |
| 129 class JsMethodMirror implements MethodMirror { | |
| 130 final String _name; | |
| 131 final dynamic _method; | |
| 132 List<ParameterMirror> _params; | |
| 133 | |
| 134 JsMethodMirror._(JsClassMirror cls, this._method) : _name = getName(cls._simpl eName) { | |
|
Jennifer Messerly
2015/06/15 16:42:01
long line
vsm
2015/06/15 20:59:46
Done.
| |
| 135 var ftype = JS('', '#.classGetConstructorType(#)', _dart, cls._cls); | |
| 136 _params = _createParameterMirrorList(ftype); | |
| 137 } | |
| 138 | |
| 139 // TODO(vsm): Support named constructors. | |
| 140 Symbol get constructorName => new Symbol(''); | |
| 141 List<ParameterMirror> get parameters => _params; | |
| 142 | |
| 143 List<ParameterMirror> _createParameterMirrorList(ftype) { | |
| 144 if (ftype == null) { | |
| 145 // TODO(vsm): No explicit constructor. Verify this. | |
| 146 return []; | |
| 147 } | |
| 148 | |
| 149 // TODO(vsm): Add named args. | |
| 150 List args = ftype.args; | |
| 151 List opts = ftype.optionals; | |
| 152 var params = new List<ParameterMirror>(args.length + opts.length); | |
| 153 | |
| 154 for (var i = 0; i < args.length; ++i) { | |
| 155 var type = args[i]; | |
| 156 // TODO(vsm): Recover the param name. | |
| 157 var param = new JsParameterMirror._('', type); | |
| 158 params[i] = param; | |
| 159 } | |
| 160 | |
| 161 for (var i = 0; i < opts.length; ++i) { | |
| 162 var type = opts[i]; | |
| 163 // TODO(vsm): Recover the param name. | |
| 164 var param = new JsParameterMirror._('', type); | |
| 165 params[i + args.length] = param; | |
| 166 } | |
| 167 | |
| 168 return params; | |
| 169 } | |
| 170 } | |
| OLD | NEW |