Utf8SourceInfo.cpp 17 KB

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