From b7d026bb019095a583e6846e78f65a1ee2f3e2b7 Mon Sep 17 00:00:00 2001 From: kitsuned <80879390+kitsuned@users.noreply.github.com> Date: Mon, 11 Sep 2023 11:38:54 +0300 Subject: [PATCH 1/2] fix `Buffer` compat with Node.js: compare --- src/bun.js/bindings/JSBuffer.cpp | 37 +++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/bun.js/bindings/JSBuffer.cpp b/src/bun.js/bindings/JSBuffer.cpp index ad901b0e49988..b435f4073d55c 100644 --- a/src/bun.js/bindings/JSBuffer.cpp +++ b/src/bun.js/bindings/JSBuffer.cpp @@ -864,28 +864,49 @@ static inline JSC::EncodedJSValue jsBufferPrototypeFunction_compareBody(JSC::JSG size_t sourceEndInit = castedThis->byteLength(); size_t sourceEnd = sourceEndInit; + JSValue targetStartValue = jsUndefined(); + JSValue targetEndValue = jsUndefined(); + JSValue sourceStartValue = jsUndefined(); + JSValue sourceEndValue = jsUndefined(); + switch (callFrame->argumentCount()) { default: - sourceEnd = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(4)); - RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + sourceEndValue = callFrame->uncheckedArgument(4); FALLTHROUGH; case 4: - sourceStart = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(3)); - RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + sourceStartValue = callFrame->uncheckedArgument(3); FALLTHROUGH; case 3: - targetEnd = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(2)); - RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + targetEndValue = callFrame->uncheckedArgument(2); FALLTHROUGH; case 2: - targetStart = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(1)); - RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + targetStartValue = callFrame->uncheckedArgument(1); break; case 1: case 0: break; } + if (!targetStartValue.isUndefined()) { + targetStart = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(1)); + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + } + + if (!targetEndValue.isUndefined()) { + targetEnd = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(2)); + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + } + + if (!sourceStartValue.isUndefined()) { + sourceStart = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(3)); + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + } + + if (!sourceEndValue.isUndefined()) { + sourceEnd = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(4)); + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + } + targetStart = std::min(targetStart, std::min(targetEnd, targetEndInit)); sourceStart = std::min(sourceStart, std::min(sourceEnd, sourceEndInit)); From e7b4d24bcdca74874a2030b4e4d0c7a6ab06a475 Mon Sep 17 00:00:00 2001 From: kitsuned <80879390+kitsuned@users.noreply.github.com> Date: Mon, 11 Sep 2023 12:27:06 +0300 Subject: [PATCH 2/2] fix `Buffer` compat with Node.js: copy --- src/bun.js/bindings/JSBuffer.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/bun.js/bindings/JSBuffer.cpp b/src/bun.js/bindings/JSBuffer.cpp index b435f4073d55c..f063102f83f8b 100644 --- a/src/bun.js/bindings/JSBuffer.cpp +++ b/src/bun.js/bindings/JSBuffer.cpp @@ -951,24 +951,40 @@ static inline JSC::EncodedJSValue jsBufferPrototypeFunction_copyBody(JSC::JSGlob size_t sourceEndInit = castedThis->byteLength(); size_t sourceEnd = sourceEndInit; + JSValue targetStartValue = jsUndefined(); + JSValue sourceStartValue = jsUndefined(); + JSValue sourceEndValue = jsUndefined(); + switch (callFrame->argumentCount()) { default: - sourceEnd = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(3)); - RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + sourceEndValue = callFrame->uncheckedArgument(3); FALLTHROUGH; case 3: - sourceStart = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(2)); - RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + sourceStartValue = callFrame->uncheckedArgument(2); FALLTHROUGH; case 2: - targetStart = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(1)); - RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + targetStartValue = callFrame->uncheckedArgument(1); break; case 1: case 0: break; } + if (!targetStartValue.isUndefined()) { + targetStart = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(1)); + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + } + + if (!sourceStartValue.isUndefined()) { + sourceStart = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(2)); + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + } + + if (!sourceEndValue.isUndefined()) { + sourceEnd = parseIndex(lexicalGlobalObject, throwScope, callFrame->uncheckedArgument(3)); + RETURN_IF_EXCEPTION(throwScope, JSValue::encode(jsUndefined())); + } + targetStart = std::min(targetStart, targetEnd); sourceEnd = std::min(sourceEnd, sourceEndInit); sourceStart = std::min(sourceStart, sourceEnd);