From cbef342ba3b6375b667e335e76a3b165af94a4be Mon Sep 17 00:00:00 2001 From: Teodor Dermendjiev Date: Wed, 1 Mar 2023 08:32:32 +0200 Subject: [PATCH 1/8] Add runScriptString public method --- NativeScript/NativeScript.h | 3 +++ NativeScript/NativeScript.mm | 13 +++++++++++++ NativeScript/runtime/ModuleInternal.h | 3 +++ NativeScript/runtime/ModuleInternal.mm | 20 ++++++++++++++++++++ NativeScript/runtime/Runtime.h | 2 ++ NativeScript/runtime/Runtime.mm | 9 +++++++++ 6 files changed, 50 insertions(+) diff --git a/NativeScript/NativeScript.h b/NativeScript/NativeScript.h index a8912188..d0a2240b 100644 --- a/NativeScript/NativeScript.h +++ b/NativeScript/NativeScript.h @@ -15,6 +15,9 @@ @interface NativeScript : NSObject - (instancetype)initWithConfig:(Config*)config; + +- (void)runScriptString: (NSString*) script runLoop: (BOOL) runLoop; + /** WARNING: this method does not return in most applications. (UIApplicationMain) */ diff --git a/NativeScript/NativeScript.mm b/NativeScript/NativeScript.mm index 806b521f..50db9eeb 100644 --- a/NativeScript/NativeScript.mm +++ b/NativeScript/NativeScript.mm @@ -59,6 +59,19 @@ - (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(); + +} + - (void)runMainApplication { runtime_->RunMainScript(); diff --git a/NativeScript/runtime/ModuleInternal.h b/NativeScript/runtime/ModuleInternal.h index 0b71496c..23d358f0 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); @@ -18,6 +20,7 @@ class ModuleInternal { v8::Local WrapModuleContent(v8::Isolate* isolate, const std::string& path); v8::Local LoadModule(v8::Isolate* isolate, const std::string& modulePath, const std::string& cacheKey); v8::Local LoadData(v8::Isolate* isolate, const std::string& modulePath); + v8::MaybeLocal RunScriptString(v8::Isolate* isolate, v8::Local context, const std::string script); std::string ResolvePath(v8::Isolate* isolate, const std::string& baseDir, const std::string& moduleName); std::string ResolvePathFromPackageJson(const std::string& packageJson, bool& error); v8::ScriptCompiler::CachedData* LoadScriptCache(const std::string& path); diff --git a/NativeScript/runtime/ModuleInternal.mm b/NativeScript/runtime/ModuleInternal.mm index ab5890ed..74024e1c 100644 --- a/NativeScript/runtime/ModuleInternal.mm +++ b/NativeScript/runtime/ModuleInternal.mm @@ -63,6 +63,26 @@ return success; } +void ModuleInternal::RunScript(Isolate* isolate, std::string script) { + std::shared_ptr cache = Caches::Get(isolate); + Local context = cache->GetContext(); + Local globalObject = context->Global(); + Local requireObj; + bool success = globalObject->Get(context, ToV8String(isolate, "require")).ToLocal(&requireObj); + tns::Assert(success && requireObj->IsFunction(), isolate); + Local result; + this->RunScriptString(isolate, context, 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