/
MethodBase.ts
153 lines (142 loc) · 4.14 KB
/
MethodBase.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
151
152
153
import type { DeoptimizableEntity } from '../../DeoptimizableEntity';
import type { HasEffectsContext } from '../../ExecutionContext';
import {
INTERACTION_ACCESSED,
INTERACTION_ASSIGNED,
INTERACTION_CALLED,
NO_ARGS,
NODE_INTERACTION_UNKNOWN_CALL,
NodeInteraction,
NodeInteractionCalled,
NodeInteractionWithThisArg
} from '../../NodeInteractions';
import {
EMPTY_PATH,
type ObjectPath,
type PathTracker,
SHARED_RECURSION_TRACKER
} from '../../utils/PathTracker';
import type PrivateIdentifier from '../PrivateIdentifier';
import {
type ExpressionEntity,
type LiteralValueOrUnknown,
UNKNOWN_EXPRESSION
} from './Expression';
import { type ExpressionNode, NodeBase } from './Node';
import type { PatternNode } from './Pattern';
export default class MethodBase extends NodeBase implements DeoptimizableEntity {
declare computed: boolean;
declare key: ExpressionNode | PrivateIdentifier;
declare kind: 'constructor' | 'method' | 'init' | 'get' | 'set';
declare value: ExpressionNode | (ExpressionNode & PatternNode);
private accessedValue: ExpressionEntity | null = null;
// As getter properties directly receive their values from fixed function
// expressions, there is no known situation where a getter is deoptimized.
deoptimizeCache(): void {}
deoptimizePath(path: ObjectPath): void {
this.getAccessedValue().deoptimizePath(path);
}
deoptimizeThisOnInteractionAtPath(
interaction: NodeInteractionWithThisArg,
path: ObjectPath,
recursionTracker: PathTracker
): void {
if (interaction.type === INTERACTION_ACCESSED && this.kind === 'get' && path.length === 0) {
return this.value.deoptimizeThisOnInteractionAtPath(
{
args: NO_ARGS,
thisArg: interaction.thisArg,
type: INTERACTION_CALLED,
withNew: false
},
EMPTY_PATH,
recursionTracker
);
}
if (interaction.type === INTERACTION_ASSIGNED && this.kind === 'set' && path.length === 0) {
return this.value.deoptimizeThisOnInteractionAtPath(
{
args: interaction.args,
thisArg: interaction.thisArg,
type: INTERACTION_CALLED,
withNew: false
},
EMPTY_PATH,
recursionTracker
);
}
this.getAccessedValue().deoptimizeThisOnInteractionAtPath(interaction, path, recursionTracker);
}
getLiteralValueAtPath(
path: ObjectPath,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
return this.getAccessedValue().getLiteralValueAtPath(path, recursionTracker, origin);
}
getReturnExpressionWhenCalledAtPath(
path: ObjectPath,
interaction: NodeInteractionCalled,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): ExpressionEntity {
return this.getAccessedValue().getReturnExpressionWhenCalledAtPath(
path,
interaction,
recursionTracker,
origin
);
}
hasEffects(context: HasEffectsContext): boolean {
return this.key.hasEffects(context);
}
hasEffectsOnInteractionAtPath(
path: ObjectPath,
interaction: NodeInteraction,
context: HasEffectsContext
): boolean {
if (this.kind === 'get' && interaction.type === INTERACTION_ACCESSED && path.length === 0) {
return this.value.hasEffectsOnInteractionAtPath(
EMPTY_PATH,
{
args: NO_ARGS,
thisArg: interaction.thisArg,
type: INTERACTION_CALLED,
withNew: false
},
context
);
}
// setters are only called for empty paths
if (this.kind === 'set' && interaction.type === INTERACTION_ASSIGNED) {
return this.value.hasEffectsOnInteractionAtPath(
EMPTY_PATH,
{
args: interaction.args,
thisArg: interaction.thisArg,
type: INTERACTION_CALLED,
withNew: false
},
context
);
}
return this.getAccessedValue().hasEffectsOnInteractionAtPath(path, interaction, context);
}
protected applyDeoptimizations() {}
protected getAccessedValue(): ExpressionEntity {
if (this.accessedValue === null) {
if (this.kind === 'get') {
this.accessedValue = UNKNOWN_EXPRESSION;
return (this.accessedValue = this.value.getReturnExpressionWhenCalledAtPath(
EMPTY_PATH,
NODE_INTERACTION_UNKNOWN_CALL,
SHARED_RECURSION_TRACKER,
this
));
} else {
return (this.accessedValue = this.value);
}
}
return this.accessedValue;
}
}