-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
CallExpressionBase.ts
150 lines (142 loc) · 4.33 KB
/
CallExpressionBase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import type { DeoptimizableEntity } from '../../DeoptimizableEntity';
import type { HasEffectsContext } from '../../ExecutionContext';
import type {
NodeInteraction,
NodeInteractionCalled,
NodeInteractionWithThisArgument
} from '../../NodeInteractions';
import { INTERACTION_ASSIGNED, INTERACTION_CALLED } from '../../NodeInteractions';
import { type ObjectPath, type PathTracker, UNKNOWN_PATH } from '../../utils/PathTracker';
import {
type ExpressionEntity,
type LiteralValueOrUnknown,
UNKNOWN_EXPRESSION,
UNKNOWN_RETURN_EXPRESSION,
UnknownValue
} from './Expression';
import { NodeBase } from './Node';
export default abstract class CallExpressionBase extends NodeBase implements DeoptimizableEntity {
protected declare interaction: NodeInteractionCalled;
protected returnExpression: [expression: ExpressionEntity, isPure: boolean] | null = null;
private readonly deoptimizableDependentExpressions: DeoptimizableEntity[] = [];
private readonly expressionsToBeDeoptimized = new Set<ExpressionEntity>();
deoptimizeCache(): void {
if (this.returnExpression?.[0] !== UNKNOWN_EXPRESSION) {
this.returnExpression = UNKNOWN_RETURN_EXPRESSION;
for (const expression of this.deoptimizableDependentExpressions) {
expression.deoptimizeCache();
}
for (const expression of this.expressionsToBeDeoptimized) {
expression.deoptimizePath(UNKNOWN_PATH);
}
}
}
deoptimizePath(path: ObjectPath): void {
if (
path.length === 0 ||
this.context.deoptimizationTracker.trackEntityAtPathAndGetIfTracked(path, this)
) {
return;
}
const [returnExpression] = this.getReturnExpression();
if (returnExpression !== UNKNOWN_EXPRESSION) {
returnExpression.deoptimizePath(path);
}
}
deoptimizeThisOnInteractionAtPath(
interaction: NodeInteractionWithThisArgument,
path: ObjectPath,
recursionTracker: PathTracker
): void {
const [returnExpression, isPure] = this.getReturnExpression(recursionTracker);
// TODO Lukas test
if (isPure) return;
if (returnExpression === UNKNOWN_EXPRESSION) {
interaction.thisArg.deoptimizePath(UNKNOWN_PATH);
} else {
recursionTracker.withTrackedEntityAtPath(
path,
returnExpression,
() => {
this.expressionsToBeDeoptimized.add(interaction.thisArg);
returnExpression.deoptimizeThisOnInteractionAtPath(interaction, path, recursionTracker);
},
null
);
}
}
getLiteralValueAtPath(
path: ObjectPath,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
const [returnExpression] = this.getReturnExpression(recursionTracker);
if (returnExpression === UNKNOWN_EXPRESSION) {
return UnknownValue;
}
return recursionTracker.withTrackedEntityAtPath(
path,
returnExpression,
() => {
this.deoptimizableDependentExpressions.push(origin);
return returnExpression.getLiteralValueAtPath(path, recursionTracker, origin);
},
UnknownValue
);
}
getReturnExpressionWhenCalledAtPath(
path: ObjectPath,
interaction: NodeInteractionCalled,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): [expression: ExpressionEntity, isPure: boolean] {
const [returnExpression] = this.getReturnExpression(recursionTracker);
if (returnExpression === UNKNOWN_EXPRESSION) {
return UNKNOWN_RETURN_EXPRESSION;
}
return recursionTracker.withTrackedEntityAtPath(
path,
returnExpression,
() => {
this.deoptimizableDependentExpressions.push(origin);
return returnExpression.getReturnExpressionWhenCalledAtPath(
path,
interaction,
recursionTracker,
origin
);
},
UNKNOWN_RETURN_EXPRESSION
);
}
hasEffectsOnInteractionAtPath(
path: ObjectPath,
interaction: NodeInteraction,
context: HasEffectsContext
): boolean {
const { type } = interaction;
if (type === INTERACTION_CALLED) {
const { args, withNew } = interaction;
if (
(withNew ? context.instantiated : context.called).trackEntityAtPathAndGetIfTracked(
path,
args,
this
)
) {
return false;
}
} else if (
(type === INTERACTION_ASSIGNED
? context.assigned
: context.accessed
).trackEntityAtPathAndGetIfTracked(path, this)
) {
return false;
}
return this.getReturnExpression()[0].hasEffectsOnInteractionAtPath(path, interaction, context);
}
protected abstract getReturnExpression(
recursionTracker?: PathTracker
): [expression: ExpressionEntity, isPure: boolean];
}