forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtf8SourceInfo.cpp
More file actions
437 lines (378 loc) · 16.6 KB
/
Copy pathUtf8SourceInfo.cpp
File metadata and controls
437 lines (378 loc) · 16.6 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeBasePch.h"
#ifdef ENABLE_SCRIPT_DEBUGGING
#include "Debug/DiagProbe.h"
#include "Debug/BreakpointProbe.h"
#include "Debug/DebugDocument.h"
#include "Debug/DebugManager.h"
#endif
namespace Js
{
// if m_cchLength < 0 it came from an external source.
// If m_cbLength > abs(m_cchLength) then m_utf8Source contains non-ASCII (multi-byte encoded) characters.
Utf8SourceInfo::Utf8SourceInfo(ISourceHolder* mappableSource, int32 cchLength,
SRCINFO const* srcInfo, DWORD_PTR secondaryHostSourceContext,
ScriptContext* scriptContext, bool isLibraryCode, Js::Var scriptSource):
sourceHolder(mappableSource),
m_cchLength(cchLength),
m_srcInfo(srcInfo),
m_secondaryHostSourceContext(secondaryHostSourceContext),
#ifdef ENABLE_SCRIPT_DEBUGGING
m_debugDocument(nullptr),
#endif
m_sourceInfoId(scriptContext->GetThreadContext()->NewSourceInfoNumber()),
m_isCesu8(false),
m_isLibraryCode(isLibraryCode),
m_isXDomain(false),
m_isXDomainString(false),
m_scriptContext(scriptContext),
m_lineOffsetCache(nullptr),
m_deferredFunctionsDictionary(nullptr),
m_deferredFunctionsInitialized(false),
topLevelFunctionInfoList(nullptr),
#ifdef ENABLE_SCRIPT_DEBUGGING
debugModeSource(nullptr),
debugModeSourceIsEmpty(false),
debugModeSourceLength(0),
m_isInDebugMode(false),
#endif
callerUtf8SourceInfo(nullptr),
boundedPropertyRecordHashSet(scriptContext->GetRecycler())
#ifndef NTBUILD
,sourceRef(scriptSource)
#endif
{
#ifdef ENABLE_SCRIPT_DEBUGGING
if (!sourceHolder->IsDeferrable())
{
this->debugModeSource = this->sourceHolder->GetSource(_u("Entering Debug Mode"));
this->debugModeSourceLength = this->sourceHolder->GetByteLength(_u("Entering Debug Mode"));
this->debugModeSourceIsEmpty = !this->HasSource() || this->debugModeSource == nullptr;
}
#endif
}
LPCUTF8 Utf8SourceInfo::GetSource(const char16 * reason) const
{
AssertMsg(this->sourceHolder != nullptr, "We have no source mapper.");
#ifdef ENABLE_SCRIPT_DEBUGGING
if (this->IsInDebugMode())
{
AssertMsg(this->debugModeSource != nullptr || this->debugModeSourceIsEmpty, "Debug mode source should have been set by this point.");
return debugModeSource;
}
else
#endif
{
return sourceHolder->GetSource(reason == nullptr ? _u("Utf8SourceInfo::GetSource") : reason);
}
}
size_t Utf8SourceInfo::GetCbLength(const char16 * reason) const
{
AssertMsg(this->sourceHolder != nullptr, "We have no source mapper.");
#ifdef ENABLE_SCRIPT_DEBUGGING
if (this->IsInDebugMode())
{
AssertMsg(this->debugModeSource != nullptr || this->debugModeSourceIsEmpty, "Debug mode source should have been set by this point.");
return debugModeSourceLength;
}
else
#endif
{
return sourceHolder->GetByteLength(reason == nullptr ? _u("Utf8SourceInfo::GetSource") : reason);
}
}
void
Utf8SourceInfo::Dispose(bool isShutdown)
{
#ifdef ENABLE_SCRIPT_DEBUGGING
ClearDebugDocument();
this->debugModeSource = nullptr;
#endif
#ifndef NTBUILD
this->sourceRef = nullptr;
#endif
};
enum
{
fsiHostManaged = 0x01,
fsiScriptlet = 0x02,
fsiDeferredParse = 0x04
};
void Utf8SourceInfo::RemoveFunctionBody(FunctionBody* functionBody)
{
Assert(this->functionBodyDictionary);
const LocalFunctionId functionId = functionBody->GetLocalFunctionId();
Assert(functionBodyDictionary->Item(functionId) == functionBody);
functionBodyDictionary->Remove(functionId);
functionBody->SetIsFuncRegistered(false);
}
void Utf8SourceInfo::SetFunctionBody(FunctionBody * functionBody)
{
Assert(this->m_scriptContext == functionBody->GetScriptContext());
Assert(this->functionBodyDictionary);
// Only register a function body when source info is ready. Note that m_pUtf8Source can still be null for lib script.
Assert(functionBody->GetSourceIndex() != Js::Constants::InvalidSourceIndex);
Assert(!functionBody->GetIsFuncRegistered());
const LocalFunctionId functionId = functionBody->GetLocalFunctionId();
FunctionBody* oldFunctionBody = nullptr;
if (functionBodyDictionary->TryGetValue(functionId, &oldFunctionBody)) {
Assert(oldFunctionBody != functionBody);
oldFunctionBody->SetIsFuncRegistered(false);
}
functionBodyDictionary->Item(functionId, functionBody);
functionBody->SetIsFuncRegistered(true);
}
void Utf8SourceInfo::AddTopLevelFunctionInfo(FunctionInfo * functionInfo, Recycler * recycler)
{
JsUtil::List<FunctionInfo *, Recycler> * list = EnsureTopLevelFunctionInfoList(recycler);
Assert(!list->Contains(functionInfo));
list->Add(functionInfo);
}
void Utf8SourceInfo::ClearTopLevelFunctionInfoList()
{
if (this->topLevelFunctionInfoList)
{
this->topLevelFunctionInfoList->Clear();
}
}
JsUtil::List<FunctionInfo *, Recycler> *
Utf8SourceInfo::EnsureTopLevelFunctionInfoList(Recycler * recycler)
{
if (this->topLevelFunctionInfoList == nullptr)
{
this->topLevelFunctionInfoList = JsUtil::List<FunctionInfo *, Recycler>::New(recycler);
}
return this->topLevelFunctionInfoList;
}
void Utf8SourceInfo::EnsureInitialized(int initialFunctionCount)
{
ThreadContext* threadContext = ThreadContext::GetContextForCurrentThread();
Recycler* recycler = threadContext->GetRecycler();
if (this->functionBodyDictionary == nullptr)
{
// This collection is allocated with leaf allocation policy. The references to the function body
// here does not keep the function alive. However, the functions remove themselves at finalize
// so if a function actually is in this map, it means that it is alive.
this->functionBodyDictionary = RecyclerNew(recycler, FunctionBodyDictionary, recycler,
initialFunctionCount, threadContext->GetFunctionBodyLock());
}
if (CONFIG_FLAG(DeferTopLevelTillFirstCall) && !m_deferredFunctionsInitialized)
{
Assert(this->m_deferredFunctionsDictionary == nullptr);
this->m_deferredFunctionsDictionary = RecyclerNew(recycler, DeferredFunctionsDictionary, recycler,
initialFunctionCount, threadContext->GetFunctionBodyLock());
m_deferredFunctionsInitialized = true;
}
}
Utf8SourceInfo*
Utf8SourceInfo::NewWithHolder(ScriptContext* scriptContext, ISourceHolder* sourceHolder,
int32 length, SRCINFO const* srcInfo, bool isLibraryCode, Js::Var scriptSource)
{
// TODO: make this finalizable? Or have a finalizable version which would HeapDelete the string? Is this needed?
DWORD_PTR secondaryHostSourceContext = Js::Constants::NoHostSourceContext;
#ifdef ENABLE_SCRIPT_DEBUGGING
if (srcInfo->sourceContextInfo->IsDynamic())
{
secondaryHostSourceContext = scriptContext->GetThreadContext()->GetDebugManager()->AllocateSecondaryHostSourceContext();
}
#endif
Recycler * recycler = scriptContext->GetRecycler();
Utf8SourceInfo* toReturn = RecyclerNewFinalized(recycler,
Utf8SourceInfo, sourceHolder, length, SRCINFO::Copy(recycler, srcInfo),
secondaryHostSourceContext, scriptContext, isLibraryCode, scriptSource);
#ifdef ENABLE_SCRIPT_DEBUGGING
if (!isLibraryCode && scriptContext->IsScriptContextInDebugMode())
{
toReturn->debugModeSource = sourceHolder->GetSource(_u("Debug Mode Loading"));
toReturn->debugModeSourceLength = sourceHolder->GetByteLength(_u("Debug Mode Loading"));
toReturn->debugModeSourceIsEmpty = toReturn->debugModeSource == nullptr || sourceHolder->IsEmpty();
}
#endif
return toReturn;
}
Utf8SourceInfo*
Utf8SourceInfo::New(ScriptContext* scriptContext, LPCUTF8 utf8String, int32 length,
size_t numBytes, SRCINFO const* srcInfo, bool isLibraryCode)
{
utf8char_t * newUtf8String = RecyclerNewArrayLeaf(scriptContext->GetRecycler(), utf8char_t, numBytes + 1);
js_memcpy_s(newUtf8String, numBytes + 1, utf8String, numBytes + 1);
return NewWithNoCopy(scriptContext, newUtf8String, length, numBytes,
srcInfo, isLibraryCode);
}
Utf8SourceInfo*
Utf8SourceInfo::NewWithNoCopy(ScriptContext* scriptContext, LPCUTF8 utf8String,
int32 length, size_t numBytes, SRCINFO const * srcInfo, bool isLibraryCode, Js::Var scriptSource)
{
ISourceHolder* sourceHolder = RecyclerNew(scriptContext->GetRecycler(), SimpleSourceHolder, utf8String, numBytes);
return NewWithHolder(scriptContext, sourceHolder, length, srcInfo, isLibraryCode, scriptSource);
}
HRESULT Utf8SourceInfo::EnsureLineOffsetCacheNoThrow()
{
HRESULT hr = S_OK;
// This is a double check, otherwise we would have to have a private function, and add an assert.
// Basically the outer check is for try/catch, inner check (inside EnsureLineOffsetCache) is for that method as its public.
if (this->m_lineOffsetCache == nullptr)
{
BEGIN_TRANSLATE_EXCEPTION_AND_ERROROBJECT_TO_HRESULT_NESTED
{
this->EnsureLineOffsetCache();
}
END_TRANSLATE_EXCEPTION_AND_ERROROBJECT_TO_HRESULT_NOASSERT(hr);
}
return hr;
}
void Utf8SourceInfo::EnsureLineOffsetCache()
{
if (this->m_lineOffsetCache == nullptr)
{
LPCUTF8 sourceStart = this->GetSource(_u("Utf8SourceInfo::AllocateLineOffsetCache"));
LPCUTF8 sourceEnd = sourceStart + this->GetCbLength(_u("Utf8SourceInfo::AllocateLineOffsetCache"));
LPCUTF8 sourceAfterBOM = sourceStart;
charcount_t startChar = FunctionBody::SkipByteOrderMark(sourceAfterBOM /* byref */);
int64 byteStartOffset = (sourceAfterBOM - sourceStart);
Recycler* recycler = this->m_scriptContext->GetRecycler();
this->m_lineOffsetCache = RecyclerNew(recycler, LineOffsetCache, recycler, sourceAfterBOM, sourceEnd, startChar, (int)byteStartOffset);
}
}
Js::FunctionBody* Utf8SourceInfo::FindFunction(Js::LocalFunctionId id) const
{
Js::FunctionBody *matchedFunctionBody = nullptr;
if (this->functionBodyDictionary)
{
// Ignore return value - OK if function is not found.
this->functionBodyDictionary->TryGetValue(id, &matchedFunctionBody);
if (matchedFunctionBody == nullptr || matchedFunctionBody->IsPartialDeserializedFunction())
{
return nullptr;
}
}
return matchedFunctionBody;
}
void Utf8SourceInfo::GetLineInfoForCharPosition(charcount_t charPosition, charcount_t *outLineNumber, charcount_t *outColumn, charcount_t *outLineByteOffset, bool allowSlowLookup)
{
AssertMsg(this->m_lineOffsetCache != nullptr || allowSlowLookup, "LineOffsetCache wasn't created, EnsureLineOffsetCache should have been called.");
AssertMsg(outLineNumber != nullptr && outColumn != nullptr && outLineByteOffset != nullptr, "Expected out parameter's can't be a nullptr.");
charcount_t lineCharOffset = 0;
int line = 0;
if (this->m_lineOffsetCache == nullptr)
{
LPCUTF8 sourceStart = this->GetSource(_u("Utf8SourceInfo::AllocateLineOffsetCache"));
LPCUTF8 sourceEnd = sourceStart + this->GetCbLength(_u("Utf8SourceInfo::AllocateLineOffsetCache"));
LPCUTF8 sourceAfterBOM = sourceStart;
lineCharOffset = FunctionBody::SkipByteOrderMark(sourceAfterBOM /* byref */);
Assert((sourceAfterBOM - sourceStart) < MAXUINT32);
charcount_t byteStartOffset = (charcount_t)(sourceAfterBOM - sourceStart);
line = LineOffsetCache::FindLineForCharacterOffset(sourceAfterBOM, sourceEnd, lineCharOffset, byteStartOffset, charPosition);
*outLineByteOffset = byteStartOffset;
}
else
{
line = this->m_lineOffsetCache->GetLineForCharacterOffset(charPosition, &lineCharOffset, outLineByteOffset);
}
Assert(charPosition >= lineCharOffset);
*outLineNumber = line;
*outColumn = charPosition - lineCharOffset;
}
void Utf8SourceInfo::CreateLineOffsetCache(const charcount_t *lineCharacterOffsets, const charcount_t *lineByteOffsets, charcount_t numberOfItems)
{
AssertMsg(this->m_lineOffsetCache == nullptr, "LineOffsetCache is already initialized!");
Recycler* recycler = this->m_scriptContext->GetRecycler();
this->m_lineOffsetCache = RecyclerNew(recycler, LineOffsetCache, recycler, lineCharacterOffsets, lineByteOffsets, numberOfItems);
}
DWORD_PTR Utf8SourceInfo::GetHostSourceContext() const
{
return m_srcInfo->sourceContextInfo->dwHostSourceContext;
}
bool Utf8SourceInfo::IsDynamic() const
{
return m_srcInfo->sourceContextInfo->IsDynamic();
}
SourceContextInfo* Utf8SourceInfo::GetSourceContextInfo() const
{
return this->m_srcInfo->sourceContextInfo;
}
// Get's the first function in the function body dictionary
// Used if the caller want's any function in this source info
Js::FunctionBody* Utf8SourceInfo::GetAnyParsedFunction()
{
if (this->functionBodyDictionary != nullptr && this->functionBodyDictionary->Count() > 0)
{
FunctionBody* functionBody = nullptr;
int i = 0;
do
{
functionBody = this->functionBodyDictionary->GetValueAt(i);
if (functionBody != nullptr && functionBody->GetByteCode() == nullptr && !functionBody->GetIsFromNativeCodeModule()) functionBody = nullptr;
i++;
}
while (functionBody == nullptr && i < this->functionBodyDictionary->Count());
return functionBody;
}
return nullptr;
}
bool Utf8SourceInfo::IsHostManagedSource() const
{
return ((this->m_srcInfo->grfsi & fsiHostManaged) == fsiHostManaged);
}
void Utf8SourceInfo::SetCallerUtf8SourceInfo(Utf8SourceInfo* callerUtf8SourceInfo)
{
this->callerUtf8SourceInfo = callerUtf8SourceInfo;
}
Utf8SourceInfo* Utf8SourceInfo::GetCallerUtf8SourceInfo() const
{
return this->callerUtf8SourceInfo;
}
void Utf8SourceInfo::TrackDeferredFunction(Js::LocalFunctionId functionID, Js::ParseableFunctionInfo *function)
{
if (this->m_scriptContext->DoUndeferGlobalFunctions())
{
Assert(m_deferredFunctionsInitialized);
if (this->m_deferredFunctionsDictionary != nullptr)
{
this->m_deferredFunctionsDictionary->Add(functionID, function);
}
}
}
void Utf8SourceInfo::StopTrackingDeferredFunction(Js::LocalFunctionId functionID)
{
if (this->m_scriptContext->DoUndeferGlobalFunctions())
{
Assert(m_deferredFunctionsInitialized);
if (this->m_deferredFunctionsDictionary != nullptr)
{
this->m_deferredFunctionsDictionary->Remove(functionID);
}
}
}
#ifdef ENABLE_SCRIPT_DEBUGGING
void Utf8SourceInfo::ClearDebugDocument(bool close)
{
if (this->m_debugDocument != nullptr)
{
if (close)
{
m_debugDocument->CloseDocument();
}
this->m_debugDocument = nullptr;
}
}
#endif
#ifdef NTBUILD
bool Utf8SourceInfo::GetDebugDocumentName(BSTR * sourceName)
{
if (this->HasDebugDocument() && this->GetDebugDocument()->HasDocumentText())
{
// ToDo (SaAgarwa): Fix for JsRT debugging
IDebugDocumentText *documentText = static_cast<IDebugDocumentText *>(this->GetDebugDocument()->GetDocumentText());
if (documentText->GetName(DOCUMENTNAMETYPE_URL, sourceName) == S_OK)
{
return true;
}
}
return false;
}
#endif
}