JsErrorCode CreateHostContext(JsRuntimeHandle runtime, int argc, wchar_t *argv [], int argumentsStart, JsContextRef *context) { JsCreateContext(runtime, context); JsSetCurrentContext(*context); JsValueRef hostObject; JsCreateObject(&hostObject); ghostObject = hostObject; JsValueRef globalObject; JsGetGlobalObject(&globalObject); JsPropertyIdRef hostPropertyId; JsGetPropertyIdFromName(L"app", &hostPropertyId); // app.XXXXX JsSetProperty(globalObject, hostPropertyId, hostObject, true); //IfFailRet(DefineHostCallback(hostObject, L"msgbox", msgbox, nullptr)); JsSetCurrentContext(JS_INVALID_REFERENCE); return JsNoError; }
int main(){ JsCreateVm(TRUE,0,NULL, NULL); CreatePrintFn(); struct JsEngine* e = JsCreateEngine(); while(1){ struct JsContext* c = JsCreateContext(e, NULL,"Main Context"); JsDispatch(c,&JsContextTask,NULL); } //安全推出主线程 JsCloseSelf(); return 0; }
JsErrorCode ChakraHost::Init() { currentSourceContext = 0; IfFailRet(JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime)); IfFailRet(JsCreateContext(runtime, &context)); IfFailRet(JsSetCurrentContext(context)); IfFailRet(JsGetGlobalObject(&globalObject)); IfFailRet(InitJson()); IfFailRet(InitConsole()); return JsNoError; }
void BasicTest(JsRuntimeAttributes attributes, LPCSTR fileName) { LPCWSTR script = nullptr; REQUIRE(FileLoadHelpers::LoadScriptFromFile(fileName, script) == S_OK); REQUIRE(script != nullptr); // Create the runtime JsRuntimeHandle runtime = JS_INVALID_RUNTIME_HANDLE; REQUIRE(JsCreateRuntime(attributes, nullptr, &runtime) == JsNoError); // Set memory limit REQUIRE(JsSetRuntimeMemoryLimit(runtime, MemoryLimit) == JsNoError); size_t memoryLimit; size_t memoryUsage; REQUIRE(JsGetRuntimeMemoryLimit(runtime, &memoryLimit) == JsNoError); CHECK(memoryLimit == MemoryLimit); REQUIRE(JsGetRuntimeMemoryUsage(runtime, &memoryUsage) == JsNoError); CHECK(memoryUsage < MemoryLimit); // Create and initialize the script context JsContextRef context = JS_INVALID_RUNTIME_HANDLE; REQUIRE(JsCreateContext(runtime, &context) == JsNoError); REQUIRE(JsSetCurrentContext(context) == JsNoError); // Invoke the script REQUIRE(JsRunScript(script, JS_SOURCE_CONTEXT_NONE, _u(""), nullptr) == JsErrorScriptException); ValidateOOMException(); REQUIRE(JsGetRuntimeMemoryLimit(runtime, &memoryLimit) == JsNoError); CHECK(memoryLimit == MemoryLimit); REQUIRE(JsGetRuntimeMemoryUsage(runtime, &memoryUsage) == JsNoError); CHECK(memoryUsage <= MemoryLimit); // Destroy the runtime REQUIRE(JsSetCurrentContext(JS_INVALID_REFERENCE) == JsNoError); REQUIRE(JsCollectGarbage(runtime) == JsNoError); REQUIRE(JsDisposeRuntime(runtime) == JsNoError); }