/
ClassNode.ts
242 lines (222 loc) · 7.75 KB
/
ClassNode.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { getOrCreate } from '../../../utils/getOrCreate';
import { CallOptions } from '../../CallOptions';
import { DeoptimizableEntity } from '../../DeoptimizableEntity';
import { HasEffectsContext } from '../../ExecutionContext';
import ChildScope from '../../scopes/ChildScope';
import Scope from '../../scopes/Scope';
import { EMPTY_PATH, ObjectPath, PathTracker } from '../../utils/PathTracker';
import { LiteralValueOrUnknown, UnknownValue, UNKNOWN_EXPRESSION } from '../../values';
import ClassBody from '../ClassBody';
import Identifier from '../Identifier';
import MethodDefinition from '../MethodDefinition';
import { ExpressionEntity } from './Expression';
import { ExpressionNode, NodeBase } from './Node';
export default class ClassNode extends NodeBase {
body!: ClassBody;
id!: Identifier | null;
superClass!: ExpressionNode | null;
private classConstructor!: MethodDefinition | null;
private deoptimizedPrototype = false;
private deoptimizedStatic = false;
// Collect deoptimization information if we can resolve a property access, by property name
private expressionsToBeDeoptimized = new Map<string, DeoptimizableEntity[]>();
// Known, simple, non-deoptimized static properties are kept in here. They are removed when deoptimized.
private staticPropertyMap: {[name: string]: ExpressionNode} | null = null;
bind() {
super.bind();
}
createScope(parentScope: Scope) {
this.scope = new ChildScope(parentScope);
}
deoptimizeAllStatics() {
for (const name in this.staticPropertyMap) {
this.deoptimizeStatic(name);
}
this.deoptimizedStatic = this.deoptimizedPrototype = true;
}
deoptimizeCache() {
this.deoptimizeAllStatics();
}
deoptimizePath(path: ObjectPath) {
const propertyMap = this.getStaticPropertyMap();
const key = path[0];
const definition = typeof key === 'string' && propertyMap[key];
if (path.length === 1) {
if (definition) {
this.deoptimizeStatic(key as string);
} else if (typeof key !== 'string') {
this.deoptimizeAllStatics();
}
} else if (key === 'prototype' && typeof path[1] !== 'string') {
this.deoptimizedPrototype = true;
} else if (path.length > 1 && definition) {
definition.deoptimizePath(path.slice(1));
}
}
deoptimizeStatic(name: string) {
delete this.staticPropertyMap![name];
const deoptEntities = this.expressionsToBeDeoptimized.get(name);
if (deoptEntities) {
for (const entity of deoptEntities) {
entity.deoptimizeCache();
}
}
}
getLiteralValueAtPath(
path: ObjectPath,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
const key = path[0];
const definition = typeof key === 'string' && this.getStaticPropertyMap()[key];
if (path.length === 0 || !definition ||
(key === 'prototype' ? this.deoptimizedPrototype : this.deoptimizedStatic)) {
return UnknownValue;
}
getOrCreate(this.expressionsToBeDeoptimized, key, () => []).push(origin);
return definition.getLiteralValueAtPath(
path.slice(1),
recursionTracker,
origin
);
}
getReturnExpressionWhenCalledAtPath(
path: ObjectPath,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): ExpressionEntity {
const key = path[0];
const definition = typeof key === 'string' && this.getStaticPropertyMap()[key];
if (path.length === 0 || !definition ||
(key === 'prototype' ? this.deoptimizedPrototype : this.deoptimizedStatic)) {
return UNKNOWN_EXPRESSION;
}
getOrCreate(this.expressionsToBeDeoptimized, key, () => []).push(origin);
return definition.getReturnExpressionWhenCalledAtPath(
path.slice(1),
recursionTracker,
origin
);
}
hasEffectsWhenAccessedAtPath(path: ObjectPath, context: HasEffectsContext) {
if (path.length === 0) return false;
if (this.deoptimizedStatic) return true;
if (this.superClass && this.superClass.hasEffectsWhenAccessedAtPath(path, context)) return true;
const key = path[0];
if (key === 'prototype') {
if (path.length === 1) return false;
if (this.deoptimizedPrototype) return true;
const key2 = path[1];
if (path.length === 2 && typeof key2 === 'string') {
return mayHaveGetterSetterEffect(this.body, 'get', false, key2, context);
}
return true;
} else if (typeof key === 'string' && path.length === 1) {
return mayHaveGetterSetterEffect(this.body, 'get', true, key, context);
} else {
return true;
}
}
hasEffectsWhenAssignedAtPath(path: ObjectPath, context: HasEffectsContext) {
if (this.deoptimizedStatic) return true;
if (this.superClass && this.superClass.hasEffectsWhenAssignedAtPath(path, context)) return true;
const key = path[0];
if (key === 'prototype') {
if (path.length === 1) return false;
if (this.deoptimizedPrototype) return true;
const key2 = path[1];
if (path.length === 2 && typeof key2 === 'string') {
return mayHaveGetterSetterEffect(this.body, 'set', false, key2, context);
}
return true;
} else if (typeof key === 'string' && path.length === 1) {
return mayHaveGetterSetterEffect(this.body, 'set', true, key, context);
} else {
return true;
}
}
hasEffectsWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
context: HasEffectsContext
) {
if (callOptions.withNew) {
return path.length > 0 ||
(this.classConstructor !== null &&
this.classConstructor.hasEffectsWhenCalledAtPath(EMPTY_PATH, callOptions, context)) ||
(this.superClass !== null &&
this.superClass.hasEffectsWhenCalledAtPath(path, callOptions, context));
} else {
if (path.length !== 1 || this.deoptimizedStatic) return true;
const key = path[0];
const definition = typeof key === 'string' && this.getStaticPropertyMap()[key];
if (!definition) return true;
return definition.hasEffectsWhenCalledAtPath([], callOptions, context);
}
}
initialise() {
if (this.id !== null) {
this.id.declare('class', this);
}
for (const method of this.body.body) {
if (method instanceof MethodDefinition && method.kind === 'constructor') {
this.classConstructor = method;
return;
}
}
this.classConstructor = null;
}
mayModifyThisWhenCalledAtPath(
path: ObjectPath
) {
const key = path[0];
const definition = typeof key === 'string' && this.getStaticPropertyMap()[key];
if (!definition || this.deoptimizedStatic) return true;
return definition.mayModifyThisWhenCalledAtPath(path.slice(1));
}
private getStaticPropertyMap(): {[name: string]: ExpressionNode} {
if (this.staticPropertyMap) return this.staticPropertyMap;
const propertyMap = this.staticPropertyMap = Object.create(null);
const seen: {[name: string]: boolean} = Object.create(null);
for (const definition of this.body.body) {
if (!definition.static) continue;
// If there are non-identifier-named statics, give up.
if (definition.computed || !(definition.key instanceof Identifier)) {
return this.staticPropertyMap = Object.create(null);
}
const key = definition.key.name;
// Not handling duplicate definitions.
if (seen[key]) {
delete propertyMap[key];
continue;
}
seen[key] = true;
if (definition instanceof MethodDefinition) {
if (definition.kind === "method") {
propertyMap[key] = definition.value;
}
} else if (definition.value) {
propertyMap[key] = definition.value;
}
}
return this.staticPropertyMap = propertyMap;
}
}
function mayHaveGetterSetterEffect(
body: ClassBody,
kind: 'get' | 'set', isStatic: boolean, name: string,
context: HasEffectsContext
) {
for (const definition of body.body) {
if (definition instanceof MethodDefinition && definition.static === isStatic && definition.kind === kind) {
if (definition.computed || !(definition.key instanceof Identifier)) {
return true;
}
if (definition.key.name === name &&
definition.value.hasEffectsWhenCalledAtPath([], {args: [], withNew: false}, context)) {
return true;
}
}
}
return false;
}