mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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: https://github.com/nodejs/node/pull/5204 Reviewed-By: bnoordhuis - Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: indutny - Fedor Indutny <fedor.indutny@gmail.com>
This commit is contained in:
committed by
Ali Sheikh
parent
34aac23d0b
commit
ebbbc5a790
@@ -72,7 +72,7 @@ using v8::Uint32;
|
|||||||
using v8::Uint32Array;
|
using v8::Uint32Array;
|
||||||
using v8::Uint8Array;
|
using v8::Uint8Array;
|
||||||
using v8::Value;
|
using v8::Value;
|
||||||
using v8::WeakCallbackData;
|
using v8::WeakCallbackInfo;
|
||||||
|
|
||||||
|
|
||||||
class CallbackInfo {
|
class CallbackInfo {
|
||||||
@@ -83,8 +83,8 @@ class CallbackInfo {
|
|||||||
FreeCallback callback,
|
FreeCallback callback,
|
||||||
void* hint = 0);
|
void* hint = 0);
|
||||||
private:
|
private:
|
||||||
static void WeakCallback(const WeakCallbackData<ArrayBuffer, CallbackInfo>&);
|
static void WeakCallback(const WeakCallbackInfo<CallbackInfo>&);
|
||||||
inline void WeakCallback(Isolate* isolate, Local<ArrayBuffer> object);
|
inline void WeakCallback(Isolate* isolate, char* const data);
|
||||||
inline CallbackInfo(Isolate* isolate,
|
inline CallbackInfo(Isolate* isolate,
|
||||||
Local<ArrayBuffer> object,
|
Local<ArrayBuffer> object,
|
||||||
FreeCallback callback,
|
FreeCallback callback,
|
||||||
@@ -122,7 +122,10 @@ CallbackInfo::CallbackInfo(Isolate* isolate,
|
|||||||
if (object->ByteLength() != 0)
|
if (object->ByteLength() != 0)
|
||||||
CHECK_NE(data, nullptr);
|
CHECK_NE(data, nullptr);
|
||||||
|
|
||||||
persistent_.SetWeak(this, WeakCallback);
|
object->SetAlignedPointerInInternalField(kBufferInternalFieldIndex, data);
|
||||||
|
|
||||||
|
persistent_.SetWeak(this, WeakCallback,
|
||||||
|
v8::WeakCallbackType::kInternalFields);
|
||||||
persistent_.SetWrapperClassId(BUFFER_ID);
|
persistent_.SetWrapperClassId(BUFFER_ID);
|
||||||
persistent_.MarkIndependent();
|
persistent_.MarkIndependent();
|
||||||
isolate->AdjustAmountOfExternalAllocatedMemory(sizeof(*this));
|
isolate->AdjustAmountOfExternalAllocatedMemory(sizeof(*this));
|
||||||
@@ -135,16 +138,15 @@ CallbackInfo::~CallbackInfo() {
|
|||||||
|
|
||||||
|
|
||||||
void CallbackInfo::WeakCallback(
|
void CallbackInfo::WeakCallback(
|
||||||
const WeakCallbackData<ArrayBuffer, CallbackInfo>& data) {
|
const WeakCallbackInfo<CallbackInfo>& data) {
|
||||||
data.GetParameter()->WeakCallback(data.GetIsolate(), data.GetValue());
|
data.GetParameter()->WeakCallback(
|
||||||
|
data.GetIsolate(),
|
||||||
|
static_cast<char*>(data.GetInternalField(kBufferInternalFieldIndex)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void CallbackInfo::WeakCallback(Isolate* isolate, Local<ArrayBuffer> buf) {
|
void CallbackInfo::WeakCallback(Isolate* isolate, char* const data) {
|
||||||
ArrayBuffer::Contents obj_c = buf->GetContents();
|
callback_(data, hint_);
|
||||||
char* const obj_data = static_cast<char*>(obj_c.Data());
|
|
||||||
buf->Neuter();
|
|
||||||
callback_(obj_data, hint_);
|
|
||||||
int64_t change_in_bytes = -static_cast<int64_t>(sizeof(*this));
|
int64_t change_in_bytes = -static_cast<int64_t>(sizeof(*this));
|
||||||
isolate->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
|
isolate->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,10 @@ namespace Buffer {
|
|||||||
static const unsigned int kMaxLength =
|
static const unsigned int kMaxLength =
|
||||||
sizeof(int32_t) == sizeof(intptr_t) ? 0x3fffffff : 0x7fffffff;
|
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 typedef void (*FreeCallback)(char* data, void* hint);
|
||||||
|
|
||||||
NODE_EXTERN bool HasInstance(v8::Local<v8::Value> val);
|
NODE_EXTERN bool HasInstance(v8::Local<v8::Value> val);
|
||||||
|
|||||||
Reference in New Issue
Block a user