forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbol.cpp
More file actions
215 lines (195 loc) · 6.63 KB
/
Symbol.cpp
File metadata and controls
215 lines (195 loc) · 6.63 KB
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeByteCodePch.h"
#if DBG_DUMP
static const char16 * const SymbolTypeNames[] = { _u("Function"), _u("Variable"), _u("MemberName"), _u("Formal"), _u("Unknown") };
#endif
bool Symbol::GetIsArguments() const
{
return decl != nullptr && (decl->grfpn & PNodeFlags::fpnArguments);
}
Js::PropertyId Symbol::EnsurePosition(ByteCodeGenerator* byteCodeGenerator)
{
// Guarantee that a symbol's name has a property ID mapping.
if (this->position == Js::Constants::NoProperty)
{
this->position = this->EnsurePositionNoCheck(byteCodeGenerator->TopFuncInfo());
}
return this->position;
}
Js::PropertyId Symbol::EnsurePosition(FuncInfo *funcInfo)
{
// Guarantee that a symbol's name has a property ID mapping.
if (this->position == Js::Constants::NoProperty)
{
this->position = this->EnsurePositionNoCheck(funcInfo);
}
return this->position;
}
Js::PropertyId Symbol::EnsurePositionNoCheck(FuncInfo *funcInfo)
{
return funcInfo->byteCodeFunction->GetOrAddPropertyIdTracked(this->GetName());
}
void Symbol::SaveToPropIdArray(Symbol *sym, Js::PropertyIdArray *propIds, ByteCodeGenerator *byteCodeGenerator, Js::PropertyId *pFirstSlot /* = null */)
{
if (sym)
{
Js::PropertyId slot = sym->scopeSlot;
if (slot != Js::Constants::NoProperty)
{
Assert((uint32)slot < propIds->count);
propIds->elements[slot] = sym->EnsurePosition(byteCodeGenerator);
if (pFirstSlot && !sym->GetIsArguments())
{
if (*pFirstSlot == Js::Constants::NoProperty ||
*pFirstSlot > slot)
{
*pFirstSlot = slot;
}
}
}
}
}
bool Symbol::NeedsSlotAlloc(FuncInfo *funcInfo)
{
return IsInSlot(funcInfo, true);
}
bool Symbol::IsInSlot(FuncInfo *funcInfo, bool ensureSlotAlloc)
{
if (this->GetIsGlobal() || this->GetIsModuleExportStorage())
{
return false;
}
if (funcInfo->GetHasHeapArguments() && this->GetIsFormal() && ByteCodeGenerator::NeedScopeObjectForArguments(funcInfo, funcInfo->root))
{
return true;
}
if (this->GetIsGlobalCatch())
{
return true;
}
if (this->scope->GetCapturesAll())
{
return true;
}
return this->GetHasNonLocalReference() && (ensureSlotAlloc || this->GetIsCommittedToSlot());
}
bool Symbol::GetIsCommittedToSlot() const
{
if (!PHASE_ON1(Js::DelayCapturePhase))
{
return true;
}
return isCommittedToSlot || this->scope->GetFunc()->GetCallsEval() || this->scope->GetFunc()->GetChildCallsEval();
}
Js::PropertyId Symbol::EnsureScopeSlot(FuncInfo *funcInfo)
{
if (this->NeedsSlotAlloc(funcInfo) && this->scopeSlot == Js::Constants::NoProperty)
{
this->scopeSlot = this->scope->AddScopeSlot();
}
return this->scopeSlot;
}
void Symbol::SetHasNonLocalReference()
{
this->hasNonLocalReference = true;
this->scope->SetHasOwnLocalInClosure(true);
}
void Symbol::SetHasMaybeEscapedUse(ByteCodeGenerator * byteCodeGenerator)
{
Assert(!this->GetIsMember());
if (!hasMaybeEscapedUse)
{
SetHasMaybeEscapedUseInternal(byteCodeGenerator);
}
}
void Symbol::SetHasMaybeEscapedUseInternal(ByteCodeGenerator * byteCodeGenerator)
{
Assert(!hasMaybeEscapedUse);
Assert(!this->GetIsFormal());
hasMaybeEscapedUse = true;
if (PHASE_TESTTRACE(Js::StackFuncPhase, byteCodeGenerator->TopFuncInfo()->byteCodeFunction))
{
Output::Print(_u("HasMaybeEscapedUse: %s\n"), this->GetName().GetBuffer());
Output::Flush();
}
if (this->GetHasFuncAssignment())
{
this->GetScope()->GetFunc()->SetHasMaybeEscapedNestedFunc(
DebugOnly(this->symbolType == STFunction ? _u("MaybeEscapedUseFuncDecl") : _u("MaybeEscapedUse")));
}
}
void Symbol::SetHasFuncAssignment(ByteCodeGenerator * byteCodeGenerator)
{
Assert(!this->GetIsMember());
if (!hasFuncAssignment)
{
SetHasFuncAssignmentInternal(byteCodeGenerator);
}
}
void Symbol::SetHasFuncAssignmentInternal(ByteCodeGenerator * byteCodeGenerator)
{
Assert(!hasFuncAssignment);
hasFuncAssignment = true;
FuncInfo * top = byteCodeGenerator->TopFuncInfo();
if (PHASE_TESTTRACE(Js::StackFuncPhase, top->byteCodeFunction))
{
Output::Print(_u("HasFuncAssignment: %s\n"), this->GetName().GetBuffer());
Output::Flush();
}
if (this->GetHasMaybeEscapedUse() || this->GetScope()->GetIsObject())
{
byteCodeGenerator->TopFuncInfo()->SetHasMaybeEscapedNestedFunc(DebugOnly(
this->GetIsFormal() ? _u("FormalAssignment") :
this->GetScope()->GetIsObject() ? _u("ObjectScopeAssignment") :
_u("MaybeEscapedUse")));
}
}
void Symbol::RestoreHasFuncAssignment()
{
Assert(hasFuncAssignment == (this->symbolType == STFunction));
Assert(this->GetIsFormal() || !this->GetHasMaybeEscapedUse());
hasFuncAssignment = true;
if (PHASE_TESTTRACE1(Js::StackFuncPhase))
{
Output::Print(_u("RestoreHasFuncAssignment: %s\n"), this->GetName().GetBuffer());
Output::Flush();
}
}
Symbol * Symbol::GetFuncScopeVarSym() const
{
if (!this->GetIsBlockVar())
{
return nullptr;
}
FuncInfo * parentFuncInfo = this->GetScope()->GetFunc();
if (parentFuncInfo->GetIsStrictMode())
{
return nullptr;
}
Symbol *fncScopeSym = parentFuncInfo->GetBodyScope()->FindLocalSymbol(this->GetName());
if (fncScopeSym == nullptr && parentFuncInfo->GetParamScope() != nullptr)
{
// We couldn't find the sym in the body scope, try finding it in the parameter scope.
Scope* paramScope = parentFuncInfo->GetParamScope();
fncScopeSym = paramScope->FindLocalSymbol(this->GetName());
}
Assert(fncScopeSym);
// Parser should have added a fake var decl node for block scoped functions in non-strict mode
// IsBlockVar() indicates a user let declared variable at function scope which
// shadows the function's var binding, thus only emit the var binding init if
// we do not have a block var symbol.
if (!fncScopeSym || fncScopeSym->GetIsBlockVar())
{
return nullptr;
}
return fncScopeSym;
}
#if DBG_DUMP
const char16 * Symbol::GetSymbolTypeName()
{
return SymbolTypeNames[symbolType];
}
#endif