GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ios/NativeScript/NativeScript.mm at main · NativeScript/ios Expand file tree/ Copy pathNativeScript.mmMore file actions 282 lines (253 loc) · 10.5 KB / Copy pathNativeScript.mmFile metadata and controls282 lines (253 loc) · 10.5 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 #include "NativeScript.h" #include <Foundation/Foundation.h> #include <dlfcn.h> #include <mach-o/dyld.h> #include <mach-o/getsect.h> #include "inspector/JsV8InspectorClient.h" #include "runtime/Console.h" #include "runtime/Helpers.h" #include "runtime/ModuleInternal.h" #include "runtime/ModuleInternalCallbacks.h" #include "runtime/NativeScriptException.h" #include "runtime/Runtime.h" #include "runtime/RuntimeConfig.h" #include "runtime/Tasks.h" #include "runtime/generated/Version.h" using namespace v8; using namespace tns; namespace tns {} @implementation Config @synthesize BaseDir; @synthesize ApplicationPath; @synthesize MetadataPtr; @synthesize IsDebug; @end @implementation NativeScript static void* HostExecutableMetadata() { // Debug builds with ENABLE_DEBUG_DYLIB link the app's code and linker flags // into <executable>.debug.dylib and leave the executable a stub. NSString* debugDylibName = [[[[NSBundle mainBundle] executablePath] lastPathComponent] stringByAppendingString:@".debug.dylib"]; for (uint32_t i = 0, count = _dyld_image_count(); i < count; i++) { const struct mach_header* header = _dyld_get_image_header(i); if (header == nullptr) { continue; } if (header->filetype != MH_EXECUTE) { const char* path = _dyld_get_image_name(i); const char* name = path != nullptr ? strrchr(path, '/') : nullptr; if (debugDylibName == nil || name == nullptr || strcmp(name + 1, debugDylibName.UTF8String) != 0) { continue; } } unsigned long size = 0; uint8_t* data = getsectiondata(reinterpret_cast<const struct mach_header_64*>(header), "__DATA", "__TNSMetadata", &size); // The app template creates an empty metadata file before the generator // runs, so an empty section means the host carries no metadata. if (data != nullptr && size > 0) { return data; } } return nullptr; } // NativeScriptDefaultMetadata.framework ships only with the NativeScriptSDK // package product, embedded next to NativeScript.framework. static void* DefaultFrameworkMetadata() { const char* accessorName = "NativeScriptDefaultMetadata"; void* accessor = dlsym(RTLD_DEFAULT, accessorName); if (accessor == nullptr) { NSString* frameworksDir = [[[NSBundle bundleForClass:[NativeScript class]] bundlePath] stringByDeletingLastPathComponent]; NSBundle* bundle = [NSBundle bundleWithPath:[frameworksDir stringByAppendingPathComponent: @"NativeScriptDefaultMetadata.framework"]]; // executablePath, unlike a hand-built path, follows Mac Catalyst's versioned layout. NSString* executable = [bundle executablePath]; void* handle = executable != nil ? dlopen(executable.fileSystemRepresentation, RTLD_NOW) : nullptr; accessor = handle != nullptr ? dlsym(handle, accessorName) : nullptr; } if (accessor == nullptr) { return nullptr; } return const_cast<void*>(reinterpret_cast<const void* (*)(void)>(accessor)()); } static void* ResolveMetadataPtr(Config* config) { if (config.MetadataPtr != nil) { return config.MetadataPtr; } if (void* hostMetadata = HostExecutableMetadata()) { return hostMetadata; } if (void* defaultMetadata = DefaultFrameworkMetadata()) { return defaultMetadata; } std::string fatal = "no NativeScript metadata found. Pass Config.MetadataPtr, link a __DATA,__TNSMetadata " "section into the app executable (-sectcreate __DATA __TNSMetadata <metadata.bin>), or " "depend on the NativeScriptSDK package product, which bundles default metadata."; Log(@"Fatal: %s", fatal.c_str()); throw tns::NativeScriptException("Fatal: " + fatal); } - (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(); } // A raw pointer so that exit() never runs ~Runtime from a static destructor: // exit() can come from any thread while the main thread and every worker are // still live, and a teardown then races them and the other static destructors. // A dying process needs no teardown; shutdownRuntime is the deliberate path. Runtime* runtime_ = nullptr; // Cleared before ~Runtime runs so no caller sees a runtime mid-destruction. static void DestroyMainRuntime() { Runtime* runtime = runtime_; runtime_ = nullptr; delete runtime; } - (void)runMainApplication { runtime_->RunMainScript(); CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true); tns::Tasks::Drain(); // Async boot handoff. For UI apps Tasks::Drain() invokes UIApplicationMain // and never returns — the app's main runloop services whatever is still in // flight. When Drain returns, the entry has not reached UIApplicationMain, // so pump a manual runloop until boot actually finishes, Node-like. A // completion may itself register the UIApplicationMain task, so drain after // each slice; if that drain calls UIApplicationMain it takes over and never // returns. // // Two independent things can leave boot unfinished, and BOTH must hold the // pump: an in-flight module-graph load, and an entry whose own evaluation // promise is still pending — a top-level await parked on anything at all // (a nested import() that does its own async work, a native init that will // call UIApplicationMain later). Gating on graph work alone let the second // case reach counter == 0 and fall off the end of main(). std::string entryRejectionReason; bool entryPending = runtime_->PollMainEntryEvaluation(&entryRejectionReason) == tns::EntryEvaluationState::kPending; bool entryRejected = false; if (entryPending || tns::HasPendingAsyncModuleGraphWork()) { const CFAbsoluteTime deadline = CFAbsoluteTimeGetCurrent() + 2 * tns::kModuleEvaluateDeadlineSeconds; bool terminated = false; for (;;) { // Termination outranks everything, including an entry that settled in the // same slice: boot must report the terminating fatal rather than take the // normal exit and carry on into an app on a stopping isolate. Nothing // pending can complete after this point either. Both signals: V8 reports // only a materialized termination, which needs JS to run, and an entry // parked on a promise nothing settles never runs any. if (runtime_->IsExecutionTerminating() || runtime_->IsTerminationRequested()) { terminated = true; break; } if (!(entryPending || tns::HasPendingAsyncModuleGraphWork())) { break; } if (CFAbsoluteTimeGetCurrent() >= deadline) { break; } CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, true); tns::Tasks::Drain(); if (entryPending) { tns::EntryEvaluationState state = runtime_->PollMainEntryEvaluation(&entryRejectionReason); // Once it settles, stop probing for good. entryPending = state == tns::EntryEvaluationState::kPending; if (state == tns::EntryEvaluationState::kRejected) { entryRejected = true; break; } } } tns::Tasks::Drain(); // A settled entry that never called UIApplicationMain is a script-style app // finishing normally. The failures below are fatal in every build: each one // evicts the entry first, so nothing can later import the half-evaluated // module the failure left behind, and then throws rather than logging and // returning into an app whose entry never ran. std::string fatal; if (terminated) { fatal = "boot ended with execution terminating before the main entry settled"; } else if (entryRejected) { fatal = "the main entry module's evaluation rejected during boot: " + entryRejectionReason; } else if (entryPending) { char within[64]; snprintf(within, sizeof(within), "%.0fs", 2 * tns::kModuleEvaluateDeadlineSeconds); fatal = std::string("main entry never settled and UIApplicationMain was never reached " "within ") + within; } if (!fatal.empty()) { runtime_->EvictMainEntry(); Log(@"Fatal: %s", fatal.c_str()); throw tns::NativeScriptException("Fatal: " + fatal); } } } - (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(); DestroyMainRuntime(); } - (instancetype)initializeWithConfig:(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 = ResolveMetadataPtr(config); RuntimeConfig.IsDebug = [config IsDebug]; RuntimeConfig.LogToSystemConsole = [config LogToSystemConsole]; Runtime::Initialize(); DestroyMainRuntime(); std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); runtime_ = new Runtime(); Isolate* isolate = runtime_->GetIsolate(); v8::Locker l(isolate); runtime_->Init(); 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 (version %s, V8 version %s)\n", duration, NATIVESCRIPT_VERSION, V8::GetVersion()); if (config.IsDebug) { Isolate::Scope isolate_scope(isolate); HandleScope handle_scope(isolate); v8_inspector::JsV8InspectorClient* inspectorClient = new v8_inspector::JsV8InspectorClient(runtime_); inspectorClient->init(); inspectorClient->registerModules(); // A nil Arguments array carries no inspector flags, whatever ArgumentsCount says. inspectorClient->connect(config.Arguments != nullptr ? config.ArgumentsCount : 0, config.Arguments); Console::AttachInspectorClient(inspectorClient); } } return self; } - (instancetype)initWithConfig:(Config*)config { return [self initializeWithConfig:config]; } - (void)restartWithConfig:(Config*)config { [self shutdownRuntime]; [self initializeWithConfig:config]; } @end |
| You are visitor 01402503 — 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. |