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

* fix `Buffer` compat with Node.js: compare

* fix `Buffer` compat with Node.js: copy
This commit is contained in:
Andrey Smirnov
2023-10-04 03:30:04 +05:00
committed by GitHub
parent ffe6bb0b7f
commit aa8ccce952

View File

@@ -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);