Initial ES modules support

This commit is contained in:
2026-10-26 12:39:58 +00:00
parent f7c5b2c80f
commit 1e12e7be2e
35 changed files with 1266 additions and 505 deletions
+7 -7
View File
@@ -1,12 +1,12 @@
// The event loop's JS side. The host owns the loop (see lib/rt/jsrt.h). A
// macrotask is an IO op or a timer, which the host runs: it calls
// `__complete(h)` when op `h` is done. A microtask is a promise job, in
// `__microtasks`. After the program's top level, end.js runs all microtasks,
// and after each macrotask, `__complete` does
// `__microtasks`. After every module's body, the entry runs all microtasks
// (see compiler/src/link.rs), and after each macrotask, `__complete` does
// What to do when IO op `h` completes: `__pending[h]`, a fn that gets `h`.
// Handles are small ints, and a handle is used again after its op completes
const __pending = [];
export const __pending = [];
// Promise jobs, in order. `head` is the next one, so that taking a job does
// not move the others. A job is a closure, which keeps its own data: the
@@ -14,11 +14,11 @@ const __pending = [];
// values of its own promise (see `docs/model.md`, section 3.3)
const __microtasks = { jobs: [], head: 0 };
function __queue_job(job) {
export function __queue_job(job) {
__microtasks.jobs.push(job);
}
function __drain_microtasks() {
export function __drain_microtasks() {
const q = __microtasks;
while (q.head < q.jobs.length) {
@@ -33,8 +33,8 @@ function __drain_microtasks() {
}
// What the host calls when op `h` is done: its callback, then the microtasks.
// The compiler finds it by name
function __complete(h) {
// The compiler finds it as this module's export
export function __complete(h) {
const done = __pending[h];
__pending[h] = undefined;
if (done !== undefined) done(h);