Skip to content
Merged
8 changes: 4 additions & 4 deletions NativeScript/inspector/JsV8InspectorClient.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "HttpLoader.h"
#include "InspectorServer.h"
#include "JsV8InspectorClient.h"
#include "ModuleUrl.h"
#include "NativeScriptException.h"
#include "NativeScriptPlatform.h"
#include "RuntimeConfig.h"
Expand Down Expand Up @@ -93,7 +94,7 @@ bool ShouldRewriteSourceMapURLs() {

std::string path;
if (sourceMapURL.rfind("file://", 0) == 0) {
path = sourceMapURL.substr(strlen("file://"));
path = tns::FileModulePath(sourceMapURL);
} else if (sourceMapURL[0] == '/') {
path = sourceMapURL;
} else {
Expand All @@ -103,8 +104,7 @@ bool ShouldRewriteSourceMapURLs() {
return message;
}
@autoreleasepool {
NSString* scriptPath =
[NSString stringWithUTF8String:scriptUrl.substr(strlen("file://")).c_str()];
NSString* scriptPath = [NSString stringWithUTF8String:tns::FileModulePath(scriptUrl).c_str()];
NSString* mapPath = [NSString stringWithUTF8String:sourceMapURL.c_str()];
if (scriptPath != nil && mapPath != nil) {
NSString* resolved = [[[scriptPath stringByDeletingLastPathComponent]
Expand Down Expand Up @@ -1071,7 +1071,7 @@ bool GetInt32(const json& value, long long& out) {
if (url.rfind(kSourceMapScheme, 0) == 0) {
path = url.substr(strlen(kSourceMapScheme));
} else if (url.rfind("file://", 0) == 0) {
path = url.substr(strlen("file://"));
path = tns::FileModulePath(url);
} else {
// Reply with a protocol error (not success:false) so DevTools falls back
// to loading the resource from the frontend host, which is the
Expand Down
7 changes: 5 additions & 2 deletions NativeScript/runtime/ModuleInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ class ModuleInternal {
// On compile failure the exception is left pending on the isolate (or a
// NativeScriptException is thrown for setup failures) and the result is
// empty. This is the resolver's file loader: the resolver must only ever
// hand V8 a compiled module — evaluation order belongs to V8.
// hand V8 a compiled module — evaluation order belongs to V8. `query` is
// the module key's query: it is part of the reported URL (script origin,
// import.meta.url), never of the file read or of the code cache's name.
static v8::MaybeLocal<v8::Module> CompileFileEsModule(
v8::Isolate* isolate, const std::string& path);
v8::Isolate* isolate, const std::string& path,
const std::string& query = std::string());
// The entry module's still-pending evaluation promise, or empty when
// evaluation has settled (classic scripts settle synchronously and always
// return empty). Callers use this after RunModule to observe a top-level
Expand Down
13 changes: 8 additions & 5 deletions NativeScript/runtime/ModuleInternal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1009,15 +1009,16 @@ ScriptOrigin origin(urlString,
return script;
}

MaybeLocal<Module> ModuleInternal::CompileFileEsModule(Isolate* isolate, const std::string& path) {
MaybeLocal<Module> ModuleInternal::CompileFileEsModule(Isolate* isolate, const std::string& path,
const std::string& query) {
std::string canonicalPath = NormalizePath(path);

struct stat st;
if (stat(canonicalPath.c_str(), &st) != 0 || !S_ISREG(st.st_mode)) {
throw NativeScriptException("Cannot find module " + canonicalPath);
}

const std::string url = ReportedModuleUrl(canonicalPath);
const std::string url = ReportedModuleUrl(canonicalPath + query);

Local<v8::String> sourceText = ModuleInternal::WrapModuleContent(isolate, canonicalPath);
auto* cacheData = ModuleInternal::LoadScriptCache(canonicalPath, ScriptCacheKind::kEsModule);
Expand Down Expand Up @@ -1541,7 +1542,8 @@ throw NativeScriptException("Module evaluation interrupted by isolate terminatio
// boot handoff for static HTTP entries). Afterwards the load below is a
// registry hit and instantiation resolves as pure lookup. On timeout or
// partial coverage the legacy synchronous path still owns correctness.
RunModuleGraphLoadPumped(isolate, context, requestPath, kModuleEvaluateDeadlineSeconds);
RunModuleGraphLoadPumped(isolate, context, ModuleGraphRoot::HttpUrl(requestPath),
kModuleEvaluateDeadlineSeconds);
ThrowIfLoadInterruptedByTermination(isolate, canonicalPath);
MaybeLocal<Module> maybeMod = LoadHttpModuleForUrl(isolate, context, requestPath);
if (!maybeMod.ToLocal(&module)) {
Expand All @@ -1565,7 +1567,8 @@ throw NativeScriptException("Module evaluation interrupted by isolate terminatio
// settle window: a strict require refuses an async EVALUATION, not an
// async fetch, so it legitimately waits here and then evaluates
// synchronously.
RunModuleGraphLoadPumped(isolate, context, canonicalPath, kModuleEvaluateDeadlineSeconds);
RunModuleGraphLoadPumped(isolate, context, ModuleGraphRoot::FilePath(canonicalPath),
kModuleEvaluateDeadlineSeconds);
ThrowIfLoadInterruptedByTermination(isolate, canonicalPath);
auto walkedIt = registry.find(canonicalPath);
if (walkedIt != registry.end()) {
Expand Down Expand Up @@ -1617,7 +1620,7 @@ throw NativeScriptException(isolate, tcCompile,
requestPath.c_str(), canonicalPath.c_str(), it != registry.end() ? "yes" : "no");
}
registry[canonicalPath].Reset(isolate, module);
IndexModuleForIsolate(isolate, canonicalPath, module);
IndexModuleForIsolate(isolate, canonicalPath, module, canonicalPath);
}

// 5) Instantiate (link) with its own TryCatch
Expand Down
34 changes: 29 additions & 5 deletions NativeScript/runtime/ModuleInternalCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,18 @@ void RemoveModuleFromRegistry(v8::Isolate* isolate,
std::string LookupModuleKeyForModule(v8::Isolate* isolate,
v8::Local<v8::Module> mod);

// Record `mod` in the identity-hash index under `canonicalKey`. Call alongside
// every registry insert performed outside ModuleInternalCallbacks.mm.
// Record `mod` in the identity-hash index under `canonicalKey`, and, for a
// file module, the file it was read from. Call alongside every registry
// insert performed outside ModuleInternalCallbacks.mm.
void IndexModuleForIsolate(v8::Isolate* isolate,
const std::string& canonicalKey,
v8::Local<v8::Module> mod);
v8::Local<v8::Module> mod,
const std::string& filePath);

// The file a file module's registry key names: the path recorded when it was
// registered, or, for a key never registered, the key read as a path plus a
// query (FileModulePath). Empty for an http(s), blob: or other URL key.
std::string ModuleFilePathForKey(v8::Isolate* isolate, const std::string& key);

// The require(esm) exports facade: a synthetic source-text module that
// re-exports everything from `target` and adds `__esModule = true`, so
Expand Down Expand Up @@ -159,9 +166,25 @@ v8::MaybeLocal<v8::Module> LoadHttpModuleForUrl(
// root — is left unregistered for the resolver (or the caller's own load
// path) to report with its own message, so the walk introduces no new failure
// modes and steals no error text.
//
// `root` says what the caller holds; the walk never infers it from a string.
struct ModuleGraphRoot {
bool http = false;
std::string url; // http: the absolute URL
std::string path; // file: the file read and probed, used whole
std::string key; // file: the registry key
std::string query; // file: the query `key` carries after the path

// A filesystem path a native caller holds (entry module, worker entry,
// require() of an ES module): the whole string names the file, and the
// module is the instance without a query.
static ModuleGraphRoot FilePath(const std::string& path);
static ModuleGraphRoot HttpUrl(const std::string& url);
};

void StartModuleGraphLoad(
v8::Isolate* isolate, v8::Local<v8::Context> context,
const std::string& root, double fetchDeadline,
const ModuleGraphRoot& root, double fetchDeadline,
std::function<void(bool ok, const std::string& errorMessage,
v8::Local<v8::Context> context)>
onComplete);
Expand All @@ -175,7 +198,8 @@ void StartModuleGraphLoad(
// caller's own load path reports root failures).
bool RunModuleGraphLoadPumped(v8::Isolate* isolate,
v8::Local<v8::Context> context,
const std::string& root, double timeoutSeconds);
const ModuleGraphRoot& root,
double timeoutSeconds);

// True while any async graph load (any isolate) has fetches or compiles
// outstanding. Read by the boot handoff in [NativeScript runMainApplication]
Expand Down
Loading
Loading