Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(361)

Unified Diff: sdk/lib/_internal/lib/js_mirrors.dart

Issue 417693002: Support optional (non-named) arguments in constructors for reflection. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/lib/lib.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/lib/js_mirrors.dart
diff --git a/sdk/lib/_internal/lib/js_mirrors.dart b/sdk/lib/_internal/lib/js_mirrors.dart
index b4a250d4a28c02cd7c7a341898906344f1f648e5..34ecb6f49fcd8c889b08179c88f153ae7b4fae22 100644
--- a/sdk/lib/_internal/lib/js_mirrors.dart
+++ b/sdk/lib/_internal/lib/js_mirrors.dart
@@ -2168,6 +2168,7 @@ function(reflectee) {
if (callName == null) {
throw new RuntimeError('Cannot find callName on "$reflectee"');
}
+ // TODO(floitsch): What about optional parameters?
int parameterCount = int.parse(callName.split(r'$')[1]);
if (reflectee is BoundClosure) {
var target = BoundClosure.targetOf(reflectee);
@@ -2181,8 +2182,9 @@ function(reflectee) {
} else {
bool isStatic = true; // TODO(ahe): Compute isStatic correctly.
var jsFunction = JS('', '#[#]', reflectee, callName);
+ var dummyOptionalParameterCount = 0;
cachedFunction = new JsMethodMirror(
- s(callName), jsFunction, parameterCount,
+ s(callName), jsFunction, parameterCount, dummyOptionalParameterCount,
false, false, isStatic, false, false);
}
JS('void', r'#.constructor[#] = #', reflectee, cacheName, cachedFunction);
@@ -2203,7 +2205,8 @@ function(reflectee) {
class JsMethodMirror extends JsDeclarationMirror implements MethodMirror {
final _jsFunction;
- final int _parameterCount;
+ final int _requiredParameterCount;
+ final int _optionalParameterCount;
final bool isGetter;
final bool isSetter;
final bool isStatic;
@@ -2216,7 +2219,8 @@ class JsMethodMirror extends JsDeclarationMirror implements MethodMirror {
JsMethodMirror(Symbol simpleName,
this._jsFunction,
- this._parameterCount,
+ this._requiredParameterCount,
+ this._optionalParameterCount,
this.isGetter,
this.isSetter,
this.isStatic,
@@ -2247,12 +2251,14 @@ class JsMethodMirror extends JsDeclarationMirror implements MethodMirror {
optionalParameterCount = int.parse(info[2]);
}
return new JsMethodMirror(
- s(name), jsFunction, requiredParameterCount + optionalParameterCount,
+ s(name), jsFunction, requiredParameterCount, optionalParameterCount,
isGetter, isSetter, isStatic, isConstructor, isOperator);
}
String get _prettyName => 'MethodMirror';
+ int get _parameterCount => _requiredParameterCount + _optionalParameterCount;
+
List<ParameterMirror> get parameters {
if (_parameters != null) return _parameters;
metadata; // Compute _parameters as a side-effect of extracting metadata.
@@ -2339,11 +2345,23 @@ class JsMethodMirror extends JsDeclarationMirror implements MethodMirror {
if (!isStatic && !isConstructor) {
throw new RuntimeError('Cannot invoke instance method without receiver.');
}
- if (_parameterCount != positionalArguments.length || _jsFunction == null) {
+ int positionalLength = positionalArguments.length;
+ if (positionalLength < _requiredParameterCount ||
+ positionalLength > _parameterCount ||
+ _jsFunction == null) {
// TODO(ahe): What receiver to use?
throw new NoSuchMethodError(
owner, simpleName, positionalArguments, namedArguments);
}
+ if (positionalLength < _parameterCount) {
+ // Fill up with default values.
+ // Make a copy so we don't modify the input.
+ positionalArguments = positionalArguments.toList();
+ for (int i = positionalLength; i < parameters.length; i++) {
+ JsParameterMirror parameter = parameters[i];
+ positionalArguments.add(parameter.defaultValue.reflectee);
+ }
+ }
// Using JS_CURRENT_ISOLATE() ('$') here is actually correct, although
// _jsFunction may not be a property of '$', most static functions do not
// care who their receiver is. But to lazy getters, it is important that
« no previous file with comments | « no previous file | tests/lib/lib.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698