pmed / v8pp

Bind C++ functions and classes into V8 JavaScript engine
http://pmed.github.io/v8pp/
Other
890 stars 120 forks source link

How to pass C++-only arguments (loggers, allocators,...) to V8 created objects #83

Closed VitaminCpp closed 5 years ago

VitaminCpp commented 5 years ago

I need a way to pass a logger and an allocator to each V8 created object like this:

MyLogger logger;
using MyClassWrapper = v8pp::class_<MyClass>;
MyClassWrapper uaNodeWrapper(isolate);
uaNodeWrapper
  .ctor<v8::FunctionCallbackInfo<v8::Value> const& args>([&](v8::FunctionCallbackInfo<v8::Value> const& args) -> MyClass* {
    return new MyClass(myLogger, args);
  })
  .set("method", &MyClass::myMethod)
  .set("value", v8pp::property(&MyClass::value, &MyClass::setValue));

But this doesn't work, because this factory method is a function pointer instead a function object. So I want to inject C++-only references. Is there any way I can do this?

pmed commented 5 years ago

Hi,

Yes, it seems the user-defined ctor function has to be std::function instead of pointer, to support lambdas.

There is a v8pp::factory template that could be specialized for the particular wrapped class. Maybe the factory specialization could work in your case as a temp.workaround.

чт, 23 авг. 2018 г. в 12:58, VitaminCpp notifications@github.com:

I need a way to pass a logger and an allocator to each V8 created object like this:

MyLogger logger; using MyClassWrapper = v8pp::class_; MyClassWrapper uaNodeWrapper(isolate); uaNodeWrapper .ctor<v8::FunctionCallbackInfo const& args>([&](v8::FunctionCallbackInfo const& args) -> MyClass* { return new MyClass(myLogger, args); }) .set("method", &MyClass::myMethod) .set("value", v8pp::property(&MyClass::value, &MyClass::setValue));

But this doesn't work, because this factory method is a function pointer instead a function object. So I want to inject C++-only references. Is there any way I can do this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pmed/v8pp/issues/83, or mute the thread https://github.com/notifications/unsubscribe-auth/ABEgUtnbDt9p60VkvNINqjyzdU7pRXcmks5uTny9gaJpZM4WJNtH .

-- Sincerely, Pavel

pmed commented 5 years ago

Hi @VitaminCpp

I've fixed this issue in master branch.

VitaminCpp commented 5 years ago

Great!!! Thanks a lot @pmed!