Utf8SourceInfo.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeBasePch.h"
  6. #include "Debug/DiagProbe.h"
  7. #include "Debug/BreakpointProbe.h"
  8. #include "Debug/DebugDocument.h"
  9. #include "Debug/DebugManager.h"
  10. namespace Js
  11. {
  12. // if m_cchLength < 0 it came from an external source.
  13. // If m_cbLength > abs(m_cchLength) then m_utf8Source contains non-ASCII (multi-byte encoded) characters.
  14. Utf8SourceInfo::Utf8SourceInfo(ISourceHolder* mappableSource, int32 cchLength,
  15. SRCINFO const* srcInfo, DWORD_PTR secondaryHostSourceContext,
  16. ScriptContext* scriptContext, bool isLibraryCode, Js::Var scriptSource):
  17. sourceHolder(mappableSource),
  18. m_cchLength(cchLength),
  19. m_pOriginalSourceInfo(nullptr),
  20. m_srcInfo(srcInfo),
  21. m_secondaryHostSourceContext(secondaryHostSourceContext),
  22. m_debugDocument(nullptr),
  23. m_sourceInfoId(scriptContext->GetThreadContext()->NewSourceInfoNumber()),
  24. m_hasHostBuffer(false),
  25. m_isCesu8(false),
  26. m_isLibraryCode(isLibraryCode),
  27. m_isXDomain(false),
  28. m_isXDomainString(false),
  29. m_scriptContext(scriptContext),
  30. m_lineOffsetCache(nullptr),
  31. m_deferredFunctionsDictionary(nullptr),
  32. m_deferredFunctionsInitialized(false),
  33. topLevelFunctionInfoList(nullptr),
  34. debugModeSource(nullptr),
  35. debugModeSourceIsEmpty(false),
  36. debugModeSourceLength(0),
  37. m_isInDebugMode(false),
  38. callerUtf8SourceInfo(nullptr)
  39. #ifndef NTBUILD
  40. ,sourceRef(scriptSource)
  41. #endif
  42. {
  43. if (!sourceHolder->IsDeferrable())
  44. {
  45. this->debugModeSource = this->sourceHolder->GetSource(_u("Entering Debug Mode"));
  46. this->debugModeSourceLength = this->sourceHolder->GetByteLength(_u("Entering Debug Mode"));
  47. this->debugModeSourceIsEmpty = !this->HasSource() || this->debugModeSource == nullptr;
  48. }
  49. }
  50. LPCUTF8 Utf8SourceInfo::GetSource(const char16 * reason) const
  51. {
  52. AssertMsg(this->sourceHolder != nullptr, "We have no source mapper.");
  53. if (this->IsInDebugMode())
  54. {
  55. AssertMsg(this->debugModeSource != nullptr || this->debugModeSourceIsEmpty, "Debug mode source should have been set by this point.");
  56. return debugModeSource;
  57. }
  58. else
  59. {
  60. return sourceHolder->GetSource(reason == nullptr ? _u("Utf8SourceInfo::GetSource") : reason);
  61. }
  62. }
  63. size_t Utf8SourceInfo::GetCbLength(const char16 * reason) const
  64. {
  65. AssertMsg(this->sourceHolder != nullptr, "We have no source mapper.");
  66. if (this->IsInDebugMode())
  67. {
  68. AssertMsg(this->debugModeSource != nullptr || this->debugModeSourceIsEmpty, "Debug mode source should have been set by this point.");
  69. return debugModeSourceLength;
  70. }
  71. else
  72. {
  73. return sourceHolder->GetByteLength(reason == nullptr ? _u("Utf8SourceInfo::GetSource") : reason);
  74. }
  75. }
  76. void
  77. Utf8SourceInfo::Dispose(bool isShutdown)
  78. {
  79. ClearDebugDocument();
  80. #ifndef NTBUILD
  81. this->sourceRef = nullptr;
  82. #endif
  83. this->debugModeSource = nullptr;
  84. if (this->m_hasHostBuffer)
  85. {
  86. PERF_COUNTER_DEC(Basic, ScriptCodeBufferCount);
  87. HeapFree(GetProcessHeap(), 0 , m_pHostBuffer);
  88. m_pHostBuffer = nullptr;
  89. }
  90. };
  91. void
  92. Utf8SourceInfo::SetHostBuffer(BYTE * pcszCode)
  93. {
  94. Assert(!this->m_hasHostBuffer);
  95. Assert(this->m_pHostBuffer == nullptr);
  96. this->m_hasHostBuffer = true;
  97. this->m_pHostBuffer = pcszCode;
  98. }
  99. enum
  100. {
  101. fsiHostManaged = 0x01,
  102. fsiScriptlet = 0x02,
  103. fsiDeferredParse = 0x04
  104. };
  105. void Utf8SourceInfo::RemoveFunctionBody(FunctionBody* functionBody)
  106. {
  107. Assert(this->functionBodyDictionary);
  108. const LocalFunctionId functionId = functionBody->GetLocalFunctionId();
  109. Assert(functionBodyDictionary->Item(functionId) == functionBody);
  110. functionBodyDictionary->Remove(functionId);
  111. functionBody->SetIsFuncRegistered(false);
  112. }
  113. void Utf8SourceInfo::SetFunctionBody(FunctionBody * functionBody)
  114. {
  115. Assert(this->m_scriptContext == functionBody->GetScriptContext());
  116. Assert(this->functionBodyDictionary);
  117. // Only register a function body when source info is ready. Note that m_pUtf8Source can still be null for lib script.
  118. Assert(functionBody->GetSourceIndex() != Js::Constants::InvalidSourceIndex);
  119. Assert(!functionBody->GetIsFuncRegistered());
  120. const LocalFunctionId functionId = functionBody->GetLocalFunctionId();
  121. FunctionBody* oldFunctionBody = nullptr;
  122. if (functionBodyDictionary->TryGetValue(functionId, &oldFunctionBody)) {
  123. Assert(oldFunctionBody != functionBody);
  124. oldFunctionBody->SetIsFuncRegistered(false);
  125. }
  126. functionBodyDictionary->Item(functionId, functionBody);
  127. functionBody->SetIsFuncRegistered(true);
  128. }
  129. void Utf8SourceInfo::AddTopLevelFunctionInfo(FunctionInfo * functionInfo, Recycler * recycler)
  130. {
  131. JsUtil::List<FunctionInfo *, Recycler> * list = EnsureTopLevelFunctionInfoList(recycler);
  132. Assert(!list->Contains(functionInfo));
  133. list->Add(functionInfo);
  134. }
  135. void Utf8SourceInfo::ClearTopLevelFunctionInfoList()
  136. {
  137. if (this->topLevelFunctionInfoList)
  138. {
  139. this->topLevelFunctionInfoList->Clear();
  140. }
  141. }
  142. JsUtil::List<FunctionInfo *, Recycler> *
  143. Utf8SourceInfo::EnsureTopLevelFunctionInfoList(Recycler * recycler)
  144. {
  145. if (this->topLevelFunctionInfoList == nullptr)
  146. {
  147. this->topLevelFunctionInfoList = JsUtil::List<FunctionInfo *, Recycler>::New(recycler);
  148. }
  149. return this->topLevelFunctionInfoList;
  150. }
  151. void Utf8SourceInfo::EnsureInitialized(int initialFunctionCount)
  152. {
  153. ThreadContext* threadContext = ThreadContext::GetContextForCurrentThread();
  154. Recycler* recycler = threadContext->GetRecycler();
  155. if (this->functionBodyDictionary == nullptr)
  156. {
  157. // This collection is allocated with leaf allocation policy. The references to the function body
  158. // here does not keep the function alive. However, the functions remove themselves at finalize
  159. // so if a function actually is in this map, it means that it is alive.
  160. this->functionBodyDictionary = RecyclerNew(recycler, FunctionBodyDictionary, recycler,
  161. initialFunctionCount, threadContext->GetEtwRundownCriticalSection());
  162. }
  163. if (CONFIG_FLAG(DeferTopLevelTillFirstCall) && !m_deferredFunctionsInitialized)
  164. {
  165. Assert(this->m_deferredFunctionsDictionary == nullptr);
  166. this->m_deferredFunctionsDictionary = RecyclerNew(recycler, DeferredFunctionsDictionary, recycler,
  167. initialFunctionCount, threadContext->GetEtwRundownCriticalSection());
  168. m_deferredFunctionsInitialized = true;
  169. }
  170. }
  171. Utf8SourceInfo*
  172. Utf8SourceInfo::NewWithHolder(ScriptContext* scriptContext, ISourceHolder* sourceHolder,
  173. int32 length, SRCINFO const* srcInfo, bool isLibraryCode, Js::Var scriptSource)
  174. {
  175. // TODO: make this finalizable? Or have a finalizable version which would HeapDelete the string? Is this needed?
  176. DWORD_PTR secondaryHostSourceContext = Js::Constants::NoHostSourceContext;
  177. if (srcInfo->sourceContextInfo->IsDynamic())
  178. {
  179. secondaryHostSourceContext = scriptContext->GetThreadContext()->GetDebugManager()->AllocateSecondaryHostSourceContext();
  180. }
  181. Recycler * recycler = scriptContext->GetRecycler();
  182. Utf8SourceInfo* toReturn = RecyclerNewFinalized(recycler,
  183. Utf8SourceInfo, sourceHolder, length, SRCINFO::Copy(recycler, srcInfo),
  184. secondaryHostSourceContext, scriptContext, isLibraryCode, scriptSource);
  185. if (!isLibraryCode && scriptContext->IsScriptContextInDebugMode())
  186. {
  187. toReturn->debugModeSource = sourceHolder->GetSource(_u("Debug Mode Loading"));
  188. toReturn->debugModeSourceLength = sourceHolder->GetByteLength(_u("Debug Mode Loading"));
  189. toReturn->debugModeSourceIsEmpty = toReturn->debugModeSource == nullptr || sourceHolder->IsEmpty();
  190. }
  191. return toReturn;
  192. }
  193. Utf8SourceInfo*
  194. Utf8SourceInfo::New(ScriptContext* scriptContext, LPCUTF8 utf8String, int32 length,
  195. size_t numBytes, SRCINFO const* srcInfo, bool isLibraryCode, Js::Var scriptSource)
  196. {
  197. utf8char_t * newUtf8String = RecyclerNewArrayLeaf(scriptContext->GetRecycler(), utf8char_t, numBytes + 1);
  198. js_memcpy_s(newUtf8String, numBytes + 1, utf8String, numBytes + 1);
  199. return NewWithNoCopy(scriptContext, newUtf8String, length, numBytes,
  200. srcInfo, isLibraryCode, scriptSource);
  201. }
  202. Utf8SourceInfo*
  203. Utf8SourceInfo::NewWithNoCopy(ScriptContext* scriptContext, LPCUTF8 utf8String,
  204. int32 length, size_t numBytes, SRCINFO const * srcInfo, bool isLibraryCode, Js::Var scriptSource)
  205. {
  206. ISourceHolder* sourceHolder = RecyclerNew(scriptContext->GetRecycler(), SimpleSourceHolder, utf8String, numBytes);
  207. return NewWithHolder(scriptContext, sourceHolder, length, srcInfo, isLibraryCode, scriptSource);
  208. }
  209. Utf8SourceInfo*
  210. Utf8SourceInfo::Clone(ScriptContext* scriptContext, const Utf8SourceInfo* sourceInfo)
  211. {
  212. Utf8SourceInfo* newSourceInfo = Utf8SourceInfo::NewWithHolder(scriptContext,
  213. sourceInfo->GetSourceHolder()->Clone(scriptContext), sourceInfo->m_cchLength,
  214. SRCINFO::Copy(scriptContext->GetRecycler(), sourceInfo->GetSrcInfo()),
  215. sourceInfo->m_isLibraryCode);
  216. newSourceInfo->m_isXDomain = sourceInfo->m_isXDomain;
  217. newSourceInfo->m_isXDomainString = sourceInfo->m_isXDomainString;
  218. newSourceInfo->m_isLibraryCode = sourceInfo->m_isLibraryCode;
  219. newSourceInfo->SetIsCesu8(sourceInfo->GetIsCesu8());
  220. newSourceInfo->m_lineOffsetCache = sourceInfo->m_lineOffsetCache;
  221. if (scriptContext->IsScriptContextInDebugMode() && !newSourceInfo->GetIsLibraryCode())
  222. {
  223. newSourceInfo->SetInDebugMode(true);
  224. }
  225. return newSourceInfo;
  226. }
  227. HRESULT Utf8SourceInfo::EnsureLineOffsetCacheNoThrow()
  228. {
  229. HRESULT hr = S_OK;
  230. // This is a double check, otherwise we would have to have a private function, and add an assert.
  231. // Basically the outer check is for try/catch, inner check (inside EnsureLineOffsetCache) is for that method as its public.
  232. if (this->m_lineOffsetCache == nullptr)
  233. {
  234. BEGIN_TRANSLATE_EXCEPTION_AND_ERROROBJECT_TO_HRESULT_NESTED
  235. {
  236. this->EnsureLineOffsetCache();
  237. }
  238. END_TRANSLATE_EXCEPTION_AND_ERROROBJECT_TO_HRESULT_NOASSERT(hr);
  239. }
  240. return hr;
  241. }
  242. void Utf8SourceInfo::EnsureLineOffsetCache()
  243. {
  244. if (this->m_lineOffsetCache == nullptr)
  245. {
  246. LPCUTF8 sourceStart = this->GetSource(_u("Utf8SourceInfo::AllocateLineOffsetCache"));
  247. LPCUTF8 sourceEnd = sourceStart + this->GetCbLength(_u("Utf8SourceInfo::AllocateLineOffsetCache"));
  248. LPCUTF8 sourceAfterBOM = sourceStart;
  249. charcount_t startChar = FunctionBody::SkipByteOrderMark(sourceAfterBOM /* byref */);
  250. int64 byteStartOffset = (sourceAfterBOM - sourceStart);
  251. Recycler* recycler = this->m_scriptContext->GetRecycler();
  252. this->m_lineOffsetCache = RecyclerNew(recycler, JsUtil::LineOffsetCache<Recycler>, recycler, sourceAfterBOM, sourceEnd, startChar, (int)byteStartOffset);
  253. }
  254. }
  255. Js::FunctionBody* Utf8SourceInfo::FindFunction(Js::LocalFunctionId id) const
  256. {
  257. Js::FunctionBody *matchedFunctionBody = nullptr;
  258. if (this->functionBodyDictionary)
  259. {
  260. // Ignore return value - OK if function is not found.
  261. this->functionBodyDictionary->TryGetValue(id, &matchedFunctionBody);
  262. if (matchedFunctionBody == nullptr || matchedFunctionBody->IsPartialDeserializedFunction())
  263. {
  264. return nullptr;
  265. }
  266. }
  267. return matchedFunctionBody;
  268. }
  269. void Utf8SourceInfo::GetLineInfoForCharPosition(charcount_t charPosition, charcount_t *outLineNumber, charcount_t *outColumn, charcount_t *outLineByteOffset, bool allowSlowLookup)
  270. {
  271. AssertMsg(this->m_lineOffsetCache != nullptr || allowSlowLookup, "LineOffsetCache wasn't created, EnsureLineOffsetCache should have been called.");
  272. AssertMsg(outLineNumber != nullptr && outColumn != nullptr && outLineByteOffset != nullptr, "Expected out parameter's can't be a nullptr.");
  273. charcount_t lineCharOffset = 0;
  274. int line = 0;
  275. if (this->m_lineOffsetCache == nullptr)
  276. {
  277. LPCUTF8 sourceStart = this->GetSource(_u("Utf8SourceInfo::AllocateLineOffsetCache"));
  278. LPCUTF8 sourceEnd = sourceStart + this->GetCbLength(_u("Utf8SourceInfo::AllocateLineOffsetCache"));
  279. LPCUTF8 sourceAfterBOM = sourceStart;
  280. lineCharOffset = FunctionBody::SkipByteOrderMark(sourceAfterBOM /* byref */);
  281. Assert((sourceAfterBOM - sourceStart) < MAXUINT32);
  282. charcount_t byteStartOffset = (charcount_t)(sourceAfterBOM - sourceStart);
  283. line = JsUtil::LineOffsetCache<Recycler>::FindLineForCharacterOffset(sourceAfterBOM, sourceEnd, lineCharOffset, byteStartOffset, charPosition);
  284. *outLineByteOffset = byteStartOffset;
  285. }
  286. else
  287. {
  288. line = this->m_lineOffsetCache->GetLineForCharacterOffset(charPosition, &lineCharOffset, outLineByteOffset);
  289. }
  290. Assert(charPosition >= lineCharOffset);
  291. *outLineNumber = line;
  292. *outColumn = charPosition - lineCharOffset;
  293. }
  294. void Utf8SourceInfo::CreateLineOffsetCache(const JsUtil::LineOffsetCache<Recycler>::LineOffsetCacheItem *items, charcount_t numberOfItems)
  295. {
  296. AssertMsg(this->m_lineOffsetCache == nullptr, "LineOffsetCache is already initialized!");
  297. Recycler* recycler = this->m_scriptContext->GetRecycler();
  298. this->m_lineOffsetCache = RecyclerNew(recycler, JsUtil::LineOffsetCache<Recycler>, recycler, items, numberOfItems);
  299. }
  300. DWORD_PTR Utf8SourceInfo::GetHostSourceContext() const
  301. {
  302. return m_srcInfo->sourceContextInfo->dwHostSourceContext;
  303. }
  304. bool Utf8SourceInfo::IsDynamic() const
  305. {
  306. return m_srcInfo->sourceContextInfo->IsDynamic();
  307. }
  308. SourceContextInfo* Utf8SourceInfo::GetSourceContextInfo() const
  309. {
  310. return this->m_srcInfo->sourceContextInfo;
  311. }
  312. // Get's the first function in the function body dictionary
  313. // Used if the caller want's any function in this source info
  314. Js::FunctionBody* Utf8SourceInfo::GetAnyParsedFunction()
  315. {
  316. if (this->functionBodyDictionary != nullptr && this->functionBodyDictionary->Count() > 0)
  317. {
  318. FunctionBody* functionBody = nullptr;
  319. int i = 0;
  320. do
  321. {
  322. functionBody = this->functionBodyDictionary->GetValueAt(i);
  323. if (functionBody != nullptr && functionBody->GetByteCode() == nullptr && !functionBody->GetIsFromNativeCodeModule()) functionBody = nullptr;
  324. i++;
  325. }
  326. while (functionBody == nullptr && i < this->functionBodyDictionary->Count());
  327. return functionBody;
  328. }
  329. return nullptr;
  330. }
  331. bool Utf8SourceInfo::IsHostManagedSource() const
  332. {
  333. return ((this->m_srcInfo->grfsi & fsiHostManaged) == fsiHostManaged);
  334. }
  335. void Utf8SourceInfo::SetCallerUtf8SourceInfo(Utf8SourceInfo* callerUtf8SourceInfo)
  336. {
  337. this->callerUtf8SourceInfo = callerUtf8SourceInfo;
  338. }
  339. Utf8SourceInfo* Utf8SourceInfo::GetCallerUtf8SourceInfo() const
  340. {
  341. return this->callerUtf8SourceInfo;
  342. }
  343. void Utf8SourceInfo::TrackDeferredFunction(Js::LocalFunctionId functionID, Js::ParseableFunctionInfo *function)
  344. {
  345. if (this->m_scriptContext->DoUndeferGlobalFunctions())
  346. {
  347. Assert(m_deferredFunctionsInitialized);
  348. if (this->m_deferredFunctionsDictionary != nullptr)
  349. {
  350. this->m_deferredFunctionsDictionary->Add(functionID, function);
  351. }
  352. }
  353. }
  354. void Utf8SourceInfo::StopTrackingDeferredFunction(Js::LocalFunctionId functionID)
  355. {
  356. if (this->m_scriptContext->DoUndeferGlobalFunctions())
  357. {
  358. Assert(m_deferredFunctionsInitialized);
  359. if (this->m_deferredFunctionsDictionary != nullptr)
  360. {
  361. this->m_deferredFunctionsDictionary->Remove(functionID);
  362. }
  363. }
  364. }
  365. void Utf8SourceInfo::ClearDebugDocument(bool close)
  366. {
  367. if (this->m_debugDocument != nullptr)
  368. {
  369. if (close)
  370. {
  371. m_debugDocument->CloseDocument();
  372. }
  373. this->m_debugDocument = nullptr;
  374. }
  375. }
  376. bool Utf8SourceInfo::GetDebugDocumentName(BSTR * sourceName)
  377. {
  378. #ifdef ENABLE_SCRIPT_DEBUGGING
  379. if (this->HasDebugDocument() && this->GetDebugDocument()->HasDocumentText())
  380. {
  381. // ToDo (SaAgarwa): Fix for JsRT debugging
  382. IDebugDocumentText *documentText = static_cast<IDebugDocumentText *>(this->GetDebugDocument()->GetDocumentText());
  383. if (documentText->GetName(DOCUMENTNAMETYPE_URL, sourceName) == S_OK)
  384. {
  385. return true;
  386. }
  387. }
  388. #endif
  389. return false;
  390. }
  391. }