本文介绍了在node.js绑定中实现继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++库周围编写Node.js绑定。我可以将C ++库中的关键对象标识为Node.js(即ObjectWrap的派生类)。我也可以看到这些对象之间的继承关系。

I am writing Node.js bindings around a C++ library. I can identify key objects in the C++ library that I can expose as classes to the Node.js (i.e. derivatives of ObjectWrap). I can also see an inheritance relationship between these objects.

如何公开 ClassA ClassB code>作为node.js类( ObjectWrap 的派生物)并操作其原型(在v8 C ++代码中),使 c $ c> ClassB ClassC ClassA

How can I expose ClassA,ClassB,ClassC as node.js classes (derivatives of ObjectWrap) and manipulate their prototypes (in v8 C++ code) so that ClassB and ClassC are derivates of ClassA?

推荐答案

这可以使用 v8 :: FunctionTemplate '继承方法。您可以在解释此问题。这是一个工作示例。

This can be done using v8::FunctionTemplate's Inherit method. It's explained here. Here's a working example.

C ++代码:

#include <v8.h>
#include <node.h>

using namespace node;
using namespace v8;

class BaseClass : ObjectWrap
{
public:
  static Persistent<FunctionTemplate> s_ct;
  static void Init(v8::Handle<Object> target)
  {
    Local<FunctionTemplate> t = FunctionTemplate::New(New);

    s_ct = Persistent<FunctionTemplate>::New(t);
    s_ct->InstanceTemplate()->SetInternalFieldCount(1);
    s_ct->SetClassName(String::NewSymbol("BaseClass"));

    NODE_SET_PROTOTYPE_METHOD(s_ct, "getName", getName);

    target->Set(String::NewSymbol("BaseClass"), s_ct->GetFunction());
  }

  BaseClass(){}
  ~BaseClass(){}

  static v8::Handle<Value> New(const Arguments& args)
  {
    HandleScope scope;
    return args.This();
  }

  static v8::Handle<Value> getName(const Arguments& args)
  {
    HandleScope scope;
    return scope.Close(String::New("Base"));
  }
};

Persistent<FunctionTemplate> BaseClass::s_ct;

class DerivedClass : ObjectWrap
{
public:
  static Persistent<FunctionTemplate> s_ct;
  static void Init(v8::Handle<Object> target)
  {
    Local<FunctionTemplate> t = FunctionTemplate::New(New);

    s_ct = Persistent<FunctionTemplate>::New(t);

    // XXX Inherit from BaseClass
    s_ct->Inherit(BaseClass::s_ct);

    s_ct->InstanceTemplate()->SetInternalFieldCount(1);
    s_ct->SetClassName(String::NewSymbol("DerivedClass"));

    NODE_SET_PROTOTYPE_METHOD(s_ct, "getAge", getAge);

    target->Set(String::NewSymbol("DerivedClass"), s_ct->GetFunction());
  }

  DerivedClass() {}
  ~DerivedClass() {}

  static v8::Handle<Value> New(const Arguments& args) {
    HandleScope scope;
    return args.This();
  }

  static v8::Handle<Value> getAge(const Arguments& args)
  {
    HandleScope scope;
    return scope.Close(Number::New(42));
  }
};

Persistent<FunctionTemplate> DerivedClass::s_ct;

extern "C" {
  static void init (v8::Handle<Object> target)
  {
    BaseClass::Init(target);
    DerivedClass::Init(target);
  }

  NODE_MODULE(mymodule, init);
}

将其放入标准 node-gyp 配置,你可以用下面的javascript测试它

Put it in standard node-gyp configuration and you can test it with following javascript

var mymodule = require('./build/Release/mymodule');
var baseObject = new mymodule.BaseClass();
var dervObject = new mymodule.DerivedClass();

console.log(baseObject.getName());
console.log(dervObject.getName(), dervObject.getAge());

这篇关于在node.js绑定中实现继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:56