Skip to content

Commit f04754c

Browse files
committed
Correct dynamic module loading/caching
Refactors some of the module loading logic. Both normal modules import and dynamic module import now share more of the same code - they both go through the slightly modified `module` function. Dynamic modules now check the cache first, before loading, and when cached, resolve the correct promise. This can now happen regardless of the module loading state. Also tried to replace some page arenas with call arenas and added some basic tests for both normal and dynamic module loading.
1 parent a8e5a48 commit f04754c

File tree

8 files changed

+204
-201
lines changed

8 files changed

+204
-201
lines changed

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
.fingerprint = 0xda130f3af836cea0,
66
.dependencies = .{
77
.v8 = .{
8-
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/7177ee1ae267a44751a0e7e012e257177699a375.tar.gz",
9-
.hash = "v8-0.0.0-xddH63TCAwC1D1hEiOtbEnLBbtz9ZPHrdiGWLcBcYQB7",
8+
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/6018980ed7222bf7f568d058998f682dbee68ad3.tar.gz",
9+
.hash = "v8-0.0.0-xddH6x_DAwBh0gSbFFv1fyiExhExXKzZpbmj5sFH4MRY",
1010
},
1111
// .v8 = .{ .path = "../zig-v8-fork" }
1212
},

src/browser/html/elements.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,8 @@ test "Browser: HTML.HtmlStyleElement" {
13431343
test "Browser: HTML.HtmlScriptElement" {
13441344
try testing.htmlRunner("html/script/script.html");
13451345
try testing.htmlRunner("html/script/inline_defer.html");
1346+
try testing.htmlRunner("html/script/import.html");
1347+
try testing.htmlRunner("html/script/dynamic_import.html");
13461348
}
13471349

13481350
test "Browser: HTML.HtmlSlotElement" {

src/runtime/js.zig

Lines changed: 161 additions & 198 deletions
Large diffs are not rendered by default.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
3+
<script src="../../testing.js"></script>
4+
5+
<script id=dynamic_import type=module>
6+
const promise1 = new Promise((resolve) => {
7+
Promise.all([
8+
import('./import.js'),
9+
import('./import.js'),
10+
import('./import2.js'),
11+
]).then(resolve);
12+
});
13+
14+
testing.async(promise1, (res) => {
15+
testing.expectEqual('hello', res[0].greeting);
16+
testing.expectEqual('hello', res[1].greeting);
17+
testing.expectEqual('world', res[2].greeting);
18+
})
19+
</script>

src/tests/html/script/import.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
3+
<script src="../../testing.js"></script>
4+
5+
<script id=import type=module>
6+
import * as im from './import.js';
7+
testing.expectEqual('hello', im.greeting);
8+
</script>
9+
10+
<script id=cached type=module>
11+
// hopefully cached, who knows, no real way to assert this
12+
// but at least it works.
13+
import * as im from './import.js';
14+
testing.expectEqual('hello', im.greeting);
15+
</script>

src/tests/html/script/import.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let greeting = 'hello';
2+
export {greeting as 'greeting'};

src/tests/html/script/import2.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let greeting = 'world';
2+
export {greeting as 'greeting'};

src/tests/testing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
}
9494

9595
async function async(promise, cb) {
96-
const script_id = document.currentScript.id;
96+
const script_id = document.currentScript ? document.currentScript.id : '<script id is unavailable in browsers>';
9797
const stack = new Error().stack;
9898
const value = await promise;
9999
this._captured = {script_id: script_id, stack: stack};

0 commit comments

Comments
 (0)