-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathWorkerWrapper.mm
More file actions
232 lines (190 loc) · 8.03 KB
/
Copy pathWorkerWrapper.mm
File metadata and controls
232 lines (190 loc) · 8.03 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
#include <Foundation/Foundation.h>
#include "DataWrapper.h"
#include "Helpers.h"
#include "Runtime.h"
#include "Caches.h"
#include "Constants.h"
using namespace v8;
namespace tns {
static NSOperationQueue* workers_ = nil;
__attribute__((constructor))
void staticInitMethod() {
workers_ = [[NSOperationQueue alloc] init];
workers_.maxConcurrentOperationCount = 100;
}
WorkerWrapper::WorkerWrapper(v8::Isolate* mainIsolate, std::function<void (v8::Isolate*, v8::Local<v8::Object> thiz, std::string)> onMessage)
: mainIsolate_(mainIsolate),
workerIsolate_(nullptr),
isRunning_(false),
isClosing_(false),
isTerminating_(false),
isDisposed_(false),
isWeak_(false),
onMessage_(onMessage) {
}
const WrapperType WorkerWrapper::Type() {
return WrapperType::Worker;
}
const int WorkerWrapper::Id() {
return this->workerId_;
}
const bool WorkerWrapper::IsRunning() {
return this->isRunning_;
}
const bool WorkerWrapper::IsClosing() {
return this->isClosing_;
}
const int WorkerWrapper::WorkerId() {
return this->workerId_;
}
void WorkerWrapper::PostMessage(std::string message) {
if (!this->isTerminating_) {
this->queue_.Push(message);
}
}
void WorkerWrapper::Start(std::shared_ptr<Persistent<Value>> poWorker, std::function<Isolate* ()> func) {
this->poWorker_ = poWorker;
this->workerId_ = nextId_.fetch_add(1, std::memory_order_relaxed) + 1;
[workers_ addOperationWithBlock:^{
this->BackgroundLooper(func);
}];
this->isRunning_ = true;
}
void WorkerWrapper::DrainPendingTasks() {
std::vector<std::string> messages = this->queue_.PopAll();
v8::Locker locker(this->workerIsolate_);
Isolate::Scope isolate_scope(this->workerIsolate_);
HandleScope handle_scope(this->workerIsolate_);
Local<Context> context = Caches::Get(this->workerIsolate_)->GetContext();
Local<Object> global = context->Global();
for (std::string message: messages) {
if (this->isTerminating_) {
break;
}
TryCatch tc(this->workerIsolate_);
this->onMessage_(this->workerIsolate_, global, message);
if (tc.HasCaught()) {
this->CallOnErrorHandlers(tc);
}
}
}
void WorkerWrapper::BackgroundLooper(std::function<Isolate* ()> func) {
if (!this->isTerminating_) {
CFRunLoopRef runLoop = CFRunLoopGetCurrent();
this->queue_.Initialize(runLoop, [](void* info) {
WorkerWrapper* w = static_cast<WorkerWrapper*>(info);
w->DrainPendingTasks();
}, this);
this->workerIsolate_ = func();
this->DrainPendingTasks();
// check again as it could terminate before this
if (!this->isTerminating_) {
CFRunLoopRun();
}
}
this->isDisposed_ = true;
Runtime* runtime = Runtime::GetCurrentRuntime();
delete runtime;
}
void WorkerWrapper::Close() {
this->isClosing_ = true;
}
void WorkerWrapper::Terminate() {
// set terminating to true atomically
bool wasTerminating = this->isTerminating_.exchange(true);
if (!wasTerminating) {
if (this->workerIsolate_ != nullptr) {
this->workerIsolate_->TerminateExecution();
}
this->queue_.Terminate();
this->isRunning_ = false;
}
}
void WorkerWrapper::CallOnErrorHandlers(TryCatch& tc) {
if (this->isTerminating_) {
return;
}
Local<Context> context = Caches::Get(this->workerIsolate_)->GetContext();
Local<Object> global = context->Global();
Local<Value> onErrorVal;
bool success = global->Get(context, tns::ToV8String(this->workerIsolate_, "onerror")).ToLocal(&onErrorVal);
Isolate* isolate = context->GetIsolate();
tns::Assert(success, isolate);
if (!onErrorVal.IsEmpty() && onErrorVal->IsFunction()) {
Local<v8::Function> onErrorFunc = onErrorVal.As<v8::Function>();
Local<Value> error = tc.Exception();
Local<Value> args[1] = { error };
Local<Value> result;
TryCatch innerTc(this->workerIsolate_);
success = onErrorFunc->Call(context, v8::Undefined(this->workerIsolate_), 1, args).ToLocal(&result);
if (success && !result.IsEmpty() && result->BooleanValue(this->workerIsolate_)) {
// Do nothing, exception is handled and does not need to be raised to the main thread's onerror handler
return;
}
if (!success && innerTc.HasCaught()) {
this->PassUncaughtExceptionFromWorkerToMain(context, innerTc);
}
this->PassUncaughtExceptionFromWorkerToMain(context, tc);
}
}
void WorkerWrapper::PassUncaughtExceptionFromWorkerToMain(Local<Context> context, TryCatch& tc, bool async) {
Isolate* workerIsolate = context->GetIsolate();
int lineNumber;
bool success = tc.Message()->GetLineNumber(context).To(&lineNumber);
Isolate* isolate = context->GetIsolate();
tns::Assert(success, isolate);
std::string message = tns::ToString(workerIsolate, tc.Message()->Get());
Local<Value> source;
success = tc.Message()->GetScriptResourceName()->ToString(context).ToLocal(&source);
tns::Assert(success, isolate);
std::string src = tns::ToString(workerIsolate, source);
std::string stackTrace = "";
Local<Value> stackTraceVal = tc.StackTrace(context).FromMaybe(Local<Value>());
if (!stackTraceVal.IsEmpty()) {
Local<v8::String> stackTraceStr = stackTraceVal->ToDetailString(context).FromMaybe(Local<v8::String>());
if (!stackTraceStr.IsEmpty()) {
stackTrace = tns::ToString(workerIsolate, stackTraceStr);
}
}
auto runtime = static_cast<Runtime*>(mainIsolate_->GetData(Constants::RUNTIME_SLOT));
if (runtime == nullptr) {
return;
}
tns::ExecuteOnRunLoop(runtime->RuntimeLoop(), [this, message, src, stackTrace, lineNumber]() {
v8::Locker locker(this->mainIsolate_);
Isolate::Scope isolate_scope(this->mainIsolate_);
HandleScope handle_scope(this->mainIsolate_);
Local<Object> worker = this->poWorker_->Get(this->mainIsolate_).As<Object>();
Local<Context> context = Caches::Get(this->mainIsolate_)->GetContext();
Local<Value> onErrorVal;
bool success = worker->Get(context, tns::ToV8String(this->mainIsolate_, "onerror")).ToLocal(&onErrorVal);
tns::Assert(success, this->mainIsolate_);
if (!onErrorVal.IsEmpty() && onErrorVal->IsFunction()) {
Local<v8::Function> onErrorFunc = onErrorVal.As<v8::Function>();
Local<Object> arg = this->ConstructErrorObject(context, message, src, stackTrace, lineNumber);
Local<Value> args[1] = { arg };
Local<Value> result;
TryCatch tc(this->mainIsolate_);
bool success = onErrorFunc->Call(context, v8::Undefined(this->mainIsolate_), 1, args).ToLocal(&result);
if (!success && tc.HasCaught()) {
Local<Value> error = tc.Exception();
Log(@"%s", tns::ToString(this->mainIsolate_, error).c_str());
this->mainIsolate_->ThrowException(error);
}
}
}, async);
}
Local<Object> WorkerWrapper::ConstructErrorObject(Local<Context> context, std::string message, std::string source, std::string stackTrace, int lineNumber) {
Isolate* isolate = context->GetIsolate();
Local<ObjectTemplate> objTemplate = ObjectTemplate::New(isolate);
Local<Object> obj;
bool success = objTemplate->NewInstance(context).ToLocal(&obj);
tns::Assert(success, isolate);
tns::Assert(obj->Set(context, tns::ToV8String(isolate, "message"), tns::ToV8String(isolate, message)).FromMaybe(false), isolate);
tns::Assert(obj->Set(context, tns::ToV8String(isolate, "filename"), tns::ToV8String(isolate, source)).FromMaybe(false), isolate);
tns::Assert(obj->Set(context, tns::ToV8String(isolate, "stackTrace"), tns::ToV8String(isolate, stackTrace)).FromMaybe(false), isolate);
tns::Assert(obj->Set(context, tns::ToV8String(isolate, "lineno"), Number::New(isolate, lineNumber)).FromMaybe(false), isolate);
return obj;
}
std::atomic<int> WorkerWrapper::nextId_(0);
}