bool WScriptJsrt::Initialize() { HRESULT hr = S_OK; JsValueRef wscript; IfJsrtErrorFail(ChakraRTInterface::JsCreateObject(&wscript), false); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "Echo", EchoCallback)); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "Quit", QuitCallback)); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "LoadScriptFile", LoadScriptFileCallback)); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "LoadModuleFile", LoadModuleFileCallback)); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "LoadScript", LoadScriptCallback)); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "LoadModule", LoadModuleCallback)); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "SetTimeout", SetTimeoutCallback)); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "ClearTimeout", ClearTimeoutCallback)); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "Attach", AttachCallback)); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "Detach", DetachCallback)); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "DumpFunctionPosition", DumpFunctionPositionCallback)); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "RequestAsyncBreak", RequestAsyncBreakCallback)); // ToDo Remove IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "Edit", EmptyCallback)); JsValueRef argsObject; if (!CreateArgumentsObject(&argsObject)) { return false; } JsPropertyIdRef argsName; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromNameUtf8("Arguments", &argsName), false); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(wscript, argsName, argsObject, true), false); JsPropertyIdRef wscriptName; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromNameUtf8("WScript", &wscriptName), false); JsValueRef global; IfJsrtErrorFail(ChakraRTInterface::JsGetGlobalObject(&global), false); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(global, wscriptName, wscript, true), false); IfFalseGo(WScriptJsrt::InstallObjectsOnObject(global, "print", EchoCallback)); Error: return hr == S_OK; }
bool WScriptJsrt::Initialize() { JsValueRef wscript; IfJsrtErrorFail(ChakraRTInterface::JsCreateObject(&wscript), false); JsValueRef echo; JsPropertyIdRef echoPropertyId; const wchar_t* echoString = L"Echo"; CreateNamedFunction(echoString, EchoCallback, &echo); IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromName(echoString, &echoPropertyId), false); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(wscript, echoPropertyId, echo, true), false); JsValueRef argsObject; if (!CreateArgumentsObject(&argsObject)) { return false; } JsPropertyIdRef argsName; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromName(L"Arguments", &argsName), false); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(wscript, argsName, argsObject, true), false); JsValueRef quit; const wchar_t* quitString = L"Quit"; JsPropertyIdRef quitPropertyId; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromName(quitString, &quitPropertyId), false); CreateNamedFunction(quitString, QuitCallback, &quit); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(wscript, quitPropertyId, quit, true), false); JsValueRef loadScriptFile; const wchar_t* loadScriptFileString = L"LoadScriptFile"; JsPropertyIdRef loadScriptFilePropertyId; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromName(loadScriptFileString, &loadScriptFilePropertyId), false); CreateNamedFunction(loadScriptFileString, LoadScriptFileCallback, &loadScriptFile); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(wscript, loadScriptFilePropertyId, loadScriptFile, true), false); JsValueRef loadModuleFile; const wchar_t* loadModuleFileString = L"LoadModuleFile"; JsPropertyIdRef loadModuleFilePropertyId; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromName(loadModuleFileString, &loadModuleFilePropertyId), false); CreateNamedFunction(loadModuleFileString, LoadModuleFileCallback, &loadModuleFile); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(wscript, loadModuleFilePropertyId, loadModuleFile, true), false); JsValueRef loadScript; JsPropertyIdRef loadScriptName; const wchar_t* loadScriptString = L"LoadScript"; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromName(loadScriptString, &loadScriptName), false); CreateNamedFunction(loadScriptString, LoadScriptCallback, &loadScript); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(wscript, loadScriptName, loadScript, true), false); JsValueRef loadModule; JsPropertyIdRef loadModuleName; const wchar_t* loadModuleString = L"LoadModule"; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromName(loadModuleString, &loadModuleName), false); CreateNamedFunction(loadModuleString, LoadModuleCallback, &loadModule); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(wscript, loadModuleName, loadModule, true), false); JsValueRef setTimeout; JsPropertyIdRef setTimeoutName; const wchar_t* setTimeoutString = L"SetTimeout"; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromName(setTimeoutString, &setTimeoutName), false); CreateNamedFunction(setTimeoutString, SetTimeoutCallback, &setTimeout); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(wscript, setTimeoutName, setTimeout, true), false); JsValueRef clearTimeout; JsPropertyIdRef clearTimeoutName; const wchar_t* clearTimeoutString = L"ClearTimeout"; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromName(clearTimeoutString, &clearTimeoutName), false); CreateNamedFunction(clearTimeoutString, ClearTimeoutCallback, &clearTimeout); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(wscript, clearTimeoutName, clearTimeout, true), false); JsPropertyIdRef wscriptName; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromName(L"WScript", &wscriptName), false); JsValueRef global; IfJsrtErrorFail(ChakraRTInterface::JsGetGlobalObject(&global), false); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(global, wscriptName, wscript, true), false); JsPropertyIdRef printName; IfJsrtErrorFail(ChakraRTInterface::JsGetPropertyIdFromName(L"print", &printName), false); IfJsrtErrorFail(ChakraRTInterface::JsSetProperty(global, printName, echo, true), false); return true; }