forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopeInfo.cpp
More file actions
371 lines (331 loc) · 15.2 KB
/
ScopeInfo.cpp
File metadata and controls
371 lines (331 loc) · 15.2 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
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
//-------------------------------------------------------------------------------------------------------
// 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"
namespace Js
{
//
// Persist one symbol info into ScopeInfo.
//
void ScopeInfo::SaveSymbolInfo(Symbol* sym, MapSymbolData* mapSymbolData)
{
// We don't need to create slot for or save "arguments"
if (!sym->GetIsArguments()
// Function expression may not have nonLocalReference, exclude them.
&& (!sym->GetFuncExpr() || sym->GetHasNonLocalReference())
&& (!mapSymbolData->func->IsInnerArgumentsSymbol(sym) || mapSymbolData->func->GetHasArguments()))
{
// Any symbol may have non-local ref from deferred child. Allocate slot for it.
Assert(sym->GetHasNonLocalReference());
Js::PropertyId scopeSlot = sym->EnsureScopeSlot(mapSymbolData->func);
Js::PropertyId propertyId = sym->EnsurePosition(mapSymbolData->func);
this->SetSymbolId(scopeSlot, propertyId);
this->SetSymbolType(scopeSlot, sym->GetSymbolType());
this->SetHasFuncAssignment(scopeSlot, sym->GetHasFuncAssignment());
this->SetIsBlockVariable(scopeSlot, sym->GetIsBlockVar());
}
TRACE_BYTECODE(_u("%12s %d\n"), sym->GetName().GetBuffer(), sym->GetScopeSlot());
}
//
// Create scope info for a deferred child to refer to its parent ParseableFunctionInfo.
//
ScopeInfo* ScopeInfo::FromParent(FunctionBody* parent)
{
return RecyclerNew(parent->GetScriptContext()->GetRecycler(), // Alloc with ParseableFunctionInfo
ScopeInfo, parent, 0);
}
inline void AddSlotCount(int& count, int addCount)
{
if (addCount != 0 && Int32Math::Add(count, addCount, &count))
{
::Math::DefaultOverflowPolicy();
}
}
//
// Create scope info for an outer scope.
//
ScopeInfo* ScopeInfo::FromScope(ByteCodeGenerator* byteCodeGenerator, FunctionBody* parent, Scope* scope, ScriptContext *scriptContext)
{
int count = scope->Count();
// Add argsPlaceHolder which includes same name args and destructuring patterns on parameters
AddSlotCount(count, scope->GetFunc()->argsPlaceHolderSlotCount);
AddSlotCount(count, scope->GetFunc()->thisScopeSlot != Js::Constants::NoRegister ? 1 : 0);
AddSlotCount(count, scope->GetFunc()->newTargetScopeSlot != Js::Constants::NoRegister ? 1 : 0);
ScopeInfo* scopeInfo = RecyclerNewPlusZ(scriptContext->GetRecycler(),
count * sizeof(SymbolInfo),
ScopeInfo, parent, count);
scopeInfo->isDynamic = scope->GetIsDynamic();
scopeInfo->isObject = scope->GetIsObject();
scopeInfo->mustInstantiate = scope->GetMustInstantiate();
scopeInfo->isCached = (scope->GetFunc()->GetBodyScope() == scope) && scope->GetFunc()->GetHasCachedScope();
scopeInfo->isGlobalEval = scope->GetScopeType() == ScopeType_GlobalEvalBlock;
scopeInfo->canMergeWithBodyScope = scope->GetCanMergeWithBodyScope();
scopeInfo->hasLocalInClosure = scope->GetHasOwnLocalInClosure();
TRACE_BYTECODE(_u("\nSave ScopeInfo: %s parent: %s #symbols: %d %s\n"),
scope->GetFunc()->name, parent->GetDisplayName(), count, scopeInfo->isObject ? _u("isObject") : _u(""));
MapSymbolData mapSymbolData = { byteCodeGenerator, scope->GetFunc() };
scope->ForEachSymbol([&mapSymbolData, scopeInfo, scope](Symbol * sym)
{
Assert(scope == sym->GetScope());
scopeInfo->SaveSymbolInfo(sym, &mapSymbolData);
});
return scopeInfo;
}
//
// Clone a ScopeInfo object
//
ScopeInfo *ScopeInfo::CloneFor(ParseableFunctionInfo *body)
{
auto count = this->symbolCount;
auto symbolsSize = count * sizeof(SymbolInfo);
auto scopeInfo = RecyclerNewPlusZ(parent->GetScriptContext()->GetRecycler(), symbolsSize,
ScopeInfo, parent, count);
scopeInfo->isDynamic = this->isDynamic;
scopeInfo->isObject = this->isObject;
scopeInfo->mustInstantiate = this->mustInstantiate;
scopeInfo->isCached = this->isCached;
scopeInfo->isGlobalEval = this->isGlobalEval;
if (funcExprScopeInfo)
{
scopeInfo->funcExprScopeInfo = funcExprScopeInfo->CloneFor(body);
}
if (paramScopeInfo)
{
scopeInfo->paramScopeInfo = paramScopeInfo->CloneFor(body);
}
memcpy_s(scopeInfo->symbols, symbolsSize, this->symbols, symbolsSize);
return scopeInfo;
}
//
// Ensure the pids referenced by this scope are tracked.
//
void ScopeInfo::EnsurePidTracking(ScriptContext* scriptContext)
{
for (int i = 0; i < symbolCount; i++)
{
auto propertyName = scriptContext->GetPropertyName(symbols[i].propertyId);
scriptContext->TrackPid(propertyName);
}
if (funcExprScopeInfo)
{
funcExprScopeInfo->EnsurePidTracking(scriptContext);
}
if (paramScopeInfo)
{
paramScopeInfo->EnsurePidTracking(scriptContext);
}
}
//
// Save needed scope info for a deferred child func. The scope info is empty and only links to parent.
//
void ScopeInfo::SaveParentScopeInfo(FuncInfo* parentFunc, FuncInfo* func)
{
Assert(func->IsDeferred());
// Parent must be parsed
FunctionBody* parent = parentFunc->byteCodeFunction->GetFunctionBody();
ParseableFunctionInfo* funcBody = func->byteCodeFunction;
TRACE_BYTECODE(_u("\nSave ScopeInfo: %s parent: %s\n\n"),
funcBody->GetDisplayName(), parent->GetDisplayName());
funcBody->SetScopeInfo(FromParent(parent));
}
//
// Save scope info for an outer func which has deferred child.
//
void ScopeInfo::SaveScopeInfo(ByteCodeGenerator* byteCodeGenerator, FuncInfo* parentFunc, FuncInfo* func)
{
ParseableFunctionInfo* funcBody = func->byteCodeFunction;
Assert((!func->IsGlobalFunction() || byteCodeGenerator->GetFlags() & fscrEvalCode) &&
(func->HasDeferredChild() || (funcBody->IsReparsed())));
// If we are reparsing a deferred function, we already have correct "parent" info in
// funcBody->scopeInfo. parentFunc is the knopProg shell and should not be used in this
// case. We should use existing parent if available.
FunctionBody * parent = funcBody->GetScopeInfo() ?
funcBody->GetScopeInfo()->GetParent() :
parentFunc ? parentFunc->byteCodeFunction->GetFunctionBody() : nullptr;
ScopeInfo* funcExprScopeInfo = nullptr;
Scope* funcExprScope = func->GetFuncExprScope();
if (funcExprScope && funcExprScope->GetMustInstantiate())
{
funcExprScopeInfo = FromScope(byteCodeGenerator, parent, funcExprScope, funcBody->GetScriptContext());
}
Scope* bodyScope = func->IsGlobalFunction() ? func->GetGlobalEvalBlockScope() : func->GetBodyScope();
ScopeInfo* paramScopeInfo = nullptr;
Scope* paramScope = func->GetParamScope();
if (paramScope && bodyScope->GetMustInstantiate())
{
paramScopeInfo = FromScope(byteCodeGenerator, parent, paramScope, funcBody->GetScriptContext());
}
ScopeInfo* scopeInfo = FromScope(byteCodeGenerator, parent, bodyScope, funcBody->GetScriptContext());
scopeInfo->SetFuncExprScopeInfo(funcExprScopeInfo);
scopeInfo->SetParamScopeInfo(paramScopeInfo);
funcBody->SetScopeInfo(scopeInfo);
}
void ScopeInfo::SaveScopeInfoForDeferParse(ByteCodeGenerator* byteCodeGenerator, FuncInfo* parentFunc, FuncInfo* funcInfo)
{
// TODO: Not technically necessary, as we always do scope look up on eval if it is
// not found in the scope chain, and block scopes are always objects in eval.
// But if we save the global eval block scope for deferred child so that we can look up
// let/const in that scope with slot index instead of doing a scope lookup.
// We will have to implement encoding block scope info to enable, which will also
// enable defer parsing function that are in block scopes.
Scope* currentScope = byteCodeGenerator->GetCurrentScope();
Assert(currentScope == funcInfo->GetBodyScope());
if (funcInfo->IsDeferred())
{
// Don't need to remember the parent function if we have a global function
if (!parentFunc->IsGlobalFunction() ||
((byteCodeGenerator->GetFlags() & fscrEvalCode) && parentFunc->HasDeferredChild()))
{
// TODO: currently we only support defer nested function that is in function scope (no block scope, no with scope, etc.)
#if DBG
if (funcInfo->GetFuncExprScope() && funcInfo->GetFuncExprScope()->GetIsObject())
{
if (funcInfo->paramScope && !funcInfo->paramScope->GetCanMergeWithBodyScope())
{
Assert(currentScope->GetEnclosingScope()->GetEnclosingScope() == funcInfo->GetFuncExprScope());
}
else
{
Assert(currentScope->GetEnclosingScope() == funcInfo->GetFuncExprScope() &&
currentScope->GetEnclosingScope()->GetEnclosingScope() ==
(parentFunc->IsGlobalFunction() ? parentFunc->GetGlobalEvalBlockScope() : parentFunc->GetBodyScope()));
}
}
else
{
if (currentScope->GetEnclosingScope() == parentFunc->GetParamScope())
{
Assert(!parentFunc->GetParamScope()->GetCanMergeWithBodyScope());
Assert(funcInfo->GetParamScope()->GetCanMergeWithBodyScope());
}
else if (currentScope->GetEnclosingScope() == funcInfo->GetParamScope())
{
Assert(!funcInfo->GetParamScope()->GetCanMergeWithBodyScope());
}
else
{
Assert(currentScope->GetEnclosingScope() ==
(parentFunc->IsGlobalFunction() ? parentFunc->GetGlobalEvalBlockScope() : parentFunc->GetBodyScope()));
}
}
#endif
Js::ScopeInfo::SaveParentScopeInfo(parentFunc, funcInfo);
}
}
else if (funcInfo->HasDeferredChild() ||
(!funcInfo->IsGlobalFunction() &&
funcInfo->byteCodeFunction &&
funcInfo->byteCodeFunction->IsReparsed() &&
funcInfo->byteCodeFunction->GetFunctionBody()->HasAllNonLocalReferenced()))
{
// When we reparse due to attach, we would need to capture all of them, since they were captured before going to debug mode.
Js::ScopeInfo::SaveScopeInfo(byteCodeGenerator, parentFunc, funcInfo);
}
}
//
// Load persisted scope info.
//
void ScopeInfo::GetScopeInfo(Parser *parser, ByteCodeGenerator* byteCodeGenerator, FuncInfo* funcInfo, Scope* scope)
{
ScriptContext* scriptContext;
ArenaAllocator* alloc;
// Load scope attributes and push onto scope stack.
scope->SetIsDynamic(this->isDynamic);
if (this->isObject)
{
scope->SetIsObject();
}
scope->SetMustInstantiate(this->mustInstantiate);
if (!this->GetCanMergeWithBodyScope())
{
scope->SetCannotMergeWithBodyScope();
}
scope->SetHasOwnLocalInClosure(this->hasLocalInClosure);
if (parser)
{
scriptContext = parser->GetScriptContext();
alloc = parser->GetAllocator();
}
else
{
TRACE_BYTECODE(_u("\nRestore ScopeInfo: %s #symbols: %d %s\n"),
funcInfo->name, symbolCount, isObject ? _u("isObject") : _u(""));
Assert(!this->isCached || scope == funcInfo->GetBodyScope());
funcInfo->SetHasCachedScope(this->isCached);
byteCodeGenerator->PushScope(scope);
if (byteCodeGenerator->UseParserBindings())
{
// The scope is already populated, so we're done.
return;
}
scriptContext = byteCodeGenerator->GetScriptContext();
alloc = byteCodeGenerator->GetAllocator();
}
// Load scope symbols
// On first access to the scopeinfo, replace the ID's with PropertyRecord*'s to save the dictionary lookup
// on later accesses. Replace them all before allocating Symbol's to prevent inconsistency on OOM.
if (!this->areNamesCached && !PHASE_OFF1(Js::CacheScopeInfoNamesPhase))
{
for (int i = 0; i < symbolCount; i++)
{
PropertyId propertyId = GetSymbolId(i);
if (propertyId != 0) // There may be empty slots, e.g. "arguments" may have no slot
{
PropertyRecord const* name = scriptContext->GetPropertyName(propertyId);
this->SetPropertyName(i, name);
}
}
this->areNamesCached = true;
}
for (int i = 0; i < symbolCount; i++)
{
PropertyRecord const* name = nullptr;
if (this->areNamesCached)
{
name = this->GetPropertyName(i);
}
else
{
PropertyId propertyId = GetSymbolId(i);
if (propertyId != 0) // There may be empty slots, e.g. "arguments" may have no slot
{
name = scriptContext->GetPropertyName(propertyId);
}
}
if (name != nullptr)
{
SymbolType symbolType = GetSymbolType(i);
SymbolName symName(name->GetBuffer(), name->GetLength());
Symbol *sym = Anew(alloc, Symbol, symName, nullptr, symbolType);
sym->SetScopeSlot(static_cast<PropertyId>(i));
sym->SetIsBlockVar(GetIsBlockVariable(i));
if (GetHasFuncAssignment(i))
{
sym->RestoreHasFuncAssignment();
}
scope->AddNewSymbol(sym);
if (parser)
{
parser->RestorePidRefForSym(sym);
}
TRACE_BYTECODE(_u("%12s %d\n"), sym->GetName().GetBuffer(), sym->GetScopeSlot());
}
}
this->scope = scope;
DebugOnly(scope->isRestored = true);
}
ScopeInfo::AutoCapturesAllScope::AutoCapturesAllScope(Scope* scope, bool turnOn)
: scope(scope)
{
oldCapturesAll = scope->GetCapturesAll();
if (turnOn)
{
scope->SetCapturesAll(true);
}
}
ScopeInfo::AutoCapturesAllScope::~AutoCapturesAllScope()
{
scope->SetCapturesAll(oldCapturesAll);
}
} // namespace Js