Skip to content

Commit

Permalink
Buffer, refactor: refactor Buffer to inherit from Uint8Array.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed May 20, 2023
1 parent e12db17 commit 012a79a
Show file tree
Hide file tree
Showing 77 changed files with 1,585 additions and 2,389 deletions.
124 changes: 39 additions & 85 deletions fibjs/include/Buffer.h
Expand Up @@ -9,57 +9,52 @@ namespace fibjs {

class Buffer : public Buffer_base {
public:
Buffer()
Buffer(const void* data = NULL, size_t length = 0)
{
init(data, length);
}

Buffer(const exlib::string& strData)
: m_data(strData)
Buffer(Buffer* buf)
: m_store(buf->m_store)
{
extMemory((int32_t)m_data.length());
}

Buffer(Buffer_base* data)
public:
uint8_t* data()
{
exlib::string str;
return (uint8_t*)m_store->Data();
}

data->toString(str);
m_data = str;
extMemory((int32_t)m_data.length());
size_t length()
{
return m_store->ByteLength();
}

Buffer(const void* pData, size_t n)
: m_data((const char*)pData, n)
static Buffer* Cast(Buffer_base* buf)
{
extMemory((int32_t)m_data.length());
return static_cast<Buffer*>(buf);
}

static Buffer* getInstance(object_base* expected)
{
return dynamic_cast<Buffer*>(expected);
}

public:
// object_base
// object
virtual v8::Local<v8::Object> wrap(v8::Local<v8::Object> o = v8::Local<v8::Object>());
virtual result_t equals(object_base* expected, bool& retVal);
virtual result_t unbind(obj_ptr<object_base>& retVal);

public:
// Buffer_base
virtual result_t _indexed_getter(uint32_t index, int32_t& retVal);
virtual result_t _indexed_setter(uint32_t index, int32_t newVal);
virtual result_t symbol_iterator(obj_ptr<Iterator_base>& retVal);
virtual result_t get_length(int32_t& retVal);
virtual result_t get_byteOffset(int32_t& retVal);
virtual result_t get_buffer(v8::Local<v8::ArrayBuffer>& retVal);
virtual result_t resize(int32_t sz);
virtual result_t append(Buffer_base* data);
virtual result_t append(exlib::string str, exlib::string codec);
virtual result_t compare(Buffer_base* buf, int32_t& retVal);
virtual result_t write(exlib::string str, int32_t offset, int32_t length, exlib::string codec, int32_t& retVal);
virtual result_t write(exlib::string str, int32_t offset, exlib::string codec, int32_t& retVal);
virtual result_t write(exlib::string str, exlib::string codec, int32_t& retVal);
virtual result_t fill(int32_t v, int32_t offset, int32_t end, obj_ptr<Buffer_base>& retVal);
virtual result_t fill(exlib::string v, int32_t offset, int32_t end, obj_ptr<Buffer_base>& retVal);
virtual result_t fill(Buffer_base* v, int32_t offset, int32_t end, obj_ptr<Buffer_base>& retVal);
virtual result_t indexOf(int32_t v, int32_t offset, int32_t& retVal);
virtual result_t indexOf(exlib::string v, int32_t offset, int32_t& retVal);
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 fill(exlib::string v, int32_t offset, int32_t end, exlib::string codec, obj_ptr<Buffer_base>& 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, int32_t& retVal);
Expand All @@ -69,15 +64,15 @@ class Buffer : public Buffer_base {
virtual result_t readUInt32BE(int32_t offset, double& retVal);
virtual result_t readUIntLE(int32_t offset, int32_t byteLength, double& retVal);
virtual result_t readUIntBE(int32_t offset, int32_t byteLength, double& retVal);
virtual result_t readInt64LE(int32_t offset, int64_t& retVal);
virtual result_t readInt64BE(int32_t offset, int64_t& retVal);
virtual result_t readInt8(int32_t offset, int32_t& retVal);
virtual result_t readInt16LE(int32_t offset, int32_t& retVal);
virtual result_t readInt16BE(int32_t offset, int32_t& retVal);
virtual result_t readInt32LE(int32_t offset, int32_t& retVal);
virtual result_t readInt32BE(int32_t offset, int32_t& retVal);
virtual result_t readIntLE(int32_t offset, int32_t byteLength, double& retVal);
virtual result_t readIntBE(int32_t offset, int32_t byteLength, double& retVal);
virtual result_t readInt64LE(int32_t offset, int64_t& retVal);
virtual result_t readInt64BE(int32_t offset, int64_t& retVal);
virtual result_t readFloatLE(int32_t offset, double& retVal);
virtual result_t readFloatBE(int32_t offset, double& retVal);
virtual result_t readDoubleLE(int32_t offset, double& retVal);
Expand All @@ -94,81 +89,40 @@ class Buffer : public Buffer_base {
virtual result_t writeInt16BE(int32_t value, int32_t offset, int32_t& retVal);
virtual result_t writeInt32LE(int32_t value, int32_t offset, int32_t& retVal);
virtual result_t writeInt32BE(int32_t value, int32_t offset, int32_t& retVal);
virtual result_t writeIntLE(int64_t value, int32_t offset, int32_t byteLength, int32_t& retVal);
virtual result_t writeIntBE(int64_t value, int32_t offset, int32_t byteLength, int32_t& retVal);
virtual result_t writeInt64LE(int64_t value, int32_t offset, int32_t& retVal);
virtual result_t writeInt64BE(int64_t value, int32_t offset, int32_t& retVal);
virtual result_t writeIntLE(int64_t value, int32_t offset, int32_t byteLength, int32_t& retVal);
virtual result_t writeIntBE(int64_t value, int32_t offset, int32_t byteLength, int32_t& retVal);
virtual result_t writeFloatLE(double value, int32_t offset, int32_t& retVal);
virtual result_t writeFloatBE(double value, int32_t offset, int32_t& retVal);
virtual result_t writeDoubleLE(double value, int32_t offset, int32_t& retVal);
virtual result_t writeDoubleBE(double value, int32_t offset, int32_t& retVal);
virtual result_t indexOf(int32_t v, int32_t offset, int32_t& retVal);
virtual result_t indexOf(Buffer_base* v, int32_t offset, int32_t& retVal);
virtual result_t indexOf(exlib::string v, int32_t offset, int32_t& retVal);
virtual result_t slice(int32_t start, obj_ptr<Buffer_base>& retVal);
virtual result_t slice(int32_t start, int32_t end, obj_ptr<Buffer_base>& retVal);
virtual result_t join(exlib::string separator, exlib::string& retVal);
virtual result_t reverse(obj_ptr<Buffer_base>& retVal);
virtual result_t toString(exlib::string codec, int32_t offset, int32_t end, exlib::string& retVal);
virtual result_t toString(exlib::string codec, int32_t offset, exlib::string& retVal);
virtual result_t toString(exlib::string& retVal);
virtual result_t toArray(v8::Local<v8::Array>& retVal);
virtual result_t hex(exlib::string& retVal);
virtual result_t base32(exlib::string& retVal);
virtual result_t base58(exlib::string& retVal);
virtual result_t base64(exlib::string& retVal);
virtual result_t keys(obj_ptr<Iterator_base>& retVal);
virtual result_t values(obj_ptr<Iterator_base>& retVal);
virtual result_t entries(obj_ptr<Iterator_base>& retVal);
virtual result_t forEach(v8::Local<v8::Function> callback, v8::Local<v8::Value> thisArg);
virtual result_t toArray(v8::Local<v8::Array>& retVal);
virtual result_t toString(exlib::string codec, int32_t offset, int32_t end, exlib::string& retVal);
virtual result_t toString(exlib::string codec, int32_t offset, exlib::string& retVal);
virtual result_t toString(exlib::string& retVal);

virtual result_t toJSON(exlib::string key, v8::Local<v8::Value>& retVal);

public:
static result_t fromJSON(Isolate* isolate, v8::Local<v8::Value> data, v8::Local<v8::Value>& o);

char* data()
{
return m_data.c_buffer();
}

bool is_safe_codec(exlib::string codec);

public:
result_t fill(const uint8_t* buf, size_t sz, int32_t offset, int32_t end);
result_t readNumber(int32_t offset, char* buf, int32_t size, int32_t value_size, bool le);
result_t writeNumber(int32_t offset, const char* buf, int32_t size, int32_t value_size, bool le, int32_t& retVal);

public:
template <typename T>
result_t _append(T datas)
{
int32_t sz = (int32_t)datas->Length();
v8::Local<v8::Context> context = datas->GetCreationContextChecked();

if (sz) {
int32_t i;
result_t hr;
exlib::string str;

str.resize(sz);
char* _str = str.c_buffer();

for (i = 0; i < sz; i++) {
JSValue v = datas->Get(context, i);
int32_t num;

hr = GetArgumentValue(v, num);
if (hr < 0)
return CHECK_ERROR(hr);

_str[i] = (char)num;
}

extMemory((int32_t)sz);
m_data.append(str);
}

return 0;
}
void init(const void* data, size_t length);
bool is_safe_codec(exlib::string codec);

private:
exlib::string m_data;
std::shared_ptr<v8::BackingStore> m_store;
v8::Global<v8::ArrayBuffer> m_buf;
};

}
4 changes: 4 additions & 0 deletions fibjs/include/ClassInfo.h
Expand Up @@ -133,6 +133,7 @@ class ClassInfo {
o = _cache->m_function.Get(isolate->m_isolate)
->NewInstance(isolate->context())
.FromMaybe(v8::Local<v8::Object>());
o->SetAlignedPointerInInternalField(0, 0);
_cache->m_cache.Reset(isolate->m_isolate, o);

o = o->Clone();
Expand Down Expand Up @@ -374,6 +375,8 @@ class ClassInfo {
}

v8::Local<v8::ObjectTemplate> ot = _class->InstanceTemplate();
ot->SetInternalFieldCount(1);

ClassData* pcd;

pcd = &m_cd;
Expand All @@ -400,6 +403,7 @@ class ClassInfo {

if (m_cd.cor) {
v8::Local<v8::Object> o = _function->NewInstance(isolate->context()).FromMaybe(v8::Local<v8::Object>());
o->SetAlignedPointerInInternalField(0, 0);
_cache->m_cache.Reset(isolate->m_isolate, o);
}

Expand Down
6 changes: 3 additions & 3 deletions fibjs/include/File.h
Expand Up @@ -3,6 +3,7 @@
#include "ifs/File.h"
#include "Stat.h"
#include "utf8.h"
#include "Buffer.h"

#include <fcntl.h>

Expand Down Expand Up @@ -81,9 +82,8 @@ class File : public File_base {

result_t Write(Buffer_base* data)
{
exlib::string strBuf;
data->toString(strBuf);
return Write(strBuf);
obj_ptr<Buffer> buf = Buffer::Cast(data);
return Write((const char*)buf->data(), (int32_t)buf->length());
}

protected:
Expand Down
1 change: 1 addition & 0 deletions fibjs/include/Isolate.h
Expand Up @@ -180,6 +180,7 @@ class Isolate : public exlib::linkitem {

int32_t m_exitCode;

v8::Global<v8::Object> m_buffer_prototype;
bool m_enable_FileSystem;
bool m_safe_buffer;
int32_t m_max_buffer_size;
Expand Down
4 changes: 2 additions & 2 deletions fibjs/include/SslSocket.h
Expand Up @@ -74,7 +74,7 @@ class SslSocket : public SslSocket_base {
if (m_send.empty())
return next(recv);

m_buf = new Buffer(m_send);
m_buf = new Buffer(m_send.c_str(), m_send.length());
m_send.resize(0);

return m_pThis->m_s->write(m_buf, next(recv));
Expand All @@ -94,7 +94,7 @@ class SslSocket : public SslSocket_base {
{
exlib::string& m_send = m_pThis->m_send;

m_buf = new Buffer(m_send);
m_buf = new Buffer(m_send.c_str(), m_send.length());
m_send.resize(0);

return m_pThis->m_s->write(m_buf, next(end));
Expand Down
76 changes: 35 additions & 41 deletions fibjs/include/StringDecoder.h
@@ -1,9 +1,9 @@
/*
* StringDecoder.h
*
* Created on: Aug 29, 2017
* Author: ngot
*/
* StringDecoder.h
*
* Created on: Aug 29, 2017
* Author: ngot
*/

#pragma once

Expand All @@ -28,38 +28,36 @@ class StringDecoder : public StringDecoder_base {
, m_lastTotal(0)
{
int32_t size = 0;
if (this->m_encoding == "utf16le") {
if (m_encoding == "utf16le") {
size = 4;
this->m_write = &StringDecoder::defaultWrite;
this->m_end1 = &StringDecoder::utf16End1;
this->m_end2 = &StringDecoder::utf16End2;
this->m_text = &StringDecoder::utf16Text;
this->m_fillLast = &StringDecoder::defaultFillLast;
} else if (this->m_encoding == "utf8") {
m_write = &StringDecoder::defaultWrite;
m_end1 = &StringDecoder::utf16End1;
m_end2 = &StringDecoder::utf16End2;
m_text = &StringDecoder::utf16Text;
m_fillLast = &StringDecoder::defaultFillLast;
} else if (m_encoding == "utf8") {
size = 4;
this->m_write = &StringDecoder::defaultWrite;
this->m_end1 = &StringDecoder::utf8End1;
this->m_end2 = &StringDecoder::utf8End2;
this->m_text = &StringDecoder::utf8Text;
this->m_fillLast = &StringDecoder::utf8FillLast;
} else if (this->m_encoding == "base64") {
m_write = &StringDecoder::defaultWrite;
m_end1 = &StringDecoder::utf8End1;
m_end2 = &StringDecoder::utf8End2;
m_text = &StringDecoder::utf8Text;
m_fillLast = &StringDecoder::utf8FillLast;
} else if (m_encoding == "base64") {
size = 3;
this->m_write = &StringDecoder::defaultWrite;
this->m_end1 = &StringDecoder::base64End1;
this->m_end2 = &StringDecoder::base64End2;
this->m_fillLast = &StringDecoder::defaultFillLast;
this->m_text = &StringDecoder::base64Text;
m_write = &StringDecoder::defaultWrite;
m_end1 = &StringDecoder::base64End1;
m_end2 = &StringDecoder::base64End2;
m_fillLast = &StringDecoder::defaultFillLast;
m_text = &StringDecoder::base64Text;
} else {
this->m_write = &StringDecoder::simpleWrite;
this->m_end1 = &StringDecoder::simpleEnd1;
this->m_end2 = &StringDecoder::simpleEnd2;
this->m_fillLast = &StringDecoder::defaultFillLast;
this->m_text = &StringDecoder::utf8Text;
m_write = &StringDecoder::simpleWrite;
m_end1 = &StringDecoder::simpleEnd1;
m_end2 = &StringDecoder::simpleEnd2;
m_fillLast = &StringDecoder::defaultFillLast;
m_text = &StringDecoder::utf8Text;
}

this->m_lastChar = new Buffer();
if (size > 0)
this->m_lastChar->resize(size);
m_lastChar = new Buffer(NULL, size);
}

public:
Expand Down Expand Up @@ -122,17 +120,15 @@ inline int32_t utf8CheckByte(int32_t byte)

inline int32_t utf8CheckIncomplete(StringDecoder* decoder, Buffer_base* buf, int32_t i)
{
int32_t bufLen;
int32_t b;
buf->get_length(bufLen);
obj_ptr<Buffer> data = Buffer::Cast(buf);
int32_t bufLen = data->length();
const uint8_t* _data = data->data();

int32_t j = bufLen - 1;
if (j < i)
return 0;

buf->_indexed_getter(j, b);

int32_t nb = utf8CheckByte(b);
int32_t nb = utf8CheckByte(_data[j]);
if (nb >= 0) {
if (nb > 0)
decoder->set_lastNeed(nb - 1);
Expand All @@ -141,8 +137,7 @@ inline int32_t utf8CheckIncomplete(StringDecoder* decoder, Buffer_base* buf, int
if (--j < i || nb == -2)
return 0;

buf->_indexed_getter(j, b);
nb = utf8CheckByte(b);
nb = utf8CheckByte(_data[j]);
if (nb >= 0) {
if (nb > 0)
decoder->set_lastNeed(nb - 2);
Expand All @@ -151,8 +146,7 @@ inline int32_t utf8CheckIncomplete(StringDecoder* decoder, Buffer_base* buf, int
if (--j < i || nb == -2)
return 0;

buf->_indexed_getter(j, b);
nb = utf8CheckByte(b);
nb = utf8CheckByte(_data[j]);
if (nb >= 0) {
if (nb > 0) {
if (nb == 2)
Expand Down

0 comments on commit 012a79a

Please sign in to comment.