diff options
author | 2023-10-04 03:30:04 +0500 | |
---|---|---|
committer | 2023-10-03 15:30:04 -0700 | |
commit | aa8ccce952f5dfa91c84f282b5abbb184c17dfc2 (patch) | |
tree | 797558d990fe14d4ffe3f404b84cf9bcf2ac924b | |
parent | ffe6bb0b7fd801ef6a3bb408708fbbf070904dd8 (diff) | |
download | bun-aa8ccce952f5dfa91c84f282b5abbb184c17dfc2.tar.gz bun-aa8ccce952f5dfa91c84f282b5abbb184c17dfc2.tar.zst bun-aa8ccce952f5dfa91c84f282b5abbb184c17dfc2.zip |
compat: Buffer: allow optional positional arguments to be undefined (#4911)
* fix `Buffer` compat with Node.js: compare
* fix `Buffer` compat with Node.js: copy
-rw-r--r-- | src/bun.js/bindings/JSBuffer.cpp | 65 |
1 files changed, 51 insertions, 14 deletions
diff --git a/src/bun.js/bindings/JSBuffer.cpp b/src/bun.js/bindings/JSBuffer.cpp index 0ff8b76a5..f77cffac3 100644 --- a/src/bun.js/bindings/JSBuffer.cpp +++ b/src/bun.js/bindings/JSBuffer.cpp @@ -865,28 +865,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)); @@ -931,24 +952,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); |