-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
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]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<ArrayBuffer, CallbackInfo>&); | ||
inline void WeakCallback(Isolate* isolate, Local<ArrayBuffer> object); | ||
static void WeakCallback(const WeakCallbackInfo<CallbackInfo>&); | ||
inline void WeakCallback(Isolate* isolate, char* const data); | ||
inline CallbackInfo(Isolate* isolate, | ||
Local<ArrayBuffer> 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); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ofrobots
Author
Contributor
|
||
|
||
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<ArrayBuffer, CallbackInfo>& data) { | ||
data.GetParameter()->WeakCallback(data.GetIsolate(), data.GetValue()); | ||
const WeakCallbackInfo<CallbackInfo>& data) { | ||
data.GetParameter()->WeakCallback( | ||
data.GetIsolate(), | ||
static_cast<char*>(data.GetInternalField(kBufferInternalFieldIndex))); | ||
} | ||
|
||
|
||
void CallbackInfo::WeakCallback(Isolate* isolate, Local<ArrayBuffer> buf) { | ||
ArrayBuffer::Contents obj_c = buf->GetContents(); | ||
char* const obj_data = static_cast<char*>(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<int64_t>(sizeof(*this)); | ||
isolate->AdjustAmountOfExternalAllocatedMemory(change_in_bytes); | ||
|
||
|
@ofrobots Is there any restriction on the alignment of pointers passed to
node::Buffer::New
? Expectingdata
to be aligned to a 2-byte boundary breaksnode-ffi
– and probably other native modules – when using the current Node.js master.