Skip to content

Commit 8cf4941

Browse files
authored
Merge pull request #4 from richerfu/DawningW-main
fix: make Buffer extend Object directly
2 parents 42187ff + c488e39 commit 8cf4941

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

include/napi-inl.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -2834,20 +2834,27 @@ inline void ArrayBuffer::Detach() {
28342834
}
28352835

28362836
template <typename T>
2837-
inline Buffer<T>::Buffer() : Uint8Array() {}
2837+
inline Buffer<T>::Buffer() : Object() {}
28382838

28392839
template <typename T>
28402840
inline Buffer<T>::Buffer(napi_env env, napi_value value)
2841-
: Uint8Array(env, value) {}
2841+
: Object(env, value) {}
28422842

28432843
template <typename T>
28442844
inline size_t Buffer<T>::Length() const {
2845-
return ByteLength() / sizeof(T);
2845+
void *data = nullptr;
2846+
size_t length = 0;
2847+
napi_status status = napi_get_buffer_info(_env, _value, &data, &length);
2848+
NAPI_THROW_IF_FAILED(_env, status, Buffer<T>());
2849+
return length;
28462850
}
28472851

28482852
template <typename T>
28492853
inline T* Buffer<T>::Data() const {
2850-
return reinterpret_cast<T*>(const_cast<uint8_t*>(Uint8Array::Data()));
2854+
void *data = nullptr;
2855+
size_t length = 0;
2856+
napi_get_buffer_info(_env, _value, &data, &length);
2857+
return reinterpret_cast<T*>(data);
28512858
}
28522859

28532860
////////////////////////////////////////////////////////////////////////////////

include/napi.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,7 @@ class Date : public Value {
15161516
};
15171517

15181518
template <typename T>
1519-
class Buffer : public Uint8Array {
1519+
class Buffer : public Object {
15201520
public:
15211521
static Buffer<T> New(napi_env env, size_t length);
15221522
#ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED

0 commit comments

Comments
 (0)