Skip to content

Commit

Permalink
add missing js_traits<T*>::wrap (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
ftk committed May 9, 2022
1 parent e747cd8 commit 926a725
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion quickjspp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,11 @@ struct js_traits<qjs::shared_ptr<T>>
template <class T>
struct js_traits<T *>
{
//static JSValue wrap(JSContext * ctx, T * ptr);
static JSValue wrap(JSContext * ctx, T * ptr) noexcept
{
static_assert(std::is_base_of_v<enable_shared_from_this<T>, T>, "T should be inherited from qjs::enable_shared_from_this<T> to enable T* conversions");
return js_traits<T>::wrap(ctx, *ptr);
}
static T * unwrap(JSContext * ctx, JSValueConst v)
{
if(JS_IsNull(v))
Expand Down
10 changes: 9 additions & 1 deletion test/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#define TYPES bool, int32_t, double, qjs::shared_ptr<test>, const qjs::shared_ptr<test>&, std::string, const std::string&

class base_test
class base_test : public qjs::enable_shared_from_this<base_test>
{
public:
std::vector<std::vector<int>> base_field;
Expand All @@ -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
Expand Down Expand Up @@ -79,6 +83,8 @@ void qjs_glue(qjs::Context::Module& m) {
.fun<&::test::d>("d") // double
.fun<&::test::spt>("spt") // ::std::shared_ptr<test>
.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>()
Expand Down Expand Up @@ -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\")");
Expand Down

0 comments on commit 926a725

Please sign in to comment.