如何在NodeJS插件中创建一个类返回另一个插件?

时间:2015-01-20 18:43:25

标签: node.js v8

通过这个类,我可以在另一个插件的源代码中创建它的实例。但是如何包装对象并返回它?

class foo : public ObjectWrap {
public:
  int mydata; // intentionally not exposed in node
  foo(){  mydata = 4; }
  static Persistent<Function> constructor;
  static void Init(){
    Local<FunctionTemplate> s = FunctionTemplate::New(New);
      s->SetClassName(String::NewSymbol("foo"));
      s->InstanceTemplate()->SetInternalFieldCount(1); 
      constructor = Persistent<Function>::New(s->GetFunction());
    }

    static Handle<Value> NewInstance() {
        HandleScope scope;
        Local<Object> in = constructor->NewInstance();
        return scope.Close(in); 
    }

    static Handle<Value> New(const Arguments& args) {
        HandleScope scope;
        foo* f= new foo();
        f->Wrap(args.This());
        return scope.Close( args.This() );
    }
};

我想做的是,从另一个类方法,创建一个新实例,然后返回它。

Handle<Value> bar_method( const Arguments& args ) {
    HandleScope scope;

    // make new instace of foo
    foo * f = new foo();
    f->mydata = 5;

    Handle<Object> obj = Object::New();
    obj->Set( String::New("property"), f??? );
    return scope.Close(obj);
}

0 个答案:

没有答案
相关问题