I am writing a node.js addon in C ++. I am migrating some class instances using node :: ObjectWrap to associate my own instance with a javascript object. My problem is that the destroyed instance of the instance never starts.
Here is an example:
point.cc
#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); };
test.js
var pointer = require('./build/default/point'); var p = new pointer.Point(1,2); console.log(p.get());
I assume that I need to configure WeakPointerCallback, which deletes the manually selected object if its V8 garbage collector wants it. How am I supposed to do this?
erenon
source share