diff --git a/src/node_buffer.cc b/src/node_buffer.cc index c6421f76c912ce..a38bc84b0e70ff 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -72,7 +72,7 @@ using v8::Uint32; using v8::Uint32Array; using v8::Uint8Array; using v8::Value; -using v8::WeakCallbackData; +using v8::WeakCallbackInfo; class CallbackInfo { @@ -83,8 +83,8 @@ class CallbackInfo { FreeCallback callback, void* hint = 0); private: - static void WeakCallback(const WeakCallbackData&); - inline void WeakCallback(Isolate* isolate, Local object); + static void WeakCallback(const WeakCallbackInfo&); + inline void WeakCallback(Isolate* isolate, char* const data); inline CallbackInfo(Isolate* isolate, Local object, FreeCallback callback, @@ -122,7 +122,10 @@ CallbackInfo::CallbackInfo(Isolate* isolate, if (object->ByteLength() != 0) CHECK_NE(data, nullptr); - persistent_.SetWeak(this, WeakCallback); + object->SetAlignedPointerInInternalField(kBufferInternalFieldIndex, data); + + persistent_.SetWeak(this, WeakCallback, + v8::WeakCallbackType::kInternalFields); persistent_.SetWrapperClassId(BUFFER_ID); persistent_.MarkIndependent(); isolate->AdjustAmountOfExternalAllocatedMemory(sizeof(*this)); @@ -135,16 +138,15 @@ CallbackInfo::~CallbackInfo() { void CallbackInfo::WeakCallback( - const WeakCallbackData& data) { - data.GetParameter()->WeakCallback(data.GetIsolate(), data.GetValue()); + const WeakCallbackInfo& data) { + data.GetParameter()->WeakCallback( + data.GetIsolate(), + static_cast(data.GetInternalField(kBufferInternalFieldIndex))); } -void CallbackInfo::WeakCallback(Isolate* isolate, Local buf) { - ArrayBuffer::Contents obj_c = buf->GetContents(); - char* const obj_data = static_cast(obj_c.Data()); - buf->Neuter(); - callback_(obj_data, hint_); +void CallbackInfo::WeakCallback(Isolate* isolate, char* const data) { + callback_(data, hint_); int64_t change_in_bytes = -static_cast(sizeof(*this)); isolate->AdjustAmountOfExternalAllocatedMemory(change_in_bytes); diff --git a/src/node_buffer.h b/src/node_buffer.h index 503cbb167547a5..2bcf245f3920ad 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -10,6 +10,10 @@ namespace Buffer { static const unsigned int kMaxLength = sizeof(int32_t) == sizeof(intptr_t) ? 0x3fffffff : 0x7fffffff; +// Buffers have two internal fields, the first of which is reserved for use by +// Node. +static const unsigned int kBufferInternalFieldIndex = 0; + NODE_EXTERN typedef void (*FreeCallback)(char* data, void* hint); NODE_EXTERN bool HasInstance(v8::Local val);