Skip to content
This repository has been archived by the owner on Dec 9, 2018. It is now read-only.

Commit

Permalink
Improved NAN.
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoopa committed Jul 27, 2013
1 parent 561efe0 commit c810090
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
6 changes: 1 addition & 5 deletions src/contextify.cc
Expand Up @@ -51,11 +51,7 @@ class ContextifyContext : ObjectWrap {
Local<Object> createDataWrapper () {
NanScope();
Local<Object> wrapper = NanPersistentToLocal(dataWrapperCtor)->NewInstance();
#if NODE_MAJOR_VERSION > 0 || (NODE_MINOR_VERSION == 9 && (NODE_PATCH_VERSION >= 6 && NODE_PATCH_VERSION <= 10)) || NODE_MINOR_VERSION >= 11
wrapper->SetAlignedPointerInInternalField(0, this);
#else
wrapper->SetPointerInInternalField(0, this);
#endif
NanSetInternalFieldPointer(wrapper, 0, this);
return scope.Close(wrapper);
}

Expand Down
35 changes: 31 additions & 4 deletions src/nan.h
@@ -1,12 +1,18 @@
/**********************************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2013 Rod Vagg
* Copyright (c) 2013 NAN contributors:
* - Rod Vagg <https://github.com/rvagg>
* - King Koopa <https://github.com/kkoopa>
* - Trevor Norris <https://github.com/trevnorris>
*
* MIT +no-false-attribs License <https://github.com/rvagg/nan/blob/master/LICENSE>
*
* Version 0.1.0 (current Node unstable: 0.11.4)
* Version 0.2.0 (current Node unstable: 0.11.4)
*
* Changelog:
* * 0.2.0 .... TODO
*
* * 0.1.0 Jul 21 2013
* - Added `NAN_GETTER`, `NAN_SETTER`
* - Added `NanThrowError` with single Local<Value> argument
Expand Down Expand Up @@ -113,6 +119,10 @@ static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
const v8::PropertyCallbackInfo<v8::Integer>& args
# define NAN_PROPERTY_QUERY(name) \
void name(v8::Local<v8::String> property, _NAN_PROPERTY_QUERY_ARGS)
# define NanGetInternalFieldPointer(object, index) \
object->GetAlignedPointerFromInternalField(index)
# define NanSetInternalFieldPointer(object, index, value) \
object->SetAlignedPointerInInternalField(index, value)
# define NanScope() v8::HandleScope scope(nan_isolate)
# define NanReturnValue(value) return args.GetReturnValue().Set(value);
# define NanReturnUndefined() return;
Expand Down Expand Up @@ -142,7 +152,7 @@ static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
THROW_ERROR(v8::Exception::RangeError, errmsg);
}

static inline void NanDispose(v8::Persistent<v8::Object> &handle) {
template<class T> static inline void NanDispose(v8::Persistent<T> &handle) {
handle.Dispose(nan_isolate);
}

Expand All @@ -151,6 +161,10 @@ static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
return node::Buffer::New(data, size);
}

static inline v8::Local<v8::Object> NanBufferUse(char* data, uint32_t size) {
return node::Buffer::Use(data, size);
}

static inline v8::Local<v8::Object> NanNewBufferHandle (uint32_t size) {
return node::Buffer::New(size);
}
Expand Down Expand Up @@ -217,6 +231,10 @@ static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
v8::Local<v8::String> property \
, _NAN_PROPERTY_QUERY_ARGS)

# define NanGetInternalFieldPointer(object, index) \
object->GetPointerFromInternalField(index)
# define NanSetInternalFieldPointer(object, index, value) \
object->SetPointerInInternalField(index, value)
# define NanScope() v8::HandleScope scope
# define NanReturnValue(value) return scope.Close(value);
# define NanReturnUndefined() return v8::Undefined();
Expand Down Expand Up @@ -248,7 +266,7 @@ static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
THROW_ERROR(v8::Exception::RangeError, errmsg);
}

static inline void NanDispose(v8::Persistent<v8::Object> &handle) {
template<class T> static inline void NanDispose(v8::Persistent<T> &handle) {
handle.Dispose();
}

Expand All @@ -257,6 +275,15 @@ static v8::Isolate* nan_isolate = v8::Isolate::GetCurrent();
return v8::Local<v8::Object>::New(node::Buffer::New(data, size)->handle_);
}

static inline void FreeData(char *data, void *hint) {
delete[] data;
}

static inline v8::Local<v8::Object> NanBufferUse(char* data, uint32_t size) {
return v8::Local<v8::Object>::New(
node::Buffer::New(data, size, FreeData, NULL)->handle_);
}

template <class TypeName>
inline v8::Local<TypeName> NanPersistentToLocal(
const v8::Persistent<TypeName>& persistent) {
Expand Down

0 comments on commit c810090

Please sign in to comment.