destructuring within arguments is now allowed as in SpiderMonkey

This commit is contained in:
satyr 2010-10-27 04:46:03 +09:00
parent 1cb6464948
commit 1aba75e3e8
6 changed files with 173 additions and 157 deletions

View File

@ -132,7 +132,7 @@
return new Param($1, $3);
})
],
ParamVar: [o('Identifier'), o('ThisProperty')],
ParamVar: [o('Identifier'), o('ThisProperty'), o('Array'), o('Object')],
Splat: [
o('Expression ...', function() {
return new Splat($1);

View File

@ -1059,9 +1059,9 @@
_ref3 = this.params;
for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
param = _ref3[_j];
if (param.attach) {
if (param.isComplex()) {
ref = param.asReference(o);
exprs.push(new Assign(param.name, param.value ? new Op('?', ref, param.value) : ref));
exprs.push(new Assign(new Value(param.name), param.value ? new Op('?', ref, param.value) : ref));
} else {
ref = param;
if (param.value) {
@ -1121,7 +1121,6 @@
this.value = _arg2;
this.splat = _arg3;
Param.__super__.constructor.call(this);
this.attach = this.name instanceof Value;
return this;
}
return Param;
@ -1136,13 +1135,16 @@
if (this.reference) {
return this.reference;
}
node = this.attach ? new Literal(o.scope.freeVariable('arg')) : this.name;
node = this.isComplex() ? new Literal(o.scope.freeVariable('arg')) : this.name;
node = new Value(node);
if (this.splat) {
node = new Splat(node);
}
return this.reference = node;
};
Param.prototype.isComplex = function() {
return this.name.isComplex();
};
return Param;
})();
exports.Splat = (function() {

File diff suppressed because one or more lines are too long

View File

@ -207,6 +207,8 @@ grammar =
ParamVar: [
o 'Identifier'
o 'ThisProperty'
o 'Array'
o 'Object'
]
# A splat that occurs outside of a parameter list.

View File

@ -863,9 +863,9 @@ exports.Code = class Code extends Base
new Value new Literal 'arguments'
break
for param in @params
if param.attach
if param.isComplex()
ref = param.asReference o
exprs.push new Assign param.name,
exprs.push new Assign new Value(param.name),
if param.value then new Op '?', ref, param.value else ref
else
ref = param
@ -908,17 +908,18 @@ exports.Param = class Param extends Base
constructor: (@name, @value, @splat) ->
super()
@attach = @name instanceof Value
compile: (o) -> @name.compile o, LEVEL_LIST
asReference: (o) ->
return @reference if @reference
node = if @attach then new Literal o.scope.freeVariable 'arg' else this.name
node = if @isComplex() then new Literal o.scope.freeVariable 'arg' else @name
node = new Value node
node = new Splat node if @splat
@reference = node
isComplex: -> @name.isComplex()
#### Splat
# A splat, either as a parameter to a function, an argument to a call,

View File

@ -43,7 +43,14 @@ eq obj.one, 1
eq obj.two, 2
# Default arguments.
# Destructuring.
(([{a: [b], c}]...) ->
eq b, 123
eq c, 456
) {a: [123], c: 456}
# Default values.
obj = f: (q = 123, @p = 456) -> q
eq obj.f(), 123
eq obj.p , 456