0

Background: I have to update old V8 embedded code (to version 5.8). Newer versions introduce a type MaybeLocal<> to make sure, that you process empty Local<> handles.

To process empty handles i wrote a short convert method which throws an exception if the handle is empty:

template<typename C>
inline v8::Local<C>
toLocalHandle(const v8::MaybeLocal<C> &handle)
{
    if (handle.IsEmpty())
        throw JSBaseClass::TerminateEx();
    return handle.ToLocalChecked();
}

Now i have to catch that exception in any of my accessor and function callbacks. I have already created macros to create wrapper for a callback functions:

#define CREATE_V8_FUNCTION_CALLBACK_WRAPPER(fktName)     \
    static void wrap_##fktName(                          \
        const v8::FunctionCallbackInfo<v8::Value> &info) \
    {                                                    \
        try {                                            \
            fktName(info);                               \
        }                                                \
        catch (JSBaseClass::TerminateEx &ex)             \
        {                                                \
            return;                                      \
        }                                                \
    };                                                   \

#define V8_FUNCTION_WRAPPER(fktName) wrap_ ## fktName

now i used this to declare wrapper for my functions callbacks:

static void
myFunc(const v8::FunctionCallbackInfo<v8::Value> &info);

CREATE_V8_FUNCTION_CALLBACK_WRAPPER(myFunc)

and use the other macro to bind the callback wrapper to a template:

Local<ObjectTemplate> tmpl = ...
...
tmpl->Set(utf8_to_string("myFkt"),
          FunctionTemplate::New(isolate, V8_FUNCTION_WRAPPER(myFkt)));

Now my question: is there a nice way to do that without macros in C++ <= 2014?

  • If I understand correctly, then you can decorate your callbacks using something like this: http://stackoverflow.com/questions/30679445/python-like-c-decorators – Kinan Al Sarmini May 19 '17 at 20:08
  • It will not work, because the wrapper function is bound to an object. The V8 Set-Method needs a `void (*)(const v8::FunctionCallbackInfo &info);` as parameter. – Marcus Hampel May 22 '17 at 08:40

0 Answers0