diff options
author | 2022-11-27 20:04:57 -0800 | |
---|---|---|
committer | 2022-11-27 20:04:57 -0800 | |
commit | 427203874355abb4e1b1f97852a9fea2d4a5aa14 (patch) | |
tree | d6c15506b9f0b601300f859ea5fe89077f244a49 | |
parent | 002f4ecc9e0dfb19ea9e5032b812b9b976706995 (diff) | |
download | bun-427203874355abb4e1b1f97852a9fea2d4a5aa14.tar.gz bun-427203874355abb4e1b1f97852a9fea2d4a5aa14.tar.zst bun-427203874355abb4e1b1f97852a9fea2d4a5aa14.zip |
Fix failing buffer tests
-rw-r--r-- | src/bun.js/bindings/JSBuffer.cpp | 18 | ||||
-rw-r--r-- | test/bun.js/buffer.test.js | 4 |
2 files changed, 14 insertions, 8 deletions
diff --git a/src/bun.js/bindings/JSBuffer.cpp b/src/bun.js/bindings/JSBuffer.cpp index d2d8c281f..8f9ce2dd2 100644 --- a/src/bun.js/bindings/JSBuffer.cpp +++ b/src/bun.js/bindings/JSBuffer.cpp @@ -968,6 +968,8 @@ static int64_t indexOf(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* if (callFrame->argumentCount() > 1) { auto byteOffset_ = callFrame->uncheckedArgument(1).toNumber(lexicalGlobalObject); + RETURN_IF_EXCEPTION(scope, -1); + if (std::isnan(byteOffset_) || std::isinf(byteOffset_)) { byteOffset = last ? length - 1 : 0; } else if (byteOffset_ < 0) { @@ -1002,7 +1004,9 @@ static int64_t indexOf(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* } if (value.isString()) { - auto* str = value.toString(lexicalGlobalObject); + auto* str = value.toStringOrNull(lexicalGlobalObject); + RETURN_IF_EXCEPTION(scope, -1); + JSC::EncodedJSValue encodedBuffer = constructFromEncoding(lexicalGlobalObject, str, encoding); auto* arrayValue = JSC::jsDynamicCast<JSC::JSUint8Array*>(JSC::JSValue::decode(encodedBuffer)); int64_t lengthValue = static_cast<int64_t>(arrayValue->byteLength()); @@ -1013,7 +1017,9 @@ static int64_t indexOf(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* return indexOf(typedVector, length, typedVectorValue, lengthValue, byteOffset); } } else if (value.isNumber()) { - uint8_t byteValue = static_cast<uint8_t>(value.toNumber(lexicalGlobalObject)); + uint8_t byteValue = static_cast<uint8_t>((value.toInt32(lexicalGlobalObject)) % 256); + RETURN_IF_EXCEPTION(scope, -1); + if (last) { for (int64_t i = byteOffset; i >= 0; --i) { if (byteValue == typedVector[i]) { @@ -1021,12 +1027,12 @@ static int64_t indexOf(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* } } } else { - for (int64_t i = byteOffset; i < length; ++i) { - if (byteValue == typedVector[i]) { - return i; - } + const void* offset = memchr(reinterpret_cast<const void*>(typedVector + byteOffset), byteValue, length - byteOffset); + if (offset != NULL) { + return static_cast<int64_t>(static_cast<const uint8_t*>(offset) - typedVector); } } + return -1; } else if (auto* arrayValue = JSC::jsDynamicCast<JSC::JSUint8Array*>(value)) { size_t lengthValue = arrayValue->byteLength(); diff --git a/test/bun.js/buffer.test.js b/test/bun.js/buffer.test.js index 5a367f725..14394bd2a 100644 --- a/test/bun.js/buffer.test.js +++ b/test/bun.js/buffer.test.js @@ -596,7 +596,7 @@ it("Buffer.from(base64)", () => { expect(buf.toString()).toBe("hello world"); expect( - Buffer.from(btoa('console.log("hello world")\n'), "base64").toString() + Buffer.from(btoa('console.log("hello world")\n'), "base64").toString(), ).toBe('console.log("hello world")\n'); }); @@ -608,7 +608,7 @@ it("Buffer.toString(base64)", () => { { expect(Buffer.from(`console.log("hello world")\n`).toString("base64")).toBe( - btoa('console.log("hello world")\n') + btoa('console.log("hello world")\n'), ); } }); |