-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RuntimeTypeCheck.java
404 lines (346 loc) · 12.7 KB
/
RuntimeTypeCheck.java
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Copyright 2010 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.StaticSourceFile;
import com.google.javascript.rhino.jstype.FunctionType;
import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.ObjectType;
import java.util.Collection;
import java.util.Comparator;
import java.util.TreeSet;
import javax.annotation.Nullable;
/**
* Inserts run-time type assertions.
*
* <p>We add markers to user-defined interfaces and classes in order to check if
* an object conforms to that type.
*
* <p>For each function, we insert a run-time type assertion for each parameter
* and return value for which the compiler has a type.
*
* <p>The JavaScript code which implements the type assertions is in
* js/runtime-type-check.js.
*
*/
class RuntimeTypeCheck implements CompilerPass {
private static final Comparator<JSType> ALPHA = new Comparator<JSType>() {
@Override
public int compare(JSType t1, JSType t2) {
return getName(t1).compareTo(getName(t2));
}
private String getName(JSType type) {
if (type.isInstanceType()) {
return ((ObjectType) type).getReferenceName();
} else if (type.isNullType()
|| type.isBooleanValueType()
|| type.isNumberValueType()
|| type.isStringValueType()
|| type.isVoidType()) {
return type.toString();
} else {
// Type unchecked at runtime, so we don't care about the sorting order.
return "";
}
}
};
private final AbstractCompiler compiler;
private final String logFunction;
RuntimeTypeCheck(AbstractCompiler compiler, @Nullable String logFunction) {
this.compiler = compiler;
this.logFunction = logFunction;
}
@Override
public void process(Node externs, Node root) {
NodeTraversal.traverseEs6(compiler, root, new AddMarkers(compiler));
NodeTraversal.traverseEs6(compiler, root, new AddChecks());
addBoilerplateCode();
new Normalize(compiler, false).process(externs, root);
}
/**
* Inserts marker properties for user-defined interfaces and classes.
*
* <p>For example, for a class C, we add
* {@code C.prototype['instance_of__C']}, and for each interface I it
* implements , we add {@code C.prototype['implements__I']}.
*
* <p>Since interfaces are not a run-time JS concept, we use these markers to
* recognize an interface implementation at runtime. We also use markers for
* user-defined classes, so that we can easily recognize them independently of
* which module they are defined in and whether the module is loaded.
*/
private static class AddMarkers
extends NodeTraversal.AbstractPostOrderCallback {
private final AbstractCompiler compiler;
private AddMarkers(AbstractCompiler compiler) {
this.compiler = compiler;
}
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.isFunction()) {
visitFunction(n);
}
}
private void visitFunction(Node n) {
FunctionType funType = n.getJSType().toMaybeFunctionType();
if (funType != null && !funType.isConstructor()) {
return;
}
Node nodeToInsertAfter = findNodeToInsertAfter(n);
nodeToInsertAfter = addMarker(funType, nodeToInsertAfter, null);
TreeSet<ObjectType> stuff = new TreeSet<>(ALPHA);
Iterables.addAll(stuff, funType.getAllImplementedInterfaces());
for (ObjectType interfaceType : stuff) {
nodeToInsertAfter =
addMarker(funType, nodeToInsertAfter, interfaceType);
}
}
private Node addMarker(
FunctionType funType,
Node nodeToInsertAfter,
@Nullable ObjectType interfaceType) {
if (funType.getSource() == null) {
return nodeToInsertAfter;
}
String className = NodeUtil.getName(funType.getSource());
// This can happen with anonymous classes declared with the type
// {@code Function}.
if (className == null) {
return nodeToInsertAfter;
}
Node classNode = NodeUtil.newQName(
compiler, className);
Node marker = IR.string(
interfaceType == null ?
"instance_of__" + className :
"implements__" + interfaceType.getReferenceName());
Node assign = IR.exprResult(IR.assign(
IR.getelem(
IR.getprop(
classNode,
IR.string("prototype")), marker),
IR.trueNode()));
nodeToInsertAfter.getParent().addChildAfter(assign, nodeToInsertAfter);
compiler.reportChangeToEnclosingScope(assign);
nodeToInsertAfter = assign;
return nodeToInsertAfter;
}
/**
* Find the node to insert the markers after. Typically, this node
* corresponds to the constructor declaration, but we want to skip any of
* the white-listed function calls.
*
* @param n the constructor function node
* @return the node to insert after
*/
private Node findNodeToInsertAfter(Node n) {
Node nodeToInsertAfter = findEnclosingConstructorDeclaration(n);
Node next = nodeToInsertAfter.getNext();
while (next != null && isClassDefiningCall(next)) {
nodeToInsertAfter = next;
next = nodeToInsertAfter.getNext();
}
return nodeToInsertAfter;
}
private static Node findEnclosingConstructorDeclaration(Node n) {
while (!n.getParent().isScript() && !n.getParent().isNormalBlock()) {
n = n.getParent();
}
return n;
}
private boolean isClassDefiningCall(Node next) {
return NodeUtil.isExprCall(next) &&
compiler.getCodingConvention().getClassesDefinedByCall(
next.getFirstChild()) != null;
}
}
/**
* Insert calls to the run-time type checking function {@code checkType}, which
* takes an expression to check and a list of checkers (one of which must
* match). It returns the expression back to facilitate checking of return
* values. We have checkers for value types, class types (user-defined and
* externed), and interface types.
*/
private class AddChecks
extends NodeTraversal.AbstractPostOrderCallback {
private AddChecks() {
}
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (NodeUtil.isInSyntheticScript(n)) {
return;
}
if (n.isFunction()) {
visitFunction(n);
} else if (n.isReturn()) {
visitReturn(t, n);
}
}
/**
* Insert checks for the parameters of the function.
*/
private void visitFunction(Node n) {
FunctionType funType = JSType.toMaybeFunctionType(n.getJSType());
Node block = n.getLastChild();
Node paramName = NodeUtil.getFunctionParameters(n).getFirstChild();
Node insertionPoint = null;
// To satisfy normalization constraints, the type checking must be
// added after any inner function declarations.
for (Node next = block.getFirstChild();
next != null && NodeUtil.isFunctionDeclaration(next);
next = next.getNext()) {
insertionPoint = next;
}
for (Node paramType : funType.getParameters()) {
// Can this ever happen?
if (paramName == null) {
return;
}
Node checkNode = createCheckTypeCallNode(
paramType.getJSType(), paramName.cloneTree());
if (checkNode == null) {
// We don't know how to check this parameter type.
paramName = paramName.getNext();
continue;
}
checkNode = IR.exprResult(checkNode);
if (insertionPoint == null) {
block.addChildToFront(checkNode);
} else {
block.addChildAfter(checkNode, insertionPoint);
}
compiler.reportChangeToEnclosingScope(block);
paramName = paramName.getNext();
insertionPoint = checkNode;
}
}
private void visitReturn(NodeTraversal t, Node n) {
Node function = t.getEnclosingFunction();
FunctionType funType = function.getJSType().toMaybeFunctionType();
Node retValue = n.getFirstChild();
if (retValue == null) {
return;
}
Node checkNode = createCheckTypeCallNode(
funType.getReturnType(), retValue.cloneTree());
if (checkNode == null) {
return;
}
n.replaceChild(retValue, checkNode);
t.reportCodeChange();
}
/**
* Creates a function call to check that the given expression matches the
* given type at runtime.
*
* <p>For example, if the type is {@code (string|Foo)}, the function call is
* {@code checkType(expr, [valueChecker('string'), classChecker('Foo')])}.
*
* @return the function call node or {@code null} if the type is not checked
*/
private Node createCheckTypeCallNode(JSType type, Node expr) {
Node arrayNode = IR.arraylit();
Collection<JSType> alternates;
if (type.isUnionType()) {
alternates = new TreeSet<>(ALPHA);
alternates.addAll(type.toMaybeUnionType().getAlternates());
} else {
alternates = ImmutableList.of(type);
}
for (JSType alternate : alternates) {
Node checkerNode = createCheckerNode(alternate);
if (checkerNode == null) {
return null;
}
arrayNode.addChildToBack(checkerNode);
}
return IR.call(jsCode("checkType"), expr, arrayNode);
}
/**
* Creates a node which evaluates to a checker for the given type (which
* must not be a union). We have checkers for value types, classes and
* interfaces.
*
* @return the checker node or {@code null} if the type is not checked
*/
private Node createCheckerNode(JSType type) {
if (type.isNullType()) {
return jsCode("nullChecker");
} else if (type.isBooleanValueType()
|| type.isNumberValueType()
|| type.isStringValueType()
|| type.isVoidType()) {
return IR.call(
jsCode("valueChecker"),
IR.string(type.toString()));
} else if (type.isInstanceType()) {
ObjectType objType = (ObjectType) type;
String refName = objType.getReferenceName();
if (refName.equals("Object")) {
return jsCode("objectChecker");
}
StaticSourceFile sourceFile =
NodeUtil.getSourceFile(objType.getConstructor().getSource());
if (sourceFile == null || sourceFile.isExtern()) {
return IR.call(
jsCode("externClassChecker"),
IR.string(refName));
}
return IR.call(
jsCode(objType.getConstructor().isInterface() ?
"interfaceChecker" : "classChecker"),
IR.string(refName));
} else if (type.isFunctionType()) {
return IR.call(jsCode("valueChecker"), IR.string("function"));
} else {
// We don't check this type (e.g. unknown & all types).
return null;
}
}
}
private void addBoilerplateCode() {
Node newNode = compiler.ensureLibraryInjected("runtime_type_check", false);
if (newNode != null) {
injectCustomLogFunction(newNode);
}
}
@VisibleForTesting
void injectCustomLogFunction(Node node) {
if (logFunction == null) {
return;
}
checkState(
NodeUtil.isValidQualifiedName(compiler.getFeatureSet(), logFunction),
"%s is not a valid qualified name", logFunction);
Node logOverride =
IR.exprResult(
IR.assign(
NodeUtil.newQName(compiler, "$jscomp.typecheck.log"),
NodeUtil.newQName(compiler, logFunction)));
checkState(node.getParent().isScript(), node.getParent());
node.getParent().addChildAfter(logOverride, node);
compiler.reportChangeToEnclosingScope(node);
}
private Node jsCode(String prop) {
return NodeUtil.newQName(
compiler, "$jscomp.typecheck." + prop);
}
}