1

我刚刚开始通过 v8 扩展(按照Node.JS 文档v8 API 文档的说明)编写与 C 库(准确地说是链接语法)的绑定。

我的问题是我收到以下构建错误:

/usr/include/v8.h: In constructor âv8::Handle<T>::Handle(v8::Handle<S>) [with S = v8::FunctionTemplate, T = v8::Value]â:
node.cc:85:68:   instantiated from here
/usr/include/v8.h:195:5: error: cannot convert âv8::FunctionTemplate*â to âv8::Value* volatileâ in assignment

...尝试构建以下代码时:

#include <v8.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include "link-includes.h"

using namespace v8;

Dictionary dict;
Parse_Options opts;

static Handle<Value> v8parse(const Arguments& args)
{
    /* snip */
}

extern "C" void init (Handle<Object> target)
{
    HandleScope scope;
    target->Set(String::New("parse"), FunctionTemplate::New(v8parse));

    setlocale(LC_ALL, "");
    opts = parse_options_create();
    dict = dictionary_create_lang("en");
}

我觉得好像我已经遵循了上述链接上的说明,并遵循了它们链接到的示例的模式,但是我得到了上述错误。不是最敏锐的 C++ 编码器,所以很可能这个错误正盯着我看。唉,我不知所措。

4

2 回答 2

2

您可能需要在函数模板上调用 GetFunction()。我只涉足了节点的 c++ 扩展,但我见过的大多数代码示例看起来都是这样的:

static Persistent<FunctionTemplate> s_ct;

static void Init(Handle<Object> target)
{
  HandleScope scope;

  Local<FunctionTemplate> t = FunctionTemplate::New(New);

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

  NODE_SET_PROTOTYPE_METHOD(s_ct, "someMethod", MyClass::SomeMethod);

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

链接语法的节点扩展会很酷:)

于 2011-08-11T12:40:47.607 回答
2

Set方法需要一个Handle<Value>,但您传递的是一个无法自动转换自身的 FunctionTemplate。您需要在模板上使用 GetFunction 来获取它的值句柄。

target->Set(String::New("parse"), FunctionTemplate::New(v8parse)->GetFunction());

但是,有一个宏,所以我建议这样做:

NODE_SET_METHOD(target, "parse", v8parse);

当我在玩这些东西时,我发现这篇博文/示例代码很有用。 https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/

I'm not 100% sure since it isn't mentioned in the nodejs docs you posted, but I think you'll also need an extern "C" { NODE_MODULE(yourmodulefilename, init); } at the bottom of your file to let node know that this is a module for loading.

于 2011-08-11T12:51:21.407 回答