diff --git a/quickjspp.hpp b/quickjspp.hpp index a320ad8ae..e0dc3b5f7 100644 --- a/quickjspp.hpp +++ b/quickjspp.hpp @@ -1455,7 +1455,11 @@ struct js_traits> template struct js_traits { - //static JSValue wrap(JSContext * ctx, T * ptr); + static JSValue wrap(JSContext * ctx, T * ptr) noexcept + { + static_assert(std::is_base_of_v, T>, "T should be inherited from qjs::enable_shared_from_this to enable T* conversions"); + return js_traits::wrap(ctx, *ptr); + } static T * unwrap(JSContext * ctx, JSValueConst v) { if(JS_IsNull(v)) diff --git a/test/class.cpp b/test/class.cpp index 173d47107..526163532 100644 --- a/test/class.cpp +++ b/test/class.cpp @@ -5,7 +5,7 @@ #define TYPES bool, int32_t, double, qjs::shared_ptr, const qjs::shared_ptr&, std::string, const std::string& -class base_test +class base_test : public qjs::enable_shared_from_this { public: std::vector> base_field; @@ -15,6 +15,10 @@ class base_test std::swap(x, base_field[0][0]); return x; } + + // TODO: make it work in inherited classes + base_test * self() { return this; } + bool check_self(base_test * self) { return(this == self); } }; class test : public base_test @@ -79,6 +83,8 @@ void qjs_glue(qjs::Context::Module& m) { .fun<&::test::d>("d") // double .fun<&::test::spt>("spt") // ::std::shared_ptr .fun<&::test::s>("s") // ::std::string + .fun<&::test::self>("self") + .fun<&::test::check_self>("check_self") .property<&test::get_d, &test::set_d>("property_rw") .property<&test::get_d>("property_ro") .mark<&::test::spt>() @@ -148,6 +154,8 @@ int main() "assert(5 === q.base_method(7));" "assert(7 === q.base_field[0][0]);" "assert(q.spt === q.fspt(t.vb, t.vi, t.vd, t, t, t.vs, \"test\"));" // same objects + "assert(q.check_self(q));" + "assert(!t.check_self(q));" "q.fi(t.vb, t.vi, t.vd, t, t, t.vs, \"test\")"); assert((int)xxx == 18); auto yyy = context.eval("q.fi.bind(t)(t.vb, t.vi, t.vd, t, t, t.vs, \"test\")");