| 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 // VM-specific implementation of the dart:mirrors library. | 5 // VM-specific implementation of the dart:mirrors library. |
| 6 | 6 |
| 7 import "dart:collection"; | 7 import "dart:collection"; |
| 8 | 8 |
| 9 final emptyList = new UnmodifiableListView([]); | 9 final emptyList = new UnmodifiableListView([]); |
| 10 final emptyMap = new _UnmodifiableMapView({}); | 10 final emptyMap = new _UnmodifiableMapView({}); |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 MethodMirror _function; | 394 MethodMirror _function; |
| 395 MethodMirror get function { | 395 MethodMirror get function { |
| 396 if (_function == null) { | 396 if (_function == null) { |
| 397 _function = _computeFunction(reflectee); | 397 _function = _computeFunction(reflectee); |
| 398 } | 398 } |
| 399 return _function; | 399 return _function; |
| 400 } | 400 } |
| 401 | 401 |
| 402 InstanceMirror apply(List<Object> positionalArguments, | 402 InstanceMirror apply(List<Object> positionalArguments, |
| 403 [Map<Symbol, Object> namedArguments]) { | 403 [Map<Symbol, Object> namedArguments]) { |
| 404 // TODO(iposva): When closures get an ordinary call method, this can be | 404 // TODO(12602): When closures get an ordinary call method, this can be |
| 405 // replaced with | 405 // replaced with |
| 406 // return this.invoke(#call, positionalArguments, namedArguments); | 406 // return this.invoke(#call, positionalArguments, namedArguments); |
| 407 // and the native ClosureMirror_apply can be removed. | 407 // and the native ClosureMirror_apply can be removed. |
| 408 int numPositionalArguments = positionalArguments.length + 1; // Receiver. | 408 int numPositionalArguments = positionalArguments.length + 1; // Receiver. |
| 409 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; | 409 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; |
| 410 int numArguments = numPositionalArguments + numNamedArguments; | 410 int numArguments = numPositionalArguments + numNamedArguments; |
| 411 List arguments = new List(numArguments); | 411 List arguments = new List(numArguments); |
| 412 arguments[0] = _reflectee; // Receiver. | 412 arguments[0] = _reflectee; // Receiver. |
| 413 arguments.setRange(1, numPositionalArguments, positionalArguments); | 413 arguments.setRange(1, numPositionalArguments, positionalArguments); |
| 414 List names = new List(numNamedArguments); | 414 List names = new List(numNamedArguments); |
| 415 int argumentIndex = numPositionalArguments; | 415 int argumentIndex = numPositionalArguments; |
| 416 int nameIndex = 0; | 416 int nameIndex = 0; |
| 417 if (numNamedArguments > 0) { | 417 if (numNamedArguments > 0) { |
| 418 namedArguments.forEach((name, value) { | 418 namedArguments.forEach((name, value) { |
| 419 arguments[argumentIndex++] = value; | 419 arguments[argumentIndex++] = value; |
| 420 names[nameIndex++] = _n(name); | 420 names[nameIndex++] = _n(name); |
| 421 }); | 421 }); |
| 422 } | 422 } |
| 423 | 423 |
| 424 // It is tempting to implement this in terms of Function.apply, but then | 424 // It is tempting to implement this in terms of Function.apply, but then |
| 425 // lazy compilation errors would be fatal. | 425 // lazy compilation errors would be fatal. |
| 426 return reflect(_apply(_reflectee, arguments, names)); | 426 return reflect(_apply(arguments, names)); |
| 427 } | 427 } |
| 428 | 428 |
| 429 Future<InstanceMirror> applyAsync(List positionalArguments, | 429 Future<InstanceMirror> applyAsync(List positionalArguments, |
| 430 [Map<Symbol, dynamic> namedArguments]) { | 430 [Map<Symbol, dynamic> namedArguments]) { |
| 431 return new Future(() { | 431 return new Future(() { |
| 432 return this.apply(_unwrapAsyncPositionals(positionalArguments), | 432 return this.apply(_unwrapAsyncPositionals(positionalArguments), |
| 433 _unwrapAsyncNamed(namedArguments)); | 433 _unwrapAsyncNamed(namedArguments)); |
| 434 }); | 434 }); |
| 435 } | 435 } |
| 436 | 436 |
| 437 InstanceMirror findInContext(Symbol name, {ifAbsent: null}) { | 437 InstanceMirror findInContext(Symbol name, {ifAbsent: null}) { |
| 438 List<String> parts = _n(name).split(".").toList(growable: false); | 438 List<String> parts = _n(name).split(".").toList(growable: false); |
| 439 if (parts.length > 3) { | 439 if (parts.length > 3) { |
| 440 throw new ArgumentError("Invalid symbol: ${name}"); | 440 throw new ArgumentError("Invalid symbol: ${name}"); |
| 441 } | 441 } |
| 442 List tuple = _computeFindInContext(_reflectee, parts); | 442 List tuple = _computeFindInContext(_reflectee, parts); |
| 443 if (tuple[0]) { | 443 if (tuple[0]) { |
| 444 return reflect(tuple[1]); | 444 return reflect(tuple[1]); |
| 445 } | 445 } |
| 446 return ifAbsent == null ? null : ifAbsent(); | 446 return ifAbsent == null ? null : ifAbsent(); |
| 447 } | 447 } |
| 448 | 448 |
| 449 String toString() => "ClosureMirror on '${Error.safeToString(_reflectee)}'"; | 449 String toString() => "ClosureMirror on '${Error.safeToString(_reflectee)}'"; |
| 450 | 450 |
| 451 static _apply(reflectee, arguments, argumentNames) | 451 static _apply(arguments, argumentNames) |
| 452 native 'ClosureMirror_apply'; | 452 native 'ClosureMirror_apply'; |
| 453 | 453 |
| 454 static _computeFunction(reflectee) | 454 static _computeFunction(reflectee) |
| 455 native 'ClosureMirror_function'; | 455 native 'ClosureMirror_function'; |
| 456 | 456 |
| 457 static _computeFindInContext(reflectee, name) | 457 static _computeFindInContext(reflectee, name) |
| 458 native 'ClosureMirror_find_in_context'; | 458 native 'ClosureMirror_find_in_context'; |
| 459 } | 459 } |
| 460 | 460 |
| 461 class _LocalClassMirrorImpl extends _LocalObjectMirrorImpl | 461 class _LocalClassMirrorImpl extends _LocalObjectMirrorImpl |
| (...skipping 1064 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1526 if (typeMirror == null) { | 1526 if (typeMirror == null) { |
| 1527 typeMirror = makeLocalTypeMirror(key); | 1527 typeMirror = makeLocalTypeMirror(key); |
| 1528 _instanitationCache[key] = typeMirror; | 1528 _instanitationCache[key] = typeMirror; |
| 1529 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1529 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
| 1530 _declarationCache[key] = typeMirror; | 1530 _declarationCache[key] = typeMirror; |
| 1531 } | 1531 } |
| 1532 } | 1532 } |
| 1533 return typeMirror; | 1533 return typeMirror; |
| 1534 } | 1534 } |
| 1535 } | 1535 } |
| OLD | NEW |