Skip to content

Commit 131f50a

Browse files
committed
add test case for exception from C++ ctor
Idea suggested by pull-request #99
1 parent a159a8e commit 131f50a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

test/test_class.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ template<typename Traits, typename X_ptr = typename v8pp::class_<X, Traits>::obj
4747
static X_ptr create_X(v8::FunctionCallbackInfo<v8::Value> const& args)
4848
{
4949
X_ptr x(new X);
50-
if (args.Length() > 0)
50+
switch (args.Length())
5151
{
52+
case 1:
5253
x->var = v8pp::from_v8<int>(args.GetIsolate(), args[0]);
54+
break;
55+
case 2:
56+
throw std::runtime_error("C++ exception");
57+
case 3:
58+
v8pp::throw_ex(args.GetIsolate(), "JS exception");
5359
}
5460
return x;
5561
}
@@ -168,6 +174,13 @@ void test_class_()
168174
.set("Y", Y_class)
169175
;
170176

177+
check_eq("C++ exception from X ctor",
178+
run_script<std::string>(context, "ret = ''; try { new X(1, 2); } catch(err) { ret = err; } ret"),
179+
"C++ exception");
180+
check_eq("V8 exception from X ctor",
181+
run_script<std::string>(context, "ret = ''; try { new X(1, 2, 3); } catch(err) { ret = err; } ret"),
182+
"JS exception");
183+
171184
check_eq("X object", run_script<int>(context, "x = new X(); x.var += x.konst"), 100);
172185
check_eq("X::rprop", run_script<int>(context, "x = new X(); x.rprop"), 1);
173186
check_eq("X::wprop", run_script<int>(context, "x = new X(); ++x.wprop"), 2);

0 commit comments

Comments
 (0)