nicolo-ribaudo / modules-import-hooks-refactor

https://nicolo-ribaudo.github.io/modules-import-hooks-refactor/
MIT License
0 stars 0 forks source link

Notes for myself #5

Open nicolo-ribaudo opened 2 years ago

nicolo-ribaudo commented 2 years ago
This is how the Node.js internal functions map to the modules loading algorithms: Phase Node.js ModuleWrap (C++) Node.js vm.Module (JS) Current spec algorithm
Graph fetching ModuleWrap::Link Module#[kLink] (called by Module#link) n/a
Graph linking ModuleWrap::Instantiate Module#link module.Link()
Graph evaluation ModuleWrap::Evaluate Module#evaluate module.Evaluate()
nicolo-ribaudo commented 2 years ago

This is how the different engines schedule the settlement of the import() promise.

Test code (./dep.js is an empty file):

(async() => {

{
  let i = 0;
  (async () => { while (++i<1000) await Promise.resolve() })();
  await import("<invalid>").catch(() => console.log("counter after error is", i));
}

await new Promise(r => setTimeout(r, 0)); // cleanup microtask queue

{
  let i = 0;
  (async () => { while (++i<1000) await Promise.resolve() })();
  await import("./dep.js").then(() => console.log("counter after success (first time) is", i));
}

await new Promise(r => setTimeout(r, 0)); // cleanup microtask queue

{
  let i = 0;
  (async () => { while (++i<1000) await Promise.resolve() })();
  await import("./dep.js").then(() => console.log("counter after success (second time) is", i));
}

})();

Node.js:

counter after error is 11
counter after success (first time) is 1000
counter after success (second time) is 14

Deno:

counter after error is 1000
counter after success (first time) is 1000
counter after success (second time) is 1000

Firefox/Chrome (I think that they don't match the HTML spec for counter after success (second time)):

counter after error is 2
counter after success (first time) is 1000
counter after success (second time) is 1000

Safari:

counter after error is 2
counter after success (first time) is 1000
counter after success (second time) is 3

With this refactor it would be:

counter after error is **host defined**
counter after success (first time) is **host defined**
counter after success (second time) is 3
nicolo-ribaudo commented 2 years ago

6 fixed the divergence