Repository navigation
Expand file tree
/
Copy pathDataWrapper.h
More file actions
739 lines (586 loc) · 24.5 KB
/
Copy pathDataWrapper.h
File metadata and controls
739 lines (586 loc) · 24.5 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#ifndef DataWrapper_h
#define DataWrapper_h
#include <objc/runtime.h>
#include <functional>
#include <mutex>
#include <optional>
#include <thread>
#include "Common.h"
#include "ConcurrentQueue.h"
#include "Metadata.h"
#include "libffi.h"
namespace v8_inspector {
class WorkerInspectorClient;
}
namespace tns {
class PrimitiveDataWrapper;
struct ObjectWeakCallbackState;
class EventLoop;
enum class WrapperType {
Base = 1 << 0,
Primitive = 1 << 1,
Enum = 1 << 2,
Struct = 1 << 3,
StructType = 1 << 4,
ObjCAllocObject = 1 << 5,
ObjCObject = 1 << 6,
ObjCClass = 1 << 7,
ObjCProtocol = 1 << 8,
Function = 1 << 9,
AnonymousFunction = 1 << 10,
Block = 1 << 11,
Reference = 1 << 12,
ReferenceType = 1 << 13,
Pointer = 1 << 14,
PointerType = 1 << 15,
FunctionReference = 1 << 16,
FunctionReferenceType = 1 << 17,
ExtVector = 1 << 18,
Worker = 1 << 19,
UnmanagedType = 1 << 20,
};
struct V8Args {
public:
virtual v8::Local<v8::Value> operator[](int i) const = 0;
virtual size_t Length() const = 0;
};
struct V8FunctionCallbackArgs : public V8Args {
public:
V8FunctionCallbackArgs(const v8::FunctionCallbackInfo<v8::Value>& info)
: info_(info) {}
v8::Local<v8::Value> operator[](int i) const override {
return this->info_[i];
}
size_t Length() const override { return this->info_.Length(); }
private:
const v8::FunctionCallbackInfo<v8::Value>& info_;
};
struct V8VectorArgs : public V8Args {
public:
V8VectorArgs(const std::vector<v8::Local<v8::Value>>& args) : args_(args) {}
v8::Local<v8::Value> operator[](int i) const override {
return this->args_[i];
}
size_t Length() const override { return this->args_.size(); }
private:
const std::vector<v8::Local<v8::Value>>& args_;
};
struct V8SimpleValueArgs : public V8Args {
public:
V8SimpleValueArgs(v8::Local<v8::Value>& value) : value_(value) {}
v8::Local<v8::Value> operator[](int i) const override { return this->value_; }
size_t Length() const override { return 1; }
private:
v8::Local<v8::Value>& value_;
};
struct V8EmptyValueArgs : public V8Args {
public:
v8::Local<v8::Value> operator[](int i) const override {
return v8::Local<v8::Value>();
}
size_t Length() const override { return 0; }
};
struct StructField {
public:
StructField(ptrdiff_t offset, ffi_type* ffiType, std::string name,
const TypeEncoding* encoding)
: offset_(offset), ffiType_(ffiType), name_(name), encoding_(encoding) {}
ptrdiff_t Offset() const { return this->offset_; }
ffi_type* FFIType() const { return this->ffiType_; }
const std::string& Name() const { return this->name_; }
const TypeEncoding* Encoding() const { return this->encoding_; }
private:
ptrdiff_t offset_;
ffi_type* ffiType_;
std::string name_;
const TypeEncoding* encoding_;
};
// Owned by FFICall's process-wide struct-info cache and immutable once
// built, so every consumer (wrappers, call sites) holds a pointer or
// reference into that cache instead of a copy. Non-copyable to keep it that
// way: a copy would make the wrappers' pointers dangle.
struct StructInfo {
public:
StructInfo(std::string name, ffi_type* ffiType,
std::vector<StructField> fields)
: name_(std::move(name)), ffiType_(ffiType), fields_(std::move(fields)) {}
StructInfo(const StructInfo&) = delete;
StructInfo& operator=(const StructInfo&) = delete;
const std::string& Name() const { return this->name_; }
ffi_type* FFIType() const { return this->ffiType_; }
const std::vector<StructField>& Fields() const { return this->fields_; }
private:
std::string name_;
ffi_type* ffiType_;
std::vector<StructField> fields_;
};
class BaseDataWrapper {
public:
BaseDataWrapper() : gcProtected_(false) {}
virtual ~BaseDataWrapper() = default;
const virtual WrapperType Type() { return WrapperType::Base; }
bool IsGcProtected() { return this->gcProtected_; }
void GcProtect() { this->gcProtected_ = true; }
void GcUnprotect() { this->gcProtected_ = false; }
private:
bool gcProtected_;
};
class EnumDataWrapper : public BaseDataWrapper {
public:
EnumDataWrapper(std::string jsCode)
: jsCode_(jsCode), hasCachedValue_(false), cachedValue_(0) {}
const WrapperType Type() { return WrapperType::Enum; }
std::string JSCode() { return jsCode_; }
// Numeric value memoized by Interop::WriteValue so the enum's JS snippet is
// compiled and evaluated at most once per wrapper.
bool HasCachedValue() const { return hasCachedValue_; }
double CachedValue() const { return cachedValue_; }
void SetCachedValue(double value) {
cachedValue_ = value;
hasCachedValue_ = true;
}
private:
std::string jsCode_;
bool hasCachedValue_;
double cachedValue_;
};
class PointerTypeWrapper : public BaseDataWrapper {
public:
const WrapperType Type() { return WrapperType::PointerType; }
};
class PointerWrapper : public BaseDataWrapper {
public:
PointerWrapper(void* data) : data_(data), isAdopted_(false) {}
const WrapperType Type() { return WrapperType::Pointer; }
void* Data() const { return this->data_; }
void SetData(void* data) { this->data_ = data; }
bool IsAdopted() const { return this->isAdopted_; }
void SetAdopted(bool value) { this->isAdopted_ = value; }
private:
void* data_;
bool isAdopted_;
};
class ReferenceTypeWrapper : public BaseDataWrapper {
public:
const WrapperType Type() { return WrapperType::ReferenceType; }
};
class ReferenceWrapper : public BaseDataWrapper {
public:
ReferenceWrapper(BaseDataWrapper* typeWrapper,
v8::Persistent<v8::Value>* value)
: typeWrapper_(typeWrapper),
value_(value),
encoding_(nullptr),
data_(nullptr),
disposeData_(false) {}
~ReferenceWrapper() {
if (this->value_ != nullptr) {
value_->Reset();
delete value_;
}
if (this->data_ != nullptr && this->disposeData_) {
std::free(this->data_);
}
}
const WrapperType Type() { return WrapperType::Reference; }
BaseDataWrapper* TypeWrapper() { return this->typeWrapper_; }
v8::Persistent<v8::Value>* Value() { return this->value_; }
void SetValue(v8::Persistent<v8::Value>* value) {
if (this->value_ != nullptr) {
this->value_->Reset();
}
this->value_ = value;
}
const TypeEncoding* Encoding() { return this->encoding_; }
void SetEncoding(const TypeEncoding* encoding) { this->encoding_ = encoding; }
void* Data() const { return this->data_; }
void SetData(void* data, bool disposeData = false) {
if (this->data_ != nullptr && this->disposeData_) {
std::free(this->data_);
}
this->data_ = data;
this->disposeData_ = disposeData;
}
private:
BaseDataWrapper* typeWrapper_;
v8::Persistent<v8::Value>* value_;
const TypeEncoding* encoding_;
void* data_;
bool disposeData_;
};
class PrimitiveDataWrapper : public BaseDataWrapper {
public:
PrimitiveDataWrapper(size_t size, const TypeEncoding* typeEncoding,
bool autoDeleteEncoding)
: size_(size),
typeEncoding_(typeEncoding),
autoDeleteEncoding_(autoDeleteEncoding) {}
~PrimitiveDataWrapper() {
if (autoDeleteEncoding_) {
std::free((struct TypeEncoding*)typeEncoding_);
}
}
const WrapperType Type() { return WrapperType::Primitive; }
size_t Size() { return this->size_; }
const TypeEncoding* TypeEncoding() { return this->typeEncoding_; }
private:
size_t size_;
const struct TypeEncoding* typeEncoding_;
bool autoDeleteEncoding_;
};
class StructTypeWrapper : public BaseDataWrapper {
public:
StructTypeWrapper(const struct StructInfo& structInfo)
: structInfo_(&structInfo) {}
const WrapperType Type() { return WrapperType::StructType; }
const struct StructInfo& StructInfo() const { return *this->structInfo_; }
private:
const struct StructInfo* structInfo_;
};
class StructWrapper : public StructTypeWrapper {
public:
StructWrapper(const struct StructInfo& structInfo, void* data,
std::shared_ptr<v8::Persistent<v8::Value>> parent)
: StructTypeWrapper(structInfo),
data_(data),
childCount_(0),
parent_(parent) {}
const WrapperType Type() { return WrapperType::Struct; }
void* Data() const { return this->data_; }
std::shared_ptr<v8::Persistent<v8::Value>> Parent() { return this->parent_; }
void IncrementChildren() { this->childCount_++; }
void DecrementChildren() { this->childCount_--; }
int ChildCount() { return this->childCount_; }
private:
void* data_;
int childCount_;
std::shared_ptr<v8::Persistent<v8::Value>> parent_;
};
class ObjCAllocDataWrapper : public BaseDataWrapper {
public:
ObjCAllocDataWrapper(Class klass) : klass_(klass) {}
const WrapperType Type() { return WrapperType::ObjCAllocObject; }
Class Klass() { return this->klass_; }
private:
Class klass_;
};
class UnmanagedTypeWrapper : public BaseDataWrapper {
public:
UnmanagedTypeWrapper(uint8_t* data, const TypeEncoding* typeEncoding)
: data_(data), typeEncoding_(typeEncoding), valueTaken_(false) {}
const WrapperType Type() { return WrapperType::UnmanagedType; }
uint8_t* Data() {
this->valueTaken_ = true;
return this->data_;
}
const TypeEncoding* TypeEncoding() { return this->typeEncoding_; }
bool ValueTaken() { return this->valueTaken_; }
private:
uint8_t* data_;
const tns::TypeEncoding* typeEncoding_;
bool valueTaken_;
};
class ObjCDataWrapper : public BaseDataWrapper {
public:
ObjCDataWrapper(id data, const TypeEncoding* typeEncoding = nullptr)
: data_(data),
typeEncoding_(typeEncoding),
klass_(object_getClass(data)) {}
const WrapperType Type() { return WrapperType::ObjCObject; }
id Data() { return this->data_; }
// True for the claim a collection adapter attaches to the plain JS object it
// was built from. The adapter owns that claim exclusively -- it must stay
// deletable from the adapter's -dealloc even after isolate teardown -- so no
// other retirement path may free it.
bool IsAdapterClaim() { return this->adapterClaim_; }
void MarkAdapterClaim() { this->adapterClaim_ = true; }
const TypeEncoding* TypeEncoding() { return this->typeEncoding_; }
// The class Data() had when this wrapper was built. Data() alone cannot tell
// whether the wrapper still describes the object living at that address, so
// anything keyed on the raw pointer needs this to detect a recycled slot.
Class Klass() { return this->klass_; }
private:
bool adapterClaim_ = false;
id data_;
const tns::TypeEncoding* typeEncoding_;
Class klass_;
};
class ObjCClassWrapper : public BaseDataWrapper {
public:
ObjCClassWrapper(Class klazz, bool extendedClass = false)
: klass_(klazz), extendedClass_(extendedClass) {}
const WrapperType Type() { return WrapperType::ObjCClass; }
Class Klass() { return this->klass_; }
bool ExtendedClass() { return this->extendedClass_; }
private:
Class klass_;
bool extendedClass_;
};
class ObjCProtocolWrapper : public BaseDataWrapper {
public:
ObjCProtocolWrapper(Protocol* proto, const ProtocolMeta* protoMeta)
: proto_(proto), protoMeta_(protoMeta) {}
const WrapperType Type() { return WrapperType::ObjCProtocol; }
Protocol* Proto() { return this->proto_; }
const ProtocolMeta* ProtoMeta() { return this->protoMeta_; }
private:
Protocol* proto_;
const ProtocolMeta* protoMeta_;
};
class FunctionWrapper : public BaseDataWrapper {
public:
FunctionWrapper(const FunctionMeta* meta) : meta_(meta) {}
const WrapperType Type() { return WrapperType::Function; }
const FunctionMeta* Meta() { return this->meta_; }
private:
const FunctionMeta* meta_;
};
class AnonymousFunctionWrapper : public BaseDataWrapper {
public:
AnonymousFunctionWrapper(void* functionPointer,
const TypeEncoding* parametersEncoding,
size_t parametersCount)
: data_(functionPointer), parametersEncoding_(parametersEncoding) {}
const WrapperType Type() { return WrapperType::AnonymousFunction; }
void* Data() { return this->data_; }
const TypeEncoding* ParametersEncoding() { return this->parametersEncoding_; }
private:
void* data_;
const TypeEncoding* parametersEncoding_;
};
class BlockWrapper : public BaseDataWrapper {
public:
BlockWrapper(void* block, const TypeEncoding* typeEncoding, bool ownsBlock)
: block_(block), typeEncoding_(typeEncoding), ownsBlock_(ownsBlock) {}
const WrapperType Type() { return WrapperType::Block; }
void* Block() { return this->block_; }
const TypeEncoding* Encodings() { return this->typeEncoding_; }
bool OwnsBlock() { return this->ownsBlock_; }
private:
void* block_;
const TypeEncoding* typeEncoding_;
bool ownsBlock_;
};
class FunctionReferenceTypeWrapper : public BaseDataWrapper {
public:
const WrapperType Type() { return WrapperType::FunctionReferenceType; }
};
class FunctionReferenceWrapper : public BaseDataWrapper {
public:
FunctionReferenceWrapper(std::shared_ptr<v8::Persistent<v8::Value>> function)
: function_(function), data_(nullptr), encoding_(nullptr) {}
const WrapperType Type() { return WrapperType::FunctionReference; }
std::shared_ptr<v8::Persistent<v8::Value>> Function() {
return this->function_;
}
void* Data() const { return this->data_; }
const TypeEncoding* Encoding() const { return this->encoding_; }
void SetData(void* data, const TypeEncoding* encoding = nullptr) {
this->data_ = data;
this->encoding_ = encoding;
}
private:
std::shared_ptr<v8::Persistent<v8::Value>> function_;
void* data_;
const TypeEncoding* encoding_;
};
class ExtVectorWrapper : public BaseDataWrapper {
public:
ExtVectorWrapper(void* data, ffi_type* ffiType,
const TypeEncoding* innerTypeEncoding,
const TypeEncoding* typeEncoding)
: data_(data),
ffiType_(ffiType),
innerTypeEncoding_(innerTypeEncoding),
typeEncoding_(typeEncoding) {}
const WrapperType Type() { return WrapperType::ExtVector; }
void* Data() { return this->data_; }
ffi_type* FFIType() { return this->ffiType_; }
const TypeEncoding* InnerTypeEncoding() { return this->innerTypeEncoding_; }
const TypeEncoding* TypeEncoding() { return this->typeEncoding_; }
private:
void* data_;
ffi_type* ffiType_;
const struct TypeEncoding* innerTypeEncoding_;
const struct TypeEncoding* typeEncoding_;
};
class WorkerWrapper : public BaseDataWrapper {
public:
WorkerWrapper(v8::Isolate* mainIsolate,
std::function<void(v8::Isolate*, v8::Local<v8::Object> thiz,
std::shared_ptr<worker::Message>)>
onMessage);
// Debugger support (no-ops in release builds). CreateInspector runs on the
// worker thread after the worker Runtime is initialized; DestroyInspector
// runs on the worker thread during teardown, before the Runtime is deleted.
void CreateInspector(v8::Isolate* isolate, const std::string& scriptPath);
void DestroyInspector();
// `func` runs on the worker thread: it creates the worker's runtime, hands
// the isolate to PublishIsolate once the runtime is initialized, and runs
// the entry script.
void Start(std::shared_ptr<v8::Persistent<v8::Value>> poWorker,
std::function<void()> func,
std::optional<int> qualityOfService = std::nullopt);
// Worker thread, once the runtime is initialized and before the entry script
// runs. From here Terminate() reaches V8: it interrupts whatever JS the
// worker runs, the entry script included, and flags the module pumps so a
// parked graph stops waiting. A terminate() that landed earlier is honored by
// the caller checking IsTerminating() right after this, before any app code.
void PublishIsolate(v8::Isolate* isolate);
// Both reporters take the isolate from their caller, which is running on
// it: they are reachable while the entry script is still evaluating.
void CallOnErrorHandlers(v8::Isolate* isolate, v8::TryCatch& tc);
// Reports a rejected entry-evaluation promise. A rejection carries a reason
// rather than a TryCatch, so it cannot go through CallOnErrorHandlers, but it
// follows the same web order: the worker scope's `onerror` first, then — only
// if that did not handle it — the parent's Worker error event.
void ReportEntryEvaluationRejection(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Value> reason);
void PassUncaughtExceptionFromWorkerToMain(v8::Local<v8::Context> context,
v8::TryCatch& tc,
bool async = true);
// Overload to pass a pre-built error payload when a TryCatch isn't available
// Note: this overload accepts only primitive types to avoid passing V8
// handles across isolates/threads.
void PassUncaughtExceptionFromWorkerToMain(const std::string& message,
const std::string& source,
const std::string& stackTrace,
int lineNumber, bool async = true);
// Forwards a drained unhandled promise rejection to the main isolate's
// worker.onerror, guarded against teardown. Shares marshaling with the
// exception overload above.
void PassUncaughtRejectionToMain(const std::string& message,
const std::string& source,
const std::string& stackTrace,
int lineNumber, bool async = true);
void PostMessage(std::shared_ptr<worker::Message> message);
// WHATWG parity: the worker's implicit port message queue starts disabled;
// Worker.mm calls this once the entry script has finished evaluating
// (including after a pending top-level await settles). From then on every
// buffered and future message dispatches whether or not a handler exists —
// a handler installed later (e.g. from a timer) misses earlier messages,
// exactly as on the web.
void EnableMessageQueue();
void Close();
void Terminate();
// Arms the near-heap-limit callback on the worker's own isolate. Runs on the
// worker thread right after the isolate exists; without it an isolate that
// reaches its heap cap aborts the whole process instead of surfacing as an
// error on the parent's Worker object. `maxOldGenerationSizeBytes` is only
// used to name the cap in the message forwarded to the parent.
void WatchHeapLimit(v8::Isolate* isolate, const std::string& scriptPath,
std::optional<size_t> maxOldGenerationSizeBytes);
// The JS Worker object is a GC root from a successful start until the worker
// ends, so a running worker is reachable the way a browser's is rather than
// depending on its finalizer to keep it. Both of these run on the main
// isolate's thread only -- they re-arm that isolate's global handle -- and
// the unroot is idempotent, so an end reached by more than one path re-arms
// the finalizer once.
void RootWorkerObject();
void UnrootWorkerObject();
// Dispatches the end-of-worker event and unroots. Main isolate's thread,
// with the isolate entered and locked by the caller.
void EndWrapperLifetime();
~WorkerWrapper();
const WrapperType Type();
const int Id();
const bool IsRunning();
const bool IsClosing();
// Set by Terminate() from any thread, by the near-heap-limit callback, and by
// the worker thread itself once a close() takes effect. One-way.
const bool IsTerminating();
const int WorkerId();
const inline v8::Isolate* GetMainIsolate() { return mainIsolate_; }
// The only route from the worker thread to the parent: see mainLoop_.
std::weak_ptr<EventLoop> MainLoop() const { return mainLoop_; }
// Deletion is decided by who still holds the wrapper. The parent's Worker
// object holds it from construction; Start() adds the worker thread. Each
// side lets go exactly once, with a compare-and-swap out of Holders::Both,
// so exactly one of them finds itself the last holder and deletes.
//
// Parent's thread, during the final disposal of the parent isolate. Returns
// true when the worker thread is already done with the wrapper (or never
// had it), which leaves the delete to the caller; false when the worker
// thread still uses it and deletes it when it ends.
bool ReleaseFromParent();
// Parent's thread, from the Worker object's finalizer. True when the worker
// thread is done with the wrapper (or never had it), so the caller may
// delete; nothing changes when false.
bool HeldByParentOnly() const;
// Worker thread, as its last touch of the wrapper and after it removed the
// wrapper's Caches::Workers entry. Deletes the wrapper when the parent has
// already let go; otherwise the parent may delete it from here on.
void ReleaseFromWorkerThread();
private:
enum class Holders : uint8_t { Parent, Both, WorkerThread };
v8::Isolate* mainIsolate_;
// Written by the worker thread only: published by PublishIsolate once the
// worker's runtime is initialized and before its entry script runs,
// withdrawn before the runtime is deleted. Any other thread reads and uses
// it under workerIsolateMutex_. Null while the runtime is being set up, so a
// Terminate() in that window cannot interrupt the builtins Runtime::Init
// evaluates; the startup function checks the terminating flag right after
// publishing instead.
v8::Isolate* workerIsolate_;
std::mutex workerIsolateMutex_;
std::atomic<bool> isRunning_;
std::atomic<bool> isClosing_;
std::atomic<bool> isTerminating_;
// The worker thread has started tearing down. Worker thread only; who
// deletes the wrapper is holders_, not this.
std::atomic<bool> isDisposed_;
std::atomic<Holders> holders_;
// False until the entry script has finished evaluating (EnableMessageQueue);
// DrainPendingTasks leaves the queue untouched while disabled.
std::atomic<bool> messagesEnabled_;
std::function<void(v8::Isolate*, v8::Local<v8::Object> thiz,
std::shared_ptr<worker::Message>)>
onMessage_;
std::shared_ptr<v8::Persistent<v8::Value>> poWorker_;
// The parent's event loop, taken on the parent's thread at construction.
// Every worker-thread post to the parent goes through it and never through
// the parent isolate: the parent runtime may be mid-teardown or its isolate
// already disposed when the post runs, whereas a loop that has shut down
// drops the post, and an expired pointer means the parent is gone entirely.
std::weak_ptr<EventLoop> mainLoop_;
ConcurrentQueue queue_;
static std::atomic<int> nextId_;
int workerId_;
// Owned by the worker thread; inspectorMutex_ makes Terminate() (main
// thread) and DestroyInspector() (worker thread) agree on liveness.
v8_inspector::WorkerInspectorClient* inspector_ = nullptr;
std::mutex inspectorMutex_;
// The isolate the near-heap-limit callback is armed on. Worker thread only.
// Kept apart from workerIsolate_ because the callback is armed before the
// isolate is published and can fire during Runtime::Init, and because
// removing a callback V8 never had registered is fatal: non-null here means
// exactly "armed, remove at teardown".
v8::Isolate* heapLimitIsolate_ = nullptr;
std::string heapLimitMessage_;
std::string heapLimitSource_;
std::atomic<bool> heapLimitExceeded_{false};
// Runs on the worker thread from inside a GC, where no JS may run and no
// handle may be created.
static size_t OnNearHeapLimit(void* data, size_t current_heap_limit,
size_t initial_heap_limit);
// Parked while the Worker object is rooted, so the unroot can re-arm the very
// finalizer ObjectManager::Register installed. Main isolate's thread only.
ObjectWeakCallbackState* weakCallbackState_ = nullptr;
bool workerObjectRooted_ = false;
// Cleared by the destructor, so a task posted from the worker thread can tell
// whether this wrapper still exists once it reaches the main isolate. The
// parent deletes the wrapper with that isolate locked, which is what the task
// takes before reading this. The worker thread only deletes it after the
// parent's final disposal, which follows the shutdown of the loop such a
// task would run on.
std::shared_ptr<std::atomic<WorkerWrapper*>> selfRef_;
void BackgroundLooper(std::function<void()> func);
void DrainPendingTasks();
void ForwardErrorPayloadToMain(const std::string& message,
const std::string& source,
const std::string& stackTrace, int lineNumber,
bool async);
};
} // namespace tns
#endif /* DataWrapper_h */