-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorker.mm
More file actions
327 lines (255 loc) · 8.87 KB
/
Worker.mm
File metadata and controls
327 lines (255 loc) · 8.87 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "Worker.h"
#include <Foundation/NSObjCRuntime.h>
#include <memory>
#include <string>
#include "ffi/NativeScriptException.h"
#include "js_native_api.h"
#include "js_native_api_types.h"
#include "jsr.h"
#include "native_api_util.h"
#include "runtime/Runtime.h"
#include "runtime/Util.h"
#include "runtime/modules/worker/WorkerImpl.h"
namespace nativescript {
void Worker::Init(napi_env env, napi_value global) { Init(env, global, true); }
void Worker::Init(napi_env env, napi_value global, bool isWorkerThread) {
if (isWorkerThread) {
napi_property_descriptor globalProperties[] = {
napi_util::desc("postMessage", PostMessageToMain),
napi_util::desc("close", CloseWorker),
};
napi_define_properties(env, global, 2, globalProperties);
}
napi_property_descriptor properties[] = {
napi_util::desc("postMessage", PostMessage),
napi_util::desc("terminate", Terminate),
};
napi_value Worker;
napi_define_class(env, "Worker", NAPI_AUTO_LENGTH, Constructor, nullptr, 2, properties, &Worker);
napi_property_descriptor globalProperties[] = {
napi_util::desc("Worker", Worker),
};
napi_define_properties(env, global, 1, globalProperties);
}
JS_METHOD(Worker::Constructor) {
try {
size_t argc = 1;
napi_value argv[1];
napi_value jsThis;
napi_get_cb_info(env, cbinfo, &argc, argv, &jsThis, nullptr);
std::string workerPath = napi_util::get_cxx_string(env, argv[0]);
WorkerImpl* worker = new WorkerImpl(env, Worker::OnMessage);
napi_ref result = nullptr;
napi_wrap(env, jsThis, worker, Worker::Finalize, nullptr, &result);
std::shared_ptr<napi_util::PersistentObject> poWorker =
std::make_shared<napi_util::PersistentObject>(env, result);
std::function<napi_env()> func([worker, workerPath]() {
Runtime* runtime = new Runtime();
runtime->Init(true);
napi_env env = runtime->GetEnv();
try {
NapiScope scope(env);
runtime->SetWorkerId(worker->WorkerId());
int workerId = worker->WorkerId();
Worker::SetWorkerId(env, workerId);
runtime->RunModule(workerPath);
} catch (NativeScriptException& ex) {
NapiScope scope(env);
worker->PassUncaughtExceptionFromWorkerToMain(env, ex, false);
worker->Terminate();
}
return env;
});
worker->Start(poWorker, func);
int workerId = worker->Id();
WorkerImpl::Workers->Insert(workerId, worker);
return jsThis;
} catch (NativeScriptException& ex) {
ex.ReThrowToJS(env);
return nullptr;
}
}
void Worker::Finalize(napi_env env, void* nativeObject, void* finalize_hint) {
WorkerImpl* worker = reinterpret_cast<WorkerImpl*>(nativeObject);
if (worker != nullptr) {
delete worker;
}
}
JS_METHOD(Worker::PostMessageToMain) {
try {
napi_value argv[1];
size_t argc = 1;
napi_value jsThis;
napi_get_cb_info(env, cbinfo, &argc, argv, &jsThis, nullptr);
if (argc < 1) {
throw NativeScriptException("Not enough arguments.");
return nullptr;
}
if (argc > 1) {
throw NativeScriptException("Too many arguments passed.");
return nullptr;
}
int workerId = Worker::GetWorkerId(env, jsThis);
WorkerImpl* worker = WorkerImpl::Workers->Get(workerId);
if (worker == nullptr) {
throw NativeScriptException("Worker is not initialized.");
return nullptr;
}
if (!worker->IsRunning()) {
return nullptr;
}
auto message = std::make_shared<worker::Message>();
napi_value object;
napi_create_object(env, &object);
napi_set_named_property(env, object, "data", argv[0]);
message->Serialize(env, object);
napi_env mainEnv = worker->GetMainEnv();
CFRunLoopRef queue = Runtime::GetRuntime(mainEnv)->RuntimeLoop();
ExecuteOnRunLoop(
queue,
[mainEnv, worker, message] {
NapiScope scope(mainEnv);
napi_value global = napi_util::global(mainEnv);
napi_value workerInstance = worker->GetWorkerObject();
Worker::OnMessage(mainEnv, workerInstance, message);
},
true);
} catch (NativeScriptException& ex) {
ex.ReThrowToJS(env);
}
return nullptr;
}
JS_METHOD(Worker::PostMessage) {
try {
napi_value argv[1];
size_t argc = 1;
napi_value jsThis;
napi_get_cb_info(env, cbinfo, &argc, argv, &jsThis, nullptr);
if (argc < 1) {
throw NativeScriptException("Not enough arguments.");
return nullptr;
}
if (argc > 1) {
throw NativeScriptException("Too many arguments passed.");
return nullptr;
}
WorkerImpl* worker = nullptr;
napi_unwrap(env, jsThis, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
throw NativeScriptException("Worker is not initialized.");
return nullptr;
}
if (!worker->IsRunning() || worker->IsClosing()) {
return nullptr;
}
auto message = std::make_shared<worker::Message>();
napi_value object;
napi_create_object(env, &object);
napi_set_named_property(env, object, "data", argv[0]);
message->Serialize(env, object);
worker->PostMessage(message);
} catch (NativeScriptException& ex) {
ex.ReThrowToJS(env);
}
return nullptr;
}
void Worker::OnMessage(napi_env env, napi_value receiver,
std::shared_ptr<worker::Message> message) {
napi_value onMessageFunc = nullptr;
napi_status status = napi_get_named_property(env, receiver, "onmessage", &onMessageFunc);
if (!napi_util::is_of_type(env, onMessageFunc, napi_function)) {
return;
}
napi_value result;
napi_value arg = message->Deserialize(env);
status = napi_call_function(env, receiver, onMessageFunc, 1, &arg, &result);
if (status != napi_ok) {
if (status == napi_pending_exception) {
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
if (exception != nullptr && napi_util::is_of_type(env, exception, napi_object)) {
napi_value stack = napi_util::get_property(env, exception, "stack");
if (stack != nullptr) {
std::string stackStr = napi_util::get_cxx_string(env, stack);
NSLog(@"Worker::OnMessage - exception stack: %s", stackStr.c_str());
}
}
}
return;
}
}
JS_METHOD(Worker::CloseWorker) {
napi_value jsThis;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr);
WorkerImpl* worker = nullptr;
napi_unwrap(env, jsThis, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
throw NativeScriptException("Worker is not initialized.");
return nullptr;
}
if (!worker->IsRunning() || worker->IsClosing()) {
return nullptr;
}
worker->Close();
napi_value oncloseVal;
napi_get_named_property(env, jsThis, "onclose", &oncloseVal);
if (!napi_util::is_of_type(env, oncloseVal, napi_function)) {
return nullptr;
}
napi_value result;
napi_status status = napi_call_function(env, jsThis, oncloseVal, 0, nullptr, &result);
if (status != napi_ok) {
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
if (exception != nullptr && napi_util::is_of_type(env, exception, napi_object)) {
worker->CallOnErrorHandlers(env, exception);
}
}
return nullptr;
}
JS_METHOD(Worker::Terminate) {
napi_value jsThis;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr);
WorkerImpl* worker = nullptr;
napi_unwrap(env, jsThis, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
throw NativeScriptException("Worker is not initialized.");
return nullptr;
}
worker->Terminate();
return nullptr;
}
napi_value Worker::Serialize(napi_env env, napi_value value) {
// Local<Context> context = isolate->GetCurrentContext();
// Local<ObjectTemplate> objTemplate = ObjectTemplate::New(isolate);
// Local<Object> obj;
// bool success = objTemplate->NewInstance(context).ToLocal(&obj);
// tns::Assert(success, isolate);
// success = obj->Set(context, tns::ToV8String(isolate, "data"), value).FromMaybe(false);
// tns::Assert(success, isolate);
// Local<Value> result;
// TryCatch tc(isolate);
// success = v8::JSON::Stringify(context, obj).ToLocal(&result);
// if (!success && tc.HasCaught()) {
// error = tc.Exception();
// return Local<v8::String>();
// }
// tns::Assert(success, isolate);
// return result.As<v8::String>();
return nullptr;
}
void Worker::SetWorkerId(napi_env env, int workerId) {
napi_value global = napi_util::global(env);
// TODO: make this a private property
napi_set_named_property(env, global, "__private_worker_id__",
napi_util::to_js_number(env, workerId));
}
int Worker::GetWorkerId(napi_env env, napi_value global) {
napi_value workerIdVal;
napi_get_named_property(env, global, "__private_worker_id__", &workerIdVal);
if (napi_util::is_of_type(env, workerIdVal, napi_number)) {
return napi_util::get_int32(env, workerIdVal);
}
return -1;
}
} // namespace nativescript