| 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 class _InvocationMirror implements InvocationMirror { | 5 class _InvocationMirror implements InvocationMirror { |
| 6 static final int METHOD = 0; | 6 // Constants describing the invocation type. |
| 7 static final int GETTER = 1; | 7 static final int _METHOD = 0; |
| 8 static final int SETTER = 2; | 8 static final int _GETTER = 1; |
| 9 static final int _SETTER = 2; |
| 9 | 10 |
| 10 // TODO(regis): Compute lazily the value of these fields, and save the | 11 // Internal representation of the invocation mirror. |
| 11 // arguments passed into _allocateInvocationMirror. | 12 final String _functionName; |
| 13 final List _argumentsDescriptor; |
| 14 final List _arguments; |
| 12 | 15 |
| 13 final String memberName; | 16 // External representation of the invocation mirror; populated on demand. |
| 14 final List positionalArguments; | 17 String _memberName; |
| 15 final Map<String, dynamic> namedArguments; | 18 int _type; |
| 19 List _positionalArguments; |
| 20 Map<String, dynamic> _namedArguments; |
| 16 | 21 |
| 17 final int _type; | 22 void _setMemberNameAndType() { |
| 23 if (_functionName.startsWith("get:")) { |
| 24 _type = _GETTER; |
| 25 _memberName = _functionName.substring(4); |
| 26 } else if (_functionName.startsWith("set:")) { |
| 27 _type = _SETTER; |
| 28 _memberName = _functionName.substring(4).concat("="); |
| 29 } else { |
| 30 _type = _METHOD; |
| 31 _memberName = _functionName; |
| 32 } |
| 33 } |
| 18 | 34 |
| 19 _InvocationMirror(this.memberName, | 35 String get memberName { |
| 20 this._type, | 36 if (_memberName == null) { |
| 21 this.positionalArguments, | 37 _setMemberNameAndType(); |
| 22 this.namedArguments); | 38 } |
| 39 return _memberName; |
| 40 } |
| 23 | 41 |
| 24 static _allocateInvocationMirror(String name, | 42 List get positionalArguments { |
| 43 if (_positionalArguments == null) { |
| 44 // Exclude receiver. |
| 45 int numPositionalArguments = _argumentsDescriptor[1] - 1; |
| 46 _positionalArguments = _arguments.getRange(1, numPositionalArguments); |
| 47 } |
| 48 return _positionalArguments; |
| 49 } |
| 50 |
| 51 Map<String, dynamic> get namedArguments { |
| 52 if (_namedArguments == null) { |
| 53 _namedArguments = new Map<String, dynamic>(); |
| 54 int numArguments = _argumentsDescriptor[0] - 1; // Exclude receiver. |
| 55 int numPositionalArguments = _argumentsDescriptor[1] - 1; |
| 56 int numNamedArguments = numArguments - numPositionalArguments; |
| 57 for (int i = 0; i < numNamedArguments; i++) { |
| 58 String arg_name = _argumentsDescriptor[2 + 2*i]; |
| 59 var arg_value = _arguments[_argumentsDescriptor[3 + 2*i]]; |
| 60 _namedArguments[arg_name] = arg_value; |
| 61 } |
| 62 } |
| 63 return _namedArguments; |
| 64 } |
| 65 |
| 66 bool get isMethod { |
| 67 if (_memberName == null) { |
| 68 _setMemberNameAndType(); |
| 69 } |
| 70 return _type == _METHOD; |
| 71 } |
| 72 |
| 73 bool get isAccessor { |
| 74 if (_memberName == null) { |
| 75 _setMemberNameAndType(); |
| 76 } |
| 77 return _type != _METHOD; |
| 78 } |
| 79 |
| 80 bool get isGetter { |
| 81 if (_memberName == null) { |
| 82 _setMemberNameAndType(); |
| 83 } |
| 84 return _type == _GETTER; |
| 85 } |
| 86 |
| 87 bool get isSetter { |
| 88 if (_memberName == null) { |
| 89 _setMemberNameAndType(); |
| 90 } |
| 91 return _type == _SETTER; |
| 92 } |
| 93 |
| 94 _InvocationMirror(this._functionName, |
| 95 this._argumentsDescriptor, |
| 96 this._arguments); |
| 97 |
| 98 static _allocateInvocationMirror(String functionName, |
| 25 List argumentsDescriptor, | 99 List argumentsDescriptor, |
| 26 List arguments) { | 100 List arguments) { |
| 27 var memberName; | 101 return new _InvocationMirror(functionName, argumentsDescriptor, arguments); |
| 28 var type; | |
| 29 if (name.startsWith("get:")) { | |
| 30 type = GETTER; | |
| 31 memberName = name.substring(4); | |
| 32 } else if (name.startsWith("set:")) { | |
| 33 type = SETTER; | |
| 34 memberName = name.substring(4).concat("="); | |
| 35 } else { | |
| 36 type = METHOD; | |
| 37 memberName = name; | |
| 38 } | |
| 39 // Exclude receiver. | |
| 40 int numArguments = argumentsDescriptor[0] - 1; | |
| 41 int numPositionalArguments = argumentsDescriptor[1] - 1; | |
| 42 int numNamedArguments = numArguments - numPositionalArguments; | |
| 43 List positionalArguments = arguments.getRange(1, numPositionalArguments); | |
| 44 Map<String, dynamic> namedArguments; | |
| 45 if (numNamedArguments > 0) { | |
| 46 namedArguments = new Map<String, dynamic>(); | |
| 47 for (int i = 0; i < numNamedArguments; i++) { | |
| 48 String arg_name = argumentsDescriptor[2 + 2*i]; | |
| 49 var arg_value = arguments[argumentsDescriptor[3 + 2*i]]; | |
| 50 namedArguments[arg_name] = arg_value; | |
| 51 } | |
| 52 } | |
| 53 return new _InvocationMirror(memberName, type, | |
| 54 positionalArguments, namedArguments); | |
| 55 } | 102 } |
| 56 | 103 |
| 57 bool get isMethod => _type == METHOD; | 104 static _invoke(Object receiver, |
| 58 bool get isAccessor => _type != METHOD; | 105 String functionName, |
| 59 bool get isGetter => _type == GETTER; | 106 List argumentsDescriptor, |
| 60 bool get isSetter => _type == SETTER; | 107 List arguments) |
| 108 native "InvocationMirror_invoke"; |
| 61 | 109 |
| 62 invokeOn(Object receiver) { | 110 invokeOn(Object receiver) { |
| 63 throw new UnsupportedError("invokeOn not implemented yet"); | 111 _invoke(receiver, _functionName, _argumentsDescriptor, _arguments); |
| 64 } | 112 } |
| 65 } | 113 } |
| 66 | 114 |
| OLD | NEW |