Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/codemode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/codemode/src/codemode/codemode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
3 changes: 3 additions & 0 deletions packages/codemode/src/codemode/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import type { Effect } from "effect"
export type Extension<Globals extends Readonly<Record<string, unknown>> = Readonly<Record<string, unknown>>> = {
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<Record<string, keyof Globals & string>> | undefined
/** How a suspended run saves the values this extension's functions returned; see `Snapshot`. */
readonly snapshot?: Snapshot | undefined
}
Expand Down Expand Up @@ -60,6 +62,7 @@ export const make = <const Globals extends Readonly<Record<string, unknown>>>(
return {
name: options.name,
globals: { ...options.globals },
...(options.modules === undefined ? {} : { modules: { ...options.modules } }),
...(options.snapshot === undefined ? {} : { snapshot: options.snapshot }),
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/codemode/src/language/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export type Options<G extends Globals = Globals> = {
* 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<Record<string, string>> | undefined
readonly limits?: Limits | undefined
readonly translate?: Translate | undefined
}
Expand Down Expand Up @@ -124,6 +126,7 @@ export const make = <const G extends Globals = {}>(options: Options<G> = {}): 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",
Expand Down
3 changes: 3 additions & 0 deletions packages/codemode/src/language/runtime/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export type Setup<R> = {
readonly translate?: Required<Translate> | undefined
/** Each run's host globals (see `Host`): made when it starts, and again for a restored run. */
readonly globals?: ((run: Run) => Readonly<Record<string, unknown>>) | undefined
/** Module names that load a global, beyond its own name; see `Runtime.Options`. */
readonly modules?: Readonly<Record<string, string>> | undefined
/** Globals made as program values (a test harness), made again the same way for a restored run. */
readonly programGlobals?: Globals<R> | undefined
}
Expand Down Expand Up @@ -167,6 +169,7 @@ const instantiate = <R>(setup: Setup<R>, run: Run, bindings?: Readonly<Record<st
const ctx = new Interpreter<R>({
builtins: createBuiltins(),
logs: [],
modules: setup.modules,
globals: (ctx) => [
...(setup.programGlobals?.(ctx) ?? []),
...(setup.globals === undefined ? [] : hostGlobals(ctx, setup.globals(run), setup.translate)),
Expand Down
6 changes: 5 additions & 1 deletion packages/codemode/src/language/runtime/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ export class Interpreter<R> {
/** The global bindings: the intrinsics, the host's globals, and the program's bindings. */
readonly globalScope: Map<string, Binding>
private readonly modules = new Map<string, Value>()
private readonly moduleGlobals: Readonly<Record<string, string>>
private readonly namespaces = new Map<string, Obj>()

constructor(options: {
readonly builtins: Builtins
readonly logs?: Array<string>
/** Module names that load a global, beyond its own name; see `Runtime.Options`. */
readonly modules?: Readonly<Record<string, string>> | undefined
/** The host's globals (extensions, a test harness): made again, the same way, when a saved run is restored. */
readonly globals?: (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>
/** Data bound for this program, part of its saved state; host globals of the same name win. */
readonly bindings?: (ctx: Interpreter<R>) => ReadonlyArray<readonly [string, Value]>
}) {
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<string, Binding>([["this", { value: undefined }]])
// Everything made from here to the host globals is an intrinsic, with a static id (see `IntrinsicNames`).
Expand Down Expand Up @@ -83,7 +87,7 @@ export class Interpreter<R> {
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",
Expand Down
10 changes: 8 additions & 2 deletions packages/codemode/src/language/runtime/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ const aliases: Record<string, (global: (name: string) => 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<Record<string, string>> = {},
): 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<string>): Value {
Expand Down
2 changes: 1 addition & 1 deletion packages/codemode/src/language/snapshot/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ const builtinSpecs = (): ReadonlyArray<ClassSpec> => [
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"],
},
]

Expand Down
25 changes: 25 additions & 0 deletions packages/codemode/test/extensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading