Skip to content

Commit b0c0ac1

Browse files
authored
0.5 sync (#48)
1 parent dd95bff commit b0c0ac1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+807
-152
lines changed

tech-preview/pyscript.core/README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
---
66

7+
## Documentation
8+
9+
Please read [the documentation page](./docs/README.md) to know all the user-facing details around this module.
10+
711
## Development
812

9-
The working folder (source code of truth) is the `./esm` one, while the `./cjs` is populated as dual module and to test (but it's 1:1 code, no trnaspilation except for imports/exports).
13+
The working folder (source code of truth) is the `./esm` one, while the `./cjs` is populated as dual module and to test (but it's 1:1 code, no transpilation except for imports/exports).
1014

1115
```sh
1216
# install all dependencies needed by core
@@ -19,7 +23,7 @@ This project requires some automatic artifact creation to:
1923

2024
* create a _Worker_ as a _Blob_ based on the same code used by this repo
2125
* create automatically the list of runtimes available via the module
22-
* create the `core.js` file used by most integration tests
26+
* create the `core.js` or the `pyscript.js` file used by most integration tests
2327
* create a sha256 version of the Blob content for CSP cases
2428

2529
Accordingly, to build latest project:
@@ -31,3 +35,11 @@ npm run build
3135
# optionally spin a server with CORS, COOP, and COEP enabled
3236
npm run server
3337
```
38+
39+
If **no minification** is desired or helpful while debugging potential issues, please use `NO_MIN=1` in front of the _build_ step:
40+
41+
```sh
42+
NO_MIN=1 npm run build
43+
44+
npm run server
45+
```

tech-preview/pyscript.core/cjs/custom.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const handleCustomType = (node) => {
4848
version,
4949
config,
5050
env,
51-
onRuntimeReady,
51+
onInterpreterReady,
5252
} = options;
5353
const name = getRuntimeID(runtime, version);
5454
const id = env || `${name}${config ? `|${config}` : ""}`;
@@ -69,7 +69,7 @@ const handleCustomType = (node) => {
6969
onAfterRunAsync,
7070
} = options;
7171

72-
const hooks = new Hook(options);
72+
const hooks = new Hook(interpreter, options);
7373

7474
const XWorker = function XWorker(...args) {
7575
return Worker.apply(hooks, args);
@@ -123,7 +123,7 @@ const handleCustomType = (node) => {
123123

124124
resolve(resolved);
125125

126-
onRuntimeReady?.(resolved, node);
126+
onInterpreterReady?.(resolved, node);
127127
});
128128
}
129129
}
@@ -137,17 +137,17 @@ exports.handleCustomType = handleCustomType;
137137
const registry = new Map();
138138

139139
/**
140-
* @typedef {Object} PluginOptions custom configuration
140+
* @typedef {Object} CustomOptions custom configuration
141141
* @prop {'pyodide' | 'micropython' | 'wasmoon' | 'ruby-wasm-wasi'} interpreter the interpreter to use
142142
* @prop {string} [version] the optional interpreter version to use
143143
* @prop {string} [config] the optional config to use within such interpreter
144-
* @prop {(environment: object, node: Element) => void} [onRuntimeReady] the callback that will be invoked once
144+
* @prop {(environment: object, node: Element) => void} [onInterpreterReady] the callback that will be invoked once
145145
*/
146146

147147
/**
148148
* Allows custom types and components on the page to receive interpreters to execute any code
149149
* @param {string} type the unique `<script type="...">` identifier
150-
* @param {PluginOptions} options the custom type configuration
150+
* @param {CustomOptions} options the custom type configuration
151151
*/
152152
const define = (type, options) => {
153153
if (defaultRegistry.has(type) || registry.has(type))

tech-preview/pyscript.core/cjs/custom/pyscript.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ document.head.appendChild(document.createElement("style")).textContent = `
115115
onAfterRunAsync(pyodide, element) {
116116
bootstrapNodeAndPlugins(pyodide, element, after, "onAfterRunAsync");
117117
},
118-
async onRuntimeReady(pyodide, element) {
118+
async onInterpreterReady(pyodide, element) {
119119
// allows plugins to do whatever they want with the element
120120
// before regular stuff happens in here
121-
for (const callback of hooks.onRuntimeReady)
121+
for (const callback of hooks.onInterpreterReady)
122122
callback(pyodide, element);
123123
if (isScript(element)) {
124124
const {
@@ -179,7 +179,7 @@ const hooks = {
179179
/** @type {Set<function>} */
180180
onAfterRunAsync: new Set(),
181181
/** @type {Set<function>} */
182-
onRuntimeReady: new Set(),
182+
onInterpreterReady: new Set(),
183183

184184
/** @type {Set<string>} */
185185
codeBeforeRunWorker: new Set(),

tech-preview/pyscript.core/cjs/interpreter/_python.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ const runAsync = (interpreter, code) =>
1010
interpreter.runPythonAsync(clean(code));
1111
exports.runAsync = runAsync;
1212

13-
const setGlobal = (interpreter, name, value) =>
13+
const setGlobal = (interpreter, name, value) => {
1414
interpreter.globals.set(name, value);
15+
};
1516
exports.setGlobal = setGlobal;
1617

17-
const deleteGlobal = (interpreter, name) =>
18+
const deleteGlobal = (interpreter, name) => {
1819
interpreter.globals.delete(name);
20+
};
1921
exports.deleteGlobal = deleteGlobal;
2022

2123
const writeFile = ({ FS }, path, buffer) =>
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
'use strict';
22
const { fetchPaths, stdio } = require("./_utils.js");
3-
const {
4-
run,
5-
runAsync,
6-
setGlobal,
7-
deleteGlobal,
8-
writeFile
9-
} = require("./_python.js");
3+
const { run, setGlobal, deleteGlobal, writeFile } = require("./_python.js");
104

115
const type = "micropython";
126

137
// REQUIRES INTEGRATION TEST
148
/* c8 ignore start */
159
module.exports = {
1610
type,
17-
module: (version = "1.20.0-253") =>
11+
module: (version = "1.20.0-268") =>
1812
`https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@${version}/micropython.mjs`,
1913
async engine({ loadMicroPython }, config, url) {
2014
const { stderr, stdout, get } = stdio();
2115
url = url.replace(/\.m?js$/, ".wasm");
22-
const runtime = await get(loadMicroPython({ stderr, stdout, url }));
23-
if (config.fetch) await fetchPaths(this, runtime, config.fetch);
24-
return runtime;
16+
const interpreter = await get(loadMicroPython({ stderr, stdout, url }));
17+
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
18+
return interpreter;
2519
},
2620
setGlobal,
2721
deleteGlobal,
2822
run,
29-
runAsync,
23+
// TODO: MicroPython doesn't have a Pyodide like top-level await,
24+
// this method should still not throw errors once invoked
25+
async runAsync(...args) {
26+
return this.run(...args);
27+
},
3028
writeFile,
3129
};
3230
/* c8 ignore stop */

tech-preview/pyscript.core/cjs/worker/class.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,24 @@ module.exports = (...args) =>
2323
function XWorker(url, options) {
2424
const worker = xworker();
2525
const { postMessage } = worker;
26+
const isHook = this instanceof Hook;
27+
2628
if (args.length) {
2729
const [type, version] = args;
2830
options = assign({}, options || { type, version });
2931
if (!options.type) options.type = type;
3032
}
33+
3134
if (options?.config) options.config = absoluteURL(options.config);
35+
3236
const bootstrap = fetch(url)
3337
.then(getText)
3438
.then((code) => {
35-
const hooks = this instanceof Hook ? this : void 0;
39+
const hooks = isHook ? this.stringHooks : void 0;
3640
postMessage.call(worker, { options, code, hooks });
3741
});
38-
return defineProperties(worker, {
42+
43+
defineProperties(worker, {
3944
postMessage: {
4045
value: (data, ...rest) =>
4146
bootstrap.then(() =>
@@ -46,4 +51,8 @@ module.exports = (...args) =>
4651
value: coincident(worker, JSON).proxy,
4752
},
4853
});
54+
55+
if (isHook) this.onWorkerReady?.(this.interpreter, worker);
56+
57+
return worker;
4958
};

tech-preview/pyscript.core/cjs/worker/hooks.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@ const workerHooks = [
99
];
1010

1111
class Hook {
12-
constructor(fields) {
13-
for (const [key, value] of workerHooks) this[key] = fields[value]?.();
12+
constructor(interpreter, options) {
13+
this.interpreter = interpreter;
14+
this.onWorkerReady = options.onWorkerReady;
15+
for (const [key, value] of workerHooks) this[key] = options[value]?.();
16+
}
17+
get stringHooks() {
18+
const hooks = {};
19+
for (const [key] of workerHooks) {
20+
if (this[key]) hooks[key] = this[key];
21+
}
22+
return hooks;
1423
}
1524
}
1625
exports.Hook = Hook

0 commit comments

Comments
 (0)