JsValueRef WScriptJsrt::SetTimeoutCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState) { LPCWSTR errorMessage = _u("invalid call to WScript.SetTimeout"); JsValueRef function; JsValueRef timerId; unsigned int time; double tmp; CallbackMessage *msg = nullptr; if (argumentCount != 3) { goto Error; } function = arguments[1]; IfJsrtError(ChakraRTInterface::JsNumberToDouble(arguments[2], &tmp)); time = static_cast<int>(tmp); msg = new CallbackMessage(time, function); messageQueue->InsertSorted(msg); #if ENABLE_TTD ChakraRTInterface::JsTTDNotifyHostCallbackCreatedOrCanceled(true /*isCreate*/, false /*isCancel*/, false /*isRepeating*/, function, msg->GetId()); #endif IfJsrtError(ChakraRTInterface::JsDoubleToNumber(static_cast<double>(msg->GetId()), &timerId)); return timerId; Error: JsValueRef errorObject; JsValueRef errorMessageString; ERROR_MESSAGE_TO_STRING(errorCode, errorMessage, errorMessageString); if (errorCode != JsNoError) { errorCode = ChakraRTInterface::JsCreateError(errorMessageString, &errorObject); if (errorCode != JsNoError) { ChakraRTInterface::JsSetException(errorObject); } } return JS_INVALID_REFERENCE; }
JsValueRef WScriptJsrt::ClearTimeoutCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState) { LPCWSTR errorMessage = _u("invalid call to WScript.ClearTimeout"); if (argumentCount != 2) { goto Error; } unsigned int timerId; double tmp; JsValueRef undef; JsValueRef global; IfJsrtError(ChakraRTInterface::JsNumberToDouble(arguments[1], &tmp)); timerId = static_cast<int>(tmp); messageQueue->RemoveById(timerId); #if ENABLE_TTD ChakraRTInterface::JsTTDNotifyHostCallbackCreatedOrCanceled(false /*isCreate*/, true /*isCancel*/, false /*isRepeating*/, nullptr, timerId); #endif IfJsrtError(ChakraRTInterface::JsGetGlobalObject(&global)); IfJsrtError(ChakraRTInterface::JsGetUndefinedValue(&undef)); return undef; Error: JsValueRef errorObject; JsValueRef errorMessageString; ERROR_MESSAGE_TO_STRING(errorCode, errorMessage, errorMessageString); if (errorCode != JsNoError) { errorCode = ChakraRTInterface::JsCreateError(errorMessageString, &errorObject); if (errorCode != JsNoError) { ChakraRTInterface::JsSetException(errorObject); } } return JS_INVALID_REFERENCE; }
JsValueRef WScriptJsrt::ClearTimeoutCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState) { LPWSTR errorMessage = L"invalid call to WScript.ClearTimeout"; if (argumentCount != 2) { goto Error; } unsigned int timerId; double tmp; JsValueRef undef; JsValueRef global; IfJsrtError(ChakraRTInterface::JsNumberToDouble(arguments[1], &tmp)); timerId = static_cast<int>(tmp); messageQueue->RemoveById(timerId); IfJsrtError(ChakraRTInterface::JsGetGlobalObject(&global)); IfJsrtError(ChakraRTInterface::JsGetUndefinedValue(&undef)); return undef; Error: JsValueRef errorObject; JsValueRef errorMessageString; JsErrorCode errorCode = ChakraRTInterface::JsPointerToString(errorMessage, wcslen(errorMessage), &errorMessageString); if (errorCode != JsNoError) { errorCode = ChakraRTInterface::JsCreateError(errorMessageString, &errorObject); if (errorCode != JsNoError) { ChakraRTInterface::JsSetException(errorObject); } } return JS_INVALID_REFERENCE; }
JsValueRef WScriptJsrt::SetTimeoutCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState) { LPWSTR errorMessage = L"invalid call to WScript.SetTimeout"; if (argumentCount != 3) { goto Error; } JsValueRef function = arguments[1]; JsValueRef timerId; unsigned int time; double tmp; CallbackMessage *msg = nullptr; IfJsrtError(ChakraRTInterface::JsNumberToDouble(arguments[2], &tmp)); time = static_cast<int>(tmp); msg = new CallbackMessage(time, function); messageQueue->Push(msg); IfJsrtError(ChakraRTInterface::JsDoubleToNumber(static_cast<double>(msg->GetId()), &timerId)); return timerId; Error: JsValueRef errorObject; JsValueRef errorMessageString; JsErrorCode errorCode = ChakraRTInterface::JsPointerToString(errorMessage, wcslen(errorMessage), &errorMessageString); if (errorCode != JsNoError) { errorCode = ChakraRTInterface::JsCreateError(errorMessageString, &errorObject); if (errorCode != JsNoError) { ChakraRTInterface::JsSetException(errorObject); } } return JS_INVALID_REFERENCE; }
JsValueRef WScriptJsrt::DetachCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState) { LPCWSTR errorMessage = _u("WScript.Detach requires a function, like WScript.Detach(foo);"); JsValueType argumentType = JsUndefined; if (argumentCount != 2) { goto Error; } IfJsrtError(ChakraRTInterface::JsGetValueType(arguments[1], &argumentType)); if (argumentType != JsFunction) { goto Error; } QueueDebugOperation(arguments[1], [](WScriptJsrt::CallbackMessage& msg) { JsContextRef currentContext = JS_INVALID_REFERENCE; ChakraRTInterface::JsGetCurrentContext(¤tContext); JsRuntimeHandle currentRuntime = JS_INVALID_RUNTIME_HANDLE; ChakraRTInterface::JsGetRuntime(currentContext, ¤tRuntime); if (Debugger::debugger != nullptr) { Debugger* debugger = Debugger::GetDebugger(currentRuntime); debugger->StopDebugging(currentRuntime); } return msg.CallFunction(""); }); Error: JsValueRef errorObject; JsValueRef errorMessageString; ERROR_MESSAGE_TO_STRING(errorCode, errorMessage, errorMessageString); if (errorCode != JsNoError) { errorCode = ChakraRTInterface::JsCreateError(errorMessageString, &errorObject); if (errorCode != JsNoError) { ChakraRTInterface::JsSetException(errorObject); } } return JS_INVALID_REFERENCE; }