Skip to content

Commit

Permalink
Buffer, bugfix: new Buffer error when TypedArray offset is not zero.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Dec 31, 2017
1 parent 9421162 commit 7e04649
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 4 additions & 5 deletions fibjs/src/global/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ result_t Buffer_base::_new(v8::Local<v8::ArrayBuffer> datas,
result_t Buffer_base::_new(v8::Local<v8::TypedArray> datas,
obj_ptr<Buffer_base>& retVal, v8::Local<v8::Object> This)
{
if (datas->IsUint8Array() || datas->IsInt8Array())
return _new(datas->Buffer(), retVal, This);
if (datas->IsUint8Array() || datas->IsInt8Array()) {
v8::Local<v8::ArrayBufferView> datas1 = datas;
return _new(datas1, retVal, This);
}

obj_ptr<Buffer> buf;
retVal = buf = new Buffer();
Expand All @@ -86,9 +88,6 @@ result_t Buffer_base::_new(v8::Local<v8::TypedArray> datas,
result_t Buffer_base::_new(v8::Local<v8::ArrayBufferView> datas,
obj_ptr<Buffer_base>& retVal, v8::Local<v8::Object> This)
{
if (datas->ByteOffset() == 0)
return _new(datas->Buffer(), retVal, This);

exlib::string str;
str.resize(datas->ByteLength());
datas->CopyContents(str.c_buffer(), str.length());
Expand Down
6 changes: 6 additions & 0 deletions test/buffer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ describe('Buffer', () => {

assert.equal(buf.length, 2);
assert.equal(buf.hex(), "88a0");

var arr = new Uint8Array([0x10, 0x20, 0x30]);
var arr1 = new Uint8Array(arr.buffer, 1, 2);
var buf = new Buffer(arr1);
assert.equal(buf.length, 2);
assert.equal(buf.hex(), "2030");
});

it('new Buffer(ArrayBuffer)', () => {
Expand Down

0 comments on commit 7e04649

Please sign in to comment.