From a98e0adc7dda63600ff811fc6928c69879334dc5 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Tue, 20 Dec 2022 22:05:47 -0800 Subject: [web] Support multiple arguments in `setTimeout`, `setInterval`, and `setImmediate` --- src/bun.js/bindings/ZigGlobalObject.cpp | 67 +++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 3 deletions(-) (limited to 'src/bun.js/bindings/ZigGlobalObject.cpp') diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index e8ee74943..7b5490a0b 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -762,7 +762,27 @@ static JSC_DEFINE_HOST_FUNCTION(functionSetTimeout, } JSC::JSValue num = callFrame->argument(1); - return Bun__Timer__setTimeout(globalObject, JSC::JSValue::encode(job), JSC::JSValue::encode(num)); + JSC::JSValue arguments = {}; + size_t argumentCount = callFrame->argumentCount() - 2; + if (argumentCount > 0) { + JSC::ObjectInitializationScope initializationScope(globalObject->vm()); + JSC::JSArray* argumentsArray = argumentsArray = JSC::JSArray::tryCreateUninitializedRestricted( + initializationScope, nullptr, + globalObject->arrayStructureForIndexingTypeDuringAllocation(JSC::ArrayWithContiguous), + argumentCount); + + if (!argumentsArray) { + auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); + JSC::throwOutOfMemoryError(globalObject, scope); + return JSC::JSValue::encode(JSC::JSValue {}); + } + + for (size_t i = 0; i < argumentCount; i++) { + argumentsArray->putDirectIndex(globalObject, i, callFrame->uncheckedArgument(i + 2)); + } + arguments = JSValue(argumentsArray); + } + return Bun__Timer__setTimeout(globalObject, JSC::JSValue::encode(job), JSC::JSValue::encode(num), JSValue::encode(arguments)); } static JSC_DEFINE_HOST_FUNCTION(functionSetInterval, @@ -785,8 +805,29 @@ static JSC_DEFINE_HOST_FUNCTION(functionSetInterval, } JSC::JSValue num = callFrame->argument(1); + + JSC::JSValue arguments = {}; + size_t argumentCount = callFrame->argumentCount() - 2; + if (argumentCount > 0) { + JSC::ObjectInitializationScope initializationScope(globalObject->vm()); + JSC::JSArray* argumentsArray = argumentsArray = JSC::JSArray::tryCreateUninitializedRestricted( + initializationScope, nullptr, + globalObject->arrayStructureForIndexingTypeDuringAllocation(JSC::ArrayWithContiguous), + argumentCount); + + if (!argumentsArray) { + auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); + JSC::throwOutOfMemoryError(globalObject, scope); + return JSC::JSValue::encode(JSC::JSValue {}); + } + + for (size_t i = 0; i < argumentCount; i++) { + argumentsArray->putDirectIndex(globalObject, i, callFrame->uncheckedArgument(i + 2)); + } + arguments = JSValue(argumentsArray); + } return Bun__Timer__setInterval(globalObject, JSC::JSValue::encode(job), - JSC::JSValue::encode(num)); + JSC::JSValue::encode(num), JSValue::encode(arguments)); } static JSC_DEFINE_HOST_FUNCTION(functionClearInterval, @@ -2753,7 +2794,27 @@ static JSC_DEFINE_HOST_FUNCTION(functionSetImmediate, return JSC::JSValue::encode(JSC::JSValue {}); } - return Bun__Timer__setTimeout(globalObject, JSC::JSValue::encode(job), JSC::JSValue::encode(jsNumber(0))); + JSC::JSValue arguments = {}; + size_t argumentCount = callFrame->argumentCount() - 1; + if (argumentCount > 0) { + JSC::ObjectInitializationScope initializationScope(globalObject->vm()); + JSC::JSArray* argumentsArray = argumentsArray = JSC::JSArray::tryCreateUninitializedRestricted( + initializationScope, nullptr, + globalObject->arrayStructureForIndexingTypeDuringAllocation(JSC::ArrayWithContiguous), + argumentCount); + + if (!argumentsArray) { + auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); + JSC::throwOutOfMemoryError(globalObject, scope); + return JSC::JSValue::encode(JSC::JSValue {}); + } + + for (size_t i = 0; i < argumentCount; i++) { + argumentsArray->putDirectIndex(globalObject, i, callFrame->uncheckedArgument(i + 1)); + } + arguments = JSValue(argumentsArray); + } + return Bun__Timer__setTimeout(globalObject, JSC::JSValue::encode(job), JSC::JSValue::encode(jsNumber(0)), JSValue::encode(arguments)); } JSC_DEFINE_CUSTOM_GETTER(JSModuleLoader_getter, (JSGlobalObject * globalObject, EncodedJSValue thisValue, PropertyName)) -- cgit v1.2.3