-
Notifications
You must be signed in to change notification settings - Fork 7
/
SMJS_BaseWrapped.h
81 lines (66 loc) · 3.09 KB
/
SMJS_BaseWrapped.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef _INCLUDE_SMJS_BASEWRAPPED_H_
#define _INCLUDE_SMJS_BASEWRAPPED_H_
#include "k7v8macros.h"
#include "SMJS.h"
#include "SMJS_Base.h"
#include <unordered_map>
class SMJS_BaseWrapped : public SMJS_Base, public IPluginDestroyedHandler {
public:
std::unordered_map<PLUGIN_ID, v8::Persistent<v8::Value>> wrappers;
int refCount;
SMJS_BaseWrapped();
virtual void OnWrapperAttached(SMJS_Plugin *plugin, v8::Persistent<v8::Value> wrapper){};
virtual v8::Persistent<v8::Value> GetWrapper(SMJS_Plugin *plugin){return v8::Persistent<v8::Value>::New(v8::Undefined());};
virtual void OnPluginDestroyed(SMJS_Plugin *plugin);
void Destroy();
static std::unordered_map<PLUGIN_ID, v8::Persistent<FunctionTemplate>> templates;
static void SetupTemplate(v8::Persistent<v8::FunctionTemplate> temp, v8::Persistent<v8::Template> proto) {}
static v8::Persistent<v8::FunctionTemplate> GetTemplateForPlugin(SMJS_Plugin *plugin, bool cache = true);
};
#define WRAPPED_CLS(cls, super) \
virtual v8::Persistent<v8::Value> GetWrapper(SMJS_Plugin *plugin); \
static std::unordered_map<PLUGIN_ID, v8::Persistent<FunctionTemplate>> templates; \
static v8::Persistent<v8::FunctionTemplate> GetTemplateForPlugin(SMJS_Plugin *plugin, bool cache = true); \
static v8::Persistent<v8::Value> CreateWrapper(SMJS_Plugin *plugin, cls* obj); \
static void SOnPluginDestroyed(SMJS_Plugin *plugin); \
static void SetupTemplate(v8::Persistent<FunctionTemplate> temp, v8::Persistent<v8::ObjectTemplate> proto) \
#define WRAPPED_CLS_CPP(cls, super) \
std::unordered_map<PLUGIN_ID, v8::Persistent<FunctionTemplate>> cls::templates; \
v8::Persistent<v8::FunctionTemplate> cls::GetTemplateForPlugin(SMJS_Plugin *plugin, bool cache){ \
auto it = templates.find(plugin->id); \
if(it != templates.end()) return it->second; \
\
auto temp = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());; \
\
auto inst = temp->InstanceTemplate(); \
inst->SetInternalFieldCount(1); \
\
auto proto = v8::Persistent<v8::ObjectTemplate>::New(temp->PrototypeTemplate()); \
\
temp->Inherit(super::GetTemplateForPlugin(plugin, false)); \
SetupTemplate(temp, proto); \
\
if(cache) templates.insert(std::pair<PLUGIN_ID, v8::Persistent<v8::FunctionTemplate>>(plugin->id, temp)); \
\
plugin->RegisterDestroyCallback(SOnPluginDestroyed); \
return temp; \
} \
v8::Persistent<v8::Value> cls::CreateWrapper(SMJS_Plugin *plugin, cls* obj) { \
auto v = GetTemplateForPlugin(plugin)->GetFunction()->NewInstance(); \
v->SetInternalField(0, v8::External::New((void*)obj)); \
return v8::Persistent<v8::Value>::New(v); \
} \
v8::Persistent<v8::Value> cls::GetWrapper(SMJS_Plugin *plugin) { \
auto it = wrappers.find(plugin->id); \
if(it != wrappers.end()) return it->second; \
auto wrapper = CreateWrapper(plugin, this); \
++refCount; \
wrappers.insert(std::pair<PLUGIN_ID, v8::Persistent<v8::Value>>(plugin->id, wrapper)); \
OnWrapperAttached(plugin, wrapper); \
plugin->RegisterDestroyCallback(this); \
return wrapper; \
} \
void cls::SOnPluginDestroyed(SMJS_Plugin *plugin) { \
templates.erase(plugin->id); \
}
#endif