8000 Buffer, refactor: implement the JavaScript version of Buffer. · fibjs/fibjs@f178961 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit f178961

Browse files
committed
Buffer, refactor: implement the JavaScript version of Buffer.
1 parent ecf693c commit f178961

30 files changed

+1832
-154
lines changed

fibjs/include/Buffer.h

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,98 @@
88
namespace fibjs {
99

1010
class Buffer : public Buffer_base {
11+
public:
12+
class store {
13+
public:
14+
store(const void* _data, size_t _length)
15+
: m_length(_length)
16+
{
17+
m_store = NewBackingStore(_length);
18+
if (_data)
19+
memcpy(data(), _data, _length);
20+
}
21+
22+
store(store& s)
23+
: m_store(s.m_store)
24+
, m_offset(s.m_offset)
25+
, m_length(s.m_length)
26+
{
27+
}
28+
29+
store(v8::Local<v8::Uint8Array> ui)
30+
: m_store(ui->Buffer()->GetBackingStore())
31+
, m_offset(ui->ByteOffset())
32+
, m_length(ui->ByteLength())
33+
{
34+
}
35+
36+
store(v8::Local<v8::ArrayBuffer> ab)
37+
: m_store(ab->GetBackingStore())
38+
, m_offset(0)
39+
, m_length(ab->ByteLength())
40+
{
41+
}
42+
43+
store(std::shared_ptr<v8::BackingStore> _store, size_t offset, size_t length)
44+
: m_store(_store)
45+
, m_offset(offset)
46+
, m_length(length)
47+
{
48+
}
49+
50+
public:
51+
uint8_t* data()
52+
{
53+
return (uint8_t*)m_store->Data() + m_offset;
54+
}
55+
56+
size_t length()
57+
{
58+
return m_length;
59+
}
60+
61+
public:
62+
std::shared_ptr<v8::BackingStore> m_store;
63+
size_t m_offset = 0;
64+
size_t m_length = 0;
65+
};
66+
1167
public:
1268
Buffer(const void* data = NULL, size_t length = 0)
69+
: m_store(data, length)
1370
{
14-
init(data, length);
71+
extMemory(length);
1572
}
1673

1774
Buffer(Buffer* buf)
1875
: m_store(buf->m_store)
1976
{
2077
}
2178

79+
Buffer(v8::Local<v8::Uint8Array> ui)
80+
: m_store(ui)
81+
{
82+
}
83+
84+
Buffer(v8::Local<v8::ArrayBuffer> ab)
85+
: m_store(ab)
86+
{
87+
}
88+
89+
Buffer(std::shared_ptr<v8::BackingStore> _store, size_t offset, size_t length)
90+
: m_store(_store, offset, length)
91+
{
92+
}
93+
2294
public:
2395
uint8_t* data()
2496
{
25-
return (uint8_t*)m_store->Data();
97+
return m_store.data();
2698
}
2799

28100
size_t length()
29101
{
30-
return m_store->ByteLength();
102+
return m_store.length();
31103
}
32104

33105
static Buffer* Cast(Buffer_base* buf)
@@ -40,10 +112,7 @@ class Buffer : public Buffer_base {
40112
return static_cast<Buffer*>(Buffer_base::getInstance(o));
41113
}
42114

43-
static Buffer* getInstance(v8::Local<v8::Value> o)
44-
{
45-
return static_cast<Buffer*>(Buffer_base::getInstance(o));
46-
}
115+
static Buffer* getInstance(v8::Local<v8::Value> o);
47116

48117
public:
49118
// object
@@ -123,11 +192,10 @@ class Buffer : public Buffer_base {
123192
result_t writeNumber(int32_t offset, const char* buf, int32_t size, int32_t value_size, bool le, int32_t& retVal);
124193

125194
public:
126-
void init(const void* data, size_t length);
127195
bool is_safe_codec(exlib::string codec);
128196

129197
private:
130-
std::shared_ptr<v8::BackingStore> m_store;
198+
store m_store;
131199
};
132200

133201
}

fibjs/include/utils.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ typedef int32_t result_t;
187187

188188
#define STREAM_BUFF_SIZE 65536
189189

190-
const int32_t kObjectPrototype = 0;
190+
enum {
191+
kObjectPrototype = 0,
192+
kBufferClassIndex = 1,
193+
kBufferPrototype = 2
194+
};
191195

192196
#if 0
193197
#define V8_SCOPE(isolate) v8::EscapableHandleScope handle_scope(isolate)
@@ -870,6 +874,10 @@ result_t GetArgumentValue(Isolate* isolate, v8::Local<v8::Value> v, obj_ptr<T>&
870874
return 0;
871875
}
872876

877+
class Buffer_base;
878+
result_t GetArgumentValue(Isolate* isolate, v8::Local<v8::Value> v, obj_ptr<Buffer_base>& vr, bool bStrict = false);
879+
result_t GetArgumentValue(Isolate* isolate, v8::Local<v8::Value> v, obj_ptr<object_base>& vr, bool bStrict = false);
880+
873881
inline bool IsJSObject(v8::Local<v8::Value> v)
874882
{
875883
if (!v->IsObject())
@@ -884,6 +892,20 @@ inline bool IsJSObject(v8::Local<v8::Value> v)
884892
return true;
885893
}
886894

895+
inline bool IsJSBuffer(v8::Local<v8::Value> v)
896+
{
897+
if (!v->IsObject())
898+
return false;
899+
900+
v8::Local<v8::Object> o = v8::Local<v8::Object>::Cast(v);
901+
v8::Local<v8::Context> _context = o->GetCreationContextChecked();
902+
JSValue proto = _context->GetEmbedderData(kBufferPrototype);
903+
if (!proto->Equals(_context, o->GetPrototype()).FromMaybe(false))
904+
return false;
905+
906+
return true;
907+
}
908+
887909
inline result_t GetArgumentValue(Isolate* isolate, v8::Local<v8::Value> v, v8::Local<v8::Object>& vr, bool bStrict = false)
888910
{
889911
if (v.IsEmpty() || !IsJSObject(v))

0 commit comments

Comments
 (0)
0