-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSMJS_BaseWrapped.cpp
More file actions
51 lines (39 loc) · 1.37 KB
/
SMJS_BaseWrapped.cpp
File metadata and controls
51 lines (39 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "extension.h"
#include "SMJS_BaseWrapped.h"
#include "SMJS_Plugin.h"
void DecWrappedRefCount(Isolate* isolate, v8::Persistent<v8::Value> object, void *parameter){
SMJS_BaseWrapped *wrapped = (SMJS_BaseWrapped*) parameter;
if(--wrapped->refCount == 0){
delete wrapped;
}
object.Dispose();
object.Clear();
}
SMJS_BaseWrapped::SMJS_BaseWrapped(){
refCount = 0;
}
void SMJS_BaseWrapped::OnPluginDestroyed(SMJS_Plugin *plugin){
--refCount;
wrappers.erase(plugin->id);
}
void SMJS_BaseWrapped::Destroy(){
if(refCount == 0){
delete this;
}else{
for(auto it = wrappers.begin(); it != wrappers.end(); ++it){
it->second.MakeWeak(GetPlugin(it->first)->GetIsolate(), (void*) this, DecWrappedRefCount);
}
}
}
std::unordered_map<PLUGIN_ID, v8::Persistent<FunctionTemplate>> SMJS_BaseWrapped::templates;
v8::Persistent<v8::FunctionTemplate> SMJS_BaseWrapped::GetTemplateForPlugin(SMJS_Plugin *plugin, bool cache){
auto it = templates.find(plugin->id);
if(it != templates.end()) return it->second;
auto temp = Persistent<FunctionTemplate>::New(FunctionTemplate::New());;
auto inst = temp->InstanceTemplate();
inst->SetInternalFieldCount(1);
auto proto = Persistent<Template>::New(temp->PrototypeTemplate());
if(cache) SetupTemplate(temp, proto);
templates.insert(std::pair<PLUGIN_ID, v8::Persistent<v8::FunctionTemplate>>(plugin->id, temp));
return temp;
}