From 40a187f0de7a18330ac572a1d70e9402c8cbeeae Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Wed, 3 Aug 2022 00:29:38 -0700 Subject: fix bug in process.hrtime() --- src/bun.js/bindings/Process.cpp | 54 ++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'src/bun.js/bindings/Process.cpp') diff --git a/src/bun.js/bindings/Process.cpp b/src/bun.js/bindings/Process.cpp index a11f965d5..e9e165736 100644 --- a/src/bun.js/bindings/Process.cpp +++ b/src/bun.js/bindings/Process.cpp @@ -180,40 +180,40 @@ static JSC_DEFINE_HOST_FUNCTION(Process_functionHRTime, = reinterpret_cast(globalObject_); auto& vm = globalObject->vm(); uint64_t time = Bun__readOriginTimer(globalObject->bunVM()); - uint64_t seconds = static_cast(time / 1000000000); - uint64_t nanoseconds = time % 1000000000; + int64_t seconds = static_cast(time / 1000000000); + int64_t nanoseconds = time % 1000000000; if (callFrame->argumentCount() > 0) { JSC::JSValue arg0 = callFrame->uncheckedArgument(0); - JSArray* relativeArray = JSC::jsDynamicCast(arg0); - auto throwScope = DECLARE_THROW_SCOPE(vm); - if ((!relativeArray && !arg0.isUndefinedOrNull()) || relativeArray->length() < 2) { - JSC::throwTypeError(globalObject, throwScope, "hrtime() argument must be an array or undefined"_s); - return JSC::JSValue::encode(JSC::JSValue {}); - } - JSValue relativeSecondsValue = relativeArray->getIndexQuickly(0); - JSValue relativeNanosecondsValue = relativeArray->getIndexQuickly(1); - if (!relativeSecondsValue.isNumber() || !relativeNanosecondsValue.isNumber()) { - JSC::throwTypeError(globalObject, throwScope, "hrtime() argument must be an array of 2 integers"_s); - return JSC::JSValue::encode(JSC::JSValue {}); - } - - uint64_t relativeSeconds = JSC__JSValue__toUInt64NoTruncate(JSC::JSValue::encode(relativeSecondsValue)); - uint64_t relativeNanoseconds = JSC__JSValue__toUInt64NoTruncate(JSC::JSValue::encode(relativeNanosecondsValue)); - seconds -= relativeSeconds; - nanoseconds -= relativeNanoseconds; - if (seconds < 0) { - seconds = 0; - } - - if (nanoseconds < 0) { - nanoseconds = 0; + if (!arg0.isUndefinedOrNull()) { + JSArray* relativeArray = JSC::jsDynamicCast(arg0); + auto throwScope = DECLARE_THROW_SCOPE(vm); + if ((!relativeArray && !arg0.isUndefinedOrNull()) || relativeArray->length() < 2) { + JSC::throwTypeError(globalObject, throwScope, "hrtime() argument must be an array or undefined"_s); + return JSC::JSValue::encode(JSC::JSValue {}); + } + JSValue relativeSecondsValue = relativeArray->getIndexQuickly(0); + JSValue relativeNanosecondsValue = relativeArray->getIndexQuickly(1); + if (!relativeSecondsValue.isNumber() || !relativeNanosecondsValue.isNumber()) { + JSC::throwTypeError(globalObject, throwScope, "hrtime() argument must be an array of 2 integers"_s); + return JSC::JSValue::encode(JSC::JSValue {}); + } + + int64_t relativeSeconds = JSC__JSValue__toInt64(JSC::JSValue::encode(relativeSecondsValue)); + int64_t relativeNanoseconds = JSC__JSValue__toInt64(JSC::JSValue::encode(relativeNanosecondsValue)); + seconds -= relativeSeconds; + nanoseconds -= relativeNanoseconds; + if (nanoseconds < 0) { + seconds--; + nanoseconds += 1000000000; + } + throwScope.release(); } } auto* array = JSArray::create(vm, globalObject->originalArrayStructureForIndexingType(ArrayWithContiguous), 2); - array->setIndexQuickly(vm, 0, JSC::jsNumber(static_cast(seconds))); - array->setIndexQuickly(vm, 1, JSC::jsNumber(static_cast(nanoseconds))); + array->setIndexQuickly(vm, 0, JSC::jsNumber(seconds)); + array->setIndexQuickly(vm, 1, JSC::jsNumber(nanoseconds)); return JSC::JSValue::encode(JSC::JSValue(array)); } static JSC_DECLARE_HOST_FUNCTION(Process_functionHRTimeBigInt); -- cgit v1.2.3