Fix #28: Work in Chrome through async compilation

This commit is contained in:
Michael Droettboom 2018-04-27 14:36:52 -04:00
parent f1e0cc9085
commit b2599e314c
2 changed files with 25 additions and 30 deletions

View File

@ -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

View File

@ -1,24 +1,20 @@
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 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();
}
Module.baseURL = baseURL;
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();
}
};
let script = document.createElement('script');
script.src = `${baseURL}pyodide.asm.js`;
script.onload = () => {
@ -26,16 +22,16 @@ var languagePluginLoader = new Promise((resolve, reject) => {
};
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