/
MethodTypes.ts
140 lines (128 loc) · 3.17 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
135
136
137
138
139
140
import { CallOptions, NO_ARGS } from '../../CallOptions';
import { HasEffectsContext, InclusionContext } from '../../ExecutionContext';
import { EVENT_CALLED, NodeEvent } from '../../NodeEvents';
import { EMPTY_PATH, ObjectPath, UNKNOWN_INTEGER_PATH } from '../../utils/PathTracker';
import {
UNKNOWN_LITERAL_BOOLEAN,
UNKNOWN_LITERAL_NUMBER,
UNKNOWN_LITERAL_STRING
} from '../../values';
import SpreadElement from '../SpreadElement';
import { ExpressionEntity, UNKNOWN_EXPRESSION } from './Expression';
import { ExpressionNode } from './Node';
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();
}
deoptimizeThisOnEventAtPath(
event: NodeEvent,
path: ObjectPath,
thisParameter: ExpressionEntity
): void {
if (event === EVENT_CALLED && path.length === 0 && this.description.mutatesSelfAsArray) {
thisParameter.deoptimizePath(UNKNOWN_INTEGER_PATH);
}
}
getReturnExpressionWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions
): ExpressionEntity {
if (path.length > 0) {
return UNKNOWN_EXPRESSION;
}
return (
this.description.returnsPrimitive ||
(this.description.returns === 'self'
? callOptions.thisParam || UNKNOWN_EXPRESSION
: this.description.returns())
);
}
hasEffectsWhenAccessedAtPath(path: ObjectPath): boolean {
return path.length > 1;
}
hasEffectsWhenAssignedAtPath(path: ObjectPath): boolean {
return path.length > 0;
}
hasEffectsWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
context: HasEffectsContext
): boolean {
if (
path.length > 0 ||
(this.description.mutatesSelfAsArray === true &&
callOptions.thisParam?.hasEffectsWhenAssignedAtPath(UNKNOWN_INTEGER_PATH, context))
) {
return true;
}
if (!this.description.callsArgs) {
return false;
}
for (const argIndex of this.description.callsArgs) {
if (
callOptions.args[argIndex]?.hasEffectsWhenCalledAtPath(
EMPTY_PATH,
{
args: NO_ARGS,
thisParam: null,
withNew: false
},
context
)
) {
return true;
}
}
return false;
}
includeCallArguments(context: InclusionContext, args: (ExpressionNode | SpreadElement)[]): void {
for (const arg of args) {
arg.include(context, 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
})
];