Commit 191ca2c7 authored by Michael Droettboom's avatar Michael Droettboom

Bulk of initial work.

parent 528b70e8
PYVERSION=3.5.2
PYMINOR=$(basename $(PYVERSION))
CPYTHON_EMSCRIPTEN_ROOT=../cpython-emscripten
CC=emcc
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 TOTAL_MEMORY=268435456 \
-s ASSERTIONS=2 \
-s EMULATE_FUNCTION_POINTER_CASTS=1 \
-s EXPORTED_FUNCTIONS='["_main"]' \
--memory-init-file 0
all: python.asm.js
python.asm.js: main.bc root
$(CC) --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 *.bc
%.bc: %.cpp $(CPYTHON_EMSCRIPTEN_ROOT)/installs/python-$(PYVERSION)/lib/python$(PYMINOR)
$(CXX) -o $@ $< $(CXXFLAGS)
root: $(CPYTHON_EMSCRIPTEN_ROOT)/installs/python-$(PYVERSION)/lib/python$(PYMINOR)
mkdir -p root/lib
cp -a $(CPYTHON_EMSCRIPTEN_ROOT)/installs/python-$(PYVERSION)/lib/python$(PYMINOR)/ root/lib
( \
cd root/lib/python$(PYMINOR); \
rm -fr test distutils ensurepip idlelib __pycache__ tkinter; \
)
$(CPYTHON_EMSCRIPTEN_ROOT)/installs/python-$(PYVERSION)/lib/python$(PYMINOR):
make -C $(CPYTHON_EMSCRIPTEN_ROOT)/$(PYVERSION)
# Calling Python from Javascript with value conversion
# Building
These instructions were tested on Linux. OSX should be substantively the same.
1. Build emscripten according to [these
instructions](https://developer.mozilla.org/en-US/docs/WebAssembly/C_to_wasm).
2. Enable the emscripten environment (`source emsdk_env.sh`)
3. Build [cpython-emscripten](https://github.com/dgym/cpython-emscripten):
1. Clone the git repository
2. cd into `3.5.2`, and type `make`.
4. Build this project.
[It assumes that `cpython-emscripten` was checked out and built in a
directory alongside this project. TODO: Provide a way to specify the
cpython-emscripten location]
Type `make`.
<!DOCTYPE html>
<html>
<!-- Quick and dirty tests... Not useful for anything else. -->
<head>
<meta charset="utf-8" />
</head>
<body>
<script src='python.asm.js'></script>
<script>
/* window.pyexec = Module.cwrap('PyRun_SimpleString', 'number', ['string']);*/
var doTest = function(code) {
var x = Module.runPython(code);
console.log("------")
console.log(code)
console.log("Return type " + typeof x)
console.log(JSON.stringify(x))
console.log()
}
var onReady = function() {
doTest("42");
doTest("42.0");
doTest("[0, 1, 32.0]")
doTest("{'foo': 42}")
doTest("'This is a string'")
doTest("def foo():\n return 42\n\nfoo()")
doTest("import sys\nsys.platform")
};
// Wait for the module to be ready before calling any functions.
var interval = setInterval(function () {
if (Module.calledRun) {
window.clearInterval(interval);
onReady();
}
}, 100);
</script>
</body>
</html>
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment