-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSMJS_SimpleWrapped.h
More file actions
96 lines (73 loc) · 2.63 KB
/
SMJS_SimpleWrapped.h
File metadata and controls
96 lines (73 loc) · 2.63 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef _INCLUDE_SMJS_SIMPLEWRAPPED_H_
#define _INCLUDE_SMJS_SIMPLEWRAPPED_H_
#include "k7v8macros.h"
#include "SMJS.h"
#include "SMJS_Base.h"
#include <unordered_map>
class SMJS_SimpleWrapped : public SMJS_Base, public IPluginDestroyedHandler {
private:
SMJS_SimpleWrapped();
protected:
v8::Persistent<v8::Value> wrapper;
int refCount;
SMJS_Plugin *plugin;
public:
SMJS_SimpleWrapped(SMJS_Plugin *pl);
virtual void OnWrapperAttached(){};
virtual v8::Persistent<v8::Value> GetWrapper(){return v8::Persistent<v8::Value>::New(v8::Undefined());};
void Destroy();
virtual void OnPluginDestroyed(SMJS_Plugin *plugin){
Destroy();
this->plugin = NULL;
}
static v8::Persistent<v8::FunctionTemplate> temp;
inline SMJS_Plugin* GetPlugin(){return plugin;}
static void SetupTemplate(v8::Persistent<v8::FunctionTemplate> temp, v8::Persistent<v8::Template> proto) {}
static v8::Persistent<v8::FunctionTemplate> GetTemplate(){
if(!temp.IsEmpty()) return temp;
auto nTemp = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());;
auto inst = nTemp->InstanceTemplate();
inst->SetInternalFieldCount(1);
auto proto = v8::Persistent<v8::ObjectTemplate>::New(nTemp->PrototypeTemplate());
SetupTemplate(nTemp, proto);
temp = nTemp;
return temp;
}
};
#define SIMPLE_WRAPPED_CLS(cls, super) \
virtual v8::Persistent<v8::Value> GetWrapper(); \
static v8::Persistent<FunctionTemplate> temp; \
static v8::Persistent<v8::FunctionTemplate> GetTemplate(); \
static v8::Persistent<v8::Value> CreateWrapper(cls* obj); \
static void SetupTemplate(v8::Persistent<FunctionTemplate> temp, v8::Persistent<v8::ObjectTemplate> proto) \
#define SIMPLE_WRAPPED_CLS_CPP(cls, super) \
v8::Persistent<FunctionTemplate> cls::temp; \
v8::Persistent<v8::FunctionTemplate> cls::GetTemplate(){ \
if(!temp.IsEmpty()) return temp; \
\
auto nTemp = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());; \
\
auto inst = nTemp->InstanceTemplate(); \
inst->SetInternalFieldCount(1); \
\
auto proto = v8::Persistent<v8::ObjectTemplate>::New(nTemp->PrototypeTemplate()); \
\
nTemp->Inherit(super::GetTemplate()); \
SetupTemplate(nTemp, proto); \
\
temp = nTemp; \
return temp; \
} \
v8::Persistent<v8::Value> cls::CreateWrapper(cls* obj) { \
auto v = GetTemplate()->GetFunction()->NewInstance(); \
v->SetInternalField(0, v8::External::New((void*)obj)); \
return v8::Persistent<v8::Value>::New(v); \
} \
v8::Persistent<v8::Value> cls::GetWrapper() { \
if(!wrapper.IsEmpty()) return wrapper; \
wrapper = CreateWrapper(this); \
++refCount; \
OnWrapperAttached(); \
return wrapper; \
}
#endif