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; }
void RegistFunction( LPCWSTR function_name, JSFUNCTION func ) { JsSetCurrentContext(jscontext); IfFailRet(DefineHostCallback(ghostObject, function_name, func, nullptr)); JsSetCurrentContext(JS_INVALID_REFERENCE); }
JsErrorCode ChakraHost::Destroy() { IfFailRet(JsSetCurrentContext(JS_INVALID_REFERENCE)); IfFailRet(JsDisposeRuntime(runtime)); return JsNoError; }
void JsAppClose() { if ( runtime ) { JsCollectGarbage(runtime); JsSetCurrentContext(JS_INVALID_REFERENCE); JsDisposeRuntime(runtime); runtime = nullptr; } }
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); }
bool TestCleanup(JsRuntimeHandle runtime) { if (runtime != nullptr) { // Disable debugging JsDiagStopDebugging(runtime, nullptr); JsSetCurrentContext(nullptr); JsDisposeRuntime(runtime); } return true; }
static JsContextRef AppInit() { JsContextRef context; int argc = 1; WCHAR* argv[1]; argv[0] = L"dumy"; JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime); CreateHostContext(runtime, argc,argv,1,&context); JsSetCurrentContext(context); return context; }
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; }
CJsValueRef JsRun(LPCWSTR script) { JsValueRef result; JsSetCurrentContext(jscontext); JsErrorCode errorCode = JsRunScript(script, currentSourceContext++, L"", &result); if ( JsNoError != errorCode ) { throw errorCode; } return CJsValueRef(result); }
void BreakpointsContextTest(JsRuntimeHandle runtime) { JsContextRef context = JS_INVALID_REFERENCE; REQUIRE(JsGetCurrentContext(&context) == JsNoError); // Verify no errors with a context set JsValueRef scriptsArray = JS_INVALID_REFERENCE; REQUIRE(JsDiagGetScripts(&scriptsArray) == JsNoError); // Verify the APIs return an error when no current context set JsSetCurrentContext(nullptr); CHECK(JsDiagGetScripts(&scriptsArray) == JsErrorNoCurrentContext); JsValueRef breakpoint = JS_INVALID_REFERENCE; CHECK(JsDiagSetBreakpoint(0, 0, 0, &breakpoint) == JsErrorNoCurrentContext); CHECK(JsDiagRemoveBreakpoint(0) == JsErrorNoCurrentContext); }