Skip to content

Commit 9c6ee42

Browse files
committed
Throw exception when calling method without instance (instead of segfault)
Fixes pmed#106
1 parent 4af5a91 commit 9c6ee42

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

test/test_class.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ void test_class_()
218218
check_eq("X::my_static_var", run_script<int>(context, "X.my_static_var"), 1);
219219
check_eq("X::my_static_var after assign", run_script<int>(context, "X.my_static_var = 123; X.my_static_var"), 123);
220220

221+
check_ex<std::runtime_error>("call method with invalid instance", [&context]() {
222+
run_script<int>(context, "x = new X(); f = x.fun1; f(1)");
223+
});
224+
221225
check_eq("JSON.stringify(X)",
222226
run_script<std::string>(context, "JSON.stringify({'obj': new X(10), 'arr': [new X(11), new X(12)] })"),
223227
R"({"obj":{"key":"obj","var":10},"arr":[{"key":"0","var":11},{"key":"1","var":12}]})"

v8pp/function.hpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,14 @@ invoke(v8::FunctionCallbackInfo<v8::Value> const& args, std::true_type /*is_memb
136136

137137
v8::Isolate* isolate = args.GetIsolate();
138138
v8::Local<v8::Object> obj = args.This();
139-
return call_from_v8<Traits, class_type, F>(
140-
*class_<class_type, Traits>::unwrap_object(isolate, obj),
141-
std::forward<F>(get_external_data<F>(args.Data())), args);
139+
auto ptr = class_<class_type, Traits>::unwrap_object(isolate, obj);
140+
if (ptr) {
141+
return call_from_v8<Traits, class_type, F>(
142+
*ptr,
143+
std::forward<F>(get_external_data<F>(args.Data())), args);
144+
} else {
145+
throw std::runtime_error("method called on null instance");
146+
}
142147
}
143148

144149
template<typename Traits, typename F>

0 commit comments

Comments
 (0)