Skip to content

Commit 6dc48ab

Browse files
vsmenoncommit-bot@chromium.org
authored andcommitted
[dartdevc] Destructure optional positional parameters
Use ES6 destructuring to handle optional positional arguments more compactly. Note, this doesn't actually change calling convention in DDC, just how optionals are handled at the callee. Given Dart: void foo(arg1, arg2, [opt1, opt2 = def2]) { ... } old JS: function foo(arg1, arg2, opt1, opt2) { if (opt1 === void 0) opt1 = null; if (opt2 === void 0) opt2 = 42; ... } new JS: function foo(arg1, arg2, opt1 = null, opt2 = def2) { ... } We should be able to similar with named params. Change-Id: I8491a4517d729ab1dec40c1ed2073b9c33cdffe0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/124107 Reviewed-by: Mark Zhou <[email protected]> Commit-Queue: Vijay Menon <[email protected]>
1 parent 53bbe6c commit 6dc48ab

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
814814
for (var ctor in superclass.constructors) {
815815
var savedUri = _currentUri;
816816
_currentUri = ctor.enclosingClass.fileUri;
817-
var jsParams = _emitParameters(ctor.function);
817+
var jsParams = _emitParameters(ctor.function, isForwarding: true);
818818
_currentUri = savedUri;
819819
var name = ctor.name.name;
820820
var ctorBody = [
@@ -2817,9 +2817,28 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
28172817
return js_ast.Fun(formals, block);
28182818
}
28192819

2820-
List<js_ast.Parameter> _emitParameters(FunctionNode f) {
2820+
js_ast.Parameter _emitParameter(VariableDeclaration node,
2821+
{bool withoutInitializer = false}) {
2822+
var initializer = node.initializer;
2823+
var id = _emitVariableDef(node);
2824+
if (initializer == null || withoutInitializer) return id;
2825+
return js_ast.DestructuredVariable(
2826+
name: id, defaultValue: _visitExpression(initializer));
2827+
}
2828+
2829+
List<js_ast.Parameter> _emitParameters(FunctionNode f,
2830+
{bool isForwarding = false}) {
2831+
// Destructure optional positional parameters in place.
2832+
// Given:
2833+
// - (arg1, arg2, [opt1, opt2 = def2])
2834+
// Emit:
2835+
// - (arg1, arg2, opt1 = null, opt2 = def2)
2836+
// Note, if [isForwarding] is set, omit initializers as this actually a
2837+
// forwarded call not a parameter list. E.g., the second in:
2838+
// - foo(arg1, opt1 = def1) => super(arg1, opt1).
28212839
var positional = f.positionalParameters;
2822-
var result = List<js_ast.Parameter>.of(positional.map(_emitVariableDef));
2840+
var result = List<js_ast.Parameter>.of(positional
2841+
.map((p) => _emitParameter(p, withoutInitializer: isForwarding)));
28232842
if (positional.isNotEmpty &&
28242843
f.requiredParameterCount == positional.length &&
28252844
positional.last.annotations.any(isJsRestAnnotation)) {
@@ -2910,7 +2929,7 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
29102929
//
29112930
// In the future, we might be able to simplify this, see:
29122931
// https://github.com/dart-lang/sdk/issues/28320
2913-
var jsParams = _emitParameters(function);
2932+
var jsParams = _emitParameters(function, isForwarding: true);
29142933
var mutatedParams = jsParams;
29152934
var gen = emitGeneratorFn((fnBody) {
29162935
var mutatedVars = js_ast.findMutatedVariables(fnBody);
@@ -3051,19 +3070,10 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
30513070
}
30523071
}
30533072

3054-
for (var p in f.positionalParameters.take(f.requiredParameterCount)) {
3073+
for (var p in f.positionalParameters) {
30553074
var jsParam = _emitIdentifier(p.name);
30563075
initParameter(p, jsParam);
30573076
}
3058-
for (var p in f.positionalParameters.skip(f.requiredParameterCount)) {
3059-
var jsParam = _emitIdentifier(p.name);
3060-
var defaultValue = _defaultParamValue(p);
3061-
if (defaultValue != null) {
3062-
body.add(js.statement(
3063-
'if (# === void 0) # = #;', [jsParam, jsParam, defaultValue]));
3064-
}
3065-
initParameter(p, jsParam);
3066-
}
30673077
for (var p in f.namedParameters) {
30683078
// Parameters will be passed using their real names, not the (possibly
30693079
// renamed) local variable.

0 commit comments

Comments
 (0)