| 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 // These values are allowed to be passed directly over the wire. | 7 // These values are allowed to be passed directly over the wire. |
| 8 bool isSimpleValue(var value) { | 8 bool isSimpleValue(var value) { |
| 9 return (value === null || value is num || value is String || value is bool); | 9 return (value === null || value is num || value is String || value is bool); |
| 10 } | 10 } |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 98 |
| 99 Future<InstanceMirror> invoke(String memberName, | 99 Future<InstanceMirror> invoke(String memberName, |
| 100 List positionalArguments, | 100 List positionalArguments, |
| 101 [Map<String,Dynamic> namedArguments]) { | 101 [Map<String,Dynamic> namedArguments]) { |
| 102 if (namedArguments !== null) { | 102 if (namedArguments !== null) { |
| 103 throw new NotImplementedException('named arguments not implemented'); | 103 throw new NotImplementedException('named arguments not implemented'); |
| 104 } | 104 } |
| 105 // Walk the arguments and make sure they are legal. | 105 // Walk the arguments and make sure they are legal. |
| 106 for (int i = 0; i < positionalArguments.length; i++) { | 106 for (int i = 0; i < positionalArguments.length; i++) { |
| 107 var arg = positionalArguments[i]; | 107 var arg = positionalArguments[i]; |
| 108 if (arg is Mirror) { | 108 _validateArgument(i, arg); |
| 109 if (arg is! InstanceMirror) { | |
| 110 throw new MirrorException( | |
| 111 'positional argument $i ($arg) was not an InstanceMirror'); | |
| 112 } | |
| 113 } else if (!isSimpleValue(arg)) { | |
| 114 throw new MirrorException( | |
| 115 'positional argument $i ($arg) was not a simple value'); | |
| 116 } | |
| 117 } | 109 } |
| 118 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); | 110 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); |
| 119 try { | 111 try { |
| 120 completer.complete( | 112 completer.complete( |
| 121 _invoke(this, memberName, positionalArguments)); | 113 _invoke(this, memberName, positionalArguments)); |
| 122 } catch (var exception) { | 114 } catch (var exception) { |
| 123 completer.completeException(exception); | 115 completer.completeException(exception); |
| 124 } | 116 } |
| 125 return completer.future; | 117 return completer.future; |
| 126 } | 118 } |
| 127 | 119 |
| 120 Future<InstanceMirror> getField(String fieldName) |
| 121 { |
| 122 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); |
| 123 try { |
| 124 completer.complete(_getField(this, fieldName)); |
| 125 } catch (var exception) { |
| 126 completer.completeException(exception); |
| 127 } |
| 128 return completer.future; |
| 129 } |
| 130 |
| 131 Future<InstanceMirror> setField(String fieldName, Object arg) |
| 132 { |
| 133 _validateArgument(0, arg); |
| 134 |
| 135 Completer<InstanceMirror> completer = new Completer<InstanceMirror>(); |
| 136 try { |
| 137 completer.complete(_setField(this, fieldName, arg)); |
| 138 } catch (var exception) { |
| 139 completer.completeException(exception); |
| 140 } |
| 141 return completer.future; |
| 142 } |
| 143 |
| 144 static _validateArgument(int i, Object arg) |
| 145 { |
| 146 if (arg is Mirror) { |
| 147 if (arg is! InstanceMirror) { |
| 148 throw new MirrorException( |
| 149 'positional argument $i ($arg) was not an InstanceMirror'); |
| 150 } |
| 151 } else if (!isSimpleValue(arg)) { |
| 152 throw new MirrorException( |
| 153 'positional argument $i ($arg) was not a simple value'); |
| 154 } |
| 155 } |
| 156 |
| 128 static _invoke(ref, memberName, positionalArguments) | 157 static _invoke(ref, memberName, positionalArguments) |
| 129 native 'LocalObjectMirrorImpl_invoke'; | 158 native 'LocalObjectMirrorImpl_invoke'; |
| 159 |
| 160 static _getField(ref, fieldName) |
| 161 native 'LocalObjectMirrorImpl_getField'; |
| 162 |
| 163 static _setField(ref, fieldName, value) |
| 164 native 'LocalObjectMirrorImpl_setField'; |
| 130 } | 165 } |
| 131 | 166 |
| 132 // Prints a string as it might appear in dart program text. | 167 // Prints a string as it might appear in dart program text. |
| 133 // TODO(turnidge): Consider truncating. | 168 // TODO(turnidge): Consider truncating. |
| 134 String _dartEscape(String str) { | 169 String _dartEscape(String str) { |
| 135 bool isNice(int code) { | 170 bool isNice(int code) { |
| 136 return (code >= 32 && code <= 126); | 171 return (code >= 32 && code <= 126); |
| 137 } | 172 } |
| 138 | 173 |
| 139 StringBuffer buf = new StringBuffer(); | 174 StringBuffer buf = new StringBuffer(); |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 | 532 |
| 498 // Creates a new local InstanceMirror | 533 // Creates a new local InstanceMirror |
| 499 static InstanceMirror makeLocalInstanceMirror(Object reflectee) | 534 static InstanceMirror makeLocalInstanceMirror(Object reflectee) |
| 500 native 'Mirrors_makeLocalInstanceMirror'; | 535 native 'Mirrors_makeLocalInstanceMirror'; |
| 501 | 536 |
| 502 // Creates a new local mirror for some Object. | 537 // Creates a new local mirror for some Object. |
| 503 static InstanceMirror mirrorOf(Object reflectee) { | 538 static InstanceMirror mirrorOf(Object reflectee) { |
| 504 return makeLocalInstanceMirror(reflectee); | 539 return makeLocalInstanceMirror(reflectee); |
| 505 } | 540 } |
| 506 } | 541 } |
| OLD | NEW |