Skip to content

Commit

Permalink
Properly handle null callback in FunctionTemplate factory
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoopa committed Aug 23, 2015
1 parent 13596c0 commit 6e99cb1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 41 deletions.
44 changes: 24 additions & 20 deletions nan_implementation_12_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,33 +118,37 @@ Factory<v8::FunctionTemplate>::New( FunctionCallback callback
, v8::Local<v8::Value> data
, v8::Local<v8::Signature> signature) {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::EscapableHandleScope scope(isolate);
static std::map<FunctionCallback, // NOLINT(build/include_what_you_use)
imp::FunctionWrapper*> cbmap;
v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New(isolate);
tpl->SetInternalFieldCount(imp::kFunctionFieldCount);
if (callback) {
v8::EscapableHandleScope scope(isolate);
static std::map<FunctionCallback, // NOLINT(build/include_what_you_use)
imp::FunctionWrapper*> cbmap;
v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New(isolate);
tpl->SetInternalFieldCount(imp::kFunctionFieldCount);
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
v8::Local<v8::Object> obj =
tpl->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
v8::Local<v8::Object> obj =
tpl->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
#else
v8::Local<v8::Object> obj = tpl->NewInstance();
v8::Local<v8::Object> obj = tpl->NewInstance();
#endif

obj->SetAlignedPointerInInternalField(
imp::kFunctionIndex
, imp::GetWrapper<FunctionCallback,
imp::FunctionWrapper>(callback));
v8::Local<v8::Value> val = v8::Local<v8::Value>::New(isolate, data);
obj->SetAlignedPointerInInternalField(
imp::kFunctionIndex
, imp::GetWrapper<FunctionCallback,
imp::FunctionWrapper>(callback));
v8::Local<v8::Value> val = v8::Local<v8::Value>::New(isolate, data);

if (!val.IsEmpty()) {
obj->SetInternalField(imp::kDataIndex, val);
}
if (!val.IsEmpty()) {
obj->SetInternalField(imp::kDataIndex, val);
}

return scope.Escape(v8::FunctionTemplate::New( isolate
, imp::FunctionCallbackWrapper
, obj
, signature));
return scope.Escape(v8::FunctionTemplate::New( isolate
, imp::FunctionCallbackWrapper
, obj
, signature));
} else {
return v8::FunctionTemplate::New(isolate, 0, data, signature);
}
}

//=== Number ===================================================================
Expand Down
47 changes: 26 additions & 21 deletions nan_implementation_pre_12_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,35 @@ Factory<v8::FunctionTemplate>::return_t
Factory<v8::FunctionTemplate>::New( FunctionCallback callback
, v8::Local<v8::Value> data
, v8::Local<v8::Signature> signature) {
v8::HandleScope scope;

static std::map<FunctionCallback, // NOLINT(build/include_what_you_use)
imp::FunctionWrapper*> cbmap;
v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New();
tpl->SetInternalFieldCount(imp::kFunctionFieldCount);
v8::Local<v8::Object> obj = tpl->NewInstance();

obj->SetPointerInInternalField(
imp::kFunctionIndex
, imp::GetWrapper<FunctionCallback,
imp::FunctionWrapper>(callback));
v8::Local<v8::Value> val = v8::Local<v8::Value>::New(data);

if (!val.IsEmpty()) {
obj->SetInternalField(imp::kDataIndex, val);
}

// Note(agnat): Emulate length argument here. Unfortunately, I couldn't find
// a way. Have at it though...
return scope.Close(
v8::FunctionTemplate::New(imp::FunctionCallbackWrapper
, obj
, signature));
if (callback) {
v8::HandleScope scope;

v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New();
tpl->SetInternalFieldCount(imp::kFunctionFieldCount);
v8::Local<v8::Object> obj = tpl->NewInstance();

obj->SetPointerInInternalField(
imp::kFunctionIndex
, imp::GetWrapper<FunctionCallback,
imp::FunctionWrapper>(callback));
v8::Local<v8::Value> val = v8::Local<v8::Value>::New(data);

if (!val.IsEmpty()) {
obj->SetInternalField(imp::kDataIndex, val);
}

// Note(agnat): Emulate length argument here. Unfortunately, I couldn't find
// a way. Have at it though...
return scope.Close(
v8::FunctionTemplate::New(imp::FunctionCallbackWrapper
, obj
, signature));
} else {
return v8::FunctionTemplate::New(0, data, signature);
}
}

//=== Number ===================================================================
Expand Down

0 comments on commit 6e99cb1

Please sign in to comment.