From b2599e314c0916af8f8630d5d4615175f09732ec Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Fri, 27 Apr 2018 14:36:52 -0400 Subject: [PATCH] Fix #28: Work in Chrome through async compilation --- Makefile | 2 +- src/pyodide.js | 53 +++++++++++++++++++++++--------------------------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index e15f92b0d..4a512a8ae 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ LDFLAGS=\ -s EMULATE_FUNCTION_POINTER_CASTS=1 \ -s EXPORTED_FUNCTIONS='["_main"]' \ -s WASM=1 \ + -s SWAPPABLE_ASM_MODULE=1 \ --memory-init-file 0 NUMPY_ROOT=numpy/build/numpy @@ -63,7 +64,6 @@ build/pyodide.asm.html: src/main.bc src/jsimport.bc src/jsproxy.bc src/js2python [ -d build ] || mkdir build $(CC) -s EXPORT_NAME="'pyodide'" --bind -o $@ $(filter %.bc,$^) $(LDFLAGS) \ $(foreach d,$(wildcard root/*),--preload-file $d@/$(notdir $d)) - sed -i -e "s#REMOTE_PACKAGE_BASE = 'pyodide.asm.data'#REMOTE_PACKAGE_BASE = pyodide.baseURL + 'pyodide.asm.data'#g" build/pyodide.asm.js build/pyodide_dev.js: src/pyodide.js diff --git a/src/pyodide.js b/src/pyodide.js index 0582b0e48..7a08db6b5 100644 --- a/src/pyodide.js +++ b/src/pyodide.js @@ -1,41 +1,37 @@ var languagePluginLoader = new Promise((resolve, reject) => { let baseURL = "{{DEPLOY}}"; - let wasmURL = `${baseURL}pyodide.asm.wasm?x=${Date.now()}`; - let wasmXHR = new XMLHttpRequest(); - wasmXHR.open('GET', wasmURL, true); - wasmXHR.responseType = 'arraybuffer'; - wasmXHR.onload = function() { - let Module = {}; + let wasmURL = `${baseURL}pyodide.asm.wasm`; + let Module = {}; - if (wasmXHR.status === 200 || wasmXHR.status === 0) { - Module.wasmBinary = wasmXHR.response; - } else { - console.warn( - `Couldn't download the pyodide.asm.wasm binary. Response was ${wasmXHR.status}`); - reject(); - } + let wasm_promise = WebAssembly.compileStreaming(fetch(wasmURL)); + Module.instantiateWasm = (info, receiveInstance) => { + wasm_promise + .then(module => WebAssembly.instantiate(module, info)) + .then(instance => receiveInstance(instance)); + return {}; + }; + Module.filePackagePrefixURL = baseURL; + Module.postRun = () => { + resolve(); + }; - Module.baseURL = baseURL; - Module.postRun = () => { - resolve(); - } - let script = document.createElement('script'); - script.src = `${baseURL}pyodide.asm.js`; - script.onload = () => { - window.pyodide = pyodide(Module); - }; - document.body.appendChild(script); + let script = document.createElement('script'); + script.src = `${baseURL}pyodide.asm.js`; + script.onload = () => { + window.pyodide = pyodide(Module); + }; + document.body.appendChild(script); + if (window.iodide !== undefined) { + // Load the custom CSS for Pyodide let link = document.createElement('link'); link.rel = 'stylesheet'; link.type = 'text/css'; link.href = `${baseURL}renderedhtml.css`; document.getElementsByTagName('head')[0].appendChild(link); - }; - wasmXHR.send(null); - if (window.iodide !== undefined) { - const py_output_handler = { + // Add a custom output handler for Python objects + window.iodide.addOutputHandler({ shouldHandle: (val) => { return (typeof val === 'object' && val['$$'] !== undefined && @@ -55,8 +51,7 @@ var languagePluginLoader = new Promise((resolve, reject) => { } return div; } - }; - window.iodide.addOutputHandler(py_output_handler); + }); } }); languagePluginLoader