adding return values for destructuring assignment.

This commit is contained in:
Jeremy Ashkenas 2010-02-25 00:43:02 -05:00
parent 05d95acfc3
commit 69feac3a01
3 changed files with 15 additions and 3 deletions

View File

@ -756,7 +756,7 @@
// object literals to a value. Peeks at their properties to assign inner names.
// See: http://wiki.ecmascript.org/doku.php?id=harmony:destructuring
compile_pattern_match: function compile_pattern_match(o) {
var _a, _b, access_class, assigns, i, idx, obj, val, val_var;
var _a, _b, access_class, assigns, code, i, idx, obj, val, val_var;
val_var = o.scope.free_variable();
assigns = [this.idt() + val_var + ' = ' + this.value.compile(o) + ';'];
o.top = true;
@ -781,7 +781,11 @@
}
assigns.push(new AssignNode(obj, val).compile(o));
}
return assigns.join("\n");
code = assigns.join("\n");
if (o.returns) {
code += '\n' + this.idt() + 'return ' + this.variable.compile(o) + ';';
}
return code;
},
compile_splice: function compile_splice(o) {
var from, l, name, plus, range, to;

View File

@ -617,7 +617,9 @@ AssignNode: exports.AssignNode: inherit BaseNode, {
idx: new LiteralNode(idx) unless typeof idx is 'object'
val: new ValueNode(new LiteralNode(val_var), [new access_class(idx)])
assigns.push(new AssignNode(obj, val).compile(o))
assigns.join("\n")
code: assigns.join("\n")
code += '\n' + @idt() + 'return ' + @variable.compile(o) + ';' if o.returns
code
compile_splice: (o) ->
name: @variable.compile(merge(o, {only_first: true}))

View File

@ -7,6 +7,12 @@ ok a is -2
ok b is -1
func: ->
[a, b]: [b, a]
ok func().join(' ') is '-1 -2'
arr: [1, 2, 3]
[a, b, c]: arr