/
FunctionNode.ts
145 lines (130 loc) · 4.65 KB
/
FunctionNode.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
import { CallOptions } from '../../CallOptions';
import { BROKEN_FLOW_NONE, HasEffectsContext, InclusionContext } from '../../ExecutionContext';
import { EVENT_CALLED, NodeEvent } from '../../NodeEvents';
import FunctionScope from '../../scopes/FunctionScope';
import { ObjectPath, UnknownKey, UNKNOWN_PATH } from '../../utils/PathTracker';
import BlockStatement from '../BlockStatement';
import Identifier, { IdentifierWithVariable } from '../Identifier';
import RestElement from '../RestElement';
import SpreadElement from '../SpreadElement';
import { ExpressionEntity, UNKNOWN_EXPRESSION } from './Expression';
import { ExpressionNode, GenericEsTreeNode, IncludeChildren, NodeBase } from './Node';
import { ObjectEntity } from './ObjectEntity';
import { OBJECT_PROTOTYPE } from './ObjectPrototype';
import { PatternNode } from './Pattern';
export default class FunctionNode extends NodeBase {
async!: boolean;
body!: BlockStatement;
id!: IdentifierWithVariable | null;
params!: PatternNode[];
preventChildBlockScope!: true;
scope!: FunctionScope;
private isPrototypeDeoptimized = false;
createScope(parentScope: FunctionScope) {
this.scope = new FunctionScope(parentScope, this.context);
}
deoptimizePath(path: ObjectPath) {
if (path.length === 1) {
if (path[0] === 'prototype') {
this.isPrototypeDeoptimized = true;
} else if (path[0] === UnknownKey) {
this.isPrototypeDeoptimized = true;
// A reassignment of UNKNOWN_PATH is considered equivalent to having lost track
// which means the return expression needs to be reassigned as well
this.scope.getReturnExpression().deoptimizePath(UNKNOWN_PATH);
}
}
}
deoptimizeThisOnEventAtPath(event: NodeEvent, path: ObjectPath, thisParameter: ExpressionEntity) {
if (event === EVENT_CALLED) {
if (path.length > 0 ) {
thisParameter.deoptimizePath(UNKNOWN_PATH);
} else {
this.scope.thisVariable.addEntityToBeDeoptimized(thisParameter);
}
}
}
getReturnExpressionWhenCalledAtPath(path: ObjectPath) {
return path.length === 0 ? this.scope.getReturnExpression() : UNKNOWN_EXPRESSION;
}
hasEffects() {
return this.id !== null && this.id.hasEffects();
}
hasEffectsWhenAccessedAtPath(path: ObjectPath) {
if (path.length <= 1) return false;
return path.length > 2 || path[0] !== 'prototype' || this.isPrototypeDeoptimized;
}
hasEffectsWhenAssignedAtPath(path: ObjectPath) {
if (path.length <= 1) {
return false;
}
return path.length > 2 || path[0] !== 'prototype' || this.isPrototypeDeoptimized;
}
hasEffectsWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
context: HasEffectsContext
) {
if (path.length > 0) return true;
for (const param of this.params) {
if (param.hasEffects(context)) return true;
}
const thisInit = context.replacedVariableInits.get(this.scope.thisVariable);
context.replacedVariableInits.set(
this.scope.thisVariable,
callOptions.withNew ? new ObjectEntity({}, OBJECT_PROTOTYPE) : UNKNOWN_EXPRESSION
);
const { brokenFlow, ignore } = context;
context.ignore = {
breaks: false,
continues: false,
labels: new Set(),
returnAwaitYield: true
};
if (this.body.hasEffects(context)) return true;
context.brokenFlow = brokenFlow;
if (thisInit) {
context.replacedVariableInits.set(this.scope.thisVariable, thisInit);
} else {
context.replacedVariableInits.delete(this.scope.thisVariable);
}
context.ignore = ignore;
return false;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
this.included = true;
if (this.id) this.id.include();
const hasArguments = this.scope.argumentsVariable.included;
for (const param of this.params) {
if (!(param instanceof Identifier) || hasArguments) {
param.include(context, includeChildrenRecursively);
}
}
const { brokenFlow } = context;
context.brokenFlow = BROKEN_FLOW_NONE;
this.body.include(context, includeChildrenRecursively);
context.brokenFlow = brokenFlow;
}
includeCallArguments(context: InclusionContext, args: (ExpressionNode | SpreadElement)[]): void {
this.scope.includeCallArguments(context, args);
}
initialise() {
if (this.id !== null) {
this.id.declare('function', this);
}
this.scope.addParameterVariables(
this.params.map(param => param.declare('parameter', UNKNOWN_EXPRESSION)),
this.params[this.params.length - 1] instanceof RestElement
);
this.body.addImplicitReturnExpressionToScope();
}
parseNode(esTreeNode: GenericEsTreeNode) {
this.body = new this.context.nodeConstructors.BlockStatement(
esTreeNode.body,
this,
this.scope.hoistedBodyVarScope
) as BlockStatement;
super.parseNode(esTreeNode);
}
}
FunctionNode.prototype.preventChildBlockScope = true;