check cached dynamic module first

This commit is contained in:
Muki Kiboigo
2025-07-22 18:58:48 -07:00
parent 7969e047c7
commit bc9f0b961d

View File

@@ -1580,38 +1580,54 @@ 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;
const module_loader = self.module_loader; if (self.module_cache.get(specifier)) |cached_module| {
const source = module_loader.func(module_loader.ptr, specifier) catch { const new_module = cached_module.castToModule();
const error_msg = v8.String.initUtf8(iso, "Failed to load module"); const status = new_module.getStatus();
_ = resolver.reject(ctx, error_msg.toValue());
return;
} orelse {
const error_msg = v8.String.initUtf8(iso, "Module source not available");
_ = resolver.reject(ctx, error_msg.toValue());
return;
};
var try_catch: TryCatch = undefined; switch (status) {
try_catch.init(self); .kEvaluated, .kEvaluating => {
defer try_catch.deinit(); 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 source = module_loader.func(module_loader.ptr, specifier) catch {
const error_msg = v8.String.initUtf8(iso, "Failed to load module");
_ = resolver.reject(ctx, error_msg.toValue());
return;
} orelse {
const error_msg = v8.String.initUtf8(iso, "Module source not available");
_ = resolver.reject(ctx, error_msg.toValue());
return;
};
const maybe_promise = self.module(source, specifier, true) catch { var try_catch: TryCatch = undefined;
log.err(.js, "module compilation failed", .{ try_catch.init(self);
.specifier = specifier, defer try_catch.deinit();
.exception = try_catch.exception(self.call_arena) catch "unknown error",
.stack = try_catch.stack(self.call_arena) catch null, const promise = self.module(source, specifier, true) catch {
.line = try_catch.sourceLineNumber() orelse 0, log.err(.js, "module compilation failed", .{
}); .specifier = specifier,
const error_msg = if (try_catch.hasCaught()) blk: { .exception = try_catch.exception(self.call_arena) catch "unknown error",
const exception_str = try_catch.exception(self.call_arena) catch "Evaluation error"; .stack = try_catch.stack(self.call_arena) catch null,
break :blk v8.String.initUtf8(iso, exception_str orelse "Evaluation error"); .line = try_catch.sourceLineNumber() orelse 0,
} else v8.String.initUtf8(iso, "Module evaluation failed"); });
_ = resolver.reject(ctx, error_msg.toValue()); const error_msg = if (try_catch.hasCaught()) blk: {
return; const exception_str = try_catch.exception(self.call_arena) catch "Evaluation error";
}; break :blk v8.String.initUtf8(iso, exception_str orelse "Evaluation error");
const new_module = self.module_cache.get(specifier).?.castToModule(); } else v8.String.initUtf8(iso, "Module evaluation failed");
_ = resolver.reject(ctx, error_msg.toValue());
return;
} orelse unreachable;
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;
} }
} }
}; };