Skip to content

Commit e8681ff

Browse files
authored
fix: use serial queues and revert string copy changes (#156)
1 parent 4f28eb0 commit e8681ff

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

NativeScript/NativeScript-Prefix.pch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef NativeScript_Prefix_pch
22
#define NativeScript_Prefix_pch
33

4-
#define NATIVESCRIPT_VERSION "8.2.2"
4+
#define NATIVESCRIPT_VERSION "8.2.3-alpha.0"
55

66
#ifdef DEBUG
77
#define SIZEOF_OFF_T 8

NativeScript/inspector/InspectorServer.mm

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@
7878
if (size) {
7979
NSString* payload = [[NSString alloc] initWithData:(NSData*)data encoding:NSUTF16LittleEndianStringEncoding];
8080

81-
if (payload) {
82-
std::string result = [payload UTF8String];
83-
onMessage(result);
84-
}
81+
onMessage([payload UTF8String]);
8582

8683
#pragma clang diagnostic push
8784
#pragma clang diagnostic ignored "-Warc-retain-cycles"

NativeScript/inspector/JsV8InspectorClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
4444
std::vector<std::string> messages_;
4545
bool runningNestedLoops_;
4646
dispatch_queue_t messagesQueue_;
47+
dispatch_queue_t messageLoopQueue_;
4748
dispatch_semaphore_t messageArrived_;
4849
std::function<void (std::string)> sender_;
4950
bool isWaitingForDebugger_;
5051

5152
void enableInspector(int argc, char** argv);
5253
void notify(std::unique_ptr<StringBuffer> message);
5354
void onFrontendConnected(std::function<void (std::string)> sender);
54-
void onFrontendMessageReceived(std::string &message);
55+
void onFrontendMessageReceived(std::string message);
5556
template <class TypeName>
5657
static v8::Local<TypeName> PersistentToLocal(v8::Isolate* isolate, const v8::Persistent<TypeName>& persistent);
5758
std::string PumpMessage();

NativeScript/inspector/JsV8InspectorClient.mm

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@
8282
: runtime_(runtime),
8383
messages_(),
8484
runningNestedLoops_(false) {
85-
this->messagesQueue_ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
85+
this->messagesQueue_ = dispatch_queue_create("NativeScript.v8.inspector.message_queue", DISPATCH_QUEUE_SERIAL);
86+
this->messageLoopQueue_ = dispatch_queue_create("NativeScript.v8.inspector.message_loop_queue", DISPATCH_QUEUE_SERIAL);
8687
this->messageArrived_ = dispatch_semaphore_create(0);
8788
}
8889

@@ -101,22 +102,27 @@
101102
this->disconnect();
102103
}
103104

104-
void JsV8InspectorClient::onFrontendMessageReceived(std::string &message) {
105-
__block std::string strCopy = message;
105+
void JsV8InspectorClient::onFrontendMessageReceived(std::string message) {
106106
dispatch_sync(this->messagesQueue_, ^{
107-
this->messages_.push_back(strCopy);
107+
this->messages_.push_back(message);
108108
dispatch_semaphore_signal(messageArrived_);
109109
});
110110

111111
tns::ExecuteOnMainThread([this, message]() {
112-
dispatch_sync(this->messagesQueue_, ^{
113-
while (this->messages_.size() > 0) {
114-
std::string message = this->PumpMessage();
112+
dispatch_sync(this->messageLoopQueue_, ^{
113+
// prevent execution if we're already pumping messages
114+
if (runningNestedLoops_ && !terminated_) {
115+
return;
116+
};
117+
std::string message;
118+
do {
119+
message = this->PumpMessage();
115120
if (!message.empty()) {
116121
this->dispatchMessage(message);
117122
}
118-
}
123+
} while (!message.empty());
119124
});
125+
120126
});
121127
}
122128

@@ -163,12 +169,20 @@
163169
}
164170

165171
void JsV8InspectorClient::runMessageLoopOnPause(int contextGroupId) {
166-
if (runningNestedLoops_) {
172+
__block auto loopsRunning = false;
173+
dispatch_sync(this->messageLoopQueue_, ^{
174+
loopsRunning = runningNestedLoops_;
175+
terminated_ = false;
176+
if (runningNestedLoops_) {
177+
return;
178+
}
179+
this->runningNestedLoops_ = true;
180+
});
181+
182+
if (loopsRunning) {
167183
return;
168184
}
169-
170-
terminated_ = false;
171-
this->runningNestedLoops_ = true;
185+
172186
bool shouldWait = false;
173187
while (!terminated_) {
174188
std::string message = this->PumpMessage();
@@ -183,16 +197,20 @@
183197
Isolate* isolate = runtime_->GetIsolate();
184198
platform::PumpMessageLoop(platform.get(), isolate, platform::MessageLoopBehavior::kDoNotWait);
185199
if(shouldWait && !terminated_) {
186-
dispatch_semaphore_wait(messageArrived_, dispatch_time(DISPATCH_TIME_NOW, 1000000)); // 1ms in ns
200+
dispatch_semaphore_wait(messageArrived_, dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_MSEC)); // 1ms
187201
}
188202
}
189-
190-
terminated_ = false;
191-
runningNestedLoops_ = false;
203+
204+
dispatch_sync(this->messageLoopQueue_, ^{
205+
terminated_ = false;
206+
runningNestedLoops_ = false;
207+
});
192208
}
193209

194210
void JsV8InspectorClient::quitMessageLoopOnPause() {
195-
terminated_ = true;
211+
dispatch_sync(this->messageLoopQueue_, ^{
212+
terminated_ = true;
213+
});
196214
}
197215

198216
void JsV8InspectorClient::sendResponse(int callId, std::unique_ptr<StringBuffer> message) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@nativescript/ios",
33
"description": "NativeScript Runtime for iOS",
4-
"version": "8.2.2",
4+
"version": "8.2.3-alpha.0",
55
"keywords": [
66
"NativeScript",
77
"iOS",

0 commit comments

Comments
 (0)