Skip to content

Instantly share code, notes, and snippets.

View kriskowal's full-sized avatar

Kris Kowal kriskowal

View GitHub Profile
class Compartment {
#modules = new Map();
#descriptors = new Map();
#referrers = new Map();
#globalThis = Object.create(null);
constructor({ resolveHook, loadHook, globals }) {
this.#resolveHook = resolveHook;
this.#loadHook = loadHook;
this.#importHook = async (importSpecifier, importMeta) => {

Modules Harmony Simplification Effort

Disclaimer: This text is exclusively focused on the existing capabilities of the language, and does not weight into additional capabilities like wasm, or virtual modules, or compartments. Those can be discussed later on.

MVP to create a module graph

The bare minimum mechanism to create a module graph that matches ESM requires few main pieces:

  1. A portable (serializable) structure that represents the source text of the module. ModuleSource must be portable across processes and across realms.
  2. A realm based Module that closes over a ModuleSource.
export { star as '*' };
const star = 42;
let mutex = Promise.resolve();
const protocol = {
command() {
const result = mutex.then(() => {
// mutual exclusion
});
mutext = next.catch(() => {});
return result;
},
close() {
const go = {
[Symbol.iterator]() {
const funcs = [];
const defer = func => funcs.push(func);
let index = 0;
const flush = () => {
let func;
while (func = funcs.pop()) {
func();
}
<!doctype html>
<head>
<style>
* {
font-family: Helvetica;
font-size: 180px;
}
body {
padding: 50px;
}
const {setTimeout, clearTimeout} = globalThis;
const handles = new WeakMap();
globalThis.setTimeout = (callback, ms) => {
if (typeof callback !== 'function') {
throw new Error(`This implementation of setTimeout has disabled evaluation behavior for ${typeof callback} callbacks`);
}
const token = harden({});
handles.set(token, setTimeout(callback, ms));
return token;
};
const streamWithContext = (context, stream) => ({
async next(value) {
return Promise.race([context.cancelled, stream.next(await value)]);
},
async return(value) {
return Promise.race([context.cancelled, stream.return(await value)]);
},
async throw(error) {
return Promise.race([context.cancelled, stream.throw(await error)]);
},
async function avery() {
await null;
for (let i = 0; i < 10; i += 1) {
console.log('await');
await null;
}
}
function blake() {
// @ts-check
/**
* @template T
* @typedef {{
* promise: Promise<T>,
* resolve: (value:T) => void,
* reject: (reason:Error) => void
* }} Deferred
*/