Skip to content

Commit bc9f0b9

Browse files
committed
check cached dynamic module first
1 parent 7969e04 commit bc9f0b9

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed

src/runtime/js.zig

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,38 +1580,54 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
15801580
const iso = self.isolate;
15811581
const ctx = self.v8_context;
15821582

1583-
const module_loader = self.module_loader;
1584-
const source = module_loader.func(module_loader.ptr, specifier) catch {
1585-
const error_msg = v8.String.initUtf8(iso, "Failed to load module");
1586-
_ = resolver.reject(ctx, error_msg.toValue());
1587-
return;
1588-
} orelse {
1589-
const error_msg = v8.String.initUtf8(iso, "Module source not available");
1590-
_ = resolver.reject(ctx, error_msg.toValue());
1591-
return;
1592-
};
1583+
if (self.module_cache.get(specifier)) |cached_module| {
1584+
const new_module = cached_module.castToModule();
1585+
const status = new_module.getStatus();
1586+
1587+
switch (status) {
1588+
.kEvaluated, .kEvaluating => {
1589+
const namespace = new_module.getModuleNamespace();
1590+
log.info(.js, "dynamic import complete", .{
1591+
.specifier = specifier,
1592+
});
1593+
_ = resolver.resolve(ctx, namespace);
1594+
return;
1595+
},
1596+
else => {},
1597+
}
1598+
} else {
1599+
const module_loader = self.module_loader;
1600+
const source = module_loader.func(module_loader.ptr, specifier) catch {
1601+
const error_msg = v8.String.initUtf8(iso, "Failed to load module");
1602+
_ = resolver.reject(ctx, error_msg.toValue());
1603+
return;
1604+
} orelse {
1605+
const error_msg = v8.String.initUtf8(iso, "Module source not available");
1606+
_ = resolver.reject(ctx, error_msg.toValue());
1607+
return;
1608+
};
15931609

1594-
var try_catch: TryCatch = undefined;
1595-
try_catch.init(self);
1596-
defer try_catch.deinit();
1610+
var try_catch: TryCatch = undefined;
1611+
try_catch.init(self);
1612+
defer try_catch.deinit();
15971613

1598-
const maybe_promise = self.module(source, specifier, true) catch {
1599-
log.err(.js, "module compilation failed", .{
1600-
.specifier = specifier,
1601-
.exception = try_catch.exception(self.call_arena) catch "unknown error",
1602-
.stack = try_catch.stack(self.call_arena) catch null,
1603-
.line = try_catch.sourceLineNumber() orelse 0,
1604-
});
1605-
const error_msg = if (try_catch.hasCaught()) blk: {
1606-
const exception_str = try_catch.exception(self.call_arena) catch "Evaluation error";
1607-
break :blk v8.String.initUtf8(iso, exception_str orelse "Evaluation error");
1608-
} else v8.String.initUtf8(iso, "Module evaluation failed");
1609-
_ = resolver.reject(ctx, error_msg.toValue());
1610-
return;
1611-
};
1612-
const new_module = self.module_cache.get(specifier).?.castToModule();
1614+
const promise = self.module(source, specifier, true) catch {
1615+
log.err(.js, "module compilation failed", .{
1616+
.specifier = specifier,
1617+
.exception = try_catch.exception(self.call_arena) catch "unknown error",
1618+
.stack = try_catch.stack(self.call_arena) catch null,
1619+
.line = try_catch.sourceLineNumber() orelse 0,
1620+
});
1621+
const error_msg = if (try_catch.hasCaught()) blk: {
1622+
const exception_str = try_catch.exception(self.call_arena) catch "Evaluation error";
1623+
break :blk v8.String.initUtf8(iso, exception_str orelse "Evaluation error");
1624+
} else v8.String.initUtf8(iso, "Module evaluation failed");
1625+
_ = resolver.reject(ctx, error_msg.toValue());
1626+
return;
1627+
} orelse unreachable;
1628+
1629+
const new_module = self.module_cache.get(specifier).?.castToModule();
16131630

1614-
if (maybe_promise) |promise| {
16151631
// This means we must wait for the evaluation.
16161632
const EvaluationData = struct {
16171633
specifier: []const u8,
@@ -1663,15 +1679,6 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
16631679
_ = resolver.reject(ctx, error_msg.toValue());
16641680
return;
16651681
};
1666-
} else {
1667-
// This means it is already present in the cache.
1668-
const namespace = new_module.getModuleNamespace();
1669-
log.info(.js, "dynamic import complete", .{
1670-
.module = new_module,
1671-
.namespace = namespace,
1672-
});
1673-
_ = resolver.resolve(ctx, namespace);
1674-
return;
16751682
}
16761683
}
16771684
};

0 commit comments

Comments
 (0)