Skip to content

Removed the ugly base WeakMap workaround #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2024
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
4 changes: 2 additions & 2 deletions docs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.js.map

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions esm/3rd-party.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* c8 ignore start */

// toml
export const toml = async (text) => (
await import(/* webpackIgnore: true */'./3rd-party/toml.js')
).parse(text);

// zip
export const zip = () => import(/* webpackIgnore: true */'./3rd-party/zip.js');

/* c8 ignore stop */
20 changes: 9 additions & 11 deletions esm/interpreter/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,13 @@ const joinPaths = (parts) => {
return parts[0].startsWith('/') ? `/${res}` : res;
};

const fetchBuffer = (config_fetch, url) =>
fetch(absoluteURL(url, base.get(config_fetch))).arrayBuffer();
const fetchBuffer = (url, baseURL) =>
fetch(absoluteURL(url, baseURL)).arrayBuffer();

export const base = new WeakMap();

export const fetchPaths = (module, interpreter, config_fetch) =>
export const fetchPaths = (module, interpreter, config_fetch, baseURL) =>
all(
calculateFetchPaths(config_fetch).map(({ url, path }) =>
fetchBuffer(config_fetch, url)
fetchBuffer(url, baseURL)
.then((buffer) => module.writeFile(interpreter, path, buffer)),
),
);
Expand Down Expand Up @@ -139,10 +137,10 @@ const calculateFilesPaths = files => {
return sourceDest;
};

export const fetchFiles = (module, interpreter, config_files) =>
export const fetchFiles = (module, interpreter, config_files, baseURL) =>
all(
calculateFilesPaths(config_files).map(({ url, path }) =>
fetchBuffer(config_files, url)
fetchBuffer(url, baseURL)
.then((buffer) => module.writeFile(
interpreter,
path,
Expand All @@ -152,17 +150,17 @@ export const fetchFiles = (module, interpreter, config_files) =>
),
);

export const fetchJSModules = ({ main, worker }) => {
export const fetchJSModules = ({ main, worker }, baseURL) => {
const promises = [];
if (worker && RUNNING_IN_WORKER) {
for (let [source, name] of entries(worker)) {
source = absoluteURL(source, base.get(worker));
source = absoluteURL(source, baseURL);
promises.push(importJS(source, name));
}
}
if (main && !RUNNING_IN_WORKER) {
for (let [source, name] of entries(main)) {
source = absoluteURL(source, base.get(main));
source = absoluteURL(source, baseURL);
if (isCSS(source)) importCSS(source);
else promises.push(importJS(source, name));
}
Expand Down
8 changes: 4 additions & 4 deletions esm/interpreter/micropython.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getFormat, loader, registerJSModule, run, runAsync, runEvent } from './
import { stdio, buffered } from './_io.js';
import { absoluteURL } from '../utils.js';
import mip from '../python/mip.js';
import zip from '../zip.js';
import { zip } from '../3rd-party.js';

const type = 'micropython';

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

// Install Micropython Package
this.writeFile(interpreter, './mip.py', mip);
Expand Down
8 changes: 4 additions & 4 deletions esm/interpreter/pyodide.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default {
type,
module: (version = '0.26.0') =>
`https://cdn.jsdelivr.net/pyodide/v${version}/full/pyodide.mjs`,
async engine({ loadPyodide }, config, url) {
async engine({ loadPyodide }, config, url, baseURL) {
// apply override ASAP then load foreign code
if (!RUNNING_IN_WORKER && config.experimental_create_proxy === 'auto')
applyOverride();
Expand All @@ -91,9 +91,9 @@ export default {
);
const py_imports = importPackages.bind(interpreter);
loader.set(interpreter, py_imports);
if (config.files) await fetchFiles(this, interpreter, config.files);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
if (config.js_modules) await fetchJSModules(config.js_modules);
if (config.files) await fetchFiles(this, interpreter, config.files, baseURL);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch, baseURL);
if (config.js_modules) await fetchJSModules(config.js_modules, baseURL);
if (config.packages) await py_imports(config.packages);
return interpreter;
},
Expand Down
8 changes: 4 additions & 4 deletions esm/interpreter/ruby-wasm-wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export default {
experimental: true,
module: (version = '2.6.1') =>
`https://cdn.jsdelivr.net/npm/@ruby/3.2-wasm-wasi@${version}/dist/browser/+esm`,
async engine({ DefaultRubyVM }, config, url) {
async engine({ DefaultRubyVM }, config, url, baseURL) {
url = url.replace(/\/browser\/\+esm$/, '/ruby.wasm');
const buffer = await fetch(url).arrayBuffer();
const module = await WebAssembly.compile(buffer);
const { vm: interpreter } = await DefaultRubyVM(module);
if (config.files) await fetchFiles(this, interpreter, config.files);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
if (config.js_modules) await fetchJSModules(config.js_modules);
if (config.files) await fetchFiles(this, interpreter, config.files, baseURL);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch, baseURL);
if (config.js_modules) await fetchJSModules(config.js_modules, baseURL);
return interpreter;
},
// Fallback to globally defined module fields (i.e. $xworker)
Expand Down
8 changes: 4 additions & 4 deletions esm/interpreter/wasmoon.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ export default {
type,
module: (version = '1.16.0') =>
`https://cdn.jsdelivr.net/npm/wasmoon@${version}/+esm`,
async engine({ LuaFactory, LuaLibraries }, config) {
async engine({ LuaFactory, LuaLibraries }, config, _, baseURL) {
const { stderr, stdout, get } = stdio();
const interpreter = await get(new LuaFactory().createEngine());
interpreter.global.getTable(LuaLibraries.Base, (index) => {
interpreter.global.setField(index, 'print', stdout);
interpreter.global.setField(index, 'printErr', stderr);
});
if (config.files) await fetchFiles(this, interpreter, config.files);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
if (config.js_modules) await fetchJSModules(config.js_modules);
if (config.files) await fetchFiles(this, interpreter, config.files, baseURL);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch, baseURL);
if (config.js_modules) await fetchJSModules(config.js_modules, baseURL);
return interpreter;
},
// Fallback to globally defined module fields
Expand Down
8 changes: 4 additions & 4 deletions esm/interpreter/webr.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default {
experimental: true,
module: (version = '0.3.3') =>
`https://cdn.jsdelivr.net/npm/webr@${version}/dist/webr.mjs`,
async engine(module, config) {
async engine(module, config, _, baseURL) {
const { get } = stdio();
const interpreter = new module.WebR();
await get(interpreter.init().then(() => interpreter));
Expand All @@ -34,9 +34,9 @@ export default {
destroy: shelter.destroy.bind(shelter),
io: io.get(interpreter),
});
if (config.files) await fetchFiles(this, interpreter, config.files);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
if (config.js_modules) await fetchJSModules(config.js_modules);
if (config.files) await fetchFiles(this, interpreter, config.files, baseURL);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch, baseURL);
if (config.js_modules) await fetchJSModules(config.js_modules, baseURL);
return interpreter;
},
// Fallback to globally defined module fields (i.e. $xworker)
Expand Down
10 changes: 0 additions & 10 deletions esm/interpreters.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The :RUNTIMES comment is a delimiter and no code should be written/changed after
// See rollup/build_interpreters.cjs to know more

import { base } from './interpreter/_utils.js';

/** @type {Map<string, object>} */
export const registry = new Map();

Expand Down Expand Up @@ -35,14 +33,6 @@ export const interpreter = new Proxy(new Map(), {
return (config, baseURL) =>
module.then((module) => {
configs.set(id, config);
for (const entry of ['files', 'fetch']) {
const value = config?.[entry];
if (value) base.set(value, baseURL);
}
for (const entry of ['main', 'worker']) {
const value = config?.js_modules?.[entry];
if (value) base.set(value, baseURL);
}
return engine(module, config, url, baseURL);
});
},
Expand Down
10 changes: 6 additions & 4 deletions esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import fetch from '@webreflection/fetch';

import { interpreter } from './interpreters.js';
import { absoluteURL, resolve } from './utils.js';
import { parse } from './toml.js';
import { toml } from './3rd-party.js';

const { parse } = JSON;

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

const parseString = config => {
try {
return JSON.parse(config);
return parse(config);
}
// eslint-disable-next-line no-unused-vars
catch (_) {
return parse(config);
return toml(config);
}
};
/* c8 ignore stop */
Expand All @@ -45,7 +47,7 @@ export const getRuntime = (id, config, configURL, options = {}) => {
if (type === 'json') {
options = fetch(absolute).json();
} else if (type === 'toml') {
options = fetch(absolute).text().then(parse);
options = fetch(absolute).text().then(toml);
} else if (type === 'string') {
options = parseString(config);
} else if (type === 'object' && config) {
Expand Down
7 changes: 0 additions & 7 deletions esm/toml.js

This file was deleted.

2 changes: 1 addition & 1 deletion esm/worker/_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ add('message', ({ data: { options, config: baseURL, configURL, code, hooks } })

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

registerJSModules(type, details, interpreter, JSModules);
details.registerJSModule(interpreter, 'polyscript', {
Expand Down
6 changes: 2 additions & 4 deletions esm/worker/js_modules.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { absoluteURL, entries, isArray, isCSS, js_modules } from '../utils.js';
import { base } from '../interpreter/_utils.js';

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

Expand Down Expand Up @@ -27,10 +26,9 @@ const proxy = (modules, window, sync, baseURL) => new Proxy(modules, {
},
});

export default (window, sync, mainModules) => {
let modules = globalThis[js_modules], baseURL = '';
export default (window, sync, mainModules, baseURL) => {
const modules = globalThis[js_modules];
if (mainModules) {
baseURL = base.get(mainModules);
for (let [source, module] of entries(mainModules)) {
let value = modules.get(module);
if (!value || isArray(value)) {
Expand Down
3 changes: 0 additions & 3 deletions esm/zip.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@
"to-json-callback": "^0.1.1"
},
"worker": {
"blob": "sha256-tXBm9wv1fC4fQxFx7pIU3PCgrGw1ba2FrPEhj9XaT4k="
"blob": "sha256-zyHtCcMh+BRecLwN+h9Bpe2aAiwfPGh5iRcSyUTUQvY="
}
}