Skip to content

Commit

Permalink
improve class_::extend()
Browse files Browse the repository at this point in the history
Removed `class` suffix since the function extende is in the `v8pp::class_` name scope already.
Added `class_::extend()` usage example to the documentation and test case.

See pull-request #136
  • Loading branch information
pmed committed Aug 15, 2020
1 parent 7b14cc3 commit b8bca11
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
6 changes: 5 additions & 1 deletion docs/wrapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ read-write property to `v8pp::class_`:
struct X
{
bool b;
X(bool b) : b(b) {}
int c;
X(bool b) : b(b), c(0) {}
};

struct Y : X
Expand Down Expand Up @@ -149,6 +150,9 @@ Y_class
.set("ext_fun", &ext_fun)
;

// Extend existing X bindings
v8pp::class_<X>::extend(isolate).set("c", &X::c);

// set class into the module template
module.set("X", X_class);
module.set("Y", Y_class);
Expand Down
10 changes: 5 additions & 5 deletions test/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ void test_class_()
.template ctor<int>()
.set("useX", &Y::useX)
.set("useX_ptr", &Y::useX_ptr<Traits>)
.set("toJSON", [](const v8::FunctionCallbackInfo<v8::Value>& args)
;

auto Y_class_find = v8pp::class_<Y, Traits>::extend(isolate);
Y_class_find.set("toJSON", [](const v8::FunctionCallbackInfo<v8::Value>& args)
{
bool const with_functions = true;
args.GetReturnValue().Set(v8pp::json_object(args.GetIsolate(), args.This(), with_functions));
})
;

auto Y_class_find = v8pp::class_<Y, Traits>::extend_class(isolate);
});

check_ex<std::runtime_error>("already wrapped class X", [isolate]()
{
Expand Down
14 changes: 10 additions & 4 deletions v8pp/class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ class class_
using ctor_function = std::function<object_pointer_type (v8::FunctionCallbackInfo<v8::Value> const& args)>;
using dtor_function = std::function<void (v8::Isolate* isolate, object_pointer_type const& obj)>;

explicit class_(v8::Isolate* isolate, detail::type_info const& existing)
: class_info_(detail::classes::find<Traits>(isolate, existing))
{
}

public:
explicit class_(v8::Isolate* isolate, dtor_function destroy = &factory<T, Traits>::destroy)
: class_info_(detail::classes::add<Traits>(isolate, detail::type_id<T>(),
Expand All @@ -191,7 +196,11 @@ class class_
{
}

static class_ extend_class(v8::Isolate* isolate) { return class_(isolate, detail::type_id<T>());}
/// Find existing class_ to extend bindings
static class_ extend(v8::Isolate* isolate)
{
return class_(isolate, detail::type_id<T>());
}

/// Set class constructor signature
template<typename ...Args, typename Create = factory_create<Args...>>
Expand Down Expand Up @@ -441,9 +450,6 @@ class class_
}

private:
explicit class_(v8::Isolate *isolate, detail::type_info const &ty) :
class_info_(detail::classes::find<Traits>(isolate, ty)) { }

template<typename Attribute>
static void member_get(v8::Local<v8::String>,
v8::PropertyCallbackInfo<v8::Value> const& info)
Expand Down

0 comments on commit b8bca11

Please sign in to comment.