-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Buffer.from not support SharedArrayBuffer #8440
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
Comments
I’ve labelled this |
Good catch on this. I'd been meaning to give this a try but hadn't gotten around to it. |
Isn't it a bit early for this? |
Perhaps, but we've taken fixes for other early bits to make sure things work. For example, the SIMD stuff in |
The fix is simple. Add a |
I would like to help with this issue. |
@ojss Awesome! I would maybe wait for #8453 to be merged, or start from there, so that the changes there don’t conflict with yours. That PR should also give a few hints as to where changes are necessary. :) A couple of hints: You’ll probably need to add one or two lines to If you have any questions, feel free to ask here or in #node-dev on Freenode. :) |
@addaleax Thank you! I will most certainly requiring help as I still don't completely understand the internals of node. |
@addaleax Yes I had assumed as much. But what I don't get is where are those functions defined? |
Yeah, that’s what it does. Using macros for that is a bit of magic, but it keeps the code short and makes it easy to change the list. If you’re asking where these functions pop up – that’s the line at the top of
Not sure what you mean – there aren’t any docs for internal stuff like that in |
Well looks like I got a few things correct.
I am sorry but I still don't understand where the definition of the function is? Is it generated on the fly? |
Okay, so here is how that whole thing works (I hope I’m not overexplaining but in any case there’s a good chance other people are reading this who would love to know more about the C++ side of node themselves): #define VALUE_METHOD_MAP(V) \
V(isArrayBuffer, IsArrayBuffer) \
V(isDataView, IsDataView) \
… That #define V(_, ucname) \
static void ucname(const FunctionCallbackInfo<Value>& args) { \
CHECK_EQ(1, args.Length()); \
args.GetReturnValue().Set(args[0]->ucname()); \
}
VALUE_METHOD_MAP(V)
#undef V Here one concrete When that The Now, scroll scroll scroll, near the bottom of the file there’s an #define V(lcname, ucname) env->SetMethod(target, #lcname, ucname);
VALUE_METHOD_MAP(V)
#undef V So, again, |
the most simple patch is like below(for node 6.5.0),but it is necessary to run node with --harmony-sharedarraybuffer : diff --git a/lib/buffer.js b/lib/buffer.js
index 8489ca0..849e223 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -251,6 +251,10 @@ function fromObject(obj) {
return fromArrayLike(obj);
}
+ if (obj instanceof SharedArrayBuffer) {
+ return new FastBuffer(obj);
+ }
+
if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
return fromArrayLike(obj.data);
} Since SharedArrayBuffer is not supported by default now,above code would throw exception if run node without --harmony-sharedarraybuffer. |
so,we maybe could do like this: diff --git a/lib/buffer.js b/lib/buffer.js
index 8489ca0..138047f 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -251,6 +251,11 @@ function fromObject(obj) {
return fromArrayLike(obj);
}
+ if (SharedArrayBuffer != undefined && SharedArrayBuffer != null) {
+ if (obj instanceof SharedArrayBuffer) {
+ return new FastBuffer(obj);
+ }
+ }
if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
return fromArrayLike(obj.data);
} @addaleax @ojss it is work with --harmony-sharedarraybuffer or without this option.What do you think? |
@wlunlimited thats probably the way to go :)
@addaleax wow that is very interesting. If you don't mind I have one more question. please check if this the right way to go about it: ojss@836186a |
That has nothing to do with it. :) The
That’s only necessary if you use
I see nothing wrong with that so far – you’ll want to update the rest of buffer.js and add some tests. (We typically want commit messages <= 50 characters in length, btw. If you’re unsure about something like that, you can have a look at the contributing.md.) Because this requires a flag you’ll probably want to put it into a separate test file – here are some tipps for that, although you can probably have looks at the other @wlunlimited One reason we’re using > vm.runInNewContext('new ArrayBuffer') instanceof ArrayBuffer
false |
@addaleax @wlunlimited |
Is the below test fine? /*global SharedArrayBuffer*/
/*eslint no-undef: "error"*/
'use strict';
// Flags: --harmony-sharedarraybuffer
require('../common');
const assert = require('assert');
const Buffer = require('buffer').Buffer;
const sab = new SharedArrayBuffer(24);
const arr = new Uint16Array(sab);
const ar = new Uint16Array(12);
ar[0] = 5000;
arr[0] = 5000;
arr[1] = 4000;
ar[1] = 4000;
var arr_buf = Buffer.from(arr.buffer);
var ar_buf = Buffer.from(ar.buffer);
assert.strictEqual(Buffer.compare(arr_buf, ar_buf), 0, 'SAB == ARBuf');
arr[1] = 6000;
ar[1] = 6000;
assert.strictEqual(Buffer.compare(arr_buf, ar_buf), 0); |
@ojss The But generally, yeah, it looks like you’re ready for opening a pull request – it’s much easier to comment on specific lines, run the tests, etc. that way. Maybe one more thing: If you want to propose a change that closes an issue like this one, you can add a line like |
@addaleax @ojss sorry,the patch above is just a demo, it is not completed yet.Because it is not handle the length of Buffer.from(arraybuffer,length). let sab =new SharedArrayBuffer(24); arr[0] = 5000; // Shares memory with arr this would ignore the length 10 and make buffer from the whole arr.buffer. |
@wlunlimited If you see the definition of or I could make another function similar to I will be opening a pull request in some time. |
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
Fixes: #8440 PR-URL: #8510 Reviewed-By: Brian White <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
Fixes: #8440 PR-URL: #8510 Reviewed-By: Brian White <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
create a buffer from SharedArrayBuffer just like the buff.from sample in doc:
let sab =new SharedArrayBuffer(24);
const arr = new Uint16Array(sab);
arr[0] = 5000;
arr[1] = 4000;
// Shares memory with
arr
const buf = Buffer.from(arr.buffer);
// Prints: <Buffer 88 13 a0 0f>
console.log(buf);
// Changing the original Uint16Array changes the Buffer also
arr[1] = 6000;
// Prints: <Buffer 88 13 70 17>
console.log(buf);
run above code :
node --harmony-sharedarraybuffer ./test.js
and then we would got a error below:
buffer.js:259
throw new TypeError(kFromErrorMsg);
^
TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.
at fromObject (buffer.js:259:9)
at Function.Buffer.from (buffer.js:96:10)
at Object. (/home/Myprojects/node_git/test_buff_sab.js:8:20)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
The text was updated successfully, but these errors were encountered: