diff --git a/src/pyodide.js b/src/pyodide.js index f4cac2fbf..92f8e84f5 100644 --- a/src/pyodide.js +++ b/src/pyodide.js @@ -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,9 +619,24 @@ globalThis.languagePluginLoader = new Promise((resolve, reject) => { }; Module.locateFile = (path) => baseURL + path; - Module.postRun = async () => { - // Unfortunately the indentation here matters. - Module.runPythonSimple(` + + 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): import pyodide import __main__ @@ -633,27 +648,16 @@ def temp(Module): Module.version = pyodide.__version__ Module.globals = globals Module.pyodide_py = pyodide - `); - Module.init_dict["temp"](Module); +`); + Module.init_dict["temp"](Module); - delete self.Module; - let response = await fetch(`${baseURL}packages.json`); - let json = await response.json(); + delete self.Module; + let response = await fetch(`${baseURL}packages.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); - }); -}); + fixRecursionLimit(self.pyodide); + self.pyodide = makePublicAPI(self.pyodide, PUBLIC_API); + self.pyodide.registerJsModule("js", globalThis); + self.pyodide.registerJsModule("pyodide_js", self.pyodide); +})(); languagePluginLoader