Skip to content

Commit 4ba213c

Browse files
Merge pull request #103 from pyscript/base-less
Removed the ugly `base` WeakMap workaround
2 parents 4de1fe2 + 61dfb54 commit 4ba213c

16 files changed

+53
-64
lines changed

docs/index.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/3rd-party.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* c8 ignore start */
2+
3+
// toml
4+
export const toml = async (text) => (
5+
await import(/* webpackIgnore: true */'./3rd-party/toml.js')
6+
).parse(text);
7+
8+
// zip
9+
export const zip = () => import(/* webpackIgnore: true */'./3rd-party/zip.js');
10+
11+
/* c8 ignore stop */

esm/interpreter/_utils.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,13 @@ const joinPaths = (parts) => {
9292
return parts[0].startsWith('/') ? `/${res}` : res;
9393
};
9494

95-
const fetchBuffer = (config_fetch, url) =>
96-
fetch(absoluteURL(url, base.get(config_fetch))).arrayBuffer();
95+
const fetchBuffer = (url, baseURL) =>
96+
fetch(absoluteURL(url, baseURL)).arrayBuffer();
9797

98-
export const base = new WeakMap();
99-
100-
export const fetchPaths = (module, interpreter, config_fetch) =>
98+
export const fetchPaths = (module, interpreter, config_fetch, baseURL) =>
10199
all(
102100
calculateFetchPaths(config_fetch).map(({ url, path }) =>
103-
fetchBuffer(config_fetch, url)
101+
fetchBuffer(url, baseURL)
104102
.then((buffer) => module.writeFile(interpreter, path, buffer)),
105103
),
106104
);
@@ -139,10 +137,10 @@ const calculateFilesPaths = files => {
139137
return sourceDest;
140138
};
141139

142-
export const fetchFiles = (module, interpreter, config_files) =>
140+
export const fetchFiles = (module, interpreter, config_files, baseURL) =>
143141
all(
144142
calculateFilesPaths(config_files).map(({ url, path }) =>
145-
fetchBuffer(config_files, url)
143+
fetchBuffer(url, baseURL)
146144
.then((buffer) => module.writeFile(
147145
interpreter,
148146
path,
@@ -152,17 +150,17 @@ export const fetchFiles = (module, interpreter, config_files) =>
152150
),
153151
);
154152

155-
export const fetchJSModules = ({ main, worker }) => {
153+
export const fetchJSModules = ({ main, worker }, baseURL) => {
156154
const promises = [];
157155
if (worker && RUNNING_IN_WORKER) {
158156
for (let [source, name] of entries(worker)) {
159-
source = absoluteURL(source, base.get(worker));
157+
source = absoluteURL(source, baseURL);
160158
promises.push(importJS(source, name));
161159
}
162160
}
163161
if (main && !RUNNING_IN_WORKER) {
164162
for (let [source, name] of entries(main)) {
165-
source = absoluteURL(source, base.get(main));
163+
source = absoluteURL(source, baseURL);
166164
if (isCSS(source)) importCSS(source);
167165
else promises.push(importJS(source, name));
168166
}

esm/interpreter/micropython.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getFormat, loader, registerJSModule, run, runAsync, runEvent } from './
55
import { stdio, buffered } from './_io.js';
66
import { absoluteURL } from '../utils.js';
77
import mip from '../python/mip.js';
8-
import zip from '../zip.js';
8+
import { zip } from '../3rd-party.js';
99

1010
const type = 'micropython';
1111

@@ -34,9 +34,9 @@ export default {
3434
const interpreter = await get(loadMicroPython({ linebuffer: false, stderr, stdout, url }));
3535
const py_imports = importPackages.bind(this, interpreter, baseURL);
3636
loader.set(interpreter, py_imports);
37-
if (config.files) await fetchFiles(this, interpreter, config.files);
38-
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
39-
if (config.js_modules) await fetchJSModules(config.js_modules);
37+
if (config.files) await fetchFiles(this, interpreter, config.files, baseURL);
38+
if (config.fetch) await fetchPaths(this, interpreter, config.fetch, baseURL);
39+
if (config.js_modules) await fetchJSModules(config.js_modules, baseURL);
4040

4141
// Install Micropython Package
4242
this.writeFile(interpreter, './mip.py', mip);

esm/interpreter/pyodide.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default {
8080
type,
8181
module: (version = '0.26.0') =>
8282
`https://cdn.jsdelivr.net/pyodide/v${version}/full/pyodide.mjs`,
83-
async engine({ loadPyodide }, config, url) {
83+
async engine({ loadPyodide }, config, url, baseURL) {
8484
// apply override ASAP then load foreign code
8585
if (!RUNNING_IN_WORKER && config.experimental_create_proxy === 'auto')
8686
applyOverride();
@@ -91,9 +91,9 @@ export default {
9191
);
9292
const py_imports = importPackages.bind(interpreter);
9393
loader.set(interpreter, py_imports);
94-
if (config.files) await fetchFiles(this, interpreter, config.files);
95-
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
96-
if (config.js_modules) await fetchJSModules(config.js_modules);
94+
if (config.files) await fetchFiles(this, interpreter, config.files, baseURL);
95+
if (config.fetch) await fetchPaths(this, interpreter, config.fetch, baseURL);
96+
if (config.js_modules) await fetchJSModules(config.js_modules, baseURL);
9797
if (config.packages) await py_imports(config.packages);
9898
return interpreter;
9999
},

esm/interpreter/ruby-wasm-wasi.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ export default {
1818
experimental: true,
1919
module: (version = '2.6.1') =>
2020
`https://cdn.jsdelivr.net/npm/@ruby/3.2-wasm-wasi@${version}/dist/browser/+esm`,
21-
async engine({ DefaultRubyVM }, config, url) {
21+
async engine({ DefaultRubyVM }, config, url, baseURL) {
2222
url = url.replace(/\/browser\/\+esm$/, '/ruby.wasm');
2323
const buffer = await fetch(url).arrayBuffer();
2424
const module = await WebAssembly.compile(buffer);
2525
const { vm: interpreter } = await DefaultRubyVM(module);
26-
if (config.files) await fetchFiles(this, interpreter, config.files);
27-
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
28-
if (config.js_modules) await fetchJSModules(config.js_modules);
26+
if (config.files) await fetchFiles(this, interpreter, config.files, baseURL);
27+
if (config.fetch) await fetchPaths(this, interpreter, config.fetch, baseURL);
28+
if (config.js_modules) await fetchJSModules(config.js_modules, baseURL);
2929
return interpreter;
3030
},
3131
// Fallback to globally defined module fields (i.e. $xworker)

esm/interpreter/wasmoon.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ export default {
1313
type,
1414
module: (version = '1.16.0') =>
1515
`https://cdn.jsdelivr.net/npm/wasmoon@${version}/+esm`,
16-
async engine({ LuaFactory, LuaLibraries }, config) {
16+
async engine({ LuaFactory, LuaLibraries }, config, _, baseURL) {
1717
const { stderr, stdout, get } = stdio();
1818
const interpreter = await get(new LuaFactory().createEngine());
1919
interpreter.global.getTable(LuaLibraries.Base, (index) => {
2020
interpreter.global.setField(index, 'print', stdout);
2121
interpreter.global.setField(index, 'printErr', stderr);
2222
});
23-
if (config.files) await fetchFiles(this, interpreter, config.files);
24-
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
25-
if (config.js_modules) await fetchJSModules(config.js_modules);
23+
if (config.files) await fetchFiles(this, interpreter, config.files, baseURL);
24+
if (config.fetch) await fetchPaths(this, interpreter, config.fetch, baseURL);
25+
if (config.js_modules) await fetchJSModules(config.js_modules, baseURL);
2626
return interpreter;
2727
},
2828
// Fallback to globally defined module fields

esm/interpreter/webr.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default {
2323
experimental: true,
2424
module: (version = '0.3.3') =>
2525
`https://cdn.jsdelivr.net/npm/webr@${version}/dist/webr.mjs`,
26-
async engine(module, config) {
26+
async engine(module, config, _, baseURL) {
2727
const { get } = stdio();
2828
const interpreter = new module.WebR();
2929
await get(interpreter.init().then(() => interpreter));
@@ -34,9 +34,9 @@ export default {
3434
destroy: shelter.destroy.bind(shelter),
3535
io: io.get(interpreter),
3636
});
37-
if (config.files) await fetchFiles(this, interpreter, config.files);
38-
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
39-
if (config.js_modules) await fetchJSModules(config.js_modules);
37+
if (config.files) await fetchFiles(this, interpreter, config.files, baseURL);
38+
if (config.fetch) await fetchPaths(this, interpreter, config.fetch, baseURL);
39+
if (config.js_modules) await fetchJSModules(config.js_modules, baseURL);
4040
return interpreter;
4141
},
4242
// Fallback to globally defined module fields (i.e. $xworker)

esm/interpreters.js

-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The :RUNTIMES comment is a delimiter and no code should be written/changed after
33
// See rollup/build_interpreters.cjs to know more
44

5-
import { base } from './interpreter/_utils.js';
6-
75
/** @type {Map<string, object>} */
86
export const registry = new Map();
97

@@ -35,14 +33,6 @@ export const interpreter = new Proxy(new Map(), {
3533
return (config, baseURL) =>
3634
module.then((module) => {
3735
configs.set(id, config);
38-
for (const entry of ['files', 'fetch']) {
39-
const value = config?.[entry];
40-
if (value) base.set(value, baseURL);
41-
}
42-
for (const entry of ['main', 'worker']) {
43-
const value = config?.js_modules?.[entry];
44-
if (value) base.set(value, baseURL);
45-
}
4636
return engine(module, config, url, baseURL);
4737
});
4838
},

esm/loader.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import fetch from '@webreflection/fetch';
22

33
import { interpreter } from './interpreters.js';
44
import { absoluteURL, resolve } from './utils.js';
5-
import { parse } from './toml.js';
5+
import { toml } from './3rd-party.js';
6+
7+
const { parse } = JSON;
68

79
// REQUIRES INTEGRATION TEST
810
/* c8 ignore start */
@@ -17,11 +19,11 @@ export const getConfigURLAndType = (config, configURL = './config.txt') => {
1719

1820
const parseString = config => {
1921
try {
20-
return JSON.parse(config);
22+
return parse(config);
2123
}
2224
// eslint-disable-next-line no-unused-vars
2325
catch (_) {
24-
return parse(config);
26+
return toml(config);
2527
}
2628
};
2729
/* c8 ignore stop */
@@ -45,7 +47,7 @@ export const getRuntime = (id, config, configURL, options = {}) => {
4547
if (type === 'json') {
4648
options = fetch(absolute).json();
4749
} else if (type === 'toml') {
48-
options = fetch(absolute).text().then(parse);
50+
options = fetch(absolute).text().then(toml);
4951
} else if (type === 'string') {
5052
options = parseString(config);
5153
} else if (type === 'object' && config) {

esm/toml.js

-7
This file was deleted.

esm/worker/_template.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ add('message', ({ data: { options, config: baseURL, configURL, code, hooks } })
157157

158158
// TODO: even this is problematic without SharedArrayBuffer
159159
// but let's see if we can manage to make it work somehow.
160-
const JSModules = createJSModules(window, sync, mainModules);
160+
const JSModules = createJSModules(window, sync, mainModules, baseURL);
161161

162162
registerJSModules(type, details, interpreter, JSModules);
163163
details.registerJSModule(interpreter, 'polyscript', {

esm/worker/js_modules.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { absoluteURL, entries, isArray, isCSS, js_modules } from '../utils.js';
2-
import { base } from '../interpreter/_utils.js';
32

43
const has = (modules, name) => modules.has(name);
54

@@ -27,10 +26,9 @@ const proxy = (modules, window, sync, baseURL) => new Proxy(modules, {
2726
},
2827
});
2928

30-
export default (window, sync, mainModules) => {
31-
let modules = globalThis[js_modules], baseURL = '';
29+
export default (window, sync, mainModules, baseURL) => {
30+
const modules = globalThis[js_modules];
3231
if (mainModules) {
33-
baseURL = base.get(mainModules);
3432
for (let [source, module] of entries(mainModules)) {
3533
let value = modules.get(module);
3634
if (!value || isArray(value)) {

esm/zip.js

-3
This file was deleted.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@
9090
"to-json-callback": "^0.1.1"
9191
},
9292
"worker": {
93-
"blob": "sha256-tXBm9wv1fC4fQxFx7pIU3PCgrGw1ba2FrPEhj9XaT4k="
93+
"blob": "sha256-zyHtCcMh+BRecLwN+h9Bpe2aAiwfPGh5iRcSyUTUQvY="
9494
}
9595
}

0 commit comments

Comments
 (0)