Skip to content

Commit 7ded0c3

Browse files
authored
feat: use messaging object to pass message to workers (#233)
1 parent bb441af commit 7ded0c3

File tree

13 files changed

+772
-44
lines changed

13 files changed

+772
-44
lines changed

Diff for: NativeScript/runtime/ConcurrentQueue.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void ConcurrentQueue::Initialize(CFRunLoopRef runLoop, void (*performWork)(void*
1414
CFRunLoopAddSource(this->runLoop_, this->runLoopTasksSource_, kCFRunLoopCommonModes);
1515
}
1616

17-
void ConcurrentQueue::Push(std::string message) {
17+
void ConcurrentQueue::Push(std::shared_ptr<worker::Message> message) {
1818
if (this->runLoopTasksSource_ != nullptr && !CFRunLoopSourceIsValid(this->runLoopTasksSource_)) {
1919
return;
2020
}
@@ -27,12 +27,12 @@ void ConcurrentQueue::Push(std::string message) {
2727
this->SignalAndWakeUp();
2828
}
2929

30-
std::vector<std::string> ConcurrentQueue::PopAll() {
30+
std::vector<std::shared_ptr<worker::Message>> ConcurrentQueue::PopAll() {
3131
std::unique_lock<std::mutex> mlock(this->mutex_);
32-
std::vector<std::string> messages;
32+
std::vector<std::shared_ptr<worker::Message>> messages;
3333

3434
while (!this->messagesQueue_.empty()) {
35-
std::string message = this->messagesQueue_.front();
35+
std::shared_ptr<worker::Message> message = this->messagesQueue_.front();
3636
this->messagesQueue_.pop();
3737
messages.push_back(message);
3838
}

Diff for: NativeScript/runtime/ConcurrentQueue.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
#include <string>
77
#include <queue>
88
#include <mutex>
9+
#include "Message.hpp"
910

1011
namespace tns {
1112

1213
struct ConcurrentQueue {
1314
public:
1415
void Initialize(CFRunLoopRef runLoop, void (*performWork)(void*), void* info);
15-
void Push(std::string message);
16-
std::vector<std::string> PopAll();
16+
void Push(std::shared_ptr<worker::Message> message);
17+
std::vector<std::shared_ptr<worker::Message>> PopAll();
1718
void Terminate();
1819
private:
19-
std::queue<std::string> messagesQueue_;
20+
std::queue<std::shared_ptr<worker::Message>> messagesQueue_;
2021
CFRunLoopSourceRef runLoopTasksSource_ = nullptr;
2122
CFRunLoopRef runLoop_ = nullptr;
2223
bool terminated = false;

Diff for: NativeScript/runtime/DataWrapper.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -654,12 +654,12 @@ class ExtVectorWrapper: public BaseDataWrapper {
654654

655655
class WorkerWrapper: public BaseDataWrapper {
656656
public:
657-
WorkerWrapper(v8::Isolate* mainIsolate, std::function<void (v8::Isolate*, v8::Local<v8::Object> thiz, std::string)> onMessage);
657+
WorkerWrapper(v8::Isolate* mainIsolate, std::function<void (v8::Isolate*, v8::Local<v8::Object> thiz, std::shared_ptr<worker::Message>)> onMessage);
658658

659659
void Start(std::shared_ptr<v8::Persistent<v8::Value>> poWorker, std::function<v8::Isolate* ()> func);
660660
void CallOnErrorHandlers(v8::TryCatch& tc);
661661
void PassUncaughtExceptionFromWorkerToMain(v8::Local<v8::Context> context, v8::TryCatch& tc, bool async = true);
662-
void PostMessage(std::string message);
662+
void PostMessage(std::shared_ptr<worker::Message> message);
663663
void Close();
664664
void Terminate();
665665

@@ -691,7 +691,7 @@ class WorkerWrapper: public BaseDataWrapper {
691691
std::atomic<bool> isTerminating_;
692692
bool isDisposed_;
693693
bool isWeak_;
694-
std::function<void (v8::Isolate*, v8::Local<v8::Object> thiz, std::string)> onMessage_;
694+
std::function<void (v8::Isolate*, v8::Local<v8::Object> thiz, std::shared_ptr<worker::Message>)> onMessage_;
695695
std::shared_ptr<v8::Persistent<v8::Value>> poWorker_;
696696
ConcurrentQueue queue_;
697697
static std::atomic<int> nextId_;

Diff for: NativeScript/runtime/Helpers.h

+51
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,57 @@ void SetConstructorFunction(v8::Isolate* isolate,
345345
SetConstructorFunctionFlag::SET_CLASS_NAME);
346346

347347

348+
template <int N>
349+
inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(
350+
v8::Isolate* isolate,
351+
const char(&data)[N]) {
352+
return OneByteString(isolate, data, N - 1);
353+
}
354+
355+
template <std::size_t N>
356+
inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(
357+
v8::Isolate* isolate,
358+
const std::array<char, N>& arr) {
359+
return OneByteString(isolate, arr.data(), N - 1);
360+
}
361+
362+
class PersistentToLocal {
363+
public:
364+
// If persistent.IsWeak() == false, then do not call persistent.Reset()
365+
// while the returned Local<T> is still in scope, it will destroy the
366+
// reference to the object.
367+
template <class TypeName>
368+
static inline v8::Local<TypeName> Default(
369+
v8::Isolate* isolate,
370+
const v8::PersistentBase<TypeName>& persistent) {
371+
if (persistent.IsWeak()) {
372+
return PersistentToLocal::Weak(isolate, persistent);
373+
} else {
374+
return PersistentToLocal::Strong(persistent);
375+
}
376+
}
377+
378+
// Unchecked conversion from a non-weak Persistent<T> to Local<T>,
379+
// use with care!
380+
//
381+
// Do not call persistent.Reset() while the returned Local<T> is still in
382+
// scope, it will destroy the reference to the object.
383+
template <class TypeName>
384+
static inline v8::Local<TypeName> Strong(
385+
const v8::PersistentBase<TypeName>& persistent) {
386+
// DCHECK(!persistent.IsWeak());
387+
return *reinterpret_cast<v8::Local<TypeName>*>(
388+
const_cast<v8::PersistentBase<TypeName>*>(&persistent));
389+
}
390+
391+
template <class TypeName>
392+
static inline v8::Local<TypeName> Weak(
393+
v8::Isolate* isolate,
394+
const v8::PersistentBase<TypeName>& persistent) {
395+
return v8::Local<TypeName>::New(isolate, persistent);
396+
}
397+
};
398+
348399
}
349400

350401
#endif /* Helpers_h */

0 commit comments

Comments
 (0)