1

Is there a way to convert from 'v8::Local' to 'void (int)'. I have a v8 function and I'm trying to pass it into signal on windows.

  v8::Local<v8::Function> function;
  function = v8::Local<v8::Function>::Cast(args[1]);
  (void)signal(SIGINT, function);

I'm unsure of the best way to work on this. The definition for signal is:

    _ACRTIMP _crt_signal_t __cdecl signal(_In_ int _Signal, _In_opt_ _crt_signal_t _Function);

But it seems _In_opt_ _crt_signal_t is equivalent to void(int)

Edit: I've attempted to do what jmrk suggested. I understand how to make it v8 persistent, but I'm confused on how to wrap it. I've tried using the below wrapper, but it doesn't work.

struct c_api_interface { void (*func_js)(v8::Persistent<v8::Function>);};

  template<typename Fn, Fn fn, typename... Args>
  typename std::result_of<Fn(Args...)>::type
    wrapper(Args... args) {
    return fn(std::forward<Args>(args)...);
  }
#define WRAPIT(FUNC) wrapper<decltype(&FUNC), &FUNC>



  v8::Local<v8::Function> function;
  function = v8::Local<v8::Function>::Cast(args[1]);
  v8::Persistent<v8::Function> value(isolate, function );
  c_api_interface my_interface;
  my_interface.func_js = WRAPIT(value);

  (void)signal(SIGINT, my_interface.func_js);


Alex
  • 131
  • 2
  • 13

1 Answers1

1

This is not going to work directly. signal needs a C++ function, whereas v8::Local<...> is a C++ data object referring internally to a V8 heap object; the latter in this case being a JavaScript function object, which in addition to having properties and a prototype and so on also happens to be callable, if you know how to call it, which is certainly not the same way as how a C++ function would be called.

So the best (only?) way to go about this would probably be to define a wrapper function in C++. Store the v8::Function in a v8::Persistent, then you can get to it. (All this is assuming that your overall goal is to execute a JavaScript function when the process receives a certain signal?)

jmrk
  • 34,271
  • 7
  • 59
  • 74