pmed / v8pp

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

allow conversion between v8::Function and std::function<void ()> #48

Closed whoozle closed 7 years ago

whoozle commented 7 years ago

Is there any possibility creating assignable callbacks a la dom, e.g.

class Image {
    std::function<void ()> _onLoad;
    void Load();
    }

    v8pp::class_<Image> imageClass(exports.isolate());
    imageClass.ctor<>();
    imageClass.set("load", &Image::Load);
    imageClass.set("onload", &Image::_onLoad);

    void Image::Load(...) { .... _onLoad(); }

and js code:
    image = new Image()
    image.onload = function() { console.log('boo hoo') } 
    image.load('https://example.com/image.png')

Ideally check for callable member pointer type in convert.hpp.

whoozle commented 7 years ago

is it a duplicate of #18 ? code above is throwing an exception (see below), and I found no reference of std::function from convert.hpp, did I miss something there?

terminate called after throwing an instance of 'std::runtime_error'
  what():  v8pp::class_<std::function<void()>> not found in isolate 0x22705d0
pmed commented 7 years ago

Yes, it's kind of. You can also look at #49