mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
move mod specifier resolution js/context => script manager
This commit is contained in:
@@ -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 {
|
pub fn getModule(self: *ScriptManager, url: [:0]const u8, referrer: []const u8) !void {
|
||||||
const gop = try self.sync_modules.getOrPut(self.allocator, url);
|
const gop = try self.sync_modules.getOrPut(self.allocator, url);
|
||||||
if (gop.found_existing) {
|
if (gop.found_existing) {
|
||||||
|
|||||||
@@ -1353,6 +1353,7 @@ test "Browser: HTML.HtmlScriptElement" {
|
|||||||
try testing.htmlRunner("html/script/inline_defer.html");
|
try testing.htmlRunner("html/script/inline_defer.html");
|
||||||
try testing.htmlRunner("html/script/import.html");
|
try testing.htmlRunner("html/script/import.html");
|
||||||
try testing.htmlRunner("html/script/dynamic_import.html");
|
try testing.htmlRunner("html/script/dynamic_import.html");
|
||||||
|
try testing.htmlRunner("html/script/importmap.html");
|
||||||
}
|
}
|
||||||
|
|
||||||
test "Browser: HTML.HtmlSlotElement" {
|
test "Browser: HTML.HtmlSlotElement" {
|
||||||
|
|||||||
@@ -251,11 +251,10 @@ pub fn module(self: *Context, comptime want_result: bool, src: []const u8, url:
|
|||||||
for (0..requests.length()) |i| {
|
for (0..requests.length()) |i| {
|
||||||
const req = requests.get(v8_context, @intCast(i)).castTo(v8.ModuleRequest);
|
const req = requests.get(v8_context, @intCast(i)).castTo(v8.ModuleRequest);
|
||||||
const specifier = try self.jsStringToZig(req.getSpecifier(), .{});
|
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,
|
self.call_arena,
|
||||||
specifier,
|
specifier,
|
||||||
owned_url,
|
owned_url,
|
||||||
.{ .alloc = .if_needed, .null_terminated = true },
|
|
||||||
);
|
);
|
||||||
const gop = try self.module_cache.getOrPut(self.arena, normalized_specifier);
|
const gop = try self.module_cache.getOrPut(self.arena, normalized_specifier);
|
||||||
if (!gop.found_existing) {
|
if (!gop.found_existing) {
|
||||||
@@ -1127,11 +1126,10 @@ pub fn dynamicModuleCallback(
|
|||||||
return @constCast(self.rejectPromise("Out of memory").handle);
|
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
|
self.arena, // might need to survive until the module is loaded
|
||||||
specifier,
|
specifier,
|
||||||
resource,
|
resource,
|
||||||
.{ .alloc = .if_needed, .null_terminated = true },
|
|
||||||
) catch |err| {
|
) catch |err| {
|
||||||
log.err(.app, "OOM", .{ .err = err, .src = "dynamicModuleCallback3" });
|
log.err(.app, "OOM", .{ .err = err, .src = "dynamicModuleCallback3" });
|
||||||
return @constCast(self.rejectPromise("Out of memory").handle);
|
return @constCast(self.rejectPromise("Out of memory").handle);
|
||||||
@@ -1171,11 +1169,10 @@ fn _resolveModuleCallback(self: *Context, referrer: v8.Module, specifier: []cons
|
|||||||
return error.UnknownModuleReferrer;
|
return error.UnknownModuleReferrer;
|
||||||
};
|
};
|
||||||
|
|
||||||
const normalized_specifier = try @import("../../url.zig").stitch(
|
const normalized_specifier = try self.script_manager.?.resolveSpecifier(
|
||||||
self.call_arena,
|
self.call_arena,
|
||||||
specifier,
|
specifier,
|
||||||
referrer_path,
|
referrer_path,
|
||||||
.{ .alloc = .if_needed, .null_terminated = true },
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const gop = try self.module_cache.getOrPut(self.arena, normalized_specifier);
|
const gop = try self.module_cache.getOrPut(self.arena, normalized_specifier);
|
||||||
|
|||||||
24
src/tests/html/script/importmap.html
Normal file
24
src/tests/html/script/importmap.html
Normal 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>
|
||||||
|
|
||||||
Reference in New Issue
Block a user