GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — inspector: --nativescript-debug-brk implementation · NativeScript/ios@efef41e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • AppWithModules/Source Files
    • main.m
  • NativeScript
    • NativeScript.h
    • NativeScript.mm
    • inspector
      • JsV8InspectorClient.h
      • JsV8InspectorClient.mm
    • runtime
      • Runtime.h
      • Runtime.mm
  • TestApp/Source Files
    • main.m
  • project-template/internal
    • main.m
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎AppWithModules/Source Files/main.m‎

Copy file name to clipboardExpand all lines: AppWithModules/Source Files/main.m
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ int main(int argc, char *argv[]) {
2727
config.SnapshotPtr = startSnapshotPtr;
2828
config.SnapshotSize = snapshotSize;
2929
config.BaseDir = baseDir;
30+
config.ArgumentsCount = argc;
31+
config.Arguments = argv;
3032

3133
[NativeScript start:config];
3234

Collapse file

‎NativeScript/NativeScript.h‎

Copy file name to clipboardExpand all lines: NativeScript/NativeScript.h
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
@property (nonatomic) const char* SnapshotPtr;
88
@property size_t SnapshotSize;
99
@property BOOL IsDebug;
10+
@property int ArgumentsCount;
11+
@property (nonatomic) char** Arguments;
1012

1113
@end
1214

Collapse file

‎NativeScript/NativeScript.mm‎

Copy file name to clipboardExpand all lines: NativeScript/NativeScript.mm
+21-3Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include <Foundation/Foundation.h>
22
#include "NativeScript.h"
3-
#include "runtime/Runtime.h"
4-
#include "runtime/Helpers.h"
3+
#include "inspector/JsV8InspectorClient.h"
54
#include "runtime/RuntimeConfig.h"
5+
#include "runtime/Helpers.h"
6+
#include "runtime/Runtime.h"
7+
#include "runtime/Tasks.h"
68

79
using namespace v8;
810
using namespace tns;
@@ -31,7 +33,23 @@ + (void)start:(Config*)config {
3133

3234
Runtime::Initialize();
3335
runtime_ = new Runtime();
34-
runtime_->InitAndRunMainScript();
36+
37+
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
38+
runtime_->Init();
39+
std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
40+
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
41+
printf("Runtime initialization took %llims\n", duration);
42+
43+
if (config.IsDebug) {
44+
v8_inspector::JsV8InspectorClient* inspectorClient = new v8_inspector::JsV8InspectorClient(runtime_);
45+
inspectorClient->init();
46+
inspectorClient->registerModules();
47+
inspectorClient->connect([config ArgumentsCount], [config Arguments]);
48+
}
49+
50+
runtime_->RunMainScript();
51+
52+
tns::Tasks::Drain();
3553
}
3654

3755
+ (bool)liveSync {
Collapse file

‎NativeScript/inspector/JsV8InspectorClient.h‎

Copy file name to clipboardExpand all lines: NativeScript/inspector/JsV8InspectorClient.h
+8-7Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
#include <vector>
77
#include <map>
88

9-
#include "include/libplatform/libplatform.h"
109
#include "include/v8-inspector.h"
10+
#include "runtime/Runtime.h"
1111

1212
namespace v8_inspector {
1313

1414
class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
1515
public:
16-
JsV8InspectorClient(v8::Platform* platform, v8::Isolate* isolate);
16+
JsV8InspectorClient(tns::Runtime* runtime);
1717
void init();
18-
void connect();
18+
void connect(int argc, char** argv);
1919
void createInspectorSession();
2020
void disconnect();
2121
void dispatchMessage(const std::string& message);
@@ -27,7 +27,9 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
2727
void runMessageLoopOnPause(int contextGroupId) override;
2828
void quitMessageLoopOnPause() override;
2929
v8::Local<v8::Context> ensureDefaultContextInGroup(int contextGroupId) override;
30-
void registerModules(std::function<void(v8::Isolate*, std::string)> runModule);
30+
31+
void scheduleBreak();
32+
void registerModules();
3133

3234
static std::map<std::string, v8::Persistent<v8::Object>*> Domains;
3335
private:
@@ -36,14 +38,13 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
3638
std::unique_ptr<V8Inspector> inspector_;
3739
v8::Persistent<v8::Context> context_;
3840
std::unique_ptr<V8InspectorSession> session_;
39-
v8::Platform* platform_;
40-
v8::Isolate* isolate_;
41+
tns::Runtime* runtime_;
4142
bool terminated_;
4243
std::vector<std::string> messages_;
4344
bool runningNestedLoops_;
4445
dispatch_queue_t messagesQueue_;
4546

46-
void enableInspector();
47+
void enableInspector(int argc, char** argv);
4748
void notify(std::unique_ptr<StringBuffer> message);
4849
template <class TypeName>
4950
static v8::Local<TypeName> PersistentToLocal(v8::Isolate* isolate, const v8::Persistent<TypeName>& persistent);
Collapse file

‎NativeScript/inspector/JsV8InspectorClient.mm‎

Copy file name to clipboardExpand all lines: NativeScript/inspector/JsV8InspectorClient.mm
+99-32Lines changed: 99 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <notify.h>
55
#include <chrono>
66
#include "JsV8InspectorClient.h"
7+
#include "include/libplatform/libplatform.h"
78
#include "src/inspector/v8-inspector-session-impl.h"
89
#include "src/inspector/v8-log-agent-impl.h"
910
#include "Helpers.h"
@@ -28,6 +29,7 @@ id inspectorLock() {
2829
}
2930
static int currentInspectorPort = 0;
3031
static dispatch_io_t inspector_io = nil;
32+
static BOOL isWaitingForDebugger = NO;
3133
static TNSInspectorSendMessageBlock globalSendMessageToFrontend = nullptr;
3234

3335
#define CheckError(retval, handler) \
@@ -212,7 +214,7 @@ static dispatch_source_t createInspectorServer(TNSInspectorFrontendConnectedHand
212214
return listenSource;
213215
}
214216

215-
void JsV8InspectorClient::enableInspector() {
217+
void JsV8InspectorClient::enableInspector(int argc, char** argv) {
216218
__block dispatch_source_t listenSource = nil;
217219
__block dispatch_io_t current_connection_inspector_io = nil;
218220

@@ -252,6 +254,16 @@ static dispatch_source_t createInspectorServer(TNSInspectorFrontendConnectedHand
252254
current_connection_inspector_io = io;
253255
}
254256

257+
if (isWaitingForDebugger) {
258+
isWaitingForDebugger = NO;
259+
CFRunLoopRef runloop = CFRunLoopGetMain();
260+
CFRunLoopPerformBlock(runloop, (__bridge CFTypeRef)(NSRunLoopCommonModes), ^{
261+
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1, false);
262+
this->scheduleBreak();
263+
});
264+
CFRunLoopWakeUp(runloop);
265+
}
266+
255267
return ^(NSString* message, NSError* error) {
256268
if (message != nil) {
257269
dispatch_sync(this->messagesQueue_, ^{
@@ -281,6 +293,27 @@ static dispatch_source_t createInspectorServer(TNSInspectorFrontendConnectedHand
281293

282294
listenSource = createInspectorServer(connectionHandler, ioErrorHandler, clearInspector);
283295

296+
int waitForDebuggerSubscription;
297+
notify_register_dispatch(NOTIFICATION("WaitForDebugger"), &waitForDebuggerSubscription, dispatch_get_main_queue(), ^(int token) {
298+
isWaitingForDebugger = YES;
299+
300+
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 30);
301+
dispatch_after(delay, dispatch_get_main_queue(), ^{
302+
if (isWaitingForDebugger) {
303+
isWaitingForDebugger = NO;
304+
NSLog(@"NativeScript waiting for debugger timeout elapsed. Continuing execution.");
305+
}
306+
});
307+
308+
NSLog(@"NativeScript waiting for debugger.");
309+
CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopDefaultMode, ^{
310+
do {
311+
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
312+
} while (isWaitingForDebugger);
313+
});
314+
CFRunLoopWakeUp(CFRunLoopGetMain());
315+
});
316+
284317
int attachRequestSubscription;
285318
notify_register_dispatch(NOTIFICATION("AttachRequest"), &attachRequestSubscription, dispatch_get_main_queue(), ^(int token) {
286319
clear();
@@ -290,12 +323,34 @@ static dispatch_source_t createInspectorServer(TNSInspectorFrontendConnectedHand
290323
notify_post(NOTIFICATION("ReadyForAttach"));
291324
});
292325

326+
notify_post(NOTIFICATION("AppLaunching"));
327+
328+
for (int i = 1; i < argc; i++) {
329+
BOOL startListening = NO;
330+
BOOL shouldWaitForDebugger = NO;
331+
332+
if (strcmp(argv[i], "--nativescript-debug-brk") == 0) {
333+
shouldWaitForDebugger = YES;
334+
} else if (strcmp(argv[i], "--nativescript-debug-start") == 0) {
335+
startListening = YES;
336+
}
337+
338+
if (startListening || shouldWaitForDebugger) {
339+
notify_post(NOTIFICATION("AttachRequest"));
340+
if (shouldWaitForDebugger) {
341+
notify_post(NOTIFICATION("WaitForDebugger"));
342+
}
343+
344+
break;
345+
}
346+
}
347+
293348
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.5, false);
349+
notify_cancel(waitForDebuggerSubscription);
294350
}
295351

296-
JsV8InspectorClient::JsV8InspectorClient(v8::Platform* platform, Isolate* isolate)
297-
: platform_(platform),
298-
isolate_(isolate),
352+
JsV8InspectorClient::JsV8InspectorClient(tns::Runtime* runtime)
353+
: runtime_(runtime),
299354
messages_(),
300355
runningNestedLoops_(false) {
301356
this->messagesQueue_ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
@@ -306,31 +361,33 @@ static dispatch_source_t createInspectorServer(TNSInspectorFrontendConnectedHand
306361
return;
307362
}
308363

309-
v8::HandleScope handle_scope(isolate_);
364+
Isolate* isolate = runtime_->GetIsolate();
365+
HandleScope handle_scope(isolate);
310366

311-
v8::Local<Context> context = isolate_->GetCurrentContext();
367+
Local<Context> context = isolate->GetCurrentContext();
312368

313-
inspector_ = V8Inspector::create(isolate_, this);
369+
inspector_ = V8Inspector::create(isolate, this);
314370

315371
inspector_->contextCreated(v8_inspector::V8ContextInfo(context, JsV8InspectorClient::contextGroupId, v8_inspector::StringView()));
316372

317-
v8::Persistent<v8::Context> persistentContext(context->GetIsolate(), JsV8InspectorClient::PersistentToLocal(isolate_, context_));
318-
context_.Reset(isolate_, persistentContext);
373+
Persistent<Context> persistentContext(context->GetIsolate(), JsV8InspectorClient::PersistentToLocal(isolate, context_));
374+
context_.Reset(isolate, persistentContext);
319375

320376
this->createInspectorSession();
321377
}
322378

323-
void JsV8InspectorClient::connect() {
379+
void JsV8InspectorClient::connect(int argc, char** argv) {
324380
this->isConnected_ = true;
325-
this->enableInspector();
381+
this->enableInspector(argc, argv);
326382
}
327383

328384
void JsV8InspectorClient::createInspectorSession() {
329385
this->session_ = this->inspector_->connect(JsV8InspectorClient::contextGroupId, this, v8_inspector::StringView());
330386
}
331387

332388
void JsV8InspectorClient::disconnect() {
333-
v8::HandleScope handleScope(isolate_);
389+
Isolate* isolate = runtime_->GetIsolate();
390+
HandleScope handleScope(isolate);
334391

335392
session_->resume();
336393
session_.reset();
@@ -353,7 +410,9 @@ static dispatch_source_t createInspectorServer(TNSInspectorFrontendConnectedHand
353410
this->dispatchMessage(message);
354411
}
355412

356-
while (v8::platform::PumpMessageLoop(platform_, isolate_)) {
413+
Platform* platform = tns::Runtime::GetPlatform();
414+
Isolate* isolate = runtime_->GetIsolate();
415+
while (platform::PumpMessageLoop(platform, isolate)) {
357416
}
358417
}
359418

@@ -393,7 +452,8 @@ static dispatch_source_t createInspectorServer(TNSInspectorFrontendConnectedHand
393452
}
394453

395454
Local<Context> JsV8InspectorClient::ensureDefaultContextInGroup(int contextGroupId) {
396-
v8::Local<v8::Context> context = PersistentToLocal(isolate_, context_);
455+
Isolate* isolate = runtime_->GetIsolate();
456+
Local<Context> context = PersistentToLocal(isolate, context_);
397457
return context;
398458
}
399459

@@ -410,43 +470,50 @@ static dispatch_source_t createInspectorServer(TNSInspectorFrontendConnectedHand
410470
}
411471

412472
template<class TypeName>
413-
inline v8::Local<TypeName> StrongPersistentToLocal(const v8::Persistent<TypeName>& persistent) {
414-
return *reinterpret_cast<v8::Local<TypeName> *>(const_cast<v8::Persistent<TypeName> *>(&persistent));
473+
inline Local<TypeName> StrongPersistentToLocal(const Persistent<TypeName>& persistent) {
474+
return *reinterpret_cast<Local<TypeName> *>(const_cast<Persistent<TypeName> *>(&persistent));
415475
}
416476

417477
template<class TypeName>
418-
inline v8::Local<TypeName> WeakPersistentToLocal(v8::Isolate* isolate, const v8::Persistent<TypeName>& persistent) {
419-
return v8::Local<TypeName>::New(isolate, persistent);
478+
inline Local<TypeName> WeakPersistentToLocal(Isolate* isolate, const Persistent<TypeName>& persistent) {
479+
return Local<TypeName>::New(isolate, persistent);
420480
}
421481

422482
template<class TypeName>
423-
inline v8::Local<TypeName> JsV8InspectorClient::PersistentToLocal(v8::Isolate* isolate, const v8::Persistent<TypeName>& persistent) {
483+
inline Local<TypeName> JsV8InspectorClient::PersistentToLocal(Isolate* isolate, const Persistent<TypeName>& persistent) {
424484
if (persistent.IsWeak()) {
425485
return WeakPersistentToLocal(isolate, persistent);
426486
} else {
427487
return StrongPersistentToLocal(persistent);
428488
}
429489
}
430490

431-
void JsV8InspectorClient::registerModules(std::function<void(v8::Isolate*, std::string)> runModule) {
432-
HandleScope scope(isolate_);
433-
Local<Context> context = isolate_->GetCurrentContext();
491+
void JsV8InspectorClient::scheduleBreak() {
492+
Isolate* isolate = runtime_->GetIsolate();
493+
HandleScope scope(isolate);
494+
this->session_->schedulePauseOnNextStatement(StringView(), StringView());
495+
}
496+
497+
void JsV8InspectorClient::registerModules() {
498+
Isolate* isolate = runtime_->GetIsolate();
499+
HandleScope scope(isolate);
500+
Local<Context> context = isolate->GetCurrentContext();
434501
Local<Object> global = context->Global();
435-
Local<Object> inspectorObject = Object::New(isolate_);
502+
Local<Object> inspectorObject = Object::New(isolate);
436503

437-
assert(global->Set(context, tns::ToV8String(isolate_, "__inspector"), inspectorObject).FromMaybe(false));
438-
v8::Local<v8::Function> func;
504+
assert(global->Set(context, tns::ToV8String(isolate, "__inspector"), inspectorObject).FromMaybe(false));
505+
Local<v8::Function> func;
439506
bool success = v8::Function::New(context, registerDomainDispatcherCallback).ToLocal(&func);
440-
assert(success && global->Set(context, tns::ToV8String(isolate_, "__registerDomainDispatcher"), func).FromMaybe(false));
507+
assert(success && global->Set(context, tns::ToV8String(isolate, "__registerDomainDispatcher"), func).FromMaybe(false));
441508

442-
Local<External> data = External::New(isolate_, this);
509+
Local<External> data = External::New(isolate, this);
443510
success = v8::Function::New(context, inspectorSendEventCallback, data).ToLocal(&func);
444-
assert(success && global->Set(context, tns::ToV8String(isolate_, "__inspectorSendEvent"), func).FromMaybe(false));
511+
assert(success && global->Set(context, tns::ToV8String(isolate, "__inspectorSendEvent"), func).FromMaybe(false));
445512

446513
success = v8::Function::New(context, inspectorTimestampCallback).ToLocal(&func);
447-
assert(success && global->Set(context, tns::ToV8String(isolate_, "__inspectorTimestamp"), func).FromMaybe(false));
514+
assert(success && global->Set(context, tns::ToV8String(isolate, "__inspectorTimestamp"), func).FromMaybe(false));
448515

449-
runModule(isolate_, "inspector_modules");
516+
runtime_->RunModule("inspector_modules");
450517
}
451518

452519
void JsV8InspectorClient::registerDomainDispatcherCallback(const FunctionCallbackInfo<Value>& args) {
@@ -484,15 +551,15 @@ static dispatch_source_t createInspectorServer(TNSInspectorFrontendConnectedHand
484551
client->dispatchMessage(message);
485552
}
486553

487-
void JsV8InspectorClient::inspectorTimestampCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
554+
void JsV8InspectorClient::inspectorTimestampCallback(const FunctionCallbackInfo<Value>& args) {
488555
Isolate* isolate = args.GetIsolate();
489556
double timestamp = std::chrono::seconds(std::chrono::seconds(std::time(NULL))).count();
490557
Local<Number> result = Number::New(isolate, timestamp);
491558
args.GetReturnValue().Set(result);
492559
}
493560

494561
int JsV8InspectorClient::contextGroupId = 1;
495-
std::map<std::string, v8::Persistent<v8::Object>*> JsV8InspectorClient::Domains;
562+
std::map<std::string, Persistent<Object>*> JsV8InspectorClient::Domains;
496563

497564
}
498565

Collapse file

‎NativeScript/runtime/Runtime.h‎

Copy file name to clipboardExpand all lines: NativeScript/runtime/Runtime.h
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,25 @@ class Runtime {
1212
public:
1313
Runtime();
1414
void Init();
15-
void InitAndRunMainScript();
15+
void RunMainScript();
1616
void RunScript(std::string file, v8::TryCatch& tc);
1717
v8::Isolate* GetIsolate();
1818

1919
const int WorkerId();
2020

2121
void SetWorkerId(int workerId);
2222

23+
void RunModule(const std::string moduleName);
24+
2325
static void Initialize();
2426

2527
static Runtime* GetCurrentRuntime() {
2628
return currentRuntime_;
2729
}
30+
31+
static v8::Platform* GetPlatform() {
32+
return platform_;
33+
}
2834
private:
2935
static thread_local Runtime* currentRuntime_;
3036
static v8::Platform* platform_;

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.