@@ -54,6 +54,15 @@ static X_ptr create_X(v8::FunctionCallbackInfo<v8::Value> const& args)
5454 return x;
5555}
5656
57+ template <typename Traits, typename E_ptr = typename v8pp::class_<E, Traits>::object_pointer_type>
58+ static E_ptr create_E (v8::FunctionCallbackInfo<v8::Value> const & args)
59+ {
60+ E_ptr e (new E);
61+ if (args.Length () == 0 )
62+ v8pp::throw_ex (args.GetIsolate (), " MyException" );
63+ return e;
64+ }
65+
5766struct Y : X
5867{
5968 static int instance_count;
@@ -71,6 +80,8 @@ int Y::instance_count = 0;
7180
7281struct Z {};
7382
83+ struct E {};
84+
7485namespace v8pp {
7586template <>
7687struct factory <Y, v8pp::raw_ptr_traits>
@@ -150,6 +161,22 @@ void test_class_()
150161 .set (" useX_ptr" , &Y::useX_ptr<Traits>)
151162 ;
152163
164+ auto const E_ctor = [](v8::FunctionCallbackInfo<v8::Value> const & args)
165+ {
166+ return create_E<Traits>(args);
167+ };
168+
169+ auto const E_dtor = [](v8::Isolate* isolate, typename Traits::template object_pointer_type<E> const & obj)
170+ {
171+ Traits::destroy (obj);
172+ };
173+
174+ v8pp::class_<E, Traits> E_class (isolate, E_dtor);
175+ E_class
176+ .template ctor <v8::FunctionCallbackInfo<v8::Value> const &>(E_ctor)
177+ .set_const (" konst" , 42 )
178+ ;
179+
153180 check_ex<std::runtime_error>(" already wrapped class X" , [isolate]()
154181 {
155182 v8pp::class_<X, Traits> X_class (isolate);
@@ -166,6 +193,7 @@ void test_class_()
166193 context
167194 .set (" X" , X_class)
168195 .set (" Y" , Y_class)
196+ .set (" E" , E_class)
169197 ;
170198
171199 check_eq (" X object" , run_script<int >(context, " x = new X(); x.var += x.konst" ), 100 );
@@ -248,6 +276,9 @@ void test_class_()
248276 v8pp::class_<Y, Traits>::destroy (isolate);
249277 check_eq (" Y count after class_<Y>::destroy" , Y::instance_count,
250278 1 + 2 * use_shared_ptr); // y1 + (y2 + y3 when use_shared_ptr)
279+
280+ check_eq (" E ctor class with exception" , run_script<std::string>(context, " retError = " " ; try { var e = new E(); } catch(err) { retError = err; } retError" ), " MyException" );
281+ check_eq (" E ctor class without exception" , run_script<std::string>(context, " retError = " " ; try { var e = new E(1); } catch(err) { retError = err; } retError" ), " " );
251282}
252283
253284template <typename Traits>
@@ -370,4 +401,4 @@ void test_class()
370401
371402 test_const_instance_in_module<v8pp::raw_ptr_traits>();
372403 test_const_instance_in_module<v8pp::shared_ptr_traits>();
373- }
404+ }
0 commit comments