Use async functions for languageLoaderPlugin (#1239)

This allows us to use more conventional "modern" javascript.

In the future, I hope to make languageLoaderPlugin a function instead of
a promise (which would be very easy given this commit), so that users
can supply a custom Module object. One useful application would be to
supply stdout/stderr handlers. This would be a breaking change so I'll
leave it for antoher time.
This commit is contained in:
Dexter Chua 2021-02-14 16:56:09 +08:00 committed by GitHub
parent cce7b66b0d
commit b6e409876c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 25 deletions

View File

@ -2,7 +2,7 @@
* The main bootstrap script for loading pyodide.
*/
globalThis.languagePluginLoader = new Promise((resolve, reject) => {
globalThis.languagePluginLoader = (async () => {
let Module = {};
// Note: PYODIDE_BASE_URL is an environement variable replaced in
// in this template in the Makefile. It's recommended to always set
@ -619,7 +619,22 @@ globalThis.languagePluginLoader = new Promise((resolve, reject) => {
};
Module.locateFile = (path) => baseURL + path;
Module.postRun = async () => {
let moduleLoaded = new Promise(r => Module.postRun = r);
const scriptSrc = `${baseURL}pyodide.asm.js`;
await loadScript(scriptSrc);
// The emscripten module needs to be at this location for the core
// filesystem to install itself. Once that's complete, it will be replaced
// by the call to `makePublicAPI` with a more limited public API.
self.pyodide = await pyodide(Module);
// There is some work to be done between the module being "ready" and postRun
// being called.
await moduleLoaded;
// Unfortunately the indentation here matters.
Module.runPythonSimple(`
def temp(Module):
@ -638,22 +653,11 @@ def temp(Module):
delete self.Module;
let response = await fetch(`${baseURL}packages.json`);
let json = await response.json();
Module.packages = await response.json();
fixRecursionLimit(self.pyodide);
self.pyodide = makePublicAPI(self.pyodide, PUBLIC_API);
self.pyodide.registerJsModule("js", globalThis);
self.pyodide.registerJsModule("pyodide_js", self.pyodide);
Module.packages = json;
resolve();
};
const scriptSrc = `${baseURL}pyodide.asm.js`;
loadScript(scriptSrc).then(async () => {
// The emscripten module needs to be at this location for the core
// filesystem to install itself. Once that's complete, it will be replaced
// by the call to `makePublicAPI` with a more limited public API.
self.pyodide = await pyodide(Module);
});
});
})();
languagePluginLoader