log module specifier on dynamic import stages

This commit is contained in:
Muki Kiboigo
2025-07-11 06:22:04 -07:00
parent 06e514cc2e
commit 287df42994

View File

@@ -767,9 +767,8 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
pub fn module(self: *JsContext, src: []const u8, url: []const u8, cacheable: bool) !?v8.Promise { pub fn module(self: *JsContext, src: []const u8, url: []const u8, cacheable: bool) !?v8.Promise {
const arena = self.context_arena; const arena = self.context_arena;
if (cacheable) { if (cacheable and self.module_cache.contains(url)) {
const value = self.module_cache.get(url); return null;
if (value != null) return null;
} }
errdefer _ = self.module_cache.remove(url); errdefer _ = self.module_cache.remove(url);
@@ -780,7 +779,11 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
errdefer _ = self.module_identifier.remove(m.getIdentityHash()); errdefer _ = self.module_identifier.remove(m.getIdentityHash());
if (cacheable) { if (cacheable) {
try self.module_cache.put(arena, owned_url, PersistentModule.init(self.isolate, m)); try self.module_cache.putNoClobber(
arena,
owned_url,
PersistentModule.init(self.isolate, m),
);
} }
// resolveModuleCallback loads module's dependencies. // resolveModuleCallback loads module's dependencies.
@@ -1552,14 +1555,14 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
} }
fn _dynamicModuleCallback( fn _dynamicModuleCallback(
context: *JsContext, self: *JsContext,
specifier: []const u8, specifier: []const u8,
resolver: *const v8.PromiseResolver, resolver: *const v8.PromiseResolver,
) !void { ) !void {
const iso = context.isolate; const iso = self.isolate;
const ctx = context.v8_context; const ctx = self.v8_context;
const module_loader = context.module_loader; const module_loader = self.module_loader;
const source = module_loader.func(module_loader.ptr, specifier) catch { const source = module_loader.func(module_loader.ptr, specifier) catch {
const error_msg = v8.String.initUtf8(iso, "Failed to load module"); const error_msg = v8.String.initUtf8(iso, "Failed to load module");
_ = resolver.reject(ctx, error_msg.toValue()); _ = resolver.reject(ctx, error_msg.toValue());
@@ -1571,34 +1574,36 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
}; };
var try_catch: TryCatch = undefined; var try_catch: TryCatch = undefined;
try_catch.init(context); try_catch.init(self);
defer try_catch.deinit(); defer try_catch.deinit();
const maybe_promise = context.module(source, specifier, true) catch { const maybe_promise = self.module(source, specifier, true) catch {
log.err(.js, "module compilation failed", .{ log.err(.js, "module compilation failed", .{
.specifier = specifier, .specifier = specifier,
.exception = try_catch.exception(context.call_arena) catch "unknown error", .exception = try_catch.exception(self.call_arena) catch "unknown error",
.stack = try_catch.stack(context.call_arena) catch null, .stack = try_catch.stack(self.call_arena) catch null,
.line = try_catch.sourceLineNumber() orelse 0, .line = try_catch.sourceLineNumber() orelse 0,
}); });
const error_msg = if (try_catch.hasCaught()) blk: { const error_msg = if (try_catch.hasCaught()) blk: {
const exception_str = try_catch.exception(context.call_arena) catch "Evaluation error"; const exception_str = try_catch.exception(self.call_arena) catch "Evaluation error";
break :blk v8.String.initUtf8(iso, exception_str orelse "Evaluation error"); break :blk v8.String.initUtf8(iso, exception_str orelse "Evaluation error");
} else v8.String.initUtf8(iso, "Module evaluation failed"); } else v8.String.initUtf8(iso, "Module evaluation failed");
_ = resolver.reject(ctx, error_msg.toValue()); _ = resolver.reject(ctx, error_msg.toValue());
return; return;
}; };
const new_module = context.module_cache.get(specifier).?.castToModule(); const new_module = self.module_cache.get(specifier).?.castToModule();
if (maybe_promise) |promise| { if (maybe_promise) |promise| {
// This means we must wait for the evaluation. // This means we must wait for the evaluation.
const EvaluationData = struct { const EvaluationData = struct {
specifier: []const u8,
module: v8.Persistent(v8.Module), module: v8.Persistent(v8.Module),
resolver: v8.Persistent(v8.PromiseResolver), resolver: v8.Persistent(v8.PromiseResolver),
}; };
const ev_data = try context.context_arena.create(EvaluationData); const ev_data = try self.context_arena.create(EvaluationData);
ev_data.* = .{ ev_data.* = .{
.specifier = specifier,
.module = v8.Persistent(v8.Module).init(iso, new_module), .module = v8.Persistent(v8.Module).init(iso, new_module),
.resolver = v8.Persistent(v8.PromiseResolver).init(iso, resolver.*), .resolver = v8.Persistent(v8.PromiseResolver).init(iso, resolver.*),
}; };
@@ -1614,10 +1619,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
const cb_resolver = data.resolver.castToPromiseResolver(); const cb_resolver = data.resolver.castToPromiseResolver();
const namespace = cb_module.getModuleNamespace(); const namespace = cb_module.getModuleNamespace();
log.info(.js, "dynamic import complete", .{ log.info(.js, "dynamic import complete", .{ .specifier = data.specifier });
.module = cb_module,
.namespace = namespace,
});
_ = cb_resolver.resolve(cb_context, namespace); _ = cb_resolver.resolve(cb_context, namespace);
} }
}.callback, external); }.callback, external);
@@ -1627,12 +1629,9 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
const cb_info = v8.FunctionCallbackInfo{ .handle = info.? }; const cb_info = v8.FunctionCallbackInfo{ .handle = info.? };
const cb_context = cb_info.getIsolate().getCurrentContext(); const cb_context = cb_info.getIsolate().getCurrentContext();
const data: *EvaluationData = @ptrCast(@alignCast(cb_info.getExternalValue())); const data: *EvaluationData = @ptrCast(@alignCast(cb_info.getExternalValue()));
const cb_module = data.module.castToModule();
const cb_resolver = data.resolver.castToPromiseResolver(); const cb_resolver = data.resolver.castToPromiseResolver();
log.err(.js, "dynamic import failed", .{ log.err(.js, "dynamic import failed", .{ .specifier = data.specifier });
.module = cb_module,
});
_ = cb_resolver.reject(cb_context, cb_info.getData()); _ = cb_resolver.reject(cb_context, cb_info.getData());
} }
}.callback, external); }.callback, external);