Skip to content

Commit a22d82a

Browse files
committedSep 1, 2018
use std::function for wrapped class ctor and dtor lambdas
See issue #83
1 parent b7cbf9b commit a22d82a

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed
 

‎test/test_class.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,21 @@ void test_class_()
9595
using x_prop_get = int (X::*)() const;
9696
using x_prop_set = void (X::*)(int);
9797

98-
v8pp::class_<X, Traits> X_class(isolate);
98+
int extra_ctor_context = 1;
99+
auto const X_ctor = [extra_ctor_context](v8::FunctionCallbackInfo<v8::Value> const& args)
100+
{
101+
return create_X<Traits>(args);
102+
};
103+
Z extra_dtor_context;
104+
auto const X_dtor = [extra_dtor_context](v8::Isolate* isolate, typename Traits::template object_pointer_type<X> const& obj)
105+
{
106+
Traits::destroy(obj);
107+
};
108+
109+
v8pp::class_<X, Traits> X_class(isolate, X_dtor);
99110
X_class
100111
.ctor(&create_X<Traits>)
112+
.template ctor<v8::FunctionCallbackInfo<v8::Value> const&>(X_ctor)
101113
.set_const("konst", 99)
102114
.set("var", &X::var)
103115
.set("rprop", v8pp::property(&X::get))

‎v8pp/class.hpp

+21-19
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ class object_registry final : public class_info
4848
using const_pointer_type = typename Traits::const_pointer_type;
4949
using object_id = typename Traits::object_id;
5050

51-
using ctor_function = pointer_type (*)(v8::FunctionCallbackInfo<v8::Value> const& args);
52-
using dtor_function = void (*)(v8::Isolate*, pointer_type const&);
51+
using ctor_function = std::function<pointer_type (v8::FunctionCallbackInfo<v8::Value> const& args)>;
52+
using dtor_function = std::function<void (v8::Isolate*, pointer_type const&)>;
5353
using cast_function = pointer_type (*)(pointer_type const&);
5454

55-
object_registry(v8::Isolate* isolate, type_info const& type, dtor_function dtor)
55+
object_registry(v8::Isolate* isolate, type_info const& type, dtor_function&& dtor)
5656
: class_info(type, type_id<Traits>())
5757
, isolate_(isolate)
58-
, ctor_(nullptr) // no wrapped class constructor available by default
59-
, dtor_(dtor)
58+
, ctor_() // no wrapped class constructor available by default
59+
, dtor_(std::move(dtor))
6060
{
6161
v8::HandleScope scope(isolate_);
6262

@@ -121,7 +121,7 @@ class object_registry final : public class_info
121121
return to_local(isolate_, js_func_);
122122
}
123123

124-
void set_ctor(ctor_function ctor) { ctor_ = ctor; }
124+
void set_ctor(ctor_function&& ctor) { ctor_ = std::move(ctor); }
125125

126126
void add_base(object_registry& info, cast_function cast)
127127
{
@@ -344,7 +344,7 @@ class classes
344344
public:
345345
template<typename Traits>
346346
static object_registry<Traits>& add(v8::Isolate* isolate, type_info const& type,
347-
typename object_registry<Traits>::dtor_function dtor)
347+
typename object_registry<Traits>::dtor_function&& dtor)
348348
{
349349
classes* info = instance(operation::add, isolate);
350350
auto it = info->find(type);
@@ -354,7 +354,7 @@ class classes
354354
throw std::runtime_error((*it)->class_name()
355355
+ " is already exist in isolate " + pointer_str(isolate));
356356
}
357-
info->classes_.emplace_back(new object_registry<Traits>(isolate, type, dtor));
357+
info->classes_.emplace_back(new object_registry<Traits>(isolate, type, std::move(dtor)));
358358
return *static_cast<object_registry<Traits>*>(info->classes_.back().get());
359359
}
360360

@@ -450,25 +450,27 @@ class class_
450450
}
451451
};
452452

453-
static void factory_destroy(v8::Isolate* isolate, pointer_type const& obj)
454-
{
455-
factory<T, Traits>::destroy(isolate, Traits::template static_pointer_cast<T>(obj));
456-
}
457-
458-
using ctor_function = object_pointer_type (*)(v8::FunctionCallbackInfo<v8::Value> const& args);
459-
using dtor_function = void (*)(v8::Isolate* isolate, pointer_type const& obj);
453+
using ctor_function = std::function<object_pointer_type (v8::FunctionCallbackInfo<v8::Value> const& args)>;
454+
using dtor_function = std::function<void (v8::Isolate* isolate, object_pointer_type const& obj)>;
460455

461456
public:
462-
explicit class_(v8::Isolate* isolate, dtor_function destroy = factory_destroy)
463-
: class_info_(detail::classes::add<Traits>(isolate, detail::type_id<T>(), destroy))
457+
explicit class_(v8::Isolate* isolate, dtor_function destroy = &factory<T, Traits>::destroy)
458+
: class_info_(detail::classes::add<Traits>(isolate, detail::type_id<T>(),
459+
[destroy = std::move(destroy)](v8::Isolate* isolate, pointer_type const& obj)
460+
{
461+
destroy(isolate, Traits::template static_pointer_cast<T>(obj));
462+
}))
464463
{
465464
}
466465

467466
/// Set class constructor signature
468467
template<typename ...Args, typename Create = factory_create<Args...>>
469-
class_& ctor(ctor_function create = Create::call)
468+
class_& ctor(ctor_function create = &Create::call)
470469
{
471-
class_info_.set_ctor(reinterpret_cast<typename object_registry::ctor_function>(create));
470+
class_info_.set_ctor([create = std::move(create)](v8::FunctionCallbackInfo<v8::Value> const& args)
471+
{
472+
return create(args);
473+
});
472474
return *this;
473475
}
474476

0 commit comments

Comments
 (0)