Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

buffer: introduce Buffer#toArrayBuffer() API #6337

Merged
merged 2 commits into from Oct 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/buffer.markdown
Expand Up @@ -671,6 +671,10 @@ buffer.
var b = new Buffer(50);
b.fill("h");

### buf.toArrayBuffer()

Creates a new `ArrayBuffer` with the copied memory of the buffer instance.

## buffer.INSPECT_MAX_BYTES

* Number, Default: 50
Expand Down
26 changes: 25 additions & 1 deletion src/node_buffer.cc
Expand Up @@ -43,7 +43,9 @@
Local<Object> obj = argT; \
size_t obj_length = obj->GetIndexedPropertiesExternalArrayDataLength(); \
char* obj_data = static_cast<char*>( \
obj->GetIndexedPropertiesExternalArrayData());
obj->GetIndexedPropertiesExternalArrayData()); \
if (obj_length > 0) \
assert(obj_data != NULL);

#define SLICE_START_END(start_arg, end_arg, end_max) \
size_t start; \
Expand All @@ -57,6 +59,7 @@
namespace node {
namespace Buffer {

using v8::ArrayBuffer;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
Expand Down Expand Up @@ -548,6 +551,25 @@ void WriteDoubleBE(const FunctionCallbackInfo<Value>& args) {
}


void ToArrayBuffer(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

ARGS_THIS(args.This());
void* adata = malloc(obj_length);

if (adata == NULL) {
FatalError("node::Buffer::ToArrayBuffer("
"const FunctionCallbackInfo<v8::Value>&)",
"Out Of Memory");
}

memcpy(adata, obj_data, obj_length);

Local<ArrayBuffer> abuf = ArrayBuffer::New(adata, obj_length);
args.GetReturnValue().Set(abuf);
}


void ByteLength(const FunctionCallbackInfo<Value> &args) {
HandleScope scope(node_isolate);

Expand Down Expand Up @@ -602,6 +624,8 @@ void SetupBufferJS(const FunctionCallbackInfo<Value>& args) {
NODE_SET_METHOD(proto, "writeFloatBE", WriteFloatBE);
NODE_SET_METHOD(proto, "writeFloatLE", WriteFloatLE);

NODE_SET_METHOD(proto, "toArrayBuffer", ToArrayBuffer);

NODE_SET_METHOD(proto, "copy", Copy);
NODE_SET_METHOD(proto, "fill", Fill);

Expand Down
12 changes: 12 additions & 0 deletions test/simple/test-buffer.js
Expand Up @@ -981,3 +981,15 @@ assert.throws(function() {
assert.equal(c[i], i);
}
})();

// Test Buffers to ArrayBuffers
var b = new Buffer(5).fill('abcdf');
var c = b.toArrayBuffer();
assert.equal(c.byteLength, 5);
assert.equal(Object.prototype.toString.call(c), '[object ArrayBuffer]');
var d = new Uint8Array(c);
for (var i = 0; i < 5; i++)
assert.equal(d[i], b[i]);

Choose a reason for hiding this comment

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

can we also add an assert that changing either a byte in one buffer doesn't change the other? for both permutations. I know it's clear from the code that's what's happening, but it would be nice if test that scenario.

Copy link
Author

Choose a reason for hiding this comment

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

good point. i'll add one

b.fill('ghijk');
for (var i = 0; i < 5; i++)
assert.notEqual(d[i], b[i]);