Skip to content

Check Dynamically Cached Module First #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 45 additions & 38 deletions src/runtime/js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1580,38 +1580,54 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
const iso = self.isolate;
const ctx = self.v8_context;

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;
};
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 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;
};

var try_catch: TryCatch = undefined;
try_catch.init(self);
defer try_catch.deinit();
var try_catch: TryCatch = undefined;
try_catch.init(self);
defer try_catch.deinit();

const maybe_promise = self.module(source, specifier, true) catch {
log.err(.js, "module compilation failed", .{
.specifier = specifier,
.exception = try_catch.exception(self.call_arena) catch "unknown error",
.stack = try_catch.stack(self.call_arena) catch null,
.line = try_catch.sourceLineNumber() orelse 0,
});
const error_msg = if (try_catch.hasCaught()) blk: {
const exception_str = try_catch.exception(self.call_arena) catch "Evaluation error";
break :blk v8.String.initUtf8(iso, exception_str orelse "Evaluation error");
} else v8.String.initUtf8(iso, "Module evaluation failed");
_ = resolver.reject(ctx, error_msg.toValue());
return;
};
const new_module = self.module_cache.get(specifier).?.castToModule();
const promise = self.module(source, specifier, true) catch {
log.err(.js, "module compilation failed", .{
.specifier = specifier,
.exception = try_catch.exception(self.call_arena) catch "unknown error",
.stack = try_catch.stack(self.call_arena) catch null,
.line = try_catch.sourceLineNumber() orelse 0,
});
const error_msg = if (try_catch.hasCaught()) blk: {
const exception_str = try_catch.exception(self.call_arena) catch "Evaluation error";
break :blk v8.String.initUtf8(iso, exception_str orelse "Evaluation error");
} 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.
const EvaluationData = struct {
specifier: []const u8,
Expand Down Expand Up @@ -1663,15 +1679,6 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
_ = resolver.reject(ctx, error_msg.toValue());
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;
}
}
};
Expand Down