Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incomplete computed property deoptimization #3267

Merged
merged 1 commit into from Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}