Skip to content

Commit

Permalink
Prevent crash for recursive "this" deoptimization (#4091)
Browse files Browse the repository at this point in the history
* Switch to using ESLint only

* Fix commit hook

* Fix tests

* Adapt VSCode config

* Fix changelog

* Fix prototypes

* Prevent crash for recursive "this" deoptimization
  • Loading branch information
lukastaegert committed May 25, 2021
1 parent 32d1b60 commit 90d2e62
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
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() {},
};
}

0 comments on commit 90d2e62

Please sign in to comment.