OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Patch library for dart:mirrors. | 5 // Patch library for dart:mirrors. |
6 | 6 |
7 import 'dart:_foreign_helper' show JS; | 7 import 'dart:_foreign_helper' show JS, JS_CURRENT_ISOLATE; |
8 import 'dart:_collection-dev' as _symbol_dev; | 8 import 'dart:_collection-dev' as _symbol_dev; |
9 import 'dart:_js_helper' show createInvocationMirror; | 9 import 'dart:_js_helper' show createInvocationMirror; |
10 import 'dart:_interceptors' show Interceptor; | 10 import 'dart:_interceptors' show Interceptor; |
11 | 11 |
12 patch class MirrorSystem { | 12 patch class MirrorSystem { |
13 patch static String getName(Symbol symbol) => _n(symbol); | 13 patch static String getName(Symbol symbol) => _n(symbol); |
14 } | 14 } |
15 | 15 |
16 class _MirrorSystem implements MirrorSystem { | 16 class _MirrorSystem implements MirrorSystem { |
17 TypeMirror get dynamicType => _dynamicType; | 17 TypeMirror get dynamicType => _dynamicType; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 Symbol symbol = _s(_classes[i]); | 65 Symbol symbol = _s(_classes[i]); |
66 _ClassMirror cls = _reflectClass(symbol, _classes[i + 1]); | 66 _ClassMirror cls = _reflectClass(symbol, _classes[i + 1]); |
67 result[symbol] = cls; | 67 result[symbol] = cls; |
68 cls._owner = this; | 68 cls._owner = this; |
69 } | 69 } |
70 return result; | 70 return result; |
71 } | 71 } |
72 | 72 |
73 InstanceMirror setField(Symbol fieldName, Object arg) { | 73 InstanceMirror setField(Symbol fieldName, Object arg) { |
74 // TODO(ahe): This is extremely dangerous!!! | 74 // TODO(ahe): This is extremely dangerous!!! |
75 JS('void', r'$[#] = #', _n(fieldName), arg); | 75 JS('void', '#[#] = #', JS_CURRENT_ISOLATE(), _n(fieldName), arg); |
76 return _reflect(arg); | 76 return _reflect(arg); |
77 } | 77 } |
78 | 78 |
79 InstanceMirror getField(Symbol fieldName) { | 79 InstanceMirror getField(Symbol fieldName) { |
80 // TODO(ahe): This is extremely dangerous!!! | 80 // TODO(ahe): This is extremely dangerous!!! |
81 return _reflect(JS('', r'$[#]', _n(fieldName))); | 81 return _reflect(JS('', '#[#]', JS_CURRENT_ISOLATE(), _n(fieldName))); |
82 } | 82 } |
83 | 83 |
84 Map<Symbol, MethodMirror> get functions { | 84 Map<Symbol, MethodMirror> get functions { |
85 var result = new Map<Symbol, MethodMirror>(); | 85 var result = new Map<Symbol, MethodMirror>(); |
86 for (int i = 0; i < _functions.length; i++) { | 86 for (int i = 0; i < _functions.length; i++) { |
87 String name = _functions[i]; | 87 String name = _functions[i]; |
88 Symbol symbol = _s(name); | 88 Symbol symbol = _s(name); |
89 int parameterCount = null; // TODO(ahe): Compute this. | 89 int parameterCount = null; // TODO(ahe): Compute this. |
90 _MethodMirror mirror = | 90 _MethodMirror mirror = |
91 // TODO(ahe): Create accessor for accessing $. It is also | 91 // TODO(ahe): Create accessor for accessing $. It is also |
92 // used in js_helper. | 92 // used in js_helper. |
93 new _MethodMirror(symbol, JS('', r'$[#]', name), parameterCount); | 93 new _MethodMirror( |
| 94 symbol, JS('', '#[#]', JS_CURRENT_ISOLATE(), name), parameterCount
); |
94 // TODO(ahe): Cache mirrors. | 95 // TODO(ahe): Cache mirrors. |
95 result[symbol] = mirror; | 96 result[symbol] = mirror; |
96 mirror._owner = this; | 97 mirror._owner = this; |
97 } | 98 } |
98 return result; | 99 return result; |
99 } | 100 } |
100 | 101 |
101 Map<Symbol, MethodMirror> get getters { | 102 Map<Symbol, MethodMirror> get getters { |
102 var result = new Map<Symbol, MethodMirror>(); | 103 var result = new Map<Symbol, MethodMirror>(); |
103 // TODO(ahe): Implement this. | 104 // TODO(ahe): Implement this. |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 result[key] = value; | 300 result[key] = value; |
300 } | 301 } |
301 getters.forEach(addToResult); | 302 getters.forEach(addToResult); |
302 setters.forEach(addToResult); | 303 setters.forEach(addToResult); |
303 variables.forEach(addToResult); | 304 variables.forEach(addToResult); |
304 return result; | 305 return result; |
305 } | 306 } |
306 | 307 |
307 InstanceMirror setField(Symbol fieldName, Object arg) { | 308 InstanceMirror setField(Symbol fieldName, Object arg) { |
308 // TODO(ahe): This is extremely dangerous!!! | 309 // TODO(ahe): This is extremely dangerous!!! |
309 JS('void', r'$[#] = #', '${_n(simpleName)}_${_n(fieldName)}', arg); | 310 JS('void', '#[#] = #', JS_CURRENT_ISOLATE(), |
| 311 '${_n(simpleName)}_${_n(fieldName)}', arg); |
310 return _reflect(arg); | 312 return _reflect(arg); |
311 } | 313 } |
312 | 314 |
313 InstanceMirror getField(Symbol fieldName) { | 315 InstanceMirror getField(Symbol fieldName) { |
314 // TODO(ahe): This is extremely dangerous!!! | 316 // TODO(ahe): This is extremely dangerous!!! |
315 return _reflect( | 317 return _reflect( |
316 JS('', r'$[#]', '${_n(simpleName)}_${_n(fieldName)}')); | 318 JS('', '#[#]', JS_CURRENT_ISOLATE(), |
| 319 '${_n(simpleName)}_${_n(fieldName)}')); |
317 } | 320 } |
318 | 321 |
319 InstanceMirror newInstance(Symbol constructorName, | 322 InstanceMirror newInstance(Symbol constructorName, |
320 List positionalArguments, | 323 List positionalArguments, |
321 [Map<Symbol,dynamic> namedArguments]) { | 324 [Map<Symbol,dynamic> namedArguments]) { |
322 if (namedArguments != null && !namedArguments.isEmpty) { | 325 if (namedArguments != null && !namedArguments.isEmpty) { |
323 throw new UnsupportedError('Named arguments are not implemented'); | 326 throw new UnsupportedError('Named arguments are not implemented'); |
324 } | 327 } |
325 String constructorName = '${_n(simpleName)}\$${_n(constructorName)}'; | 328 String constructorName = '${_n(simpleName)}\$${_n(constructorName)}'; |
326 return _reflect(JS('', r'$[#].apply($, #)', constructorName, | 329 return _reflect(JS('', r'#[#].apply(#, #)', JS_CURRENT_ISOLATE(), |
| 330 constructorName, |
| 331 JS_CURRENT_ISOLATE(), |
327 new List.from(positionalArguments))); | 332 new List.from(positionalArguments))); |
328 } | 333 } |
329 | 334 |
330 Future<InstanceMirror> newInstanceAsync( | 335 Future<InstanceMirror> newInstanceAsync( |
331 Symbol constructorName, | 336 Symbol constructorName, |
332 List positionalArguments, | 337 List positionalArguments, |
333 [Map<Symbol, dynamic> namedArguments]) { | 338 [Map<Symbol, dynamic> namedArguments]) { |
334 if (namedArguments != null && !namedArguments.isEmpty) { | 339 if (namedArguments != null && !namedArguments.isEmpty) { |
335 throw new UnsupportedError('Named arguments are not implemented'); | 340 throw new UnsupportedError('Named arguments are not implemented'); |
336 } | 341 } |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 | 462 |
458 Symbol get qualifiedName => _computeQualifiedName(owner, simpleName); | 463 Symbol get qualifiedName => _computeQualifiedName(owner, simpleName); |
459 } | 464 } |
460 | 465 |
461 Symbol _computeQualifiedName(DeclarationMirror owner, Symbol simpleName) { | 466 Symbol _computeQualifiedName(DeclarationMirror owner, Symbol simpleName) { |
462 if (owner == null) return simpleName; | 467 if (owner == null) return simpleName; |
463 String ownerName = _n(owner.qualifiedName); | 468 String ownerName = _n(owner.qualifiedName); |
464 if (ownerName == '') return simpleName; | 469 if (ownerName == '') return simpleName; |
465 return _s('$ownerName.${_n(simpleName)}'); | 470 return _s('$ownerName.${_n(simpleName)}'); |
466 } | 471 } |
OLD | NEW |