-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNativeObject.hpp
229 lines (222 loc) · 6.28 KB
/
NativeObject.hpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* NativeObject.hpp
*
* Define native objects for use.
* Copyright (C) 2018-2019 khjxiaogu
*
* Author: khjxiaogu
* Web: http://www.khjxiaogu.com
*/
#pragma once
#include<mutex>
#include <functional>
#include <map>
//Native Object to provide bridge between TJS Object call and Native Object
class NativeObject : public tTJSDispatch
{
public:
//map to store interal functions
std::map<std::wstring,tTJSVariant> functions;
//lock to prevent multi-threading errors
std::mutex concurrent;
//Constructors are defined by children.
NativeObject() {
}
//free all functions within this object
virtual ~NativeObject() {
//std::lock_guard<std::mutex> lock(concurrent);
/*for (std::map<std::wstring, tTJSVariant>::iterator it = functions.begin(); it != functions.end(); it++) {
try {
it->second->Release();
}
catch (...)
{
}
}*/
//TVPAddLog("native obj released");
}
//for instanceof operation,reload this to set the class name
virtual const wchar_t* getClass() {
return L"NativeClass";
}
virtual tjs_error TJS_INTF_METHOD FuncCall( // function invocation
tjs_uint32 flag, // calling flag
const tjs_char* membername,// member name ( NULL for a default member )
tjs_uint32* hint, // hint for the member name (in/out)
tTJSVariant* result, // result1
tjs_int numparams, // number of parameters
tTJSVariant** param, // parameters
iTJSDispatch2* objthis // object as "this"
) {
//std::lock_guard<std::mutex> lock(concurrent);
//TVPAddLog("fcbn");
if (!membername)return TJS_E_MEMBERNOTFOUND;
auto it = functions.find(membername);
if (it != functions.end()) {
int rst=it->second.AsObjectNoAddRef()->FuncCall(flag, NULL, hint, result, numparams, param,it->second.AsObjectThisNoAddRef());
return rst;
}
else
return TJS_E_MEMBERNOTFOUND;
}
virtual tjs_error TJS_INTF_METHOD PropGet(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
tTJSVariant* result,
iTJSDispatch2* objthis
)
{
std::lock_guard<std::mutex> lock(concurrent);
if (!membername) {
*result = tTJSVariant(this);
return TJS_S_OK;
}
else {
auto it = functions.find(membername);
if (it!= functions.end()) {
//
if (it->second.Type() == tvtObject) {
if (it->second.AsObjectNoAddRef()->PropGet(NULL, NULL, NULL, result, it->second.AsObjectNoAddRef()) == TJS_S_OK)return TJS_S_OK;
}
*result = it->second;
return TJS_S_OK;
}
else
return TJS_E_MEMBERNOTFOUND;
}
}
virtual tjs_error TJS_INTF_METHOD PropSet(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
const tTJSVariant* param,
iTJSDispatch2* objthis
)
{
std::lock_guard<std::mutex> lock(concurrent);
if (!membername) {
return TJS_E_NOTIMPL;
}
else {/* if (param->Type() == tvtObject) {*/
auto it = functions.find(membername);
if (it != functions.end()) {
if (it->second.Type() == tvtObject) {
tjs_error hr=it->second.AsObjectNoAddRef()->PropSet(0, NULL, NULL, param,objthis);
if (TJS_SUCCEEDED(hr)) return hr;
if (hr != TJS_E_NOTIMPL && hr != TJS_E_INVALIDTYPE &&
hr != TJS_E_INVALIDOBJECT)
return hr;
}
functions.erase(it);
}
functions.insert(std::pair<std::wstring, tTJSVariant>(std::wstring(membername), *param));
}
/* //functions.emplace(std::pair<std::wstring, iTJSDispatch2*>(std::wstring(membername), (param->AsObjectNoAddRef())));
return TJS_S_OK;
}
else
if (functions.find(membername) != functions.end()) {
functions.at(membername)->PropSet(flag, membername, NULL, param, objthis);
return TJS_S_OK;
}
else
return TJS_E_MEMBERNOTFOUND;*/
return TJS_S_OK;
}
//macro to put lambda function into this object
void putFunc(const std::wstring str, NativeTJSFunction func) {
std::lock_guard<std::mutex> lock(concurrent);
functions.emplace(std::pair<std::wstring, tTJSVariant>(str, (new FunctionCaller(func))));
}
//macro to put lambda properto into this object.
template<typename T> void putProp(const std::wstring str,
typename PropertyCaller<T>::getterT getter = NULL,
typename PropertyCaller<T>::setterT setter = NULL) {
std::lock_guard<std::mutex> lock(concurrent);
functions.emplace(std::pair<std::wstring, tTJSVariant>(str, (new PropertyCaller<T>(getter, setter))));
}
tjs_error TJS_INTF_METHOD
EnumMembers(tjs_uint32 flag, tTJSVariantClosure* callback, iTJSDispatch2* objthis)
{
std::lock_guard<std::mutex> lock(concurrent);
tTJSVariant* par[3];
par[0] = new tTJSVariant();
par[1] = new tTJSVariant(NULL);
par[2] = new tTJSVariant();
for (std::map<std::wstring, tTJSVariant>::iterator it = functions.begin(); it != functions.end(); it++) {
*par[0] = ttstr(it->first.c_str());
*par[2] = it->second;
callback->FuncCall(NULL, NULL, NULL, &tTJSVariant(), 3, par, objthis);
}
return TJS_S_OK;
}
tjs_error TJS_INTF_METHOD
Invalidate(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
iTJSDispatch2* objthis
)
{
if (membername)
return TJS_E_MEMBERNOTFOUND;
else {
delete this;
return TJS_S_OK;
}
}
tjs_error TJS_INTF_METHOD
DeleteMember(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
iTJSDispatch2* objthis
)
{
std::lock_guard<std::mutex> lock(concurrent);
if (!membername) {
return TJS_E_MEMBERNOTFOUND;
}
if (functions.find(membername) != functions.end()) {
//functions.at(membername)->Release();
functions.erase(membername);
return TJS_S_OK;
}
return TJS_E_MEMBERNOTFOUND;
}
tjs_error TJS_INTF_METHOD
IsValid(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
iTJSDispatch2* objthis
)
{
std::lock_guard<std::mutex> lock(concurrent);
if (!membername) {
return TJS_S_TRUE;
}
if (functions.find(membername) != functions.end()) {
return TJSIsObjectValid(functions.at(membername).AsObjectNoAddRef()->IsValid(flag, NULL, hint, objthis));
}
return TJS_E_MEMBERNOTFOUND;
}
tjs_error TJS_INTF_METHOD
IsInstanceOf(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
const tjs_char* classname,
iTJSDispatch2* objthis
)
{
if (!membername) {
if (!wcscmp(classname, L"Class") || !wcscmp(classname, getClass()))
return TJS_S_TRUE;
}
else
return functions.at(membername).AsObjectNoAddRef()->IsInstanceOf(flag, NULL, hint, classname, objthis);
return TJS_S_FALSE;
}
};