From 23589acb1c97e6199a83e46789ebe04ec3bf99ca Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Fri, 12 Feb 2016 13:58:26 +0500 Subject: [PATCH 1/4] src: replace usage of deprecated SetAccessor --- src/node.cc | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/node.cc b/src/node.cc index 2a5fe73333913a..10ee9264e3306b 100644 --- a/src/node.cc +++ b/src/node.cc @@ -116,6 +116,7 @@ using v8::Local; using v8::Locker; using v8::MaybeLocal; using v8::Message; +using v8::Name; using v8::Number; using v8::Object; using v8::ObjectTemplate; @@ -2467,7 +2468,7 @@ static void LinkedBinding(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(exports); } -static void ProcessTitleGetter(Local property, +static void ProcessTitleGetter(Local property, const PropertyCallbackInfo& info) { char buffer[512]; uv_get_process_title(buffer, sizeof(buffer)); @@ -2475,7 +2476,7 @@ static void ProcessTitleGetter(Local property, } -static void ProcessTitleSetter(Local property, +static void ProcessTitleSetter(Local property, Local value, const PropertyCallbackInfo& info) { node::Utf8Value title(info.GetIsolate(), value); @@ -2704,13 +2705,13 @@ static Local GetFeatures(Environment* env) { } -static void DebugPortGetter(Local property, +static void DebugPortGetter(Local property, const PropertyCallbackInfo& info) { info.GetReturnValue().Set(debug_port); } -static void DebugPortSetter(Local property, +static void DebugPortSetter(Local property, Local value, const PropertyCallbackInfo& info) { debug_port = value->Int32Value(); @@ -2722,7 +2723,7 @@ static void DebugPause(const FunctionCallbackInfo& args); static void DebugEnd(const FunctionCallbackInfo& args); -void NeedImmediateCallbackGetter(Local property, +void NeedImmediateCallbackGetter(Local property, const PropertyCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); const uv_check_t* immediate_check_handle = env->immediate_check_handle(); @@ -2733,7 +2734,7 @@ void NeedImmediateCallbackGetter(Local property, static void NeedImmediateCallbackSetter( - Local property, + Local property, Local value, const PropertyCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); @@ -2822,10 +2823,12 @@ void SetupProcessObject(Environment* env, Local process = env->process_object(); - process->SetAccessor(env->title_string(), - ProcessTitleGetter, - ProcessTitleSetter, - env->as_external()); + auto maybe = process->SetAccessor(env->context(), + env->title_string(), + ProcessTitleGetter, + ProcessTitleSetter, + env->as_external()); + CHECK(maybe.FromJust()); // process.version READONLY_PROPERTY(process, @@ -2989,10 +2992,12 @@ void SetupProcessObject(Environment* env, READONLY_PROPERTY(process, "pid", Integer::New(env->isolate(), getpid())); READONLY_PROPERTY(process, "features", GetFeatures(env)); - process->SetAccessor(env->need_imm_cb_string(), - NeedImmediateCallbackGetter, - NeedImmediateCallbackSetter, - env->as_external()); + maybe = process->SetAccessor(env->context(), + env->need_imm_cb_string(), + NeedImmediateCallbackGetter, + NeedImmediateCallbackSetter, + env->as_external()); + CHECK(maybe.FromJust()); // -e, --eval if (eval_string) { @@ -3067,10 +3072,13 @@ void SetupProcessObject(Environment* env, process->Set(env->exec_path_string(), exec_path_value); delete[] exec_path; - process->SetAccessor(env->debug_port_string(), - DebugPortGetter, - DebugPortSetter, - env->as_external()); + maybe = process->SetAccessor(env->context(), + env->debug_port_string(), + DebugPortGetter, + DebugPortSetter, + env->as_external()); + CHECK(maybe.FromJust()); + // define various internal methods env->SetMethod(process, From 14ba959be1173b6243a67d632f91f1750e17da6e Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Sat, 13 Feb 2016 08:02:45 +0500 Subject: [PATCH 2/4] src: fix deprecated SetWeak usage in base-object --- src/base-object-inl.h | 5 +++-- src/base-object.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/base-object-inl.h b/src/base-object-inl.h index db0daa1e82f559..87159ffc6838c7 100644 --- a/src/base-object-inl.h +++ b/src/base-object-inl.h @@ -39,7 +39,7 @@ inline Environment* BaseObject::env() const { template inline void BaseObject::WeakCallback( - const v8::WeakCallbackData& data) { + const v8::WeakCallbackInfo& data) { Type* self = data.GetParameter(); self->persistent().Reset(); delete self; @@ -53,7 +53,8 @@ inline void BaseObject::MakeWeak(Type* ptr) { CHECK_GT(handle->InternalFieldCount(), 0); Wrap(handle, ptr); handle_.MarkIndependent(); - handle_.SetWeak(ptr, WeakCallback); + handle_.SetWeak(ptr, WeakCallback, + v8::WeakCallbackType::kParameter); } diff --git a/src/base-object.h b/src/base-object.h index 5a7b95827e8f11..8574a904e71b1a 100644 --- a/src/base-object.h +++ b/src/base-object.h @@ -40,7 +40,7 @@ class BaseObject { template static inline void WeakCallback( - const v8::WeakCallbackData& data); + const v8::WeakCallbackInfo& data); v8::Persistent handle_; Environment* env_; From 34bf1da4e1020661bbc2fbf034b09d83a4915c2d Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Sun, 14 Feb 2016 08:38:35 +0500 Subject: [PATCH 3/4] buffer: cleanup CallbackInfo Dynamic checks that CallbackInfo holds an ArrayBuffer handle can be converted into compiler enforced checks. Removed unused code, and other minor cleanup. --- src/node_buffer.cc | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 988e41dbc9aa22..c6421f76c912ce 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -79,20 +79,18 @@ class CallbackInfo { public: static inline void Free(char* data, void* hint); static inline CallbackInfo* New(Isolate* isolate, - Local object, + Local object, FreeCallback callback, void* hint = 0); - inline void Dispose(Isolate* isolate); - inline Persistent* persistent(); private: - static void WeakCallback(const WeakCallbackData&); - inline void WeakCallback(Isolate* isolate, Local object); + static void WeakCallback(const WeakCallbackData&); + inline void WeakCallback(Isolate* isolate, Local object); inline CallbackInfo(Isolate* isolate, - Local object, + Local object, FreeCallback callback, void* hint); ~CallbackInfo(); - Persistent persistent_; + Persistent persistent_; FreeCallback const callback_; void* const hint_; DISALLOW_COPY_AND_ASSIGN(CallbackInfo); @@ -105,30 +103,25 @@ void CallbackInfo::Free(char* data, void*) { CallbackInfo* CallbackInfo::New(Isolate* isolate, - Local object, + Local object, FreeCallback callback, void* hint) { return new CallbackInfo(isolate, object, callback, hint); } -void CallbackInfo::Dispose(Isolate* isolate) { - WeakCallback(isolate, PersistentToLocal(isolate, persistent_)); -} - - -Persistent* CallbackInfo::persistent() { - return &persistent_; -} - - CallbackInfo::CallbackInfo(Isolate* isolate, - Local object, + Local object, FreeCallback callback, void* hint) : persistent_(isolate, object), callback_(callback), hint_(hint) { + ArrayBuffer::Contents obj_c = object->GetContents(); + char* const data = static_cast(obj_c.Data()); + if (object->ByteLength() != 0) + CHECK_NE(data, nullptr); + persistent_.SetWeak(this, WeakCallback); persistent_.SetWrapperClassId(BUFFER_ID); persistent_.MarkIndependent(); @@ -142,19 +135,14 @@ CallbackInfo::~CallbackInfo() { void CallbackInfo::WeakCallback( - const WeakCallbackData& data) { + const WeakCallbackData& data) { data.GetParameter()->WeakCallback(data.GetIsolate(), data.GetValue()); } -void CallbackInfo::WeakCallback(Isolate* isolate, Local object) { - CHECK(object->IsArrayBuffer()); - Local buf = object.As(); +void CallbackInfo::WeakCallback(Isolate* isolate, Local buf) { ArrayBuffer::Contents obj_c = buf->GetContents(); char* const obj_data = static_cast(obj_c.Data()); - if (buf->ByteLength() != 0) - CHECK_NE(obj_data, nullptr); - buf->Neuter(); callback_(obj_data, hint_); int64_t change_in_bytes = -static_cast(sizeof(*this)); From dcecbde399df0dbffc12203a223f610ce4ae712f Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Sun, 14 Feb 2016 08:53:48 +0500 Subject: [PATCH 4/4] 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. --- src/node_buffer.cc | 24 +++++++++++++----------- src/node_buffer.h | 4 ++++ 2 files changed, 17 insertions(+), 11 deletions(-) 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);