move mod specifier resolution js/context => script manager

This commit is contained in:
Pierre Tachoire
2025-10-09 11:23:19 +02:00
parent 1b462da4aa
commit 7afecf0f85
4 changed files with 38 additions and 6 deletions

View File

@@ -248,6 +248,16 @@ pub fn addFromElement(self: *ScriptManager, element: *parser.Element, comptime c
});
}
// Resolve a module specifier to an valid URL.
pub fn resolveSpecifier(_: *ScriptManager, arena: Allocator, specifier: []const u8, base: []const u8) ![:0]const u8 {
return URL.stitch(
arena,
specifier,
base,
.{ .alloc = .if_needed, .null_terminated = true },
);
}
pub fn getModule(self: *ScriptManager, url: [:0]const u8, referrer: []const u8) !void {
const gop = try self.sync_modules.getOrPut(self.allocator, url);
if (gop.found_existing) {

View File

@@ -1353,6 +1353,7 @@ test "Browser: HTML.HtmlScriptElement" {
try testing.htmlRunner("html/script/inline_defer.html");
try testing.htmlRunner("html/script/import.html");
try testing.htmlRunner("html/script/dynamic_import.html");
try testing.htmlRunner("html/script/importmap.html");
}
test "Browser: HTML.HtmlSlotElement" {

View File

@@ -251,11 +251,10 @@ pub fn module(self: *Context, comptime want_result: bool, src: []const u8, url:
for (0..requests.length()) |i| {
const req = requests.get(v8_context, @intCast(i)).castTo(v8.ModuleRequest);
const specifier = try self.jsStringToZig(req.getSpecifier(), .{});
const normalized_specifier = try @import("../../url.zig").stitch(
const normalized_specifier = try self.script_manager.?.resolveSpecifier(
self.call_arena,
specifier,
owned_url,
.{ .alloc = .if_needed, .null_terminated = true },
);
const gop = try self.module_cache.getOrPut(self.arena, normalized_specifier);
if (!gop.found_existing) {
@@ -1127,11 +1126,10 @@ pub fn dynamicModuleCallback(
return @constCast(self.rejectPromise("Out of memory").handle);
};
const normalized_specifier = @import("../../url.zig").stitch(
const normalized_specifier = self.script_manager.?.resolveSpecifier(
self.arena, // might need to survive until the module is loaded
specifier,
resource,
.{ .alloc = .if_needed, .null_terminated = true },
) catch |err| {
log.err(.app, "OOM", .{ .err = err, .src = "dynamicModuleCallback3" });
return @constCast(self.rejectPromise("Out of memory").handle);
@@ -1171,11 +1169,10 @@ fn _resolveModuleCallback(self: *Context, referrer: v8.Module, specifier: []cons
return error.UnknownModuleReferrer;
};
const normalized_specifier = try @import("../../url.zig").stitch(
const normalized_specifier = try self.script_manager.?.resolveSpecifier(
self.call_arena,
specifier,
referrer_path,
.{ .alloc = .if_needed, .null_terminated = true },
);
const gop = try self.module_cache.getOrPut(self.arena, normalized_specifier);

View File

@@ -0,0 +1,24 @@
<!DOCTYPE html>
<script src="../../testing.js"></script>
<script id=importmap type=importmap>
{
"imports": {
"core": "./import.js",
}
}
</script>
<script id=use_importmap type=module>
import * as im from 'core';
testing.expectEqual('hello', im.greeting);
</script>
<script id=cached_importmpa type=module>
// hopefully cached, who knows, no real way to assert this
// but at least it works.
import * as im from 'core';
testing.expectEqual('hello', im.greeting);
</script>