Skip to content

Commit

Permalink
Merge branch 'atvise-add_set_static_function'
Browse files Browse the repository at this point in the history
  • Loading branch information
pmed committed Dec 29, 2018
2 parents 7eebfdc + 8d59bca commit d2e4d6b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/wrapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ X_class
.set("b", &X::b)
// set const property
.set_const("str", "abc")
// set static property
.set_static("A", 1)
// set static const property
.set_static("B", 42, true)
;

// Bind class Y
Expand Down Expand Up @@ -155,6 +159,16 @@ module.set("Y", Y_class);
var x = new module.X(true);
assert(x.b == true);
assert(x.str == "abc");
// static property
assert(x.A === undefined);
assert(module.X.A === 1);
module.X.A = 2;
assert(module.X.A === 2);
// static const property
assert(x.B === undefined);
assert(module.X.B === 42);
module.X.B = 123;
assert(module.X.B === 42);
var y = new module.Y(10, false);
assert(y.b == false);
Expand Down
6 changes: 6 additions & 0 deletions test/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ void test_class_()
.set("static_lambda", [](int x) { return x + 3; })
.set("extern_fun", extern_fun<Traits>)
.set("toJSON", &X::to_json)
.set_static("my_static_const_var", 42, true)
.set_static("my_static_var", 1)
;

v8pp::class_<Y, Traits> Y_class(isolate);
Expand Down Expand Up @@ -199,6 +201,10 @@ void test_class_()
check_eq("X::static_lambda(1)", run_script<int>(context, "X.static_lambda(1)"), 4);
check_eq("X::extern_fun(5)", run_script<int>(context, "x = new X(); x.extern_fun(5)"), 6);
check_eq("X::extern_fun(6)", run_script<int>(context, "X.extern_fun(6)"), 6);
check_eq("X::my_static_const_var", run_script<int>(context, "X.my_static_const_var"), 42);
check_eq("X::my_static_const_var after assign", run_script<int>(context, "X.my_static_const_var = 123; X.my_static_const_var"), 42);
check_eq("X::my_static_var", run_script<int>(context, "X.my_static_var"), 1);
check_eq("X::my_static_var after assign", run_script<int>(context, "X.my_static_var = 123; X.my_static_var"), 123);

check_eq("JSON.stringify(X)",
run_script<std::string>(context, "JSON.stringify({'obj': new X(10), 'arr': [new X(11), new X(12)] })"),
Expand Down
15 changes: 14 additions & 1 deletion v8pp/class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ class class_

public:
explicit class_(v8::Isolate* isolate, dtor_function destroy = &factory<T, Traits>::destroy)
: class_info_(detail::classes::add<Traits>(isolate, detail::type_id<T>(),
: class_info_(detail::classes::add<Traits>(isolate, detail::type_id<T>(),
[destroy = std::move(destroy)](v8::Isolate* isolate, pointer_type const& obj)
{
destroy(isolate, Traits::template static_pointer_cast<T>(obj));
Expand Down Expand Up @@ -588,6 +588,19 @@ class class_
return *this;
}

/// Set a static value
template<typename Value>
class_& set_static(char const* name, Value const& value, bool readonly = false)
{
v8::HandleScope scope(isolate());

class_info_.js_function_template()->GetFunction(isolate()->GetCurrentContext()).ToLocalChecked()
->DefineOwnProperty(isolate()->GetCurrentContext(),
v8pp::to_v8(isolate(), name), to_v8(isolate(), value),
v8::PropertyAttribute(v8::DontDelete | (readonly ? v8::ReadOnly : 0))).FromJust();
return *this;
}

/// v8::Isolate where the class bindings belongs
v8::Isolate* isolate() { return class_info_.isolate(); }

Expand Down

0 comments on commit d2e4d6b

Please sign in to comment.