diff options
-rw-r--r-- | src/bun.js/bindings/JSBuffer.cpp | 29 | ||||
-rw-r--r-- | test/bun.js/buffer.test.js | 27 |
2 files changed, 35 insertions, 21 deletions
diff --git a/src/bun.js/bindings/JSBuffer.cpp b/src/bun.js/bindings/JSBuffer.cpp index 034d237cf..a6f321b21 100644 --- a/src/bun.js/bindings/JSBuffer.cpp +++ b/src/bun.js/bindings/JSBuffer.cpp @@ -800,25 +800,23 @@ static inline JSC::EncodedJSValue jsBufferPrototypeFunction_copyBody(JSC::JSGlob } size_t targetStart = 0; - size_t targetEndInit = view->byteLength(); - size_t targetEnd = targetEndInit; + size_t targetEnd = view->byteLength(); size_t sourceStart = 0; size_t sourceEndInit = castedThis->byteLength(); size_t sourceEnd = sourceEndInit; if (callFrame->argumentCount() > 1) { - if (auto targetEnd_ = callFrame->uncheckedArgument(1).tryGetAsUint32Index()) { - targetStart = targetEnd_.value(); + if (auto targetStart_ = callFrame->uncheckedArgument(1).tryGetAsUint32Index()) { + targetStart = targetStart_.value(); } else { throwVMTypeError(lexicalGlobalObject, throwScope, "Expected number"_s); return JSValue::encode(jsUndefined()); } if (callFrame->argumentCount() > 2) { - auto targetEndArgument = callFrame->uncheckedArgument(2); - if (auto targetEnd_ = targetEndArgument.tryGetAsUint32Index()) { - targetEnd = targetEnd_.value(); + if (auto sourceStart_ = callFrame->uncheckedArgument(2).tryGetAsUint32Index()) { + sourceStart = sourceStart_.value(); } else { throwVMTypeError(lexicalGlobalObject, throwScope, "Expected number"_s); return JSValue::encode(jsUndefined()); @@ -826,19 +824,8 @@ static inline JSC::EncodedJSValue jsBufferPrototypeFunction_copyBody(JSC::JSGlob } if (callFrame->argumentCount() > 3) { - auto targetEndArgument = callFrame->uncheckedArgument(3); - if (auto targetEnd_ = targetEndArgument.tryGetAsUint32Index()) { - sourceStart = targetEnd_.value(); - } else { - throwVMTypeError(lexicalGlobalObject, throwScope, "Expected number"_s); - return JSValue::encode(jsUndefined()); - } - } - - if (callFrame->argumentCount() > 4) { - auto targetEndArgument = callFrame->uncheckedArgument(4); - if (auto targetEnd_ = targetEndArgument.tryGetAsUint32Index()) { - sourceEnd = targetEnd_.value(); + if (auto sourceEnd_ = callFrame->uncheckedArgument(3).tryGetAsUint32Index()) { + sourceEnd = sourceEnd_.value(); } else { throwVMTypeError(lexicalGlobalObject, throwScope, "Expected number"_s); return JSValue::encode(jsUndefined()); @@ -846,7 +833,7 @@ static inline JSC::EncodedJSValue jsBufferPrototypeFunction_copyBody(JSC::JSGlob } } - targetStart = std::min(targetStart, std::min(targetEnd, targetEndInit)); + targetStart = std::min(targetStart, targetEnd); sourceStart = std::min(sourceStart, std::min(sourceEnd, sourceEndInit)); auto sourceLength = sourceEnd - sourceStart; diff --git a/test/bun.js/buffer.test.js b/test/bun.js/buffer.test.js index 87937e65e..a7fa539c0 100644 --- a/test/bun.js/buffer.test.js +++ b/test/bun.js/buffer.test.js @@ -209,6 +209,33 @@ it("Buffer.copy", () => { gc(); expect(array1.copy(array2)).toBe(128); expect(array1.join("")).toBe(array2.join("")); + + { + // Create two `Buffer` instances. + const buf1 = Buffer.allocUnsafe(26); + const buf2 = Buffer.allocUnsafe(26).fill('!'); + + for (let i = 0; i < 26; i++) { + // 97 is the decimal ASCII value for 'a'. + buf1[i] = i + 97; + } + + // Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`. + buf1.copy(buf2, 8, 16, 20); + expect(buf2.toString('ascii', 0, 25)).toBe('!!!!!!!!qrst!!!!!!!!!!!!!'); + } + + { + const buf = Buffer.allocUnsafe(26); + + for (let i = 0; i < 26; i++) { + // 97 is the decimal ASCII value for 'a'. + buf[i] = i + 97; + } + + buf.copy(buf, 0, 4, 10); + expect(buf.toString()).toBe('efghijghijklmnopqrstuvwxyz'); + } }); export function fillRepeating(dstBuffer, start, end) { |