-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Fixes an issue where Buffer.from was not supporting SharedArrayBuffer #8510
Conversation
@@ -0,0 +1,26 @@ | |||
/*global SharedArrayBuffer*/ | |||
/*eslint no-undef: "error"*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already set in the main project .eslintrc
file so this comment should be unnecessary, unless there's something I'm missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second line isn't necessary but the first one is absolutely required or else the linter fails.
add value method map to check if given object is a SharedArrayBuffer.
add an isSharedBuffer check to Buffer.from() fromArrayBuffer() can now handle a SharedArrayBuffer object. Fixes an issue where Buffer.from was not supporting SharedArrayBuffer Fixes: nodejs#8440
refer issue nodejs#8440
@mscdex I will be force pushing a squashed set of commits is that alright? |
@ojss Yes |
ar[1] = 4000; | ||
|
||
var arr_buf = Buffer.from(arr.buffer); | ||
var ar_buf = Buffer.from(ar.buffer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these can be const
, too
Hi @ojss, this looks great so far! There are a couple of other places in |
@addaleax namely these areas I assume: Will do. :) |
@ojss exactly, thanks! |
Updated test-buffer-sharedarraybuffer.js to have more readable variable names. Replaced var definitions with const.
@addaleax please have a look. |
@@ -264,7 +264,8 @@ function fromObject(obj) { | |||
} | |||
|
|||
if (obj) { | |||
if (isArrayBuffer(obj.buffer) || 'length' in obj) { | |||
if (isArrayBuffer(obj.buffer) || 'length' in obj | |||
|| isSharedArrayBuffer(obj)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I prefer this style as well, this project's style places the operators on the right-hand side:
if (isArrayBuffer(obj.buffer) || 'length' in obj ||
isSharedArrayBuffer(obj)) {
You might also move the second condition to a new line as well to make the code easier to scan visually.
Added a isSharedArrayBuffer check in Buffer.byteLength method. Added a isSharedArrayBuffer check in fromObject method. Buffer.byteLength can potentially benefit from this change as it can be used interchangeably with the byteLength property of SharedArrayBuffer.
The new assertion checks of the output of Buffer.byteLength() is equal to the SharedArrayBuffer property byteLength.
@@ -264,7 +264,8 @@ function fromObject(obj) { | |||
} | |||
|
|||
if (obj) { | |||
if (isArrayBuffer(obj.buffer) || 'length' in obj) { | |||
if (isArrayBuffer(obj.buffer) || 'length' in obj || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mscdex changed the style here.
@ojss Just CI infrastructure issues. LGTM. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Awesome, great to have you on board! It’s quite usual that it takes a couple of days until PRs are landed, but if you feel like something is ready to land and it has been a while, pinging is exactly the right thing to do. |
@@ -264,7 +264,8 @@ function fromObject(obj) { | |||
} | |||
|
|||
if (obj) { | |||
if (isArrayBuffer(obj.buffer) || 'length' in obj) { | |||
if (isArrayBuffer(obj.buffer) || 'length' in obj || | |||
isSharedArrayBuffer(obj)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ojss Should this have been obj.buffer
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@addaleax you're right! I am so sorry about this mistake, I will fix this right away.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sorry I didn’t notice that during review myself! :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while we're at it can we do something about 'length' in obj
? by "do something" i mean let's just simplify it for !!obj.length
or some such. using in
here is wasting a lot of performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wait. this already landed. okay, ill take care of it another time.
@thealphanerd yeah… no… maybe? Is there some kind of succinct description of what commits should or should not go into LTS branches that one could judge commits by? I think I’ve heard something like “changes that increase stability”, and I wouldn’t count this as such. It’s really not that important for v4, it only adds more support for a feature that’s still behind a flag. |
@addaleax that was exactly what I wanted to know, if it is behind a flag then it shouldn't land Thanks |
isSharedArrayBuffer in fromObject was missing obj.buffer moved the 'length' in obj check so that it is checked first making the code slightly more performant and able to handle SharedArrayBuffer without relying on an explicit check. Ref: #8510 PR-URL: #8739 Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
isSharedArrayBuffer in fromObject was missing obj.buffer moved the 'length' in obj check so that it is checked first making the code slightly more performant and able to handle SharedArrayBuffer without relying on an explicit check. Ref: #8510 PR-URL: #8739 Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
isSharedArrayBuffer in fromObject was missing obj.buffer moved the 'length' in obj check so that it is checked first making the code slightly more performant and able to handle SharedArrayBuffer without relying on an explicit check. Ref: #8510 PR-URL: #8739 Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Checklist
make -j4 test
(UNIX), orvcbuild test nosign
(Windows) passesAffected core subsystem(s)
Description of change
Exposes
isSharedArrayBuffer
to check forSharedArrayBuffer
type object in JS.Buffer.from can now take a
SharedArrayBuffer
as an argument.refer to issue #8440.