-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathCallbackHandlers.h
More file actions
261 lines (191 loc) · 10 KB
/
CallbackHandlers.h
File metadata and controls
261 lines (191 loc) · 10 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#ifndef CALLBACKHANDLERS_H_
#define CALLBACKHANDLERS_H_
#include <string>
#include <map>
#include <vector>
#include "JEnv.h"
#include "ArgsWrapper.h"
#include "MetadataEntry.h"
#include "FieldCallbackData.h"
#include "MetadataTreeNode.h"
#include "NumericCasts.h"
#include "FieldAccessor.h"
#include "ArrayElementAccessor.h"
#include "ObjectManager.h"
#include "include/v8.h"
namespace tns {
class CallbackHandlers {
public:
/*
* Stores persistent handles of all 'Worker' objects initialized on the main thread
* Note: No isolates different than that of the main thread should access this map
*/
static std::map<int, v8::Persistent<v8::Object> *> id2WorkerMap;
static int nextWorkerId;
static void Init(v8::Isolate *isolate);
static v8::Local<v8::Object>
CreateJSWrapper(v8::Isolate *isolate, jint javaObjectID, const std::string &typeName);
static bool RegisterInstance(v8::Isolate *isolate, const v8::Local<v8::Object> &jsObject,
const std::string &fullClassName,
const ArgsWrapper &argWrapper,
const v8::Local<v8::Object> &implementationObject,
bool isInterface,
const std::string &baseClassName = std::string());
static jclass ResolveClass(v8::Isolate *isolate, const std::string &baseClassName,
const std::string &fullClassName,
const v8::Local<v8::Object> &implementationObject,
bool isInterface);
static std::string ResolveClassName(v8::Isolate *isolate, jclass &clazz);
static v8::Local<v8::Value>
GetArrayElement(v8::Local<v8::Context> context, const v8::Local<v8::Object> &array, uint32_t index,
const std::string &arraySignature);
static void
SetArrayElement(v8::Local<v8::Context> context, const v8::Local<v8::Object> &array, uint32_t index,
const std::string &arraySignature, v8::Local<v8::Value> &value);
static int GetArrayLength(v8::Isolate *isolate, const v8::Local<v8::Object> &arr);
static void
CallJavaMethod(const v8::Local<v8::Object> &caller, const std::string &className,
const std::string &methodName, MetadataEntry *entry, bool isFromInterface,
bool isStatic, bool isSuper,
const v8::FunctionCallbackInfo<v8::Value> &args);
static v8::Local<v8::Value>
CallJSMethod(v8::Isolate *isolate, JNIEnv *_env, const v8::Local<v8::Object> &jsObject,
const std::string &methodName, jobjectArray args);
static v8::Local<v8::Value>
GetJavaField(v8::Isolate *isolate, const v8::Local<v8::Object> &caller,
FieldCallbackData *fieldData);
static void SetJavaField(v8::Isolate *isolate, const v8::Local<v8::Object> &target,
const v8::Local<v8::Value> &value, FieldCallbackData *fieldData);
static void LogMethodCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
static void TimeCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
static void
DumpReferenceTablesMethodCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
static void DrainMicrotaskCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
static void DumpReferenceTablesMethod();
static void ExitMethodCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
static void CreateGlobalCastFunctions(v8::Isolate *isolate,
const v8::Local<v8::ObjectTemplate> &globalTemplate);
static std::vector<std::string> GetTypeMetadata(const std::string &name, int index);
/*
* Gets all methods in the implementation object, and packs them in a jobjectArray
* to pass them to Java Land, so that their corresponding Java callbacks are written when
* the dexFactory generates the class
*/
static jobjectArray
GetMethodOverrides(JEnv &env, const v8::Local<v8::Object> &implementationObject);
/*
* Gets all interfaces declared in the 'interfaces' array inside the implementation object,
* and packs them in a jobjectArray to pass them to Java Land, so that they may be
* implemented when the dexFactory generates the corresponding class
*/
static jobjectArray
GetImplementedInterfaces(JEnv &env, const v8::Local<v8::Object> &implementationObject);
static void
EnableVerboseLoggingMethodCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
static void
DisableVerboseLoggingMethodCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
static void ReleaseNativeCounterpartCallback(const v8::FunctionCallbackInfo<v8::Value> &info);
static v8::Local<v8::Object> FindClass(v8::Isolate *isolate, const std::string &className);
static void NewThreadCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
/*
* main -> worker messaging
* Fired when a Worker instance's postMessage is called
*/
static void
WorkerObjectPostMessageCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
/*
* main -> worker messaging
* Fired when worker object has "postMessage" and the worker has implemented "onMessage" handler
* In case "onMessage" handler isn't implemented no exception is thrown
*/
static void WorkerGlobalOnMessageCallback(v8::Isolate *isolate, jstring message);
/*
* worker -> main thread messaging
* Fired when a Worker script's "postMessage" is called
*/
static void
WorkerGlobalPostMessageCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
/*
* worker -> main messaging
* Fired when worker has sent a message to main and the worker object has implemented "onMessage" handler
* In case "onMessage" handler isn't implemented no exception is thrown
*/
static void
WorkerObjectOnMessageCallback(v8::Isolate *isolate, jint workerId, jstring message);
/*
* Fired when a Worker instance's terminate is called (immediately stops execution of the thread)
*/
static void WorkerObjectTerminateCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
/*
* Fired when a Worker script's close is called
*/
static void WorkerGlobalCloseCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
/*
* Clears the persistent Worker object handle associated with a workerId
* Occurs when calling a worker object's `terminate` or a worker thread's global scope `close`
*/
static void ClearWorkerPersistent(int workerId);
/*
* Terminates the currently executing Isolate. No scripts can be executed after this call
*/
static void TerminateWorkerThread(v8::Isolate *isolate);
/*
* Is called when an unhandled exception is thrown inside the worker
* Will execute 'onerror' if one is provided inside the Worker Scope
* Will make the exception "bubble up" through to main, to be handled by the Worker Object
* if 'onerror' isn't implemented or returns false
*/
static void CallWorkerScopeOnErrorHandle(v8::Isolate *isolate, v8::TryCatch &tc);
/*
* Is called when an unhandled exception bubbles up from the worker scope to the main thread Worker Object
* Will execute `onerror` if one is implemented for the Worker Object instance
* Will throw a NativeScript Exception if 'onerror' isn't implemented or returns false
*/
static void
CallWorkerObjectOnErrorHandle(v8::Isolate *isolate, jint workerId, jstring message,
jstring stackTrace, jstring filename, jint lineno,
jstring threadName);
private:
CallbackHandlers() {
}
static void AdjustAmountOfExternalAllocatedMemory(JEnv &env, v8::Isolate *isolate);
/*
* Helper method that creates a java string array for sending strings over JNI
*/
static jobjectArray GetJavaStringArray(JEnv &env, int length);
static void validateProvidedArgumentsLength(const v8::FunctionCallbackInfo<v8::Value> &args, int expectedSize);
static short MAX_JAVA_STRING_ARRAY_LENGTH;
static jclass RUNTIME_CLASS;
static jclass JAVA_LANG_STRING;
static jmethodID RESOLVE_CLASS_METHOD_ID;
static jfieldID CURRENT_OBJECTID_FIELD_ID;
static jmethodID MAKE_INSTANCE_STRONG_ID;
static jmethodID GET_TYPE_METADATA;
static jmethodID ENABLE_VERBOSE_LOGGING_METHOD_ID;
static jmethodID DISABLE_VERBOSE_LOGGING_METHOD_ID;
static jmethodID INIT_WORKER_METHOD_ID;
static NumericCasts castFunctions;
static ArrayElementAccessor arrayElementAccessor;
static FieldAccessor fieldAccessor;
static std::string JAVA_LANG_LONG;
static std::string JAVA_LANG_INT;
static std::string JAVA_LANG_FLOAT;
static std::string JAVA_LANG_DOUBLE;
static std::string JAVA_LANG_SHORT;
static std::string JAVA_LANG_BOOLEAN;
struct JavaObjectIdScope {
JavaObjectIdScope(JEnv &env, jfieldID fieldId, jobject runtime, int javaObjectId)
: _env(env), _fieldID(fieldId), _runtime(runtime) {
_env.SetIntField(_runtime, _fieldID, javaObjectId);
}
~JavaObjectIdScope() {
_env.SetIntField(_runtime, _fieldID, -1);
}
private:
JEnv _env;
jfieldID _fieldID;
jobject _runtime;
};
};
}
#endif /* CALLBACKHANDLERS_H_ */