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

Fixes an issue where Buffer.from was not supporting SharedArrayBuffer #8510

Closed
wants to merge 6 commits into from

Conversation

ojss
Copy link
Contributor

@ojss ojss commented Sep 13, 2016

Checklist
  • make -j4 test (UNIX), or vcbuild test nosign (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
  • buffer
Description of change

Exposes isSharedArrayBuffer to check for SharedArrayBuffer type object in JS.
Buffer.from can now take a SharedArrayBuffer as an argument.
refer to issue #8440.

@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. labels Sep 13, 2016
@@ -0,0 +1,26 @@
/*global SharedArrayBuffer*/
/*eslint no-undef: "error"*/
Copy link
Member

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.

Copy link
Contributor Author

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
@mscdex
Copy link
Contributor

mscdex commented Sep 13, 2016

@ojss
Copy link
Contributor Author

ojss commented Sep 13, 2016

@mscdex I will be force pushing a squashed set of commits is that alright?

@mscdex
Copy link
Contributor

mscdex commented Sep 13, 2016

@ojss Yes

ar[1] = 4000;

var arr_buf = Buffer.from(arr.buffer);
var ar_buf = Buffer.from(ar.buffer);
Copy link
Member

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

@addaleax
Copy link
Member

Hi @ojss, this looks great so far! There are a couple of other places in lib/buffer.js where isArrayBuffer() is used… Could you take a look at these, too?

@ojss
Copy link
Contributor Author

ojss commented Sep 13, 2016

@addaleax
Copy link
Member

@ojss exactly, thanks!

Updated test-buffer-sharedarraybuffer.js to have more readable
variable names.
Replaced var definitions with const.
@ojss
Copy link
Contributor Author

ojss commented Sep 13, 2016

@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)) {
Copy link
Contributor

@mscdex mscdex Sep 13, 2016

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 ||
Copy link
Contributor Author

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.

@mscdex
Copy link
Contributor

mscdex commented Sep 13, 2016

@ojss
Copy link
Contributor Author

ojss commented Sep 14, 2016

@mscdex @addaleax why is that test pending for so long?

@mscdex
Copy link
Contributor

mscdex commented Sep 14, 2016

@ojss Just CI infrastructure issues.

LGTM.

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

LGTM

@ojss
Copy link
Contributor Author

ojss commented Sep 17, 2016

@addaleax @mscdex by when will the pull request to be accepted?

@mscdex
Copy link
Contributor

mscdex commented Sep 17, 2016

Landed in 2a2ec9d. Thanks @ojss!

@mscdex mscdex closed this Sep 17, 2016
mscdex pushed a commit that referenced this pull request Sep 17, 2016
Fixes: #8440
PR-URL: #8510
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@ojss
Copy link
Contributor Author

ojss commented Sep 17, 2016

Thanks for the help @addaleax and @mscdex!

@ojss ojss deleted the 8440-fix branch September 17, 2016 09:20
@addaleax
Copy link
Member

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)) {
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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! :/

Copy link
Contributor

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.

Copy link
Contributor

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.

@MylesBorins
Copy link
Contributor

@mscdex @addaleax should this be backported?

@addaleax
Copy link
Member

addaleax commented Oct 6, 2016

@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.

@MylesBorins
Copy link
Contributor

@addaleax that was exactly what I wanted to know, if it is behind a flag then it shouldn't land

Thanks

addaleax pushed a commit that referenced this pull request Oct 6, 2016
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>
jasnell pushed a commit that referenced this pull request Oct 10, 2016
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>
Fishrock123 pushed a commit that referenced this pull request Oct 11, 2016
Fixes: #8440
PR-URL: #8510
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Fishrock123 pushed a commit that referenced this pull request Oct 11, 2016
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants