Skip to content

Commit

Permalink
Buffer, feat: support Buffer.set
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Apr 22, 2020
1 parent eff0aaa commit c54335d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions fibjs/include/Buffer.h
Expand Up @@ -59,6 +59,7 @@ class Buffer : public Buffer_base {
virtual result_t indexOf(Buffer_base* v, int32_t offset, int32_t& retVal);
virtual result_t compare(Buffer_base* buf, int32_t& retVal);
virtual result_t copy(Buffer_base* targetBuffer, int32_t targetStart, int32_t sourceStart, int32_t sourceEnd, int32_t& retVal);
virtual result_t set(Buffer_base* src, int32_t start, int32_t& retVal);
virtual result_t readUInt8(int32_t offset, bool noAssert, int32_t& retVal);
virtual result_t readUInt16LE(int32_t offset, bool noAssert, int32_t& retVal);
virtual result_t readUInt16BE(int32_t offset, bool noAssert, int32_t& retVal);
Expand Down
21 changes: 21 additions & 0 deletions fibjs/include/ifs/Buffer.h
Expand Up @@ -61,6 +61,7 @@ class Buffer_base : public object_base {
virtual result_t indexOf(Buffer_base* v, int32_t offset, int32_t& retVal) = 0;
virtual result_t indexOf(exlib::string v, int32_t offset, int32_t& retVal) = 0;
virtual result_t copy(Buffer_base* targetBuffer, int32_t targetStart, int32_t sourceStart, int32_t sourceEnd, int32_t& retVal) = 0;
virtual result_t set(Buffer_base* src, int32_t start, int32_t& retVal) = 0;
virtual result_t readUInt8(int32_t offset, bool noAssert, int32_t& retVal) = 0;
virtual result_t readUInt16LE(int32_t offset, bool noAssert, int32_t& retVal) = 0;
virtual result_t readUInt16BE(int32_t offset, bool noAssert, int32_t& retVal) = 0;
Expand Down Expand Up @@ -141,6 +142,7 @@ class Buffer_base : public object_base {
static void s_fill(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_indexOf(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_copy(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_set(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_readUInt8(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_readUInt16LE(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_readUInt16BE(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down Expand Up @@ -215,6 +217,7 @@ inline ClassInfo& Buffer_base::class_info()
{ "fill", s_fill, false },
{ "indexOf", s_indexOf, false },
{ "copy", s_copy, false },
{ "set", s_set, false },
{ "readUInt8", s_readUInt8, false },
{ "readUInt16LE", s_readUInt16LE, false },
{ "readUInt16BE", s_readUInt16BE, false },
Expand Down Expand Up @@ -765,6 +768,24 @@ inline void Buffer_base::s_copy(const v8::FunctionCallbackInfo<v8::Value>& args)
METHOD_RETURN();
}

inline void Buffer_base::s_set(const v8::FunctionCallbackInfo<v8::Value>& args)
{
int32_t vr;

METHOD_NAME("Buffer.set");
METHOD_INSTANCE(Buffer_base);
METHOD_ENTER();

METHOD_OVER(2, 2);

ARG(obj_ptr<Buffer_base>, 0);
ARG(int32_t, 1);

hr = pInst->set(v0, v1, vr);

METHOD_RETURN();
}

inline void Buffer_base::s_readUInt8(const v8::FunctionCallbackInfo<v8::Value>& args)
{
int32_t vr;
Expand Down
13 changes: 12 additions & 1 deletion fibjs/src/global/Buffer.cpp
Expand Up @@ -597,7 +597,7 @@ result_t Buffer::copy(Buffer_base* targetBuffer, int32_t targetStart, int32_t so
int32_t sourceEnd, int32_t& retVal)
{
if (targetStart < 0 || sourceStart < 0)
return CHECK_ERROR(CALL_E_INVALIDARG);
return CHECK_ERROR(CALL_E_OUTRANGE);

if (sourceStart > (int32_t)m_data.length())
return CHECK_ERROR(CALL_E_OUTRANGE);
Expand Down Expand Up @@ -626,6 +626,17 @@ result_t Buffer::copy(Buffer_base* targetBuffer, int32_t targetStart, int32_t so
return 0;
}

result_t Buffer::set(Buffer_base* src, int32_t start, int32_t& retVal)
{
int32_t len;

src->get_length(len);
if (len + start > (int32_t)m_data.length())
return CHECK_ERROR(Runtime::setError("Buffer: Source is too large."));

return src->copy(this, start, 0, -1, retVal);
}

result_t Buffer::readNumber(int32_t offset, char* buf, int32_t size, bool noAssert, bool le)
{
int32_t sz = size;
Expand Down
7 changes: 7 additions & 0 deletions idl/zh-cn/Buffer.idl
Expand Up @@ -263,6 +263,13 @@ interface Buffer : object
*/
Integer copy(Buffer targetBuffer, Integer targetStart = 0, Integer sourceStart = 0, Integer sourceEnd = -1);

/*! @brief 从源缓存对象区域拷贝数据到目标缓存对象区域
@param src 目标缓存对象
@param start 源缓存对象开始字节位置
@return 拷贝的数据字节长度
*/
Integer set(Buffer src, Integer start);

/*! @brief 从缓存对象读取一个 8 位无符号整型数值
@param offset 指定读取的起始位置,缺省为 0
@param noAssert 指定读取越界时不抛出错误,缺省为 flase,抛出
Expand Down
15 changes: 15 additions & 0 deletions test/buffer_test.js
Expand Up @@ -614,6 +614,21 @@ describe('Buffer', () => {

});

it('set', () => {
var buf = new Buffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);

buf.set([1, 2, 3], 3);
assert.deepEqual(buf, new Buffer([0, 0, 0, 1, 2, 3, 0, 0, 0, 0]));

assert.throws(() => {
buf.set([1, 2, 3], -1);
});

assert.throws(() => {
buf.set([1, 2, 3], 9);
});
});

it("readNumber", () => {
var buf = new Buffer([0x23, 0x42]);

Expand Down

0 comments on commit c54335d

Please sign in to comment.