Created
December 28, 2011 03:02
-
-
Save tristanz/1525967 to your computer and use it in GitHub Desktop.
Node Header Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <node.h> | |
#include "modulename.hpp" | |
using namespace v8; | |
void RegisterModule(Handle<Object> target) { | |
MyObject::Init(target); | |
} | |
NODE_MODULE(modulename, RegisterModule); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MODULENAME_HPP | |
#define MODULENAME_HPP | |
#include <node.h> | |
// Do not include this line. It's generally frowned upon to use namespaces | |
// in header files as it may cause issues with other code that includes your | |
// header file. | |
using namespace v8; | |
class MyObject : public node::ObjectWrap { | |
public: | |
static v8::Persistent<v8::FunctionTemplate> constructor; | |
static void Init(v8::Handle<v8::Object> target) { | |
HandleScope scope; | |
Local<FunctionTemplate> tpl = FunctionTemplate::New(New); | |
Local<String> name = String::NewSymbol("MyObject"); | |
constructor = Persistent<FunctionTemplate>::New(tpl); | |
// ObjectWrap uses the first internal field to store the wrapped pointer. | |
constructor->InstanceTemplate()->SetInternalFieldCount(1); | |
constructor->SetClassName(name); | |
// Add all prototype methods, getters and setters here. | |
NODE_SET_PROTOTYPE_METHOD(constructor, "value", Value); | |
// This has to be last, otherwise the properties won't show up on the | |
// object in JavaScript. | |
target->Set(name, constructor->GetFunction()); | |
} | |
protected: | |
MyObject(int val) { | |
value_ = val; | |
} | |
static v8::Handle<v8::Value> New(const v8::Arguments& args) { | |
HandleScope scope; | |
if (!args.IsConstructCall()) { | |
return ThrowException(Exception::TypeError( | |
String::New("Use the new operator to create instances of this object.")) | |
); | |
} | |
if (args.Length() < 1) { | |
return ThrowException(Exception::TypeError( | |
String::New("First argument must be a number"))); | |
} | |
// Creates a new instance object of this type and wraps it. | |
MyObject* obj = new MyObject(args[0]->ToInteger()->Value()); | |
obj->Wrap(args.This()); | |
return args.This(); | |
} | |
static v8::Handle<v8::Value> Value(const v8::Arguments& args) { | |
HandleScope scope; | |
// Retrieves the pointer to the wrapped object instance. | |
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This()); | |
return scope.Close(Integer::New(obj->value_)); | |
} | |
// Your own object variables here | |
int value_; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment