JsrtContextCore.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Copyright (c) ChakraCore Project Contributors. All rights reserved.
  4. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  5. //-------------------------------------------------------------------------------------------------------
  6. #include "Runtime.h"
  7. #include "JsrtContext.h"
  8. #include "SCACorePch.h"
  9. #include "JsrtContextCore.h"
  10. JsrtContext *JsrtContext::New(JsrtRuntime * runtime)
  11. {
  12. return JsrtContextCore::New(runtime);
  13. }
  14. /* static */
  15. bool JsrtContext::Is(void * ref)
  16. {
  17. return VirtualTableInfo<JsrtContextCore>::HasVirtualTable(ref);
  18. }
  19. void JsrtContext::OnScriptLoad(Js::JavascriptFunction * scriptFunction, Js::Utf8SourceInfo* utf8SourceInfo, CompileScriptException* compileException)
  20. {
  21. ((JsrtContextCore *)this)->OnScriptLoad(scriptFunction, utf8SourceInfo, compileException);
  22. }
  23. #if ENABLE_TTD
  24. void JsrtContext::OnScriptLoad_TTDCallback(Js::FunctionBody* body, Js::Utf8SourceInfo* utf8SourceInfo, CompileScriptException* compileException, bool notify)
  25. {
  26. JsrtContextCore* rcvr = ((JsrtContextCore*)this);
  27. JsrtDebugManager* jsrtDebugManager = rcvr->GetRuntime()->GetJsrtDebugManager();
  28. if(jsrtDebugManager != nullptr)
  29. {
  30. jsrtDebugManager->ReportScriptCompile_TTD(body, utf8SourceInfo, compileException, notify);
  31. }
  32. }
  33. void JsrtContext::OnReplayDisposeContext_TTDCallback(FinalizableObject* jsrtCtx)
  34. {
  35. ((JsrtContextCore *)jsrtCtx)->Dispose(false);
  36. }
  37. #endif
  38. JsrtContextCore::JsrtContextCore(JsrtRuntime * runtime) :
  39. JsrtContext(runtime)
  40. {
  41. EnsureScriptContext();
  42. Link();
  43. }
  44. /* static */
  45. JsrtContextCore *JsrtContextCore::New(JsrtRuntime * runtime)
  46. {
  47. return RecyclerNewFinalized(runtime->GetThreadContext()->EnsureRecycler(), JsrtContextCore, runtime);
  48. }
  49. void JsrtContextCore::Dispose(bool isShutdown)
  50. {
  51. if (this->GetJavascriptLibrary())
  52. {
  53. Unlink();
  54. this->SetJavascriptLibrary(nullptr);
  55. }
  56. }
  57. void JsrtContextCore::Finalize(bool isShutdown)
  58. {
  59. }
  60. Js::ScriptContext* JsrtContextCore::EnsureScriptContext()
  61. {
  62. Assert(this->GetJavascriptLibrary() == nullptr);
  63. ThreadContext* localThreadContext = this->GetRuntime()->GetThreadContext();
  64. AutoPtr<Js::ScriptContext> newScriptContext(Js::ScriptContext::New(localThreadContext));
  65. newScriptContext->Initialize();
  66. hostContext = HeapNew(ChakraCoreHostScriptContext, newScriptContext);
  67. newScriptContext->SetHostScriptContext(hostContext);
  68. this->SetJavascriptLibrary(newScriptContext.Detach()->GetLibrary());
  69. Js::JavascriptLibrary *library = this->GetScriptContext()->GetLibrary();
  70. Assert(library != nullptr);
  71. localThreadContext->GetRecycler()->RootRelease(library->GetGlobalObject());
  72. library->GetEvalFunctionObject()->SetEntryPoint(&Js::GlobalObject::EntryEval);
  73. library->GetFunctionConstructor()->SetEntryPoint(&Js::JavascriptFunction::NewInstance);
  74. return this->GetScriptContext();
  75. }
  76. void JsrtContextCore::OnScriptLoad(Js::JavascriptFunction * scriptFunction, Js::Utf8SourceInfo* utf8SourceInfo, CompileScriptException* compileException)
  77. {
  78. #ifdef ENABLE_SCRIPT_DEBUGGING
  79. JsrtDebugManager* jsrtDebugManager = this->GetRuntime()->GetJsrtDebugManager();
  80. if (jsrtDebugManager != nullptr)
  81. {
  82. jsrtDebugManager->ReportScriptCompile(scriptFunction, utf8SourceInfo, compileException);
  83. }
  84. #endif
  85. }
  86. HRESULT ChakraCoreHostScriptContext::FetchImportedModule(Js::ModuleRecordBase* referencingModule, LPCOLESTR specifier, Js::ModuleRecordBase** dependentModuleRecord)
  87. {
  88. if (fetchImportedModuleCallback == nullptr)
  89. {
  90. return E_INVALIDARG;
  91. }
  92. Js::JavascriptString* specifierVar = Js::JavascriptString::NewCopySz(specifier, GetScriptContext());
  93. JsModuleRecord dependentRecord = JS_INVALID_REFERENCE;
  94. {
  95. AUTO_NO_EXCEPTION_REGION;
  96. JsErrorCode errorCode = fetchImportedModuleCallback(referencingModule, specifierVar, &dependentRecord);
  97. if (errorCode == JsNoError)
  98. {
  99. *dependentModuleRecord = static_cast<Js::ModuleRecordBase*>(dependentRecord);
  100. return NOERROR;
  101. }
  102. }
  103. return E_INVALIDARG;
  104. }
  105. HRESULT ChakraCoreHostScriptContext::FetchImportedModuleFromScript(JsSourceContext dwReferencingSourceContext, LPCOLESTR specifier, Js::ModuleRecordBase** dependentModuleRecord)
  106. {
  107. if (fetchImportedModuleFromScriptCallback == nullptr)
  108. {
  109. return E_INVALIDARG;
  110. }
  111. Js::JavascriptString* specifierVar = Js::JavascriptString::NewCopySz(specifier, GetScriptContext());
  112. JsModuleRecord dependentRecord = JS_INVALID_REFERENCE;
  113. {
  114. AUTO_NO_EXCEPTION_REGION;
  115. JsErrorCode errorCode = fetchImportedModuleFromScriptCallback(dwReferencingSourceContext, specifierVar, &dependentRecord);
  116. if (errorCode == JsNoError)
  117. {
  118. *dependentModuleRecord = static_cast<Js::ModuleRecordBase*>(dependentRecord);
  119. return NOERROR;
  120. }
  121. }
  122. return E_INVALIDARG;
  123. }
  124. HRESULT ChakraCoreHostScriptContext::NotifyHostAboutModuleReady(Js::ModuleRecordBase* referencingModule, Js::Var exceptionVar)
  125. {
  126. if (notifyModuleReadyCallback == nullptr)
  127. {
  128. return E_INVALIDARG;
  129. }
  130. {
  131. AUTO_NO_EXCEPTION_REGION;
  132. JsErrorCode errorCode = notifyModuleReadyCallback(referencingModule, exceptionVar);
  133. if (errorCode == JsNoError)
  134. {
  135. return NOERROR;
  136. }
  137. }
  138. return E_INVALIDARG;
  139. }
  140. HRESULT ChakraCoreHostScriptContext::InitializeImportMeta(Js::ModuleRecordBase* referencingModule, Js::Var importMetaObject)
  141. {
  142. if (initializeImportMetaCallback == nullptr)
  143. {
  144. return E_INVALIDARG;
  145. }
  146. {
  147. AUTO_NO_EXCEPTION_REGION;
  148. JsErrorCode errorCode = initializeImportMetaCallback(referencingModule, importMetaObject);
  149. if (errorCode == JsNoError)
  150. {
  151. return NOERROR;
  152. }
  153. }
  154. return E_INVALIDARG;
  155. }
  156. bool ChakraCoreHostScriptContext::ReportModuleCompletion(Js::ModuleRecordBase* module, Js::Var exception)
  157. {
  158. if (reportModuleCompletionCallback == nullptr)
  159. {
  160. return false;
  161. }
  162. {
  163. AUTO_NO_EXCEPTION_REGION;
  164. JsErrorCode errorCode = reportModuleCompletionCallback(module, exception);
  165. if (errorCode == JsNoError)
  166. {
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. ChakraCoreStreamWriter::~ChakraCoreStreamWriter()
  173. {
  174. HeapDelete(m_serializerCore);
  175. }
  176. byte * ChakraCoreStreamWriter::ExtendBuffer(byte *oldBuffer, size_t newSize, size_t *allocatedSize)
  177. {
  178. m_data = this->reallocateBufferMemory(this->callbackState, oldBuffer, newSize, allocatedSize);
  179. m_length = newSize;
  180. if (m_data == nullptr)
  181. {
  182. // free(m_data);
  183. OutOfMemory_unrecoverable_error();
  184. }
  185. return m_data;
  186. }
  187. bool ChakraCoreStreamWriter::WriteHostObject(void* data)
  188. {
  189. return this->writeHostObject(this->callbackState, data);
  190. }
  191. void ChakraCoreStreamWriter::SetSerializer(Js::SCACore::Serializer *s)
  192. {
  193. m_serializerCore = s;
  194. }
  195. void ChakraCoreStreamWriter::WriteRawBytes(const void* source, size_t length)
  196. {
  197. Assert(m_serializerCore);
  198. m_serializerCore->WriteRawBytes(source, length);
  199. }
  200. bool ChakraCoreStreamWriter::WriteValue(JsValueRef root)
  201. {
  202. Assert(m_serializerCore);
  203. return m_serializerCore->WriteValue((Js::Var)root);
  204. }
  205. bool ChakraCoreStreamWriter::ReleaseData(byte** data, size_t *dataLength)
  206. {
  207. if (m_data)
  208. {
  209. Assert(m_serializerCore);
  210. return m_serializerCore->Release(data, dataLength);
  211. }
  212. return false;
  213. }
  214. bool ChakraCoreStreamWriter::DetachArrayBuffer()
  215. {
  216. Assert(m_serializerCore);
  217. return m_serializerCore->DetachArrayBuffer();
  218. }
  219. JsErrorCode ChakraCoreStreamWriter::SetTransferableVars(JsValueRef *transferableVars, size_t transferableVarsCount)
  220. {
  221. Assert(m_serializerCore);
  222. HRESULT hr = m_serializerCore->SetTransferableVars((Js::Var *)transferableVars, transferableVarsCount);
  223. if (hr == S_OK)
  224. {
  225. return JsNoError;
  226. }
  227. else if (hr == E_SCA_TRANSFERABLE_UNSUPPORTED)
  228. {
  229. return JsTransferableNotSupported;
  230. }
  231. else if (hr == E_SCA_TRANSFERABLE_NEUTERED)
  232. {
  233. return JsTransferableAlreadyDetached;
  234. }
  235. return JsSerializerNotSupported;
  236. }
  237. void ChakraCoreStreamWriter::FreeSelf()
  238. {
  239. HeapDelete(this);
  240. }
  241. ChakraHostDeserializerHandle::~ChakraHostDeserializerHandle()
  242. {
  243. HeapDelete(m_deserializer);
  244. }
  245. void ChakraHostDeserializerHandle::SetDeserializer(Js::SCACore::Deserializer *deserializer)
  246. {
  247. m_deserializer = deserializer;
  248. }
  249. bool ChakraHostDeserializerHandle::ReadRawBytes(size_t length, void **data)
  250. {
  251. Assert(m_deserializer);
  252. return m_deserializer->ReadRawBytes(length, data);
  253. }
  254. bool ChakraHostDeserializerHandle::ReadBytes(size_t length, void **data)
  255. {
  256. Assert(m_deserializer);
  257. return m_deserializer->ReadBytes(length, data);
  258. }
  259. JsValueRef ChakraHostDeserializerHandle::ReadValue()
  260. {
  261. Assert(m_deserializer);
  262. return m_deserializer->ReadValue();
  263. }
  264. JsErrorCode ChakraHostDeserializerHandle::SetTransferableVars(JsValueRef *transferableVars, size_t transferableVarsCount)
  265. {
  266. Assert(m_deserializer);
  267. HRESULT hr = m_deserializer->SetTransferableVars((Js::Var *)transferableVars, transferableVarsCount);
  268. if (hr == S_OK)
  269. {
  270. return JsNoError;
  271. }
  272. else if (hr == E_SCA_TRANSFERABLE_UNSUPPORTED)
  273. {
  274. return JsTransferableNotSupported;
  275. }
  276. else if (hr == E_SCA_TRANSFERABLE_NEUTERED)
  277. {
  278. return JsTransferableAlreadyDetached;
  279. }
  280. return JsSerializerNotSupported;
  281. }
  282. void ChakraHostDeserializerHandle::FreeSelf()
  283. {
  284. HeapDelete(this);
  285. }
  286. Js::Var ChakraHostDeserializerHandle::ReadHostObject()
  287. {
  288. return (Js::Var)this->readHostObject(this->callbackState);
  289. }