-
Notifications
You must be signed in to change notification settings - Fork 1
/
load.ts
165 lines (144 loc) · 4.38 KB
/
load.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import type { PSEnv, PSMap, PSModule, PSValue } from "./types.ts";
import type { Operation } from "./deps.ts";
import { expect, useAbortSignal } from "./deps.ts";
import { exclude, lookup } from "./psmap.ts";
import { createYSEnv, parse } from "./evaluate.ts";
import { recognize } from "./recognize.ts";
import * as data from "./data.ts";
export interface LoadOptions {
location: string | URL;
base?: string;
env?: PSEnv;
canon?: PSMap;
}
export function* load(options: LoadOptions): Operation<PSModule> {
let { location, base, env, canon } = options;
let url = typeof location === "string" ? new URL(location, base) : location;
let content = yield* read(url);
let source = parse(content);
return yield* moduleEval({ source, location: url, env, canon });
}
export interface ModuleEvalOptions {
location: string | URL;
source: PSValue;
env?: PSEnv;
canon?: PSMap;
}
export function* moduleEval(options: ModuleEvalOptions): Operation<PSModule> {
let { location, source, env = createYSEnv() } = options;
let url = typeof location === "string" ? new URL(location) : location;
let mod: PSModule = {
location: location.toString(),
source,
value: source,
imports: [],
};
if (source.type !== "map") {
return mod;
}
let scope = data.map({});
let canon = options.canon ?? data.map({});
let imports = lookup("$import", source);
let rest = exclude(["$import"], source);
if (imports.type === "just") {
if (imports.value.type !== "map") {
throw new Error(
`imports must be specified as a mapping of names: URL but was ${imports.value.type}`,
);
}
for (let [names, loc] of imports.value.value.entries()) {
if (names.type !== "string") {
throw new Error(
`imported symbols should be a string, but was ${names.type}`,
);
}
if (loc.type !== "string") {
throw new Error(
`import location should be a url string, but was ${loc.type}`,
);
}
let bindings = matchBindings(names.value);
let dep = loc.value === "--canon--"
? ({
location: loc.value,
source: canon,
value: canon,
imports: [],
})
: yield* load({
location: loc.value,
base: url.toString(),
env,
});
mod.imports.push({
module: dep,
bindings,
});
for (let binding of bindings) {
let name = binding.alias ?? binding.name;
let value;
if (binding.all) {
value = dep.value;
} else if (dep.value.type !== "map") {
throw new Error(
`tried to import a name from ${dep.location}, but it is not a 'map'. It is a ${dep.value.type}`,
);
} else {
let result = lookup(binding.name, dep.value);
if (result.type === "nothing") {
throw new Error(
`module ${dep.location} does not have a member named '${binding.name}'`,
);
} else {
value = result.value;
}
}
scope.value.set(data.string(name), value);
}
}
}
mod.value = data.map({});
let expanded = recognize(rest);
if (expanded.type !== "map") {
mod.value = yield* env.eval(expanded, scope);
} else {
for (let [key, value] of expanded.value.entries()) {
let evaluated = yield* env.eval(value, scope);
scope.value.set(key, evaluated);
mod.value.value.set(key, yield* env.eval(value, scope));
}
}
return mod;
}
function* read(url: URL): Operation<string> {
if (url.protocol === "file:") {
return yield* expect(Deno.readTextFile(url));
} else if (url.protocol.startsWith("http")) {
let signal = yield* useAbortSignal();
let response = yield* expect(fetch(url, { signal }));
if (!response.ok) {
throw new Error(`${url}: ${response.status} ${response.statusText}`);
}
return yield* expect(response.text());
} else {
throw new Error(
`cannot load module from ${url}: unsupported protocol '${url.protocol}'`,
);
}
}
function matchBindings(spec: string): PSModule["imports"][number]["bindings"] {
return spec.trim().split(/\s*,\s*/).map((spec) => {
let splatMatch = spec.match(/(.+)<<$/);
if (splatMatch) {
return {
name: splatMatch[1],
all: true,
};
} else {
return {
name: spec,
all: false,
};
}
});
}