2018-05-22 08:08:27 +00:00
|
|
|
// Copyright (c) 2018 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:56:58 +00:00
|
|
|
#ifndef SHELL_COMMON_API_CONSTRUCTOR_H_
|
|
|
|
#define SHELL_COMMON_API_CONSTRUCTOR_H_
|
2018-05-22 08:08:27 +00:00
|
|
|
|
|
|
|
#include "native_mate/constructor.h"
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
// Create a FunctionTemplate that can be "new"ed in JavaScript.
|
|
|
|
// It is user's responsibility to ensure this function is called for one type
|
|
|
|
// only ONCE in the program's whole lifetime, otherwise we would have memory
|
|
|
|
// leak.
|
|
|
|
template <typename T, typename Sig>
|
2019-05-29 20:02:15 +00:00
|
|
|
v8::Local<v8::Function> CreateConstructor(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const base::RepeatingCallback<Sig>& func) {
|
2018-05-22 08:08:27 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
static bool called = false;
|
|
|
|
CHECK(!called) << "CreateConstructor can only be called for one type once";
|
|
|
|
called = true;
|
|
|
|
#endif
|
|
|
|
v8::Local<v8::FunctionTemplate> templ = CreateFunctionTemplate(
|
2019-05-03 19:08:41 +00:00
|
|
|
isolate, base::BindRepeating(&mate::internal::InvokeNew<Sig>, func));
|
2018-05-22 08:08:27 +00:00
|
|
|
templ->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
T::BuildPrototype(isolate, templ);
|
2019-01-09 19:17:05 +00:00
|
|
|
return templ->GetFunction(isolate->GetCurrentContext()).ToLocalChecked();
|
2018-05-22 08:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
2019-06-19 20:56:58 +00:00
|
|
|
#endif // SHELL_COMMON_API_CONSTRUCTOR_H_
|