Skip to content

Commit

Permalink
Detect unbound types at run-time to avoid crash on Node.js target.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed Dec 27, 2016
1 parent 125093e commit 992799e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 7 additions & 1 deletion include/nbind/BindClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,14 @@ BindClassBase &BindWrapper<Bound> :: getBindClass() {

template <class Bound>
void BindWrapper<Bound> :: testInstance(v8::Local<v8::Object> arg) {
BindClassBase &bindClass = getBindClass();

if(bindClass.superTemplate.IsEmpty()) {
throw(std::runtime_error("Unbound type"));
}

if(
!Nan::New(getBindClass().superTemplate)->HasInstance(arg) ||
!Nan::New(bindClass.superTemplate)->HasInstance(arg) ||
arg->InternalFieldCount() != 1
) {
throw(std::runtime_error("Type mismatch"));
Expand Down
9 changes: 8 additions & 1 deletion include/nbind/v8/ValueObj.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ static inline WireType makeExternal(TypeFlags flags, TargetType *ptr, ArgType &&
#endif // DUPLICATE_POINTERS

unsigned int constructorNum = BindClass<BaseType>::getInstance().wrapperConstructorNum;
v8::Local<v8::Function> constructor = Overloader::getDef(constructorNum).constructorJS->GetFunction();
Nan::Callback *constructorJS = Overloader::getDef(constructorNum).constructorJS;

if(constructorJS == nullptr) {
Nan::ThrowError("Unbound type");
return(Nan::Undefined());
}

v8::Local<v8::Function> constructor = constructorJS->GetFunction();

// TODO: first argument should be a unique marker of some kind.

Expand Down

0 comments on commit 992799e

Please sign in to comment.