diff --git a/.gitignore b/.gitignore index a58ef21d7..a531f59e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ root/ -python.asm.* +pyodide.asm.* *.bc diff --git a/Makefile b/Makefile index 50a5a2f74..86060212b 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ CXX=em++ OPTFLAGS=-O2 CXXFLAGS=-std=c++14 $(OPTFLAGS) -g -I $(CPYTHON_EMSCRIPTEN_ROOT)/installs/python-$(PYVERSION)/include/python$(PYMINOR)/ -Wno-warn-absolute-paths LDFLAGS=$(OPTFLAGS) $(CPYTHON_EMSCRIPTEN_ROOT)/installs/python-$(PYVERSION)/lib/libpython$(PYMINOR).a \ + -s "BINARYEN_METHOD='native-wasm,interpret-binary,interpret-asm2wasm'" \ -s TOTAL_MEMORY=268435456 \ -s ASSERTIONS=2 \ -s EMULATE_FUNCTION_POINTER_CASTS=1 \ @@ -14,27 +15,22 @@ LDFLAGS=$(OPTFLAGS) $(CPYTHON_EMSCRIPTEN_ROOT)/installs/python-$(PYVERSION)/lib/ --memory-init-file 0 -all: python.asm.js +all: pyodide.asm.html -python.asm.js: main.bc root - $(CC) --bind -o $@ $(filter %.bc,$^) $(LDFLAGS) \ +pyodide.asm.html: main.bc root + $(CC) -s WASM=1 --bind -o $@ $(filter %.bc,$^) $(LDFLAGS) \ $(foreach d,$(wildcard root/*),--preload-file $d@/$(notdir $d)) -serve: python.asm.js - @echo "Serving on port 8062" - python -m SimpleHTTPServer 8062 - - clean: -rm -fr root - -rm python.asm.js python.asm.data python.asm.wasm + -rm pyodide.asm.* -rm *.bc %.bc: %.cpp $(CPYTHON_EMSCRIPTEN_ROOT)/installs/python-$(PYVERSION)/lib/python$(PYMINOR) - $(CXX) -o $@ $< $(CXXFLAGS) + $(CXX) --bind -o $@ $< $(CXXFLAGS) root: $(CPYTHON_EMSCRIPTEN_ROOT)/installs/python-$(PYVERSION)/lib/python$(PYMINOR) diff --git a/pyodide.js b/pyodide.js new file mode 100644 index 000000000..e7045b866 --- /dev/null +++ b/pyodide.js @@ -0,0 +1,36 @@ +var Module = {} + +{ + let wasmURL = 'pyodide.asm.wasm'; + let wasmXHR = new XMLHttpRequest(); + wasmXHR.open('GET', wasmURL, true); + wasmXHR.responseType = 'arraybuffer'; + wasmXHR.onload = function() { + if (wasmXHR.status === 200 || wasmXHR.status === 0) { + Module.wasmBinary = wasmXHR.response; + } else { + var wasmURLBytes = tryParseAsDataURI(wasmURL); + if (wasmURLBytes) { + Module.wasmBinary = wasmURLBytes.buffer; + } + } + + var memoryInitializer = 'pyodide.asm.html.mem'; + if (typeof Module['locateFile'] === 'function') { + memoryInitializer = Module['locateFile'](memoryInitializer); + } else if (Module['memoryInitializerPrefixURL']) { + memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer; + } + Module['memoryInitializerRequestURL'] = memoryInitializer; + var meminitXHR = Module['memoryInitializerRequest'] = new XMLHttpRequest(); + meminitXHR.open('GET', memoryInitializer, true); + meminitXHR.responseType = 'arraybuffer'; + meminitXHR.send(null); + + var script = document.createElement('script'); + script.src = "pyodide.asm.js"; + document.body.appendChild(script); + + }; + wasmXHR.send(null); +}