diff --git a/NativeScript/NativeScript.h b/NativeScript/NativeScript.h index a8912188..f9bcc9f1 100644 --- a/NativeScript/NativeScript.h +++ b/NativeScript/NativeScript.h @@ -15,6 +15,10 @@ @interface NativeScript : NSObject - (instancetype)initWithConfig:(Config*)config; +- (void)runScriptString: (NSString*) script runLoop: (BOOL) runLoop; +- (void)restartWithConfig:(Config*)config; +- (void)shutdownRuntime; + /** WARNING: this method does not return in most applications. (UIApplicationMain) */ diff --git a/NativeScript/NativeScript.mm b/NativeScript/NativeScript.mm index 5e7fd101..0aea242c 100644 --- a/NativeScript/NativeScript.mm +++ b/NativeScript/NativeScript.mm @@ -21,10 +21,51 @@ @implementation Config @implementation NativeScript -std::unique_ptr runtime_; +extern char defaultStartOfMetadataSection __asm("section$start$__DATA$__TNSMetadata"); -- (instancetype)initWithConfig:(Config*)config { +- (void)runScriptString: (NSString*) script runLoop: (BOOL) runLoop { + + std::string cppString = std::string([script UTF8String]); + runtime_->RunScript(cppString); + if (runLoop) { + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true); + } + + + tns::Tasks::Drain(); + +} + +std::unique_ptr runtime_; + +- (void)runMainApplication { + runtime_->RunMainScript(); + + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true); + tns::Tasks::Drain(); +} + +- (bool)liveSync { + if (runtime_ == nullptr) { + return false; + } + + Isolate* isolate = runtime_->GetIsolate(); + return tns::LiveSync(isolate); +} + +- (void)shutdownRuntime { + if (RuntimeConfig.IsDebug) { + Console::DetachInspectorClient(); + } + tns::Tasks::ClearTasks(); + if (runtime_ != nullptr) { + runtime_ = nullptr; + } +} + +- (instancetype)initializeWithConfig:(Config*)config { if (self = [super init]) { RuntimeConfig.BaseDir = [config.BaseDir UTF8String]; if (config.ApplicationPath != nil) { @@ -32,7 +73,11 @@ - (instancetype)initWithConfig:(Config*)config { } else { RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:@"app"] UTF8String]; } - RuntimeConfig.MetadataPtr = [config MetadataPtr]; + if (config.MetadataPtr != nil) { + RuntimeConfig.MetadataPtr = [config MetadataPtr]; + } else { + RuntimeConfig.MetadataPtr = &defaultStartOfMetadataSection; + } RuntimeConfig.IsDebug = [config IsDebug]; RuntimeConfig.LogToSystemConsole = [config LogToSystemConsole]; @@ -58,26 +103,19 @@ - (instancetype)initWithConfig:(Config*)config { Console::AttachInspectorClient(inspectorClient); } } - return self; } -- (void)runMainApplication { - runtime_->RunMainScript(); - - CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true); +- (instancetype)initWithConfig:(Config*)config { + return [self initializeWithConfig:config]; +} - tns::Tasks::Drain(); +- (void)restartWithConfig:(Config*)config { + [self shutdownRuntime]; + [self initializeWithConfig:config]; } -- (bool)liveSync { - if (runtime_ == nullptr) { - return false; - } - Isolate* isolate = runtime_->GetIsolate(); - return tns::LiveSync(isolate); -} @end diff --git a/NativeScript/metadata-arm64.bin b/NativeScript/metadata-arm64.bin new file mode 100644 index 00000000..3d1cb1fc Binary files /dev/null and b/NativeScript/metadata-arm64.bin differ diff --git a/NativeScript/runtime/Console.cpp b/NativeScript/runtime/Console.cpp index e3bed68e..c355587c 100644 --- a/NativeScript/runtime/Console.cpp +++ b/NativeScript/runtime/Console.cpp @@ -39,6 +39,10 @@ void Console::AttachInspectorClient(v8_inspector::JsV8InspectorClient* aInspecto inspector = aInspector; } +void Console::DetachInspectorClient() { + inspector = nullptr; +} + void Console::LogCallback(const FunctionCallbackInfo& args) { // TODO: implement 'forceLog' override option like android has, to force logs in prod if desired if (!RuntimeConfig.LogToSystemConsole) { diff --git a/NativeScript/runtime/Console.h b/NativeScript/runtime/Console.h index 9ce0539c..f4b23a7b 100644 --- a/NativeScript/runtime/Console.h +++ b/NativeScript/runtime/Console.h @@ -11,6 +11,7 @@ class Console { public: static void Init(v8::Local context); static void AttachInspectorClient(v8_inspector::JsV8InspectorClient* inspector); + static void DetachInspectorClient(); private: using ConsoleAPIType = v8_inspector::ConsoleAPIType; diff --git a/NativeScript/runtime/ModuleInternal.h b/NativeScript/runtime/ModuleInternal.h index 0b71496c..b81ada9a 100644 --- a/NativeScript/runtime/ModuleInternal.h +++ b/NativeScript/runtime/ModuleInternal.h @@ -10,6 +10,8 @@ class ModuleInternal { public: ModuleInternal(v8::Local context); bool RunModule(v8::Isolate* isolate, std::string path); + void RunScript(v8::Isolate* isolate, std::string script); + private: static void RequireCallback(const v8::FunctionCallbackInfo& info); v8::Local GetRequireFunction(v8::Isolate* isolate, const std::string& dirName); @@ -23,6 +25,8 @@ class ModuleInternal { v8::ScriptCompiler::CachedData* LoadScriptCache(const std::string& path); void SaveScriptCache(const v8::Local script, const std::string& path); std::string GetCacheFileName(const std::string& path); + v8::MaybeLocal RunScriptString(v8::Isolate* isolate, v8::Local context, const std::string script); + std::unique_ptr> requireFunction_; std::unique_ptr> requireFactoryFunction_; diff --git a/NativeScript/runtime/ModuleInternal.mm b/NativeScript/runtime/ModuleInternal.mm index 1fd5196b..59292202 100644 --- a/NativeScript/runtime/ModuleInternal.mm +++ b/NativeScript/runtime/ModuleInternal.mm @@ -283,6 +283,28 @@ return script; } +MaybeLocal ModuleInternal::RunScriptString(Isolate* isolate, Local context, const std::string scriptString) { + ScriptCompiler::CompileOptions options = ScriptCompiler::kNoCompileOptions; + ScriptCompiler::Source source(tns::ToV8String(isolate, scriptString)); + TryCatch tc(isolate); + Local