Skip to content

Commit

Permalink
deoptimized a = delete b.c (gkz/LiveScript#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Jan 29, 2013
1 parent e181b27 commit 8410f8c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
37 changes: 12 additions & 25 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ exports.Block = Block = (function(superclass){
break;
default:
this.lines.push(it);
if (that = it.back, delete it.back, that) {
if (that = (ref$ = it.back, delete it.back, ref$)) {
this.back = that;
}
}
Expand Down Expand Up @@ -377,7 +377,7 @@ exports.Block = Block = (function(superclass){
scope: this.scope = Scope.root = new Scope
}, options));
delete o.filename;
o.indent = (bare = o.bare, delete o.bare, bare) ? '' : TAB;
o.indent = (bare = (ref$ = o.bare, delete o.bare, ref$)) ? '' : TAB;
if (/^\s*(?:[/#]|javascript:)/.test((ref$ = this.lines[0]) != null ? ref$.code : void 8)) {
prefix = this.lines.shift().code + '\n';
}
Expand Down Expand Up @@ -659,7 +659,7 @@ exports.Chain = Chain = (function(superclass){
this.head.sproto = true;
}
}
if (that = it.vivify, delete it.vivify, that) {
if (that = (ref$ = it.vivify, delete it.vivify, ref$)) {
this.head = Assign(Chain(this.head, this.tails.splice(0, 9e9)), that(), '=', '||');
}
return this;
Expand Down Expand Up @@ -1471,14 +1471,7 @@ exports.Unary = Unary = (function(superclass){
prototype.compilePluck = function(o){
var ref$, get, del, code, ref;
ref$ = Chain(this.it).cacheReference(o), get = ref$[0], del = ref$[1];
code = this.assigned
? ''
: (ref = o.scope.temporary()) + " = ";
code += get.compile(o, LEVEL_LIST) + ", delete " + del.compile(o, LEVEL_LIST);
if (this.assigned) {
return code;
}
code += ", " + o.scope.free(ref);
code = (ref = o.scope.temporary()) + " = " + get.compile(o, LEVEL_LIST) + ", delete " + del.compile(o, LEVEL_LIST) + ", " + o.scope.free(ref);
if (o.level < LEVEL_LIST) {
return code;
} else {
Expand Down Expand Up @@ -1764,9 +1757,9 @@ exports.Assign = Assign = (function(superclass){
}
};
prototype.unfoldSoak = function(o){
var that, ref$, rite, temps;
var that, ref$, ref1$, rite, temps;
if (this.left instanceof Existence) {
if (that = (ref$ = this.left = this.left.it).name, delete ref$.name, that) {
if (that = (ref1$ = (ref$ = this.left = this.left.it).name, delete ref$.name, ref1$)) {
rite = this.right;
rite = Assign(this.right = Var(that), rite);
} else {
Expand All @@ -1780,7 +1773,7 @@ exports.Assign = Assign = (function(superclass){
return this.access && this;
};
prototype.compileNode = function(o){
var left, ref$, i$, len$, op, right, minmax, lefs, reft, ref, x0$, code, name, lvar, del, that, res;
var left, ref$, i$, len$, op, right, minmax, lefs, reft, ref, x0$, code, name, lvar, that, res;
left = this.left.expandSlice(o, true).unwrap();
if (!this.right) {
left.isAssignable() || left.carp('invalid unary assign');
Expand Down Expand Up @@ -1833,7 +1826,6 @@ exports.Assign = Assign = (function(superclass){
(right = right.unparen()).ripName(left = left.unwrap());
code = name = (left.front = true, left).compile(o, LEVEL_LIST);
if (lvar = left instanceof Var) {
del = right.op === 'delete';
if (op === '=') {
o.scope.declare(name, left, this['const']);
} else if (that = o.scope.checkReadOnly(name)) {
Expand All @@ -1843,14 +1835,9 @@ exports.Assign = Assign = (function(superclass){
code += " " + (replace$.call(op, ':', '')) + " ";
code = !o.level && right instanceof While && !right['else'] && (lvar || left instanceof Chain && left.isSimpleAccess())
? (res = o.scope.temporary('res')) + " = [];\n" + this.tab + right.makeReturn(res).compile(o) + "\n" + this.tab + code + o.scope.free(res)
: code + (right.assigned = true, right).compile(o, LEVEL_LIST);
if (that = o.level) {
if (del) {
code += ", " + name;
}
if (that > (del ? LEVEL_PAREN : LEVEL_LIST)) {
code = "(" + code + ")";
}
: code + right.compile(o, LEVEL_LIST);
if (o.level > LEVEL_LIST) {
code = "(" + code + ")";
}
return code;
};
Expand Down Expand Up @@ -2212,7 +2199,7 @@ exports.Fun = Fun = (function(superclass){
this.name || (this.name = it.varName());
};
prototype.compileNode = function(o){
var pscope, sscope, scope, that, loop, body, name, tab, code, ref$;
var pscope, sscope, scope, that, loop, ref$, body, name, tab, code;
pscope = o.scope;
sscope = pscope.shared || pscope;
scope = o.scope = this.body.scope = new Scope(this.wrapper ? pscope : sscope, this.wrapper && sscope);
Expand All @@ -2223,7 +2210,7 @@ exports.Fun = Fun = (function(superclass){
if (that = this.cname) {
scope.assign('constructor', that);
}
if (loop = o.loop, delete o.loop, loop) {
if (loop = (ref$ = o.loop, delete o.loop, ref$)) {
o.indent = this.tab = '';
}
o.indent += TAB;
Expand Down
16 changes: 6 additions & 10 deletions src/ast.co
Original file line number Diff line number Diff line change
Expand Up @@ -887,11 +887,10 @@ class exports.Unary extends Node
# `v = delete o.k`
compilePluck: (o) ->
[get, del] = Chain @it .cacheReference o
code = if @assigned then '' else "#{ ref = o.scope.temporary! } = "
code += "#{ get.compile o, LEVEL_LIST }
, delete #{ del.compile o, LEVEL_LIST }"
return code if @assigned
code += ", #{ o.scope.free ref }"
code = "#{ ref = o.scope.temporary! } = \
#{ get.compile o, LEVEL_LIST }, delete \
#{ del.compile o, LEVEL_LIST }, \
#{ o.scope.free ref }"
if o.level < LEVEL_LIST then code else "(#code)"

#### Binary operators
Expand Down Expand Up @@ -1106,7 +1105,6 @@ class exports.Assign extends Node
(right.=unparen!)ripName left.=unwrap!
code = name = (left <<< {+front})compile o, LEVEL_LIST
if lvar = left instanceof Var
del = right.op is \delete
if op is \=
o.scope.declare name, left, @const
else if o.scope.checkReadOnly name
Expand All @@ -1119,10 +1117,8 @@ class exports.Assign extends Node
#{@tab}#{ right.makeReturn(res)compile o }
#{@tab}#code#{ o.scope.free res }"""
else
code + (right <<< {+assigned})compile o, LEVEL_LIST
if o.level
code += ", #name" if del
code = "(#code)" if that > (if del then LEVEL_PAREN else LEVEL_LIST)
code + right.compile o, LEVEL_LIST
code = "(#code)" if o.level > LEVEL_LIST
code

!function lefseek node, lefs
Expand Down
8 changes: 8 additions & 0 deletions test/operator.co
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ compileThrows 'invalid decrement' 1 'q.=p--'


### `delete`
# - Disallows variable deletion.
# - Returns the deleted value rather than the useless configurability check.
i = 0
O = ->
switch ++i
Expand All @@ -369,6 +371,12 @@ eq o[7], void
compileThrows 'invalid delete' 1 'delete a'
compileThrows 'invalid delete' 1 'delete a.=b'

# [livescript#273](https://github.com/gkz/LiveScript/issues/273)
a = b = ^{0} <<< [1]
a = delete a.0
eq 1 a
eq 0 b.0


### [[Class]] sniffing
eq \RegExp typeof! /^/
Expand Down

0 comments on commit 8410f8c

Please sign in to comment.