Cache function templates created by CreateObjectWithName
This commit is contained in:
parent
ffc2870ccb
commit
d8d963b780
1 changed files with 16 additions and 0 deletions
|
@ -2,19 +2,35 @@
|
||||||
// Use of this source code is governed by the MIT license that can be
|
// Use of this source code is governed by the MIT license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "atom/common/api/object_life_monitor.h"
|
#include "atom/common/api/object_life_monitor.h"
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
|
#include "base/stl_util.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
#include "v8/include/v8-profiler.h"
|
#include "v8/include/v8-profiler.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
using FunctionTemplateHandle =
|
||||||
|
v8::CopyablePersistentTraits<v8::FunctionTemplate>::CopyablePersistent;
|
||||||
|
|
||||||
|
// The handles are leaked on purpose.
|
||||||
|
std::map<std::string, FunctionTemplateHandle> function_templates_;
|
||||||
|
|
||||||
v8::Local<v8::Object> CreateObjectWithName(v8::Isolate* isolate,
|
v8::Local<v8::Object> CreateObjectWithName(v8::Isolate* isolate,
|
||||||
const std::string& name) {
|
const std::string& name) {
|
||||||
if (name == "Object")
|
if (name == "Object")
|
||||||
return v8::Object::New(isolate);
|
return v8::Object::New(isolate);
|
||||||
|
|
||||||
|
if (ContainsKey(function_templates_, name))
|
||||||
|
return v8::Local<v8::FunctionTemplate>::New(
|
||||||
|
isolate, function_templates_[name])->GetFunction()->NewInstance();
|
||||||
|
|
||||||
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate);
|
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate);
|
||||||
t->SetClassName(mate::StringToV8(isolate, name));
|
t->SetClassName(mate::StringToV8(isolate, name));
|
||||||
|
function_templates_[name] = FunctionTemplateHandle(isolate, t);
|
||||||
return t->GetFunction()->NewInstance();
|
return t->GetFunction()->NewInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue