diff --git a/packages/codemode/README.md b/packages/codemode/README.md index c4a3f5afec33..1f60ac06386c 100644 --- a/packages/codemode/README.md +++ b/packages/codemode/README.md @@ -310,7 +310,8 @@ A global can also be an object of functions, such as `fs`. Every function reacha global, so calls fire the `extension` hooks with their path as the name (`fs.readFile`, `fs.promises.readFile`). Programs load globals the way Node code loads modules. `import` and `require` take a global by its Node module name, -with or without `node:`; `fs/promises`, `url`, `util`, and `buffer` resolve to the globals that provide them. There are +with or without `node:`; `fs/promises`, `url`, `util`, and `buffer` resolve to the globals that provide them, and an +extension can add its own names (`modules: { bun: "Bun" }` makes `import { file } from "bun"` load `Bun`). There are no packages, so anything else throws. ```js diff --git a/packages/codemode/src/codemode/codemode.ts b/packages/codemode/src/codemode/codemode.ts index 805b383ff268..d5d393ecf40f 100644 --- a/packages/codemode/src/codemode/codemode.ts +++ b/packages/codemode/src/codemode/codemode.ts @@ -265,6 +265,8 @@ export function make< ), ), }), + // A later extension's module name wins over an earlier one's. + modules: Object.assign({}, ...picked.map((extension) => extension.modules ?? {})), limits: { timeoutMs: limits.timeoutMs, maxOutputBytes: limits.maxOutputBytes }, // Arguments cross as JSON would carry them: functions are left out and promises are awaited. translate: { functions: "drop", promises: "await" }, diff --git a/packages/codemode/src/codemode/extension.ts b/packages/codemode/src/codemode/extension.ts index 49ff9ffb0435..245f6c45554f 100644 --- a/packages/codemode/src/codemode/extension.ts +++ b/packages/codemode/src/codemode/extension.ts @@ -22,6 +22,8 @@ import type { Effect } from "effect" export type Extension> = Readonly>> = { readonly name: string readonly globals: Globals + /** Module names that load one of these globals, as `{ bun: "Bun" }` makes `import { file } from "bun"` work. */ + readonly modules?: Readonly> | undefined /** How a suspended run saves the values this extension's functions returned; see `Snapshot`. */ readonly snapshot?: Snapshot | undefined } @@ -60,6 +62,7 @@ export const make = >>( return { name: options.name, globals: { ...options.globals }, + ...(options.modules === undefined ? {} : { modules: { ...options.modules } }), ...(options.snapshot === undefined ? {} : { snapshot: options.snapshot }), } } diff --git a/packages/codemode/src/language/runtime.ts b/packages/codemode/src/language/runtime.ts index b464ec60d02a..4320d5e83a79 100644 --- a/packages/codemode/src/language/runtime.ts +++ b/packages/codemode/src/language/runtime.ts @@ -57,6 +57,8 @@ export type Options = { * run's own state in `run.metadata`. */ readonly globals?: ((run: Run) => G) | undefined + /** Module names that load a global, beyond its own name: `{ bun: "Bun" }` makes `import "bun"` load `Bun`. */ + readonly modules?: Readonly> | undefined readonly limits?: Limits | undefined readonly translate?: Translate | undefined } @@ -124,6 +126,7 @@ export const make = (options: Options = {}): Ru maxOutputBytes: validateLimit("maxOutputBytes", options.limits?.maxOutputBytes, 0), }, globals: options.globals, + modules: options.modules, translate: { functions: options.translate?.functions ?? "error", promises: options.translate?.promises ?? "error", diff --git a/packages/codemode/src/language/runtime/execute.ts b/packages/codemode/src/language/runtime/execute.ts index 0797cf7d9088..6b746c963d25 100644 --- a/packages/codemode/src/language/runtime/execute.ts +++ b/packages/codemode/src/language/runtime/execute.ts @@ -74,6 +74,8 @@ export type Setup = { readonly translate?: Required | undefined /** Each run's host globals (see `Host`): made when it starts, and again for a restored run. */ readonly globals?: ((run: Run) => Readonly>) | undefined + /** Module names that load a global, beyond its own name; see `Runtime.Options`. */ + readonly modules?: Readonly> | undefined /** Globals made as program values (a test harness), made again the same way for a restored run. */ readonly programGlobals?: Globals | undefined } @@ -167,6 +169,7 @@ const instantiate = (setup: Setup, run: Run, bindings?: Readonly({ builtins: createBuiltins(), logs: [], + modules: setup.modules, globals: (ctx) => [ ...(setup.programGlobals?.(ctx) ?? []), ...(setup.globals === undefined ? [] : hostGlobals(ctx, setup.globals(run), setup.translate)), diff --git a/packages/codemode/src/language/runtime/interpreter.ts b/packages/codemode/src/language/runtime/interpreter.ts index 55fec9169ddb..191b076f3d2b 100644 --- a/packages/codemode/src/language/runtime/interpreter.ts +++ b/packages/codemode/src/language/runtime/interpreter.ts @@ -37,11 +37,14 @@ export class Interpreter { /** The global bindings: the intrinsics, the host's globals, and the program's bindings. */ readonly globalScope: Map private readonly modules = new Map() + private readonly moduleGlobals: Readonly> private readonly namespaces = new Map() constructor(options: { readonly builtins: Builtins readonly logs?: Array + /** Module names that load a global, beyond its own name; see `Runtime.Options`. */ + readonly modules?: Readonly> | undefined /** The host's globals (extensions, a test harness): made again, the same way, when a saved run is restored. */ readonly globals?: (ctx: Interpreter) => ReadonlyArray /** Data bound for this program, part of its saved state; host globals of the same name win. */ @@ -49,6 +52,7 @@ export class Interpreter { }) { this.builtins = options.builtins this.logs = options.logs ?? [] + this.moduleGlobals = options.modules ?? {} // Program code has no receiver: top-level `this` is undefined, as in a module. this.globalScope = new Map([["this", { value: undefined }]]) // Everything made from here to the host globals is an intrinsic, with a static id (see `IntrinsicNames`). @@ -83,7 +87,7 @@ export class Interpreter { const name = moduleName(specifier) const cached = this.modules.get(name) if (cached !== undefined) return cached - const module = resolveModule(name, (global) => this.globalScope.get(global)?.value) + const module = resolveModule(name, (global) => this.globalScope.get(global)?.value, this.moduleGlobals) if (module === undefined) throw new PendingThrow( "Error", diff --git a/packages/codemode/src/language/runtime/modules.ts b/packages/codemode/src/language/runtime/modules.ts index d68704d0f1be..1d9699c91c89 100644 --- a/packages/codemode/src/language/runtime/modules.ts +++ b/packages/codemode/src/language/runtime/modules.ts @@ -18,9 +18,15 @@ const aliases: Record Value) => Value> = { /** `node:fs` names the same module as `fs`. */ export const moduleName = (specifier: string) => (specifier.startsWith("node:") ? specifier.slice(5) : specifier) -export const resolveModule = (specifier: string, global: (name: string) => Value): Value => { +/** `hosted` names the global a host's own module names load (`{ bun: "Bun" }`). */ +export const resolveModule = ( + specifier: string, + global: (name: string) => Value, + hosted: Readonly> = {}, +): Value => { const name = moduleName(specifier) - return global(name) ?? aliases[name]?.(global) + const named = Object.hasOwn(hosted, name) ? hosted[name] : undefined + return global(name) ?? (named === undefined ? undefined : global(named)) ?? aliases[name]?.(global) } function pick(global: (name: string) => Value, names: ReadonlyArray): Value { diff --git a/packages/codemode/src/language/snapshot/codec.ts b/packages/codemode/src/language/snapshot/codec.ts index 84ef67ad5034..e45e6cadce17 100644 --- a/packages/codemode/src/language/snapshot/codec.ts +++ b/packages/codemode/src/language/snapshot/codec.ts @@ -323,7 +323,7 @@ const builtinSpecs = (): ReadonlyArray => [ ctor: Interpreter, anchor: "ctx", fields: ["logs", "globalScope", "modules", "namespaces"], - skip: ["builtins", "vm", "intrinsics", "hosts", "hostNamespaces", "sources", "prelude"], + skip: ["builtins", "vm", "intrinsics", "hosts", "hostNamespaces", "moduleGlobals", "sources", "prelude"], }, ] diff --git a/packages/codemode/test/extensions.test.ts b/packages/codemode/test/extensions.test.ts index c4b598ae1148..c6d263af9605 100644 --- a/packages/codemode/test/extensions.test.ts +++ b/packages/codemode/test/extensions.test.ts @@ -602,6 +602,31 @@ describe("imports", () => { expect(calls).toEqual(["files:fs.readFile", "files:fs.promises.readFile"]) }) + test("an extension's module names load its globals, only in runs that get the extension", async () => { + const bun = Extension.make({ + name: "bun", + globals: { Bun: { file: (path: string) => ({ name: path }) } }, + modules: { bun: "Bun" }, + }) + const target = CodeMode.make({ extensions: [bun] }) + expect( + await value( + `import Loaded, { file } from "bun" + return [file("a").name, Loaded === Bun, typeof require("bun").file, typeof (await import("bun")).file]`, + target, + ), + ).toEqual(["a", true, "function", "function"]) + const without = await Effect.runPromise(target.execute(`import { file } from "bun"`, undefined, { extensions: [] })) + expect(without).toMatchObject({ ok: false, error: { message: expect.stringContaining(`Cannot import "bun"`) } }) + expect((await failure(`import { file } from "bun"`)).message).toStartWith(`Error: Cannot import "bun"`) + }) + + test("a later extension's module name wins", async () => { + const one = Extension.make({ name: "one", globals: { A: { id: () => "a" } }, modules: { shared: "A" } }) + const two = Extension.make({ name: "two", globals: { B: { id: () => "b" } }, modules: { shared: "B" } }) + expect(await value(`return require("shared").id()`, CodeMode.make({ extensions: [one, two] }))).toBe("b") + }) + test("a module that isn't provided, or a missing export, fails at the import", async () => { const target = CodeMode.make({ extensions: [files] }) expect((await failure(`import yaml from "yaml"`, target)).message).toStartWith(