diff --git a/atom/common/api/atom_api_v8_util.cc b/atom/common/api/atom_api_v8_util.cc index dfa65ab8ecae..7b9490df6a8b 100644 --- a/atom/common/api/atom_api_v8_util.cc +++ b/atom/common/api/atom_api_v8_util.cc @@ -2,19 +2,35 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. +#include +#include + #include "atom/common/api/object_life_monitor.h" #include "atom/common/node_includes.h" +#include "base/stl_util.h" #include "native_mate/dictionary.h" #include "v8/include/v8-profiler.h" namespace { +using FunctionTemplateHandle = + v8::CopyablePersistentTraits::CopyablePersistent; + +// The handles are leaked on purpose. +std::map function_templates_; + v8::Local CreateObjectWithName(v8::Isolate* isolate, const std::string& name) { if (name == "Object") return v8::Object::New(isolate); + + if (ContainsKey(function_templates_, name)) + return v8::Local::New( + isolate, function_templates_[name])->GetFunction()->NewInstance(); + v8::Local t = v8::FunctionTemplate::New(isolate); t->SetClassName(mate::StringToV8(isolate, name)); + function_templates_[name] = FunctionTemplateHandle(isolate, t); return t->GetFunction()->NewInstance(); }