diff --git a/test/test_class.cpp b/test/test_class.cpp index f69d9e81..8b2359ee 100644 --- a/test/test_class.cpp +++ b/test/test_class.cpp @@ -54,6 +54,15 @@ static X_ptr create_X(v8::FunctionCallbackInfo const& args) return x; } +template::object_pointer_type> +static E_ptr create_E(v8::FunctionCallbackInfo const& args) +{ + E_ptr e(new E); + if (args.Length() == 0) + v8pp::throw_ex(args.GetIsolate(), "MyException"); + return e; +} + struct Y : X { static int instance_count; @@ -71,6 +80,8 @@ int Y::instance_count = 0; struct Z {}; +struct E {}; + namespace v8pp { template<> struct factory @@ -150,6 +161,22 @@ void test_class_() .set("useX_ptr", &Y::useX_ptr) ; + auto const E_ctor = [](v8::FunctionCallbackInfo const& args) + { + return create_E(args); + }; + + auto const E_dtor = [](v8::Isolate* isolate, typename Traits::template object_pointer_type const& obj) + { + Traits::destroy(obj); + }; + + v8pp::class_ E_class(isolate, E_dtor); + E_class + .template ctor const&>(E_ctor) + .set_const("konst", 42) + ; + check_ex("already wrapped class X", [isolate]() { v8pp::class_ X_class(isolate); @@ -166,6 +193,7 @@ void test_class_() context .set("X", X_class) .set("Y", Y_class) + .set("E", E_class) ; check_eq("X object", run_script(context, "x = new X(); x.var += x.konst"), 100); @@ -248,6 +276,9 @@ void test_class_() v8pp::class_::destroy(isolate); check_eq("Y count after class_::destroy", Y::instance_count, 1 + 2 * use_shared_ptr); // y1 + (y2 + y3 when use_shared_ptr) + + check_eq("E ctor class with exception", run_script(context, "retError = ""; try { var e = new E(); } catch(err) { retError = err; } retError"), "MyException"); + check_eq("E ctor class without exception", run_script(context, "retError = ""; try { var e = new E(1); } catch(err) { retError = err; } retError"), ""); } template @@ -370,4 +401,4 @@ void test_class() test_const_instance_in_module(); test_const_instance_in_module(); -} +} \ No newline at end of file diff --git a/v8pp/class.hpp b/v8pp/class.hpp index b2f8dada..f55c8378 100644 --- a/v8pp/class.hpp +++ b/v8pp/class.hpp @@ -259,7 +259,18 @@ class object_registry final : public class_info //assert(false && "create not allowed"); throw std::runtime_error(class_name() + " has no constructor"); } - return wrap_object(ctor_(args), true); + + v8::EscapableHandleScope scope(args.GetIsolate()); + v8::TryCatch try_catch(args.GetIsolate()); + v8::Local result; + + auto ctorRes = ctor_(args); + if (try_catch.HasCaught()) + try_catch.ReThrow(); + else + result = wrap_object(ctorRes, true); + + return scope.Escape(result); } pointer_type unwrap_object(v8::Local value)