/
MethodTypes.ts
134 lines (125 loc) · 3 KB
/
MethodTypes.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
import type { HasEffectsContext } from '../../ExecutionContext';
import {
INTERACTION_ACCESSED,
INTERACTION_CALLED,
NODE_INTERACTION_UNKNOWN_ASSIGNMENT,
NODE_INTERACTION_UNKNOWN_CALL,
NodeInteraction,
NodeInteractionCalled,
NodeInteractionWithThisArg
} from '../../NodeInteractions';
import { EMPTY_PATH, type ObjectPath, UNKNOWN_INTEGER_PATH } from '../../utils/PathTracker';
import {
UNKNOWN_LITERAL_BOOLEAN,
UNKNOWN_LITERAL_NUMBER,
UNKNOWN_LITERAL_STRING
} from '../../values';
import { ExpressionEntity, UNKNOWN_EXPRESSION } from './Expression';
type MethodDescription = {
callsArgs: number[] | null;
mutatesSelfAsArray: boolean | 'deopt-only';
} & (
| {
returns: 'self' | (() => ExpressionEntity);
returnsPrimitive: null;
}
| {
returns: null;
returnsPrimitive: ExpressionEntity;
}
);
export class Method extends ExpressionEntity {
constructor(private readonly description: MethodDescription) {
super();
}
deoptimizeThisOnInteractionAtPath(
{ type, thisArg }: NodeInteractionWithThisArg,
path: ObjectPath
): void {
if (type === INTERACTION_CALLED && path.length === 0 && this.description.mutatesSelfAsArray) {
thisArg.deoptimizePath(UNKNOWN_INTEGER_PATH);
}
}
getReturnExpressionWhenCalledAtPath(
path: ObjectPath,
{ thisArg }: NodeInteractionCalled
): ExpressionEntity {
if (path.length > 0) {
return UNKNOWN_EXPRESSION;
}
return (
this.description.returnsPrimitive ||
(this.description.returns === 'self'
? thisArg || UNKNOWN_EXPRESSION
: this.description.returns())
);
}
hasEffectsOnInteractionAtPath(
path: ObjectPath,
interaction: NodeInteraction,
context: HasEffectsContext
): boolean {
const { type } = interaction;
if (path.length > (type === INTERACTION_ACCESSED ? 1 : 0)) {
return true;
}
if (type === INTERACTION_CALLED) {
if (
this.description.mutatesSelfAsArray === true &&
interaction.thisArg?.hasEffectsOnInteractionAtPath(
UNKNOWN_INTEGER_PATH,
NODE_INTERACTION_UNKNOWN_ASSIGNMENT,
context
)
) {
return true;
}
if (this.description.callsArgs) {
for (const argIndex of this.description.callsArgs) {
if (
interaction.args[argIndex]?.hasEffectsOnInteractionAtPath(
EMPTY_PATH,
NODE_INTERACTION_UNKNOWN_CALL,
context
)
) {
return true;
}
}
}
}
return false;
}
}
export const METHOD_RETURNS_BOOLEAN = [
new Method({
callsArgs: null,
mutatesSelfAsArray: false,
returns: null,
returnsPrimitive: UNKNOWN_LITERAL_BOOLEAN
})
];
export const METHOD_RETURNS_STRING = [
new Method({
callsArgs: null,
mutatesSelfAsArray: false,
returns: null,
returnsPrimitive: UNKNOWN_LITERAL_STRING
})
];
export const METHOD_RETURNS_NUMBER = [
new Method({
callsArgs: null,
mutatesSelfAsArray: false,
returns: null,
returnsPrimitive: UNKNOWN_LITERAL_NUMBER
})
];
export const METHOD_RETURNS_UNKNOWN = [
new Method({
callsArgs: null,
mutatesSelfAsArray: false,
returns: null,
returnsPrimitive: UNKNOWN_EXPRESSION
})
];