Skip to content

Commit ebbbc5a

Browse files
ofrobotsAli Sheikh
authored and
Ali Sheikh
committed
buffer: replace deprecated SetWeak usage
Old style SetWeak is now deprecated, and weakness now works like phantom references. This means we no longer have a reference to the object in the weak callback. We use a kInternalFields style weak callback which provides us with the contents of 2 internal fields where we can squirrel away the native buffer pointer. We can no longer neuter the buffer in the weak callback, but that should be unnecessary as the object is going to be GC'd during the current gc cycle. PR-URL: #5204 Reviewed-By: bnoordhuis - Ben Noordhuis <[email protected]> Reviewed-By: indutny - Fedor Indutny <[email protected]>
1 parent 34aac23 commit ebbbc5a

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/node_buffer.cc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ using v8::Uint32;
7272
using v8::Uint32Array;
7373
using v8::Uint8Array;
7474
using v8::Value;
75-
using v8::WeakCallbackData;
75+
using v8::WeakCallbackInfo;
7676

7777

7878
class CallbackInfo {
@@ -83,8 +83,8 @@ class CallbackInfo {
8383
FreeCallback callback,
8484
void* hint = 0);
8585
private:
86-
static void WeakCallback(const WeakCallbackData<ArrayBuffer, CallbackInfo>&);
87-
inline void WeakCallback(Isolate* isolate, Local<ArrayBuffer> object);
86+
static void WeakCallback(const WeakCallbackInfo<CallbackInfo>&);
87+
inline void WeakCallback(Isolate* isolate, char* const data);
8888
inline CallbackInfo(Isolate* isolate,
8989
Local<ArrayBuffer> object,
9090
FreeCallback callback,
@@ -122,7 +122,10 @@ CallbackInfo::CallbackInfo(Isolate* isolate,
122122
if (object->ByteLength() != 0)
123123
CHECK_NE(data, nullptr);
124124

125-
persistent_.SetWeak(this, WeakCallback);
125+
object->SetAlignedPointerInInternalField(kBufferInternalFieldIndex, data);
126+
127+
persistent_.SetWeak(this, WeakCallback,
128+
v8::WeakCallbackType::kInternalFields);
126129
persistent_.SetWrapperClassId(BUFFER_ID);
127130
persistent_.MarkIndependent();
128131
isolate->AdjustAmountOfExternalAllocatedMemory(sizeof(*this));
@@ -135,16 +138,15 @@ CallbackInfo::~CallbackInfo() {
135138

136139

137140
void CallbackInfo::WeakCallback(
138-
const WeakCallbackData<ArrayBuffer, CallbackInfo>& data) {
139-
data.GetParameter()->WeakCallback(data.GetIsolate(), data.GetValue());
141+
const WeakCallbackInfo<CallbackInfo>& data) {
142+
data.GetParameter()->WeakCallback(
143+
data.GetIsolate(),
144+
static_cast<char*>(data.GetInternalField(kBufferInternalFieldIndex)));
140145
}
141146

142147

143-
void CallbackInfo::WeakCallback(Isolate* isolate, Local<ArrayBuffer> buf) {
144-
ArrayBuffer::Contents obj_c = buf->GetContents();
145-
char* const obj_data = static_cast<char*>(obj_c.Data());
146-
buf->Neuter();
147-
callback_(obj_data, hint_);
148+
void CallbackInfo::WeakCallback(Isolate* isolate, char* const data) {
149+
callback_(data, hint_);
148150
int64_t change_in_bytes = -static_cast<int64_t>(sizeof(*this));
149151
isolate->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
150152

src/node_buffer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ namespace Buffer {
1010
static const unsigned int kMaxLength =
1111
sizeof(int32_t) == sizeof(intptr_t) ? 0x3fffffff : 0x7fffffff;
1212

13+
// Buffers have two internal fields, the first of which is reserved for use by
14+
// Node.
15+
static const unsigned int kBufferInternalFieldIndex = 0;
16+
1317
NODE_EXTERN typedef void (*FreeCallback)(char* data, void* hint);
1418

1519
NODE_EXTERN bool HasInstance(v8::Local<v8::Value> val);

0 commit comments

Comments
 (0)