Skip to content

Commit 7c908ff

Browse files
committed
Enabled experimental files config field
1 parent b600336 commit 7c908ff

File tree

11 files changed

+79
-10
lines changed

11 files changed

+79
-10
lines changed

docs/core.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/interpreter/_utils.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,47 @@ export const fetchPaths = (module, interpreter, config_fetch) =>
124124
.then((buffer) => module.writeFile(interpreter, path, buffer)),
125125
),
126126
);
127+
128+
const fillName = (source, dest) => dest.endsWith('/') ?
129+
`${dest}${source.split('/').pop()}` : dest;
130+
131+
const parseTemplate = (src, map) => src.replace(
132+
/\{.+?\}/g,
133+
k => {
134+
if (!map.has(k))
135+
throw new SyntaxError(`Invalid template: ${k}`);
136+
return map.get(k);
137+
}
138+
);
139+
140+
const calculateFilesPaths = files => {
141+
const map = new Map;
142+
const targets = new Set;
143+
const sourceDest = [];
144+
for (const [source, dest] of Object.entries(files)) {
145+
if (/^\{.+\}$/.test(source)) {
146+
if (map.has(source))
147+
throw new SyntaxError(`Duplicated template: ${source}`);
148+
map.set(source, parseTemplate(dest, map));
149+
}
150+
else {
151+
const url = parseTemplate(source, map);
152+
const path = fillName(url, parseTemplate(dest, map));
153+
if (targets.has(path))
154+
throw new SyntaxError(`Duplicated destination: ${path}`);
155+
targets.add(path);
156+
sourceDest.push({ url, path });
157+
}
158+
}
159+
return sourceDest;
160+
};
161+
162+
export const fetchFiles = (module, interpreter, config_files) =>
163+
all(
164+
calculateFilesPaths(config_files).map(({ url, path }) =>
165+
fetchResolved(config_files, url)
166+
.then(getBuffer)
167+
.then((buffer) => module.writeFile(interpreter, path, buffer)),
168+
),
169+
);
127170
/* c8 ignore stop */

esm/interpreter/micropython.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fetchPaths, stdio, writeFile } from './_utils.js';
1+
import { fetchFiles, fetchPaths, stdio, writeFile } from './_utils.js';
22
import { registerJSModule, run, runAsync, runEvent } from './_python.js';
33

44
const type = 'micropython';
@@ -13,6 +13,7 @@ export default {
1313
const { stderr, stdout, get } = stdio();
1414
url = url.replace(/\.m?js$/, '.wasm');
1515
const interpreter = await get(loadMicroPython({ stderr, stdout, url }));
16+
if (config.files) await fetchFiles(this, interpreter, config.files);
1617
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
1718
return interpreter;
1819
},

esm/interpreter/pyodide.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fetchPaths, stdio, writeFile } from './_utils.js';
1+
import { fetchFiles, fetchPaths, stdio, writeFile } from './_utils.js';
22
import { registerJSModule, run, runAsync, runEvent } from './_python.js';
33

44
const type = 'pyodide';
@@ -16,6 +16,7 @@ export default {
1616
const interpreter = await get(
1717
loadPyodide({ stderr, stdout, indexURL }),
1818
);
19+
if (config.files) await fetchFiles(this, interpreter, config.files);
1920
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
2021
if (config.packages) {
2122
await interpreter.loadPackage('micropip');

esm/interpreter/ruby-wasm-wasi.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dedent } from '../utils.js';
2-
import { fetchPaths } from './_utils.js';
2+
import { fetchFiles, fetchPaths } from './_utils.js';
33

44
const type = 'ruby-wasm-wasi';
55
const jsType = type.replace(/\W+/g, '_');
@@ -22,6 +22,7 @@ export default {
2222
);
2323
const module = await WebAssembly.compile(await response.arrayBuffer());
2424
const { vm: interpreter } = await DefaultRubyVM(module);
25+
if (config.files) await fetchFiles(this, interpreter, config.files);
2526
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
2627
return interpreter;
2728
},

esm/interpreter/wasmoon.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dedent } from '../utils.js';
2-
import { fetchPaths, io, stdio, writeFileShim } from './_utils.js';
2+
import { fetchFiles, fetchPaths, io, stdio, writeFileShim } from './_utils.js';
33

44
const type = 'wasmoon';
55

@@ -19,6 +19,7 @@ export default {
1919
interpreter.global.setField(index, 'print', stdout);
2020
interpreter.global.setField(index, 'printErr', stderr);
2121
});
22+
if (config.files) await fetchFiles(this, interpreter, config.files);
2223
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
2324
return interpreter;
2425
},

esm/interpreters.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ export const interpreter = new Proxy(new Map(), {
3434
return (config, baseURL) =>
3535
module.then((module) => {
3636
configs.set(id, config);
37-
const fetch = config?.fetch;
38-
if (fetch) base.set(fetch, baseURL);
37+
for (const entry of ['files', 'fetch']) {
38+
const value = config?.[entry];
39+
if (value) base.set(value, baseURL);
40+
}
3941
return engine(module, config, url);
4042
});
4143
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@
7171
"html-escaper": "^3.0.3"
7272
},
7373
"worker": {
74-
"blob": "sha256-wpW1JDUswE7n7bkoJJYLv66C9OuoZbVItbc+oW3ttD8="
74+
"blob": "sha256-OHYVqYaMc7x/7emaT0QXD2uRDdZ3gulKX60rGVxOh7g="
7575
}
7676
}

test/files.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
6+
<title>python</title>
7+
<script type="module" src="../core.js"></script>
8+
</head>
9+
<body>
10+
<script type="micropython" config="./files.toml">
11+
import js
12+
import a, b
13+
js.console.log(a.x)
14+
js.console.log(b.x)
15+
</script>
16+
</body>
17+
</html>

test/files.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[files]
2+
"./a.py" = "./"
3+
"./b.py" = "./"

0 commit comments

Comments
 (0)