Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compat: Buffer: allow optional positional arguments to be undefined #4911

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 51 additions & 14 deletions src/bun.js/bindings/JSBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -930,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);
Expand Down
Loading