Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
base-object: add BaseObject
Browse files Browse the repository at this point in the history
BaseObject is a class that just handles the Persistent handle attached
to the class instance.

This also removed WeakObject. Reordering the inheritance chain helps
prevent unneeded calls on instances that don't call MakeCallback.
  • Loading branch information
trevnorris committed Nov 12, 2013
1 parent 6cea16f commit d120d92
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 160 deletions.
4 changes: 2 additions & 2 deletions node.gyp
Expand Up @@ -117,6 +117,8 @@
# headers to make for a more pleasant IDE experience
'src/async-wrap.h',
'src/async-wrap-inl.h',
'src/base-object.h',
'src/base-object-inl.h',
'src/env.h',
'src/env-inl.h',
'src/handle_wrap.h',
Expand Down Expand Up @@ -145,8 +147,6 @@
'src/tree.h',
'src/util.h',
'src/util-inl.h',
'src/weak-object.h',
'src/weak-object-inl.h',
'deps/http_parser/http_parser.h',
'<(SHARED_INTERMEDIATE_DIR)/node_natives.h',
# javascript files to make for an even more pleasant IDE experience
Expand Down
24 changes: 4 additions & 20 deletions src/async-wrap-inl.h
Expand Up @@ -23,21 +23,21 @@
#define SRC_ASYNC_WRAP_INL_H_

#include "async-wrap.h"
#include "base-object.h"
#include "base-object-inl.h"
#include "env.h"
#include "env-inl.h"
#include "util.h"
#include "util-inl.h"

#include "v8.h"
#include <assert.h>

namespace node {

inline AsyncWrap::AsyncWrap(Environment* env, v8::Handle<v8::Object> object)
: object_(env->isolate(), object),
env_(env),
: BaseObject(env, object),
async_flags_(NO_OPTIONS) {
assert(!object.IsEmpty());

if (!env->has_async_listeners())
return;

Expand All @@ -54,7 +54,6 @@ inline AsyncWrap::AsyncWrap(Environment* env, v8::Handle<v8::Object> object)


inline AsyncWrap::~AsyncWrap() {
assert(persistent().IsEmpty());
}


Expand Down Expand Up @@ -89,21 +88,6 @@ inline bool AsyncWrap::has_async_queue() {
}


inline Environment* AsyncWrap::env() const {
return env_;
}


inline v8::Local<v8::Object> AsyncWrap::object() {
return PersistentToLocal(env()->isolate(), persistent());
}


inline v8::Persistent<v8::Object>& AsyncWrap::persistent() {
return object_;
}


inline v8::Handle<v8::Value> AsyncWrap::MakeCallback(
const v8::Handle<v8::Function> cb,
int argc,
Expand Down
13 changes: 2 additions & 11 deletions src/async-wrap.h
Expand Up @@ -22,12 +22,13 @@
#ifndef SRC_ASYNC_WRAP_H_
#define SRC_ASYNC_WRAP_H_

#include "base-object.h"
#include "env.h"
#include "v8.h"

namespace node {

class AsyncWrap {
class AsyncWrap : public BaseObject {
public:
enum AsyncFlags {
NO_OPTIONS = 0,
Expand All @@ -49,14 +50,6 @@ class AsyncWrap {

inline bool has_async_queue();

inline Environment* env() const;

// Returns the wrapped object. Illegal to call in your destructor.
inline v8::Local<v8::Object> object();

// Parent class is responsible to Dispose.
inline v8::Persistent<v8::Object>& persistent();

// Only call these within a valid HandleScope.
inline v8::Handle<v8::Value> MakeCallback(const v8::Handle<v8::Function> cb,
int argc,
Expand All @@ -79,8 +72,6 @@ class AsyncWrap {
static inline void RemoveAsyncListener(
const v8::FunctionCallbackInfo<v8::Value>& args);

v8::Persistent<v8::Object> object_;
Environment* const env_;
uint32_t async_flags_;
};

Expand Down
71 changes: 46 additions & 25 deletions src/weak-object-inl.h → src/base-object-inl.h
Expand Up @@ -19,48 +19,69 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#ifndef SRC_WEAK_OBJECT_INL_H_
#define SRC_WEAK_OBJECT_INL_H_
#ifndef SRC_BASE_OBJECT_INL_H_
#define SRC_BASE_OBJECT_INL_H_

#include "weak-object.h"
#include "async-wrap.h"
#include "async-wrap-inl.h"
#include "base-object.h"
#include "util.h"
#include "util-inl.h"
#include "v8.h"

#include <assert.h>

namespace node {

WeakObject::WeakObject(Environment* env, v8::Local<v8::Object> object)
: AsyncWrap(env, object) {
persistent().MarkIndependent();
inline BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> handle)
: handle_(env->isolate(), handle),
env_(env) {
assert(!handle.IsEmpty());
}


// The pointer is resolved as void*.
Wrap<WeakObject>(object, this);
MakeWeak();
inline BaseObject::~BaseObject() {
assert(handle_.IsEmpty());
}

WeakObject::~WeakObject() {

inline v8::Persistent<v8::Object>& BaseObject::persistent() {
return handle_;
}

void WeakObject::MakeWeak() {
persistent().MakeWeak(this, WeakCallback);

inline v8::Local<v8::Object> BaseObject::object() {
return PersistentToLocal(env_->isolate(), handle_);
}

void WeakObject::ClearWeak() {
persistent().ClearWeak();

inline Environment* BaseObject::env() const {
return env_;
}

void WeakObject::WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* persistent,
WeakObject* self) {
// Dispose now instead of in the destructor to avoid child classes that call
// `delete this` in their destructor from blowing up.
// Dispose the class member instead of the argument or else the IsEmpty()
// check in ~AsyncWrap will fail.
self->persistent().Dispose();

template <typename Type>
inline void BaseObject::WeakCallback(
const v8::WeakCallbackData<v8::Object, Type>& data) {
Type* self = data.GetParameter();
self->persistent().Reset();
delete self;
}


template <typename Type>
inline void BaseObject::MakeWeak(Type* ptr) {
v8::HandleScope scope(env_->isolate());
v8::Local<v8::Object> handle = object();
assert(handle->InternalFieldCount() > 0);
Wrap<Type>(handle, ptr);
handle_.MarkIndependent();
handle_.SetWeak<Type>(ptr, WeakCallback<Type>);
}


inline void BaseObject::ClearWeak() {
handle_.ClearWeak();
}

} // namespace node

#endif // SRC_WEAK_OBJECT_INL_H_
#endif // SRC_BASE_OBJECT_INL_H_
42 changes: 28 additions & 14 deletions src/weak-object.h → src/base-object.h
Expand Up @@ -19,29 +19,43 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#ifndef SRC_WEAK_OBJECT_H_
#define SRC_WEAK_OBJECT_H_
#ifndef SRC_BASE_OBJECT_H_
#define SRC_BASE_OBJECT_H_

#include "async-wrap.h"
#include "env.h"
#include "v8.h"

namespace node {

class WeakObject : public AsyncWrap {
protected:
// |object| should be an instance of a v8::ObjectTemplate that has at least
// one internal field reserved with v8::ObjectTemplate::SetInternalFieldCount.
inline WeakObject(Environment* env, v8::Local<v8::Object> object);
virtual inline ~WeakObject();
inline void MakeWeak();
class BaseObject {
public:
BaseObject(Environment* env, v8::Local<v8::Object> handle);
~BaseObject();

// Returns the wrapped object. Illegal to call in your destructor.
inline v8::Local<v8::Object> object();

// Parent class is responsible to Dispose.
inline v8::Persistent<v8::Object>& persistent();

inline Environment* env() const;

template <typename Type>
inline void MakeWeak(Type* ptr);

inline void ClearWeak();

private:
inline static void WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* persistent,
WeakObject* self);
BaseObject();

template <typename Type>
static inline void WeakCallback(
const v8::WeakCallbackData<v8::Object, Type>& data);

v8::Persistent<v8::Object> handle_;
Environment* env_;
};

} // namespace node

#endif // SRC_WEAK_OBJECT_H_
#endif // SRC_BASE_OBJECT_H_
9 changes: 5 additions & 4 deletions src/node_contextify.cc
Expand Up @@ -22,12 +22,12 @@
#include "node.h"
#include "node_internals.h"
#include "node_watchdog.h"
#include "base-object.h"
#include "base-object-inl.h"
#include "env.h"
#include "env-inl.h"
#include "util.h"
#include "util-inl.h"
#include "weak-object.h"
#include "weak-object-inl.h"

namespace node {

Expand Down Expand Up @@ -381,7 +381,7 @@ class ContextifyContext {
}
};

class ContextifyScript : public WeakObject {
class ContextifyScript : public BaseObject {
private:
Persistent<Script> script_;

Expand Down Expand Up @@ -607,7 +607,8 @@ class ContextifyScript : public WeakObject {


ContextifyScript(Environment* env, Local<Object> object)
: WeakObject(env, object) {
: BaseObject(env, object) {
MakeWeak<ContextifyScript>(this);
}


Expand Down
2 changes: 2 additions & 0 deletions src/node_crypto.cc
Expand Up @@ -26,6 +26,8 @@
#include "node_crypto_groups.h"
#include "tls_wrap.h" // TLSCallbacks

#include "async-wrap.h"
#include "async-wrap-inl.h"
#include "env.h"
#include "env-inl.h"
#include "string_bytes.h"
Expand Down

0 comments on commit d120d92

Please sign in to comment.