-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
CallExpressionBase.ts
146 lines (136 loc) · 4.23 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
import type { CallOptions } from '../../CallOptions';
import type { DeoptimizableEntity } from '../../DeoptimizableEntity';
import type { HasEffectsContext } from '../../ExecutionContext';
import { type NodeEvent } from '../../NodeEvents';
import { type ObjectPath, type PathTracker, UNKNOWN_PATH } from '../../utils/PathTracker';
import {
type ExpressionEntity,
type LiteralValueOrUnknown,
UNKNOWN_EXPRESSION,
UnknownValue
} from './Expression';
import { NodeBase } from './Node';
export default abstract class CallExpressionBase extends NodeBase implements DeoptimizableEntity {
protected declare callOptions: CallOptions;
protected returnExpression: ExpressionEntity | null = null;
private readonly deoptimizableDependentExpressions: DeoptimizableEntity[] = [];
private readonly expressionsToBeDeoptimized = new Set<ExpressionEntity>();
deoptimizeCache(): void {
if (this.returnExpression !== UNKNOWN_EXPRESSION) {
this.returnExpression = UNKNOWN_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);
}
}
deoptimizeThisOnEventAtPath(
event: NodeEvent,
path: ObjectPath,
thisParameter: ExpressionEntity,
recursionTracker: PathTracker
): void {
const returnExpression = this.getReturnExpression(recursionTracker);
if (returnExpression === UNKNOWN_EXPRESSION) {
thisParameter.deoptimizePath(UNKNOWN_PATH);
} else {
recursionTracker.withTrackedEntityAtPath(
path,
returnExpression,
() => {
this.expressionsToBeDeoptimized.add(thisParameter);
returnExpression.deoptimizeThisOnEventAtPath(
event,
path,
thisParameter,
recursionTracker
);
},
undefined
);
}
}
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,
callOptions: CallOptions,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): ExpressionEntity {
const returnExpression = this.getReturnExpression(recursionTracker);
if (this.returnExpression === UNKNOWN_EXPRESSION) {
return UNKNOWN_EXPRESSION;
}
return recursionTracker.withTrackedEntityAtPath(
path,
returnExpression,
() => {
this.deoptimizableDependentExpressions.push(origin);
return returnExpression.getReturnExpressionWhenCalledAtPath(
path,
callOptions,
recursionTracker,
origin
);
},
UNKNOWN_EXPRESSION
);
}
hasEffectsWhenAccessedAtPath(path: ObjectPath, context: HasEffectsContext): boolean {
return (
!context.accessed.trackEntityAtPathAndGetIfTracked(path, this) &&
this.getReturnExpression().hasEffectsWhenAccessedAtPath(path, context)
);
}
hasEffectsWhenAssignedAtPath(path: ObjectPath, context: HasEffectsContext): boolean {
return (
!context.assigned.trackEntityAtPathAndGetIfTracked(path, this) &&
this.getReturnExpression().hasEffectsWhenAssignedAtPath(path, context)
);
}
hasEffectsWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
context: HasEffectsContext
): boolean {
return (
!(
callOptions.withNew ? context.instantiated : context.called
).trackEntityAtPathAndGetIfTracked(path, callOptions, this) &&
this.getReturnExpression().hasEffectsWhenCalledAtPath(path, callOptions, context)
);
}
protected abstract getReturnExpression(recursionTracker?: PathTracker): ExpressionEntity;
}