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
17 changes: 14 additions & 3 deletions load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ export interface LoadOptions {
}

export function* load(options: LoadOptions): Operation<PSModule> {
let { location, base } = options;
let { location, base, env } = options;
let url = typeof location === "string" ? new URL(location, base) : location;

let content = yield* read(url);
let body = parse(content);

return yield* moduleEval({ body, location: url, env });
}

export interface ModuleEvalOptions {
location: string | URL;
body: PSValue;
env?: PSEnv;
}

export function* moduleEval(options: ModuleEvalOptions): Operation<PSModule> {
let { location, body, env = createYSEnv() } = options;
let url = typeof location === "string" ? new URL(location) : location;

let result: PSValue = body;

let scope: PSMap = {
Expand All @@ -30,8 +43,6 @@ export function* load(options: LoadOptions): Operation<PSModule> {
value: new Map(),
};

let env = options.env ?? createYSEnv();

function* importSymbols(map: PSValue): Operation<void> {
if (map.type !== "map") {
throw new Error(
Expand Down
6 changes: 5 additions & 1 deletion platformscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import type { PSEnv, PSFn, PSMap, PSModule, PSValue } from "./types.ts";
import type { Operation, Task } from "./deps.ts";
import { createYSEnv, global } from "./evaluate.ts";
import { concat } from "./psmap.ts";
import { load } from "./load.ts";
import { load, moduleEval } from "./load.ts";
import { map } from "./data.ts";
import { run as $run } from "./deps.ts";

export interface PlatformScript {
run<T>(block: (env: PSEnv) => Operation<T>): Task<T>;
call(fn: PSFn, arg: PSValue, options?: PSMap): Task<PSValue>;
eval(value: PSValue, bindings?: PSMap): Task<PSValue>;
moduleEval(value: PSValue, url: string | URL): Task<PSModule>;
load(url: string | URL, base?: string): Task<PSModule>;
}

Expand All @@ -33,6 +34,9 @@ export function createPlatformScript(
eval(value, bindings) {
return run((env) => env.eval(value, bindings));
},
moduleEval(value, url) {
return run((env) => moduleEval({ body: value, location: url, env }));
},
load(location, base) {
return run((env) => load({ location, base, env }));
},
Expand Down
19 changes: 18 additions & 1 deletion test/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { PSModule } from "../types.ts";
import type { Task } from "../deps.ts";

import { describe, expect, it, useStaticFileServer } from "./suite.ts";
import { load, ps2js } from "../mod.ts";
import { load, moduleEval, parse, ps2js } from "../mod.ts";
import { run } from "../deps.ts";
import { lookup, lookup$ } from "../psmap.ts";

Expand Down Expand Up @@ -70,6 +70,23 @@ describe("a PlatformScript module", () => {
// it("can use imported symbols from another module");
// it("can handle circular module references");
// it("can be specified using WASM");

it("supports evaluating a module directly without loading it from a url", async () => {
await run(function* () {
let body = parse(`
$import:
names: [five]
from: nodeps.yaml
myfive: $five
$do: $myfive
`);
let mod = yield* moduleEval({
body,
location: new URL(`modules/virtual-module.yaml`, import.meta.url),
});
expect(mod.value.value).toEqual(5);
});
});
});

function loadmod(url: string): Task<PSModule> {
Expand Down