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

Prevent crash for recursive "this" deoptimization #4091

Merged
merged 8 commits into from
May 25, 2021
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
19 changes: 13 additions & 6 deletions src/ast/variables/ThisVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { AstContext } from '../../Module';
import { HasEffectsContext } from '../ExecutionContext';
import { NodeEvent } from '../NodeEvents';
import { ExpressionEntity, UNKNOWN_EXPRESSION } from '../nodes/shared/Expression';
import { ObjectPath, SHARED_RECURSION_TRACKER } from '../utils/PathTracker';
import {
DiscriminatedPathTracker,
ObjectPath,
SHARED_RECURSION_TRACKER
} from '../utils/PathTracker';
import LocalVariable from './LocalVariable';

interface ThisDeoptimizationEvent {
Expand All @@ -14,7 +18,8 @@ interface ThisDeoptimizationEvent {
export default class ThisVariable extends LocalVariable {
private deoptimizedPaths: ObjectPath[] = [];
private entitiesToBeDeoptimized = new Set<ExpressionEntity>();
private thisDeoptimizations: ThisDeoptimizationEvent[] = [];
private thisDeoptimizationList: ThisDeoptimizationEvent[] = [];
private thisDeoptimizations = new DiscriminatedPathTracker();

constructor(context: AstContext) {
super('this', null, null, context);
Expand All @@ -24,7 +29,7 @@ export default class ThisVariable extends LocalVariable {
for (const path of this.deoptimizedPaths) {
entity.deoptimizePath(path);
}
for (const thisDeoptimization of this.thisDeoptimizations) {
for (const thisDeoptimization of this.thisDeoptimizationList) {
this.applyThisDeoptimizationEvent(entity, thisDeoptimization);
}
this.entitiesToBeDeoptimized.add(entity);
Expand Down Expand Up @@ -53,10 +58,12 @@ export default class ThisVariable extends LocalVariable {
path,
thisParameter
};
for (const entity of this.entitiesToBeDeoptimized) {
this.applyThisDeoptimizationEvent(entity, thisDeoptimization);
if (!this.thisDeoptimizations.trackEntityAtPathAndGetIfTracked(path, event, thisParameter)) {
for (const entity of this.entitiesToBeDeoptimized) {
this.applyThisDeoptimizationEvent(entity, thisDeoptimization);
}
this.thisDeoptimizationList.push(thisDeoptimization);
}
this.thisDeoptimizations.push(thisDeoptimization);
}

hasEffectsWhenAccessedAtPath(path: ObjectPath, context: HasEffectsContext): boolean {
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/recursive-this-deoptimization/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles recursive "this" deoptimizations (#4089)'
};
11 changes: 11 additions & 0 deletions test/form/samples/recursive-this-deoptimization/_expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
getObj().getThis().method();
getObj().getThis().getThis().method();

function getObj() {
return {
getThis() {
return this;
},
method() {},
};
}
11 changes: 11 additions & 0 deletions test/form/samples/recursive-this-deoptimization/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
getObj().getThis().method();
getObj().getThis().getThis().method();

function getObj() {
return {
getThis() {
return this;
},
method() {},
};
}