Skip to content

Instantly share code, notes, and snippets.

@erenon
Created July 14, 2011 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erenon/1082040 to your computer and use it in GitHub Desktop.
Save erenon/1082040 to your computer and use it in GitHub Desktop.
Why my constructor doesn't run?
#include <node.h>
#include <v8.h>
#include <iostream>
using namespace v8;
using namespace node;
class Point
:ObjectWrap
{
protected:
int x;
int y;
public:
Point(int x, int y) :x(x), y(y) {
std::cout << "point constructs" << std::endl;
}
~Point() {
std::cout << "point destructs" << std::endl;
}
static Handle<Value> New(const Arguments &args){
HandleScope scope;
// arg check is omitted for brevity
Point *point = new Point(args[0]->Int32Value(), args[1]->Int32Value());
point->Wrap(args.This());
return scope.Close(args.This());
}
static void Initialize(Handle<Object> target){
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(t, "get", Point::get);
target->Set(String::NewSymbol("Point"), t->GetFunction());
}
static Handle<Value> get(const Arguments &args){
HandleScope scope;
Point *p = ObjectWrap::Unwrap<Point>(args.This());
Local<Object> result = Object::New();
result->Set(v8::String::New("x"), v8::Integer::New(p->x));
result->Set(v8::String::New("y"), v8::Integer::New(p->y));
return scope.Close(result);
}
};
extern "C" void init(Handle<Object> target) {
HandleScope scope;
Point::Initialize(target);
};
var pointer = require('./build/default/point');
var p = new pointer.Point(1,2);
console.log(p.get());
import Options
srcdir = '.'
blddir = 'build'
VERSION = '0.0.1'
def set_options(opt):
opt.tool_options('compiler_cxx')
def configure(conf):
conf.check_tool('compiler_cxx')
conf.check_tool('node_addon')
def build(bld):
obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
obj.cxxflags = ["-g"]
obj.target = 'point'
obj.source = 'point.cc'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment