1

I am writing a NodeJS addon where I use a C library that lets you register a callback at certain events. When the callback is fired I want to call a NodeJS callback function. The problem is that in my C callback function I get a segmentation fault when trying to do anything V8 related, like creating a HandleScope.

In test.js:

...

myaddon.register(function(data) {
  console.log("data: " + JSON.stringify(data));
});

...

In test.c:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <node.h>
#include <v8.h>

using namespace v8;

void WINAPI myEvent(int num, void * context) {
  HandleScope scope; // Segmentation fault here!

  Local<Function> * cb = (Local<Function>*)(context);

  Local<Object> obj = Object::New();
  obj->Set(String::NewSymbol("id"), Number::New(num));

  const int argc = 1;
  Local<Value> argv[argc] = { obj };
  (*cb)->Call(Context::GetCurrent()->Global(), argc, argv);

  sleep(1);
}

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

    Local<Function> cb = Local<Function>::Cast(args[0]);

    int callbackId  = registerEvent((Event)&myEvent, &cb );
    printf("callback id: %i\n", callbackId);

    init();

    return scope.Close(Integer::New(callbackId));
}

void init(Handle<Object> exports) {
  exports->Set(String::NewSymbol("register"),
      FunctionTemplate::New(RegisterEvent)->GetFunction());
}

NODE_MODULE(test, init)

EDIT: Updated with real code.

EDIT: I just changed the title of this issue since the problem is probably that my callback function can't access the V8 Context. Since I get a segmentation fault when creating HandleScope instance I can't see what else it might be. In addition to this question I AM trying to find the answer in the V8 documentation, but it is huge and I don't have that much time to test and investigate.

Andreas Selenwall
  • 5,705
  • 11
  • 45
  • 58

2 Answers2

1

Your handler function myEvent() must be called in V8 thread. If not, you have to post the event notification into the V8 thread:

https://stackoverflow.com/a/15701160/1355844

https://stackoverflow.com/a/22946062/1355844

Community
  • 1
  • 1
pmed
  • 1,536
  • 8
  • 13
  • Your answer together with this post: http://stackoverflow.com/questions/13826803/calling-javascript-function-from-a-c-callback-in-v8 solved my issue. Now it works great. Thanks. – Andreas Selenwall Jan 26 '15 at 13:10
0

It appears that you might have forgotten to create a HandleScope for your variable. This should work for you.

void callbackFunc() {
  HandleScope scope;
  Local<Object> obj = Object::New();
}
E_net4
  • 27,810
  • 13
  • 101
  • 139
  • Tried to add HandleScope at the beginning of my callback function, and now I get the SegmentationFault at HandleScope instead. So I think we are getting closer :) Might this be a V8 thing, since the callback is executing whenever depending on when the C library (3rd party) gets an event do I have to rely on V8 features only or might be that V8 is not declared in that scope for some reason? – Andreas Selenwall Jan 20 '15 at 13:58