Skip to content

Commit

Permalink
Fix incomplete computed property deoptimization (#3267)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Dec 1, 2019
1 parent d7e3993 commit d5651be
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/ast/nodes/MemberExpression.ts
Expand Up @@ -11,6 +11,7 @@ import {
ObjectPath,
ObjectPathKey,
PathTracker,
UNKNOWN_PATH,
UnknownKey
} from '../utils/PathTracker';
import { LiteralValueOrUnknown, UnknownValue } from '../values';
Expand Down Expand Up @@ -77,6 +78,7 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE

private bound = false;
private expressionsToBeDeoptimized: DeoptimizableEntity[] = [];
private hasDeoptimizedPath = false;
private replacement: string | null = null;

addExportedVariables(): void {}
Expand Down Expand Up @@ -112,6 +114,10 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE
}

deoptimizeCache() {
this.propertyKey = UnknownKey;
if (this.hasDeoptimizedPath) {
this.object.deoptimizePath(UNKNOWN_PATH);
}
for (const expression of this.expressionsToBeDeoptimized) {
expression.deoptimizeCache();
}
Expand All @@ -124,7 +130,12 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE
this.variable.deoptimizePath(path);
} else {
if (this.propertyKey === null) this.analysePropertyKey();
this.object.deoptimizePath([this.propertyKey as ObjectPathKey, ...path]);
if (this.propertyKey === UnknownKey) {
this.object.deoptimizePath(UNKNOWN_PATH);
} else {
this.hasDeoptimizedPath = true;
this.object.deoptimizePath([this.propertyKey as ObjectPathKey, ...path]);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/ast/nodes/ObjectExpression.ts
Expand Up @@ -36,11 +36,12 @@ interface PropertyMap {
};
}

export default class ObjectExpression extends NodeBase {
export default class ObjectExpression extends NodeBase implements DeoptimizableEntity {
properties!: (Property | SpreadElement)[];
type!: NodeType.tObjectExpression;

private deoptimizedPaths = new Set<string>();

// We collect deoptimization information if we can resolve a computed property access
private expressionsToBeDeoptimized = new Map<string, DeoptimizableEntity[]>();
private hasUnknownDeoptimizedProperty = false;
Expand Down
3 changes: 3 additions & 0 deletions test/function/samples/deoptimize-cached-prop/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'deoptimizes cached properties and return values if necessary (#3264)'
};
24 changes: 24 additions & 0 deletions test/function/samples/deoptimize-cached-prop/main.js
@@ -0,0 +1,24 @@
const obj1 = {};
let prop = 'x';

updateProp();

obj1[prop] = true;

assert.strictEqual(obj1.y ? 'WORKING' : 'BUG', 'WORKING');

function updateProp() {
prop = 'y';
}

const obj2 = {};
obj2[getResult().prop] = 1;

assert.equal(obj2.foo ? 'WORKING' : 'BUG', 'WORKING');

function getResult() {
const result = {};
result.prop = 'foo';
return result;
}

0 comments on commit d5651be

Please sign in to comment.