Package Exports
- nan
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nan) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Native Abstractions for Node.js
A header file filled with macro and utility goodness for making addon development for Node.js easier across versions 0.8, 0.10 and 0.11, and eventually 0.12.
Current version: 0.2.2 (See nan.h for changelog)
Thanks to the crazy changes in V8 (and some in Node core), keeping native addons compiling happily across versions, particularly 0.10 to 0.11/0.12, is a minor nightmare. The goal of this project is to store all logic necessary to develop native Node.js addons without having to inspect NODE_MODULE_VERSION and get yourself into a macro-tangle.
This project also contains some helper utilities that make addon development a bit more pleasant.
Usage
Simply copy nan.h from this repository into your project and load it in your source files with #include "nan.h". The version is listed at the top of the file so be sure to check back here regularly for updates.
Example
See LevelDOWN for a full example of NAN in use.
or a simpler example, see the async pi estimation example in the examples directory for full code an explanation of what this Monte Carlo Pi estimation example does. Below are just some parts of the full example that illustrate the use of NAN.
Compare to the current 0.10 version of this example, found in the node-addon-examples repository and also a 0.11 version of the same found here.
Note that there is no embedded version sniffing going on here and also the async work is made much simpler, see below for details on the NanAsyncWorker class.
// addon.cc
#include <node.h>
#include "nan.h"
// ...
using namespace v8;
void InitAll(Handle<Object> exports) {
exports->Set(NanSymbol("calculateSync"),
FunctionTemplate::New(CalculateSync)->GetFunction());
exports->Set(NanSymbol("calculateAsync"),
FunctionTemplate::New(CalculateAsync)->GetFunction());
}
NODE_MODULE(addon, InitAll)// sync.h
#include <node.h>
#include "nan.h"
NAN_METHOD(CalculateSync);// sync.cc
#include <node.h>
#include "nan.h"
#include "sync.h"
// ...
using namespace v8;
// Simple synchronous access to the `Estimate()` function
NAN_METHOD(CalculateSync) {
NanScope();
// expect a number as the first argument
int points = args[0]->Uint32Value();
double est = Estimate(points);
NanReturnValue(Number::New(est));
}// async.cc
#include <node.h>
#include "nan.h"
#include "async.h"
// ...
using namespace v8;
class PiWorker : public NanAsyncWorker {
public:
PiWorker(NanCallback *callback, int points)
: NanAsyncWorker(callback), points(points) {}
~PiWorker() {}
// Executed inside the worker-thread.
// It is not safe to access V8, or V8 data structures
// here, so everything we need for input and output
// should go on `this`.
void Execute () {
estimate = Estimate(points);
}
// Executed when the async work is complete
// this function will be run inside the main event loop
// so it is safe to use V8 again
void HandleOKCallback () {
NanScope();
Local<Value> argv[] = {
Local<Value>::New(Null())
, Number::New(estimate)
};
callback->Call(2, argv);
};
private:
int points;
double estimate;
};
// Asynchronous access to the `Estimate()` function
NAN_METHOD(CalculateAsync) {
NanScope();
int points = args[0]->Uint32Value();
NanCallback *callback = new NanCallback(args[1].As<Function>());
NanAsyncQueueWorker(new PiWorker(callback, points));
NanReturnUndefined();
}
API
NAN_METHODNAN_GETTERNAN_SETTERNAN_PROPERTY_GETTERNAN_PROPERTY_SETTERNAN_PROPERTY_ENUMERATORNAN_PROPERTY_DELETERNAN_PROPERTY_QUERYNAN_WEAK_CALLBACKNanReturnValueNanReturnUndefinedNanScopeNanGetInternalFieldPointerNanSetInternalFieldPointerNanObjectWrapHandleNanMakeWeakNanSymbolNanGetPointerSafeNanSetPointerSafeNanFromV8StringNanBooleanOptionValueNanUInt32OptionValueNanThrowError,NanThrowTypeError,NanThrowRangeError,NanThrowError(Local) NanNewBufferHandle(char *, size_t, FreeCallback, void *),NanNewBufferHandle(char *, uint32_t),NanNewBufferHandle(uint32_t)NanBufferUse(char *, uint32_t)NanNewContextHandleNanHasInstanceNanPersistentToLocalNanDisposeNanAssignPersistentNanCallbackNanAsyncWorkerNanAsyncQueueWorker
NAN_METHOD(methodname)
Use NAN_METHOD to define your V8 accessible methods:
// .h:
class Foo : public node::ObjectWrap {
...
static NAN_METHOD(Bar);
static NAN_METHOD(Baz);
}
// .cc:
NAN_METHOD(Foo::Bar) {
...
}
NAN_METHOD(Foo::Baz) {
...
}The reason for this macro is because of the method signature change in 0.11:
// 0.10 and below:
v8::Handle<v8::Value> name(const v8::Arguments& args)
// 0.11 and above
void name(const v8::FunctionCallbackInfo<v8::Value>& args)The introduction of FunctionCallbackInfo brings additional complications:
NAN_GETTER(methodname)
Use NAN_GETTER to declare your V8 accessible getters. You get a Local<String> property and an appropriately typed args object that can act like the args argument to a NAN_METHOD call.
You can use NanReturnUndefined() and NanReturnValue() in a NAN_GETTER.
NAN_SETTER(methodname)
Use NAN_SETTER to declare your V8 accessible setters. Same as NAN_GETTER but you also get a Local<Value> value object to work with.
You can use NanReturnUndefined() and NanReturnValue() in a NAN_SETTER.
NAN_PROPERTY_GETTER(cbname)
Use NAN_PROPERTY_GETTER to declare your V8 accessible property getters. You get a Local<String> property and an appropriately typed args object that can act similar to the args argument to a NAN_METHOD call.
You can use NanReturnUndefined() and NanReturnValue() in a NAN_PROPERTY_GETTER.
NAN_PROPERTY_SETTER(cbname)
Use NAN_PROPERTY_SETTER to declare your V8 accessible property setters. Same as NAN_PROPERTY_GETTER but you also get a Local<Value> value object to work with.
You can use NanReturnUndefined() and NanReturnValue() in a NAN_PROPERTY_SETTER.
NAN_PROPERTY_ENUMERATOR(cbname)
Use NAN_PROPERTY_ENUMERATOR to declare your V8 accessible property enumerators. You get an appropriately typed args object like the args argument to a NAN_PROPERTY_GETTER call.
You can use NanReturnUndefined() and NanReturnValue() in a NAN_PROPERTY_ENUMERATOR.
NAN_PROPERTY_DELETER(cbname)
Use NAN_PROPERTY_DELETER to declare your V8 accessible property deleters. Same as NAN_PROPERTY_GETTER.
You can use NanReturnUndefined() and NanReturnValue() in a NAN_PROPERTY_ENUMERATOR.
NAN_PROPERTY_QUERY(cbname)
Use NAN_PROPERTY_QUERY to declare your V8 accessible property queries. Same as NAN_PROPERTY_GETTER.
You can use NanReturnUndefined() and NanReturnValue() in a NAN_PROPERTY_ENUMERATOR.
NAN_WEAK_CALLBACK(type, cbname)
Use NAN_WEAK_CALLBACK to declare your V8 WeakReference callbacks. There is an object argument accessible through NAN_WEAK_CALLBACK_OBJECT. The type argument gives the type of the data argument, accessible through NAN_WEAK_CALLBACK_DATA(type).
static NAN_WEAK_CALLBACK(BufferReference*, WeakCheck) {
if (NAN_WEAK_CALLBACK_DATA(BufferReference*)->noLongerNeeded_) {
delete NAN_WEAK_CALLBACK_DATA(BufferReference*);
} else {
// Still in use, revive, prevent GC
NanMakeWeak(NAN_WEAK_CALLBACK_OBJECT, NAN_WEAK_CALLBACK_DATA(BufferReference*), &WeakCheck);
}
}
NanReturnValue(v8::Handle<v8::Value>)
Use NanReturnValue when you want to return a value from your V8 accessible method:
NAN_METHOD(Foo::Bar) {
...
NanReturnValue(v8::String::New("FooBar!"));
}No return statement required.
NanReturnUndefined()
Use NanReturnUndefined when you don't want to return anything from your V8 accessible method:
NAN_METHOD(Foo::Baz) {
...
NanReturnUndefined();
}
NanScope()
The introduction of isolate references for many V8 calls in Node 0.11 makes NanScope() necessary, use it in place of v8::HandleScope scope:
NAN_METHOD(Foo::Bar) {
NanScope();
NanReturnValue(v8::String::New("FooBar!"));
}
void * NanGetInternalFieldPointer(v8::Handle<v8::Object>, int)
Gets a pointer to the internal field with at index from a V8 Object handle.
Local<Object> obj;
...
NanGetInternalFieldPointer(obj, 0);
void NanSetInternalFieldPointer(v8::Handle<v8::Object>, int, void *)
Sets the value of the internal field at index on a V8 Object handle.
static Persistent<Function> dataWrapperCtor;
...
Local<Object> wrapper = NanPersistentToLocal(dataWrapperCtor)->NewInstance();
NanSetInternalFieldPointer(wrapper, 0, this);
v8::Local<v8::Object> NanObjectWrapHandle(Object)
When you want to fetch the V8 object handle from a native object you've wrapped with Node's ObjectWrap, you should use NanObjectWrapHandle:
NanObjectWrapHandle(iterator)->Get(v8::String::NewSymbol("end"))
NanMakeWeak(v8::Persistent<T>, parameter, callback)
Make a persistent reference weak.
v8::String NanSymbol(char *)
This isn't strictly about compatibility, it's just an easier way to create string symbol objects (i.e. v8::String::NewSymbol(x)), for getting and setting object properties, or names of objects.
bool foo = false;
if (obj->Has(NanSymbol("foo")))
foo = optionsObj->Get(NanSymbol("foo"))->BooleanValue()
Type NanGetPointerSafe(Type *[, Type])
A helper for getting values from optional pointers. If the pointer is NULL, the function returns the optional default value, which defaults to 0. Otherwise, the function returns the value the pointer points to.
char *plugh(uint32_t *optional) {
char res[] = "xyzzy";
uint32_t param = NanGetPointerSafe<uint32_t>(optional, 0x1337);
switch (param) {
...
}
NanSetPointerSafe<uint32_t>(optional, 0xDEADBEEF);
}
bool NanSetPointerSafe(Type *, Type)
A helper for setting optional argument pointers. If the pointer is NULL, the function simply return false. Otherwise, the value is assigned to the variable the pointer points to.
const char *plugh(size_t *outputsize) {
char res[] = "xyzzy";
if !(NanSetPointerSafe<size_t>(outputsize, strlen(res) + 1)) {
...
}
...
}
char* NanFromV8String(v8::Handle<v8::Value>[, enum node::encoding, size_t *])
When you want to convert a V8 string to a char* use NanFromV8String. It is possible to define an encoding that defaults to node::UTF8 as well as a pointer to a variable that will be assigned the number of bytes in the returned string. On versions prior to 0.11, the node::BINARY and node::BUFFER encodings currently may not work properly yet. do not Just remember that you'll end up with an object that you'll need to delete[] at some point:
size_t count;
char* name = NanFromV8String(args[0]);
char* decoded = NanFromV8String(args[1], node::BASE64, &count);
char param_copy[count];
memcpy(param_copy, decoded, count);
delete[] decoded;
bool NanBooleanOptionValue(v8::Handle<v8::Value>, v8::Handle<v8::String>[, bool])
When you have an "options" object that you need to fetch properties from, boolean options can be fetched with this pair. They check first if the object exists (IsEmpty), then if the object has the given property (Has) then they get and convert/coerce the property to a bool.
The optional last parameter is the default value, which is false if left off:
// `foo` is false unless the user supplies a truthy value for it
bool foo = NanBooleanOptionValue(optionsObj, NanSymbol("foo"));
// `bar` is true unless the user supplies a falsy value for it
bool bar = NanBooleanOptionValueDefTrue(optionsObj, NanSymbol("bar"), true);
uint32_t NanUInt32OptionValue(v8::Handle<v8::Value>, v8::Handle<v8::String>, uint32_t)
Similar to NanBooleanOptionValue, use NanUInt32OptionValue to fetch an integer option from your options object. Can be any kind of JavaScript Number and it will be coerced to an unsigned 32-bit integer.
Requires all 3 arguments as a default is not optional:
uint32_t count = NanUInt32OptionValue(optionsObj, NanSymbol("count"), 1024);
NanThrowError(message), NanThrowTypeError(message), NanThrowRangeError(message), NanThrowError(Local<Value>)
For throwing Error, TypeError and RangeError objects. You should return this call:
return NanThrowError("you must supply a callback argument");Can also handle any custom object you may want to throw.
v8::Local<v8::Object> NanNewBufferHandle(char *, uint32_t), v8::Local<v8::Object> NanNewBufferHandle(uint32_t)
The Buffer API has changed a little in Node 0.11, this helper provides consistent access to Buffer creation:
NanNewBufferHandle((char*)value.data(), value.size());Can also be used to initialize a Buffer with just a size argument.
Can also be supplied with a NAN_WEAK_CALLBACK and a hint for the garbage collector, when dealing with weak references.
v8::Local<v8::Object> NanBufferUse(char*, uint32_t)
Buffer::New(char*, uint32_t) prior to 0.11 would make a copy of the data.
While it was possible to get around this, it required a shim by passing a
callback. So the new API Buffer::Use(char*, uint32_t) was introduced to remove
needing to use this shim.
NanBufferUse uses the char* passed as the backing data, and will free the
memory automatically when the weak callback is called. Keep this in mind, as
careless use can lead to "double free or corruption" and other cryptic failures.
bool NanHasInstance(Persistent<FunctionTemplate>&, Handle<Value>)
Can be used to check the type of an object to determine it is of a particular class you have already defined and have a Persistent<FunctionTemplate> handle for.
v8::Local<Type> NanPersistentToLocal(v8::Persistent<Type>&)
Aside from FunctionCallbackInfo, the biggest and most painful change to V8 in Node 0.11 is the many restrictions now placed on Persistent handles. They are difficult to assign and difficult to fetch the original value out of.
Use NanPersistentToLocal to convert a Persistent handle back to a Local handle.
v8::Local<v8::Object> handle = NanPersistentToLocal(persistentHandle);
### Local<Context> NanNewContextHandle([ExtensionConfiguration*, Handle<ObjectTemplate>, Handle<Value>])
Creates a new `Localv8::Local<v8::FunctionTemplate> ftmpl = v8::FunctionTemplate::New();
v8::Local<v8::ObjectTemplate> otmpl = ftmpl->InstanceTemplate();
v8::Local<v8::Context> ctx = NanNewContextHandle(NULL, otmpl);
template<class T> void NanDispose(v8::Persistent<T> &)
Use NanDispose to dispose a Persistent handle.
NanDispose(persistentHandle);
NanAssignPersistent(type, handle, object)
Use NanAssignPersistent to assign a non-Persistent handle to a Persistent one. You can no longer just declare a Persistent handle and assign directly to it later, you have to Reset it in Node 0.11, so this makes it easier.
In general it is now better to place anything you want to protect from V8's garbage collector as properties of a generic v8:Object and then assign that to a Persistent. This works in older versions of Node also if you use NanAssignPersistent:
v8::Persistent<v8::Object> persistentHandle;
...
v8::Local<v8::Object> obj = v8::Object::New();
obj->Set(NanSymbol("key"), keyHandle); // where keyHandle might be a v8::Local<v8::String>
NanAssignPersistent(v8::Object, persistentHandle, obj)
NanCallback
Because of the difficulties imposed by the changes to Persistent handles in V8 in Node 0.11, creating Persistent versions of your v8::Local<v8::Function> handles is annoyingly tricky. NanCallback makes it easier by taking your Local handle, making it persistent until the NanCallback is deleted and even providing a handy Call() method to fetch and execute the callback Function.
v8::Local<v8::Function> callbackHandle = callback = args[0].As<v8::Function>();
NanCallback *callback = new NanCallback(callbackHandle);
// pass `callback` around and it's safe from GC until you:
delete callback;You can execute the callback like so:
// no arguments:
callback->Call(0, NULL);
// an error argument:
v8::Local<v8::Value> argv[] = {
v8::Exception::Error(v8::String::New("fail!"))
};
callback->Call(1, argv);
// a success argument:
v8::Local<v8::Value> argv[] = {
v8::Local<v8::Value>::New(v8::Null()),
v8::String::New("w00t!")
};
callback->Call(2, argv);NanCallback also has a Local<Function> GetCallback() method that you can use to fetch a local handle to the underlying callback function if you need it.
NanAsyncWorker
NanAsyncWorker is an abstract class that you can subclass to have much of the annoying async queuing and handling taken care of for you. It can even store arbitrary V8 objects for you and have them persist while the async work is in progress.
See a rough outline of the implementation:
class NanAsyncWorker {
public:
NanAsyncWorker (NanCallback *callback);
// Clean up persistent handles and delete the *callback
virtual ~NanAsyncWorker ();
// Check the `char *errmsg` property and call HandleOKCallback()
// or HandleErrorCallback depending on whether it has been set or not
virtual void WorkComplete ();
// You must implement this to do some async work. If there is an
// error then allocate `errmsg` to to a message and the callback will
// be passed that string in an Error object
virtual void Execute ();
protected:
// Set this if there is an error, otherwise it's NULL
const char *errmsg;
// Save a V8 object in a Persistent handle to protect it from GC
void SavePersistent(const char *key, v8::Local<v8::Object> &obj);
// Fetch a stored V8 object (don't call from within `Execute()`)
v8::Local<v8::Object> GetFromPersistent(const char *key);
// Default implementation calls the callback function with no arguments.
// Override this to return meaningful data
virtual void HandleOKCallback ();
// Default implementation calls the callback function with an Error object
// wrapping the `errmsg` string
virtual void HandleErrorCallback ();
};
NanAsyncQueueWorker(NanAsyncWorker *)
NanAsyncQueueWorker will run a NanAsyncWorker asynchronously via libuv. Both the execute and after_work steps are taken care of for you—most of the logic for this is embedded in NanAsyncWorker.
Contributors
NAN is only possible due to the excellent work of the following contributors:
| Rod Vagg | GitHub/rvagg | Twitter/@rvagg |
|---|---|---|
| Benjamin Byholm | GitHub/kkoopa | |
| Trevor Norris | GitHub/trevnorris | Twitter/@trevnorris |
Licence & copyright
Copyright (c) 2013 Rod Vagg & NAN contributors (listed above).
Native Abstractions for Node.js is licensed under an MIT +no-false-attribs license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.