Question

I'm just starting out with writing a binding to a C library (link-grammar, to be precise) via a v8 extension (following instructions on Node.JS docs and v8 API docs).

My problem is that I am getting the following build error:

/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

...when trying to build the following code:

#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");
}

I feel as if I've followed the instructions on the above links, and followed the pattern of the examples they link to, however I get the aforementioned error. Not the sharpest C++ coder out there, so it's very possible the error is staring me in the face. Alas, I'm at a loss.

Was it helpful?

Solution

The Set method expects a Handle<Value>, but you are passing a FunctionTemplate, which can't convert itself automatically. You need to be use GetFunction on the template to get a value handle for it.

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

However, there is a macro for this, so I would recommend doing this:

NODE_SET_METHOD(target, "parse", v8parse);

I found this blog post/sample code useful when I was playing with this stuff. 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.

OTHER TIPS

You probably need to call GetFunction() on your function template. I've only dabbled with c++ extensions to node, but seems like most of the code examples I've seen look something like this:

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());
}

A node extension for link-grammar will be cool :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top