1 Commits

Author SHA1 Message Date
Muki Kiboigo
bc9f0b961d check cached dynamic module first 2025-07-30 15:40:52 -07:00

View File

@@ -1580,6 +1580,22 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
const iso = self.isolate; const iso = self.isolate;
const ctx = self.v8_context; const ctx = self.v8_context;
if (self.module_cache.get(specifier)) |cached_module| {
const new_module = cached_module.castToModule();
const status = new_module.getStatus();
switch (status) {
.kEvaluated, .kEvaluating => {
const namespace = new_module.getModuleNamespace();
log.info(.js, "dynamic import complete", .{
.specifier = specifier,
});
_ = resolver.resolve(ctx, namespace);
return;
},
else => {},
}
} else {
const module_loader = self.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");
@@ -1595,7 +1611,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
try_catch.init(self); try_catch.init(self);
defer try_catch.deinit(); defer try_catch.deinit();
const maybe_promise = self.module(source, specifier, true) catch { const 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(self.call_arena) catch "unknown error", .exception = try_catch.exception(self.call_arena) catch "unknown error",
@@ -1608,10 +1624,10 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
} 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;
}; } orelse unreachable;
const new_module = self.module_cache.get(specifier).?.castToModule(); const new_module = self.module_cache.get(specifier).?.castToModule();
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, specifier: []const u8,
@@ -1663,15 +1679,6 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
_ = resolver.reject(ctx, error_msg.toValue()); _ = resolver.reject(ctx, error_msg.toValue());
return; return;
}; };
} else {
// This means it is already present in the cache.
const namespace = new_module.getModuleNamespace();
log.info(.js, "dynamic import complete", .{
.module = new_module,
.namespace = namespace,
});
_ = resolver.resolve(ctx, namespace);
return;
} }
} }
}; };