HRESULT CreateAndRunSerializedScript(const char* fileName, LPCSTR fileContents, char *fullPath) { HRESULT hr = S_OK; JsRuntimeHandle runtime = JS_INVALID_RUNTIME_HANDLE; JsContextRef context = JS_INVALID_REFERENCE, current = JS_INVALID_REFERENCE; BYTE *bcBuffer = nullptr; DWORD bcBufferSize = 0; IfFailGo(GetSerializedBuffer(fileContents, &bcBuffer, &bcBufferSize)); // Bytecode buffer is created in one runtime and will be executed on different runtime. IfFailGo(CreateRuntime(&runtime)); chRuntime = runtime; IfJsErrorFailLog(ChakraRTInterface::JsCreateContext(runtime, &context)); IfJsErrorFailLog(ChakraRTInterface::JsGetCurrentContext(¤t)); IfJsErrorFailLog(ChakraRTInterface::JsSetCurrentContext(context)); // Initialized the WScript object on the new context if (!WScriptJsrt::Initialize()) { IfFailGo(E_FAIL); } IfFailGo(RunScript(fileName, fileContents, bcBuffer, fullPath)); Error: if (current != JS_INVALID_REFERENCE) { ChakraRTInterface::JsSetCurrentContext(current); } if (runtime != JS_INVALID_RUNTIME_HANDLE) { ChakraRTInterface::JsDisposeRuntime(runtime); } if (bcBuffer != nullptr) { delete[] bcBuffer; } return hr; }
nsresult CycleCollectedJSContext::Initialize(JSRuntime* aParentRuntime, uint32_t aMaxBytes, uint32_t aMaxNurseryBytes) { MOZ_ASSERT(!mJSContext); mozilla::dom::InitScriptSettings(); mJSContext = JS_NewContext(aMaxBytes, aMaxNurseryBytes, aParentRuntime); if (!mJSContext) { return NS_ERROR_OUT_OF_MEMORY; } mRuntime = CreateRuntime(mJSContext); InitializeCommon(); nsCycleCollector_registerJSContext(this); return NS_OK; }