Skip to content

Commit

Permalink
fixes #145, optional params+initializing formals+private fields
Browse files Browse the repository at this point in the history
  • Loading branch information
John Messerly committed Apr 21, 2015
1 parent b5e4a91 commit bbe6801
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
16 changes: 9 additions & 7 deletions pkg/dev_compiler/lib/src/codegen/js_codegen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -885,23 +885,25 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {

var body = [];
for (var param in parameters.parameters) {
// TODO(justinfagnani): rename identifier if necessary
var name = param.identifier.name;
var jsParam = _visit(param.identifier);

if (param.kind == ParameterKind.NAMED) {
// Parameters will be passed using their real names, not the (possibly
// renamed) local variable.
var paramName = js.string(param.identifier.name, "'");
body.add(js.statement('let # = # && # in # ? #.# : #;', [
name,
jsParam,
_namedArgTemp,
js.string(name, "'"),
paramName,
_namedArgTemp,
_namedArgTemp,
name,
paramName,
_defaultParamValue(param),
]));
} else if (param.kind == ParameterKind.POSITIONAL) {
body.add(js.statement('if (# === void 0) # = #;', [
name,
name,
jsParam,
jsParam,
_defaultParamValue(param)
]));
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/dev_compiler/test/codegen/expect/temps.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,27 @@ var temps;
this[_function] = func;
}
}
let _opt = Symbol('_opt');
class OptionalArg extends core.Object {
OptionalArg(opt) {
if (opt === void 0)
opt = 123;
this[_opt] = opt;
}
named(opts) {
let opt = opts && '_opt' in opts ? opts._opt : 456;
this[_opt] = opt;
}
}
dart.defineNamedConstructor(OptionalArg, 'named');
// Function main: () → dynamic
function main() {
core.print(new FormalCollision(1, 2, x => x));
core.print(new OptionalArg()[_opt]);
core.print(new OptionalArg.named()[_opt]);
}
// Exports:
exports.FormalCollision = FormalCollision;
exports.OptionalArg = OptionalArg;
exports.main = main;
})(temps || (temps = {}));
8 changes: 8 additions & 0 deletions pkg/dev_compiler/test/codegen/temps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class FormalCollision {
FormalCollision(this._x, this.__x, this._function);
}

class OptionalArg {
int _opt;
OptionalArg([this._opt = 123]);
OptionalArg.named({this._opt: 456});
}

main() {
print(new FormalCollision(1, 2, (x) => x));
print(new OptionalArg()._opt);
print(new OptionalArg.named()._opt);
}

0 comments on commit bbe6801

Please sign in to comment.