ftk / quickjspp

QuickJS C++ wrapper
414 stars 85 forks source link

wrap is not a member of qjs::js_traits<MyCustomClass,void> #55

Open projectitis opened 2 years ago

projectitis commented 2 years ago

Hi FTK,

I understand that this error is indicating something else is wrong somewhere.

I seem to get errors like this for any getters that return a DisplayObject*, so I'm guessing my error is in my qjs code for DisplayObject somewhere. When I comment out the lines that appear to be the issue, similar build errors occur just with other parts of the code.

Any idea why these errors would usually occur? What sort of problems should I be looking for in my code?

My code looks something like this (these are just snippets of what is in Script.cpp):

// Dispatcher
module.class_<Dispatcher>( "Dispatcher" )
    .constructor<>()
    .fun<&Dispatcher::dispatch>( "dispatch" )
    .fun<&Dispatcher::listen>( "listen" )
    .fun<&Dispatcher::remove>( "remove" )
    .fun<&Dispatcher::clear>( "clear" );

// Display object
typedef HitArea*( DisplayObject::* DisplayObject_getHitAreaF )( void );
typedef void( DisplayObject::* DisplayObject_setHitAreaF )( HitArea* );
module.class_<DisplayObject>("DisplayObject")
    .base<Dispatcher>()
    .constructor<>()
    .property<&DisplayObject::parent>( "parent" )
    .property<&DisplayObject::numChildren>( "numChildren" )
    .property<&DisplayObject::first>( "first" )
    .property<&DisplayObject::last>( "last" )
    .property<&DisplayObject::first>( "next" )
    .property<&DisplayObject::last>( "prev" )
    .property<(DisplayObject_getHitAreaF)&DisplayObject::hitArea, (DisplayObject_setHitAreaF)&DisplayObject::hitArea>( "hitArea" )
    .fun<&DisplayObject::x>( "x" )
    .fun<&DisplayObject::y>( "y" )
    .fun<&DisplayObject::width>( "width" )
    .fun<&DisplayObject::height>( "height" )
    .fun<&DisplayObject::scaleX>( "scaleX" )
    .fun<&DisplayObject::scaleY>( "scaleY" )
    .fun<&DisplayObject::rotation>( "rotation" )
    .fun<&DisplayObject::originX>( "originX" )
    .fun<&DisplayObject::originY>( "originY" )
    .fun<&DisplayObject::visible>( "visible" )
    .fun<&DisplayObject::alpha>( "alpha" )
    .fun<&DisplayObject::mouse>( "mouse" )
    .fun<&DisplayObject::addChild>( "addChild" )
    .fun<&DisplayObject::addChildAt>( "addChildAt" )
    .fun<&DisplayObject::removeChild>( "removeChild" )
    .fun<&DisplayObject::removeChildAt>( "removeChildAt" )
    .fun<&DisplayObject::removeAllChildren>( "removeAllChildren" )
    .fun<&DisplayObject::childAt>( "childAt" )
    .fun<&DisplayObject::indexOfChild>( "indexOfChild" )
    .fun<&DisplayObject::addBefore>( "addBefore" )
    .fun<&DisplayObject::addAfter>( "addAfter" )
    .fun<&DisplayObject::remove>( "remove" );

// Player
typedef int( Player::* Player_getIntF )( );
typedef void( Player::* Player_setIntF )( int );
typedef colorARGB( Player::* Player_getColorARGBF )( );
typedef void( Player::* Player_setColorARGBF )(colorARGB);
module.class_<Player>("Player")
    .constructor<int, int, string, bool>()
    .property<&Player::stage>("stage") // Get
    .property<(Player_getIntF)&Player::displayMode, (Player_setIntF)&Player::displayMode>("displayMode")
    .property<(Player_getIntF)&Player::scaleMode, (Player_setIntF)&Player::scaleMode>("scaleMode")
    .property<(Player_getIntF)&Player::alignMode, (Player_setIntF)&Player::alignMode>("alignMode")
    .property<(Player_getColorARGBF)&Player::backgroundColor, (Player_setColorARGBF)&Player::backgroundColor>("backgroundColor")
    .property<(Player_getColorARGBF)&Player::letterboxColor, (Player_setColorARGBF)&Player::letterboxColor>("letterboxColor")
    .fun<&Player::run>("run");

And the errors I get are like this:

1>Script.cpp
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(622,48): error C2039: 'wrap': is not a member of 'qjs::js_traits<derive::display::DisplayObject *,void>'
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(622): message : see declaration of 'qjs::js_traits<derive::display::DisplayObject *,void>'
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(703): message : see reference to function template instantiation 'JSValue qjs::detail::wrap_this_call<R,qjs::shared_ptr<T>,,derive::display::DisplayObject*(__cdecl derive::Player::* )(void)>(JSContext *,Callable &&,JSValue,int,JSValue *) noexcept' being compiled
1>        with
1>        [
1>            R=derive::display::DisplayObject *,
1>            T=derive::Player,
1>            Callable=derive::display::DisplayObject *(__cdecl derive::Player::* )(void)
1>        ]
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(703): message : while compiling class template member function 'JSValue qjs::js_traits<fgetter,void>::wrap(JSContext *,qjs::fwrapper<derive::display::DisplayObject *derive::Player::stage(void),true>) noexcept'
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(1130): message : see reference to function template instantiation 'JSValue qjs::js_traits<fgetter,void>::wrap(JSContext *,qjs::fwrapper<derive::display::DisplayObject *derive::Player::stage(void),true>) noexcept' being compiled
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(1131): message : see reference to class template instantiation 'qjs::js_traits<fgetter,void>' being compiled
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(1701): message : see reference to function template instantiation 'qjs::Value &qjs::Value::add_getter<derive::display::DisplayObject *derive::Player::stage(void)>(const char *)' being compiled
1>C:\Projects\derive\derive\src\Script.cpp(368): message : see reference to function template instantiation 'qjs::Context::Module::class_registrar<derive::Player> &qjs::Context::Module::class_registrar<derive::Player>::property<derive::display::DisplayObject *derive::Player::stage(void),nullptr>(const char *)' being compiled
1>C:\Projects\derive\derive\src\Script.cpp(374): message : see reference to function template instantiation 'qjs::Context::Module::class_registrar<derive::Player> &qjs::Context::Module::class_registrar<derive::Player>::property<derive::display::DisplayObject *derive::Player::stage(void),nullptr>(const char *)' being compiled
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(611,1): error C3861: 'wrap': identifier not found
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(622,48): error C2039: 'wrap': is not a member of 'qjs::js_traits<derive::geom::HitArea *,void>'
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(622): message : see declaration of 'qjs::js_traits<derive::geom::HitArea *,void>'
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(703): message : see reference to function template instantiation 'JSValue qjs::detail::wrap_this_call<R,qjs::shared_ptr<T>,,DisplayObject_getHitAreaF>(JSContext *,Callable &&,JSValue,int,JSValue *) noexcept' being compiled
1>        with
1>        [
1>            R=derive::geom::HitArea *,
1>            T=derive::display::DisplayObject,
1>            Callable=DisplayObject_getHitAreaF
1>        ]
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(703): message : while compiling class template member function 'JSValue qjs::js_traits<fgetter,void>::wrap(JSContext *,qjs::fwrapper<{&derive::display::DisplayObject::[thunk]: __cdecl derive::display::DisplayObject::`vcall'{216,{flat}}' }',0},true>) noexcept'
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(1113): message : see reference to function template instantiation 'JSValue qjs::js_traits<fgetter,void>::wrap(JSContext *,qjs::fwrapper<{&derive::display::DisplayObject::[thunk]: __cdecl derive::display::DisplayObject::`vcall'{216,{flat}}' }',0},true>) noexcept' being compiled
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(1114): message : see reference to class template instantiation 'qjs::js_traits<fgetter,void>' being compiled
1>C:\Projects\derive\dependencies\quickjspp\quickjspp.hpp(1703): message : see reference to function template instantiation 'qjs::Value &qjs::Value::add_getter_setter<{&derive::display::DisplayObject::[thunk]: __cdecl derive::display::DisplayObject::`vcall'{216,{flat}}' }',0},{&derive::display::DisplayObject::[thunk]: __cdecl derive::display::DisplayObject::`vcall'{208,{flat}}' }',0}>(const char *)' being compiled
1>C:\Projects\derive\derive\src\Script.cpp(310): message : see reference to function template instantiation 'qjs::Context::Module::class_registrar<derive::display::DisplayObject> &qjs::Context::Module::class_registrar<derive::display::DisplayObject>::property<{&derive::display::DisplayObject::[thunk]: __cdecl derive::display::DisplayObject::`vcall'{216,{flat}}' }',0},{&derive::display::DisplayObject::[thunk]: __cdecl derive::display::DisplayObject::`vcall'{208,{flat}}' }',0}>(const char *)' being compiled
1>C:\Projects\derive\derive\src\Script.cpp(332): message : see reference to function template instantiation 'qjs::Context::Module::class_registrar<derive::display::DisplayObject> &qjs::Context::Module::class_registrar<derive::display::DisplayObject>::property<{&derive::display::DisplayObject::[thunk]: __cdecl derive::display::DisplayObject::`vcall'{216,{flat}}' }',0},{&derive::display::DisplayObject::[thunk]: __cdecl derive::display::DisplayObject::`vcall'{208,{flat}}' }',0}>(const char *)' being compiled
1>main.cpp
cykoder commented 2 years ago

which branch are you using? v2/sharedptr or v1? either way, some things ive learned:

expose base classes first (for inheritance and if your class returns another class). for example: class a returns class b ptr, but class b was defined after class a. in this case i seem to get a compile error. in the v2 branch, you have to make sure your class ptr to expose has the base class:

class MyClass : public qjs::enable_shared_from_this<MyClass>

however, I guess this is incomplete (as theres a todo comment on this class) and I have to do a hack to get my existing native ptr exposed to quickjs:

  this->nativeObject.shared_this.ctx = qjsCtx.ctx;
  this->nativeObject.shared_this.reset<NativeObjectClass>(&this->nativeObject);

then it all works. without the above hack, the code compiles but acecssing var.nativeObject returns null. I guess this is because in my case I created the native object outside of the script context. I haven't seen any example of how to do this properly in the code for v2. Previously in v1 itd just work aslong as the class was defined.

@ftk any thoughts here would be appreciated

projectitis commented 1 year ago

Coming back to this after some time. ok, more than some time!

I'm not able to make any/all of my classes inherit from qjs::enable_shared_from_this<T>. This is because the classes are not exclusive to quickjs. They are used natively (c++) in some projects, and are exposed to quickjs in others.

Is there a way around this?

luodaoyi commented 1 year ago

@projectitis I'm like you, is there any solution?

projectitis commented 1 year ago

@luodaoyi I haven't found one yet. Have paused this project for now :(

luodaoyi commented 1 year ago

@projectitis Me too , I have to change script solution to others :(

I think it is unsolvable. Because the js engine needs to manage the life cycle of objects.