Exemple #1
0
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;
}
JsErrorCode ChakraHost::SetGlobalVariable(const wchar_t* szPropertyName, JsValueRef value)
{
    JsPropertyIdRef globalVarId;
    IfFailRet(JsGetPropertyIdFromName(szPropertyName, &globalVarId));
    IfFailRet(JsSetProperty(globalObject, globalVarId, value, true));
    return JsNoError;
}
JsErrorCode DefineHostCallback(JsValueRef globalObject, const wchar_t *callbackName, JsNativeFunction callback, void *callbackState)
{
    JsPropertyIdRef propertyId;
    IfFailRet(JsGetPropertyIdFromName(callbackName, &propertyId));

    JsValueRef function;
    IfFailRet(JsCreateFunction(callback, callbackState, &function));
    IfFailRet(JsSetProperty(globalObject, propertyId, function, true));

    return JsNoError;
}
JsErrorCode ChakraHost::InitConsole()
{
    JsPropertyIdRef consolePropertyId;
    IfFailRet(JsGetPropertyIdFromName(L"console", &consolePropertyId));

    JsValueRef consoleObject;
    IfFailRet(JsCreateObject(&consoleObject));
    IfFailRet(JsSetProperty(globalObject, consolePropertyId, consoleObject, true));

    IfFailRet(DefineHostCallback(consoleObject, L"info", ConsoleInfo, nullptr));
    IfFailRet(DefineHostCallback(consoleObject, L"log", ConsoleLog, nullptr));
    IfFailRet(DefineHostCallback(consoleObject, L"warn", ConsoleWarn, nullptr));
    IfFailRet(DefineHostCallback(consoleObject, L"error", ConsoleError, nullptr));

    return JsNoError;
}