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

fix the rest of node:buffer #15722

Draft
wants to merge 50 commits into
base: main
Choose a base branch
from
Draft

fix the rest of node:buffer #15722

wants to merge 50 commits into from

Conversation

nektro
Copy link
Member

@nektro nektro commented Dec 12, 2024

missing some cosmetic tests but all the functionality is there now

@robobun
Copy link

robobun commented Dec 12, 2024

Updated 10:03 AM PT - Dec 23rd, 2024

@nektro, your commit c052ebf has some failures in #8461


🧪   try this PR locally:

bunx bun-pr 15722

@nektro nektro marked this pull request as ready for review December 14, 2024 03:15

const toPrimitive = $tryGetByIdWithWellKnownSymbol(value, "toPrimitive");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to do this instead of value + ""?

Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments but getting close

@nektro nektro requested a review from Jarred-Sumner December 14, 2024 07:18
Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of unnecessary work here. Bun doesn't stay fast by doing unnecessary work.

JSC::EncodedJSValue V::validateInteger(JSC::ThrowScope& scope, JSC::JSGlobalObject* globalObject, JSC::JSValue value, const WTF::String& name, JSC::JSValue min, JSC::JSValue max)
{
if (!value.isNumber()) return Bun::ERR::INVALID_ARG_TYPE(scope, globalObject, name, "number"_s, value);
if (min.isUndefined()) min = jsDoubleNumber(JSC::minSafeInteger());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When these values are coming from C++ (not JavaScript), we shouldn't be converting from C++ -> JS types -> back.

Especially when it's compile-time known.


if (!JSC::isArray(globalObject, value)) return Bun::ERR::INVALID_ARG_TYPE(scope, globalObject, name, "Array"_s, value);

auto length = value.get(globalObject, Identifier::fromString(vm, "length"_s));
Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner Dec 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This getter call, and the length coercion below is unnecessary and likely will cause bugs.

Plus, the identifier "length" is in the special list of already available properties in the JSC::VM. vm.propertyNames.length.


if (minLength.isUndefined()) minLength = jsNumber(0);

if (!JSC::isArray(globalObject, value)) return Bun::ERR::INVALID_ARG_TYPE(scope, globalObject, name, "Array"_s, value);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using JSC::isArray, and then otherwise treating it as a normal object, we can cast it to a JSArray* and then call length on that JSArray. It is stored as an unsigned integer.

Suggested change
if (!JSC::isArray(globalObject, value)) return Bun::ERR::INVALID_ARG_TYPE(scope, globalObject, name, "Array"_s, value);
auto *array = jsDynamicCast<JSC::JSArray*>(value);
if (!array)
return Bun::ERR::INVALID_ARG_TYPE(scope, globalObject, name, "Array"_s, value);

@Jarred-Sumner
Copy link
Collaborator

This PR makes Buffer-related methods 3x - 5x slower. We are not merging this until that performance regression is fixed.

image

@@ -201,14 +204,14 @@ WTF::String JSValueToStringSafe(JSC::JSGlobalObject* globalObject, JSValue arg)
auto name = JSC::getCalculatedDisplayName(vm, cell->getObject());
if (catchScope.exception()) {
catchScope.clearException();
name = "Function"_s;
name = ""_s;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name = ""_s;
return "[Function: (anonymous)]"_s;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assuming that is fine to ignore this exception

@nektro nektro marked this pull request as draft January 6, 2025 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants