Skip to content

Commit

Permalink
feat: run app from NativeScript initializer instead of static method (#…
Browse files Browse the repository at this point in the history
…137)

Co-authored-by: Dermendzhiev, Teodor (external - Project) <tdermendjievft@gmail.com>
  • Loading branch information
NathanWalker and tdermendjiev committed Mar 7, 2022
1 parent 7c881f5 commit a676ecf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion AppWithModules/Source Files/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main(int argc, char *argv[]) {
config.ArgumentsCount = argc;
config.Arguments = argv;

[NativeScript start:config];
[[NativeScript alloc] initWithConfig: config];

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions NativeScript/NativeScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@interface NativeScript : NSObject

+ (void)start:(Config*)config;
+ (bool)liveSync;
- (instancetype)initWithConfig:(Config*)config;
- (bool)liveSync;

@end
84 changes: 45 additions & 39 deletions NativeScript/NativeScript.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,54 @@ @implementation Config

@implementation NativeScript

static std::shared_ptr<Runtime> runtime_;

+ (void)start:(Config*)config {
RuntimeConfig.BaseDir = [config.BaseDir UTF8String];
if (config.ApplicationPath != nil) {
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:config.ApplicationPath] UTF8String];
} else {
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:@"app"] UTF8String];
std::unique_ptr<Runtime> runtime_;

- (instancetype)initWithConfig:(Config*)config {

if (self = [super init]) {
RuntimeConfig.BaseDir = [config.BaseDir UTF8String];
if (config.ApplicationPath != nil) {
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:config.ApplicationPath] UTF8String];
} else {
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:@"app"] UTF8String];
}
RuntimeConfig.MetadataPtr = [config MetadataPtr];
RuntimeConfig.IsDebug = [config IsDebug];
RuntimeConfig.LogToSystemConsole = [config LogToSystemConsole];

Runtime::Initialize();
runtime_ = std::make_unique<Runtime>();

std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
Isolate* isolate = runtime_->CreateIsolate();
runtime_->Init(isolate);
std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
printf("Runtime initialization took %llims\n", duration);

if (config.IsDebug) {
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
v8_inspector::JsV8InspectorClient* inspectorClient = new v8_inspector::JsV8InspectorClient(runtime_.get());
inspectorClient->init();
inspectorClient->registerModules();
inspectorClient->connect([config ArgumentsCount], [config Arguments]);
}

runtime_->RunMainScript();

CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);

tns::Tasks::Drain();

runtime_.reset();
}
RuntimeConfig.MetadataPtr = [config MetadataPtr];
RuntimeConfig.IsDebug = [config IsDebug];
RuntimeConfig.LogToSystemConsole = [config LogToSystemConsole];

Runtime::Initialize();
runtime_ = std::make_shared<Runtime>();

std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
Isolate* isolate = runtime_->CreateIsolate();
runtime_->Init(isolate);
std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
printf("Runtime initialization took %llims\n", duration);

if (config.IsDebug) {
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
v8_inspector::JsV8InspectorClient* inspectorClient = new v8_inspector::JsV8InspectorClient(runtime_.get());
inspectorClient->init();
inspectorClient->registerModules();
inspectorClient->connect([config ArgumentsCount], [config Arguments]);
}

runtime_->RunMainScript();

CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);

tns::Tasks::Drain();

runtime_.reset();

return self;

}

+ (bool)liveSync {
- (bool)liveSync {
if (runtime_ == nullptr) {
return false;
}
Expand Down

0 comments on commit a676ecf

Please sign in to comment.