diff --git a/packages/react-native/ReactCommon/react/runtime/TimerManager.cpp b/packages/react-native/ReactCommon/react/runtime/TimerManager.cpp index 1f4cade5f856..e3cc923460da 100644 --- a/packages/react-native/ReactCommon/react/runtime/TimerManager.cpp +++ b/packages/react-native/ReactCommon/react/runtime/TimerManager.cpp @@ -258,49 +258,36 @@ void TimerManager::attachGlobals(jsi::Runtime& runtime) { } runtime.global().setProperty( - runtime, - "setTimeout", - jsi::Function::createFromHostFunction( - runtime, - jsi::PropNameID::forAscii(runtime, "setTimeout"), - 3, // Function, delay, ...args - [this]( - jsi::Runtime& rt, - const jsi::Value& thisVal, - const jsi::Value* args, - size_t count) { + runtime, + "setTimeout", + jsi::Function::createFromHostFunction( + runtime, + jsi::PropNameID::forAscii(runtime, "setTimeout"), + 3, // Function, delay, ...args + [this](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) { if (count == 0) { - throw jsi::JSError( - rt, - "setTimeout must be called with at least one argument (the function to call)."); + throw jsi::JSError(rt, "setTimeout must be called with at least one argument (the function to call)."); } - if (!args[0].isObject() || !args[0].asObject(rt).isFunction(rt)) { - throw jsi::JSError( - rt, "The first argument to setTimeout must be a function."); - } - auto callback = args[0].getObject(rt).getFunction(rt); + auto callback = args[0].isObject() && args[0].asObject(rt).isFunction(rt) + ? args[0].getObject(rt).getFunction(rt) + : jsi::Function::createFromHostFunction( + rt, + jsi::PropNameID::forAscii(rt, "dummyFunction"), + 0, + [](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) { + return jsi::Value::undefined(); + }); - if (count > 1 && !args[1].isNumber() && !args[1].isUndefined()) { - throw jsi::JSError( - rt, - "The second argument to setTimeout must be a number or undefined."); - } - auto delay = - count > 1 && args[1].isNumber() ? args[1].getNumber() : 0; + auto delay = (count > 1 && args[1].isNumber()) ? args[1].getNumber() : 0; - // Package up the remaining argument values into one place. std::vector moreArgs; for (size_t extraArgNum = 2; extraArgNum < count; extraArgNum++) { - moreArgs.emplace_back(rt, args[extraArgNum]); + moreArgs.emplace_back(rt, args[extraArgNum]); } - return createTimer( - std::move(callback), - std::move(moreArgs), - delay, - TimerSource::SetTimeout); - })); + return createTimer(std::move(callback), std::move(moreArgs), delay); + })); runtime.global().setProperty( runtime, @@ -333,26 +320,15 @@ void TimerManager::attachGlobals(jsi::Runtime& runtime) { const jsi::Value& thisVal, const jsi::Value* args, size_t count) { - if (count == 0) { - throw jsi::JSError( - rt, - "setInterval must be called with at least one argument (the function to call)."); - } - - if (!args[0].isObject() || !args[0].asObject(rt).isFunction(rt)) { + if (count == 0 || !args[0].isObject() || + !args[0].asObject(rt).isFunction(rt)) { throw jsi::JSError( - rt, "The first argument to setInterval must be a function."); + rt, "setInterval must be called with a function."); } auto callback = args[0].getObject(rt).getFunction(rt); auto delay = - count > 1 && args[1].isNumber() ? args[1].getNumber() : 0; - - // Package up the remaining argument values into one place. - std::vector moreArgs; - for (size_t extraArgNum = 2; extraArgNum < count; extraArgNum++) { - moreArgs.emplace_back(rt, args[extraArgNum]); - } - + (count > 1 && args[1].isNumber()) ? args[1].getNumber() : 0; + std::vector moreArgs(args + 2, args + count); return createRecurringTimer( std::move(callback), std::move(moreArgs),