Skip to content

Commit 49d125b

Browse files
committed
Initial attempt at empython
0 parents  commit 49d125b

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

Makefile

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
EMFLAGS=\
2+
--pre-js js/preJs.js --post-js js/postJs.js
3+
4+
EMEXPORTS=\
5+
-s EXPORTED_FUNCTIONS="['_Py_Initialize', '_PyRun_SimpleString']"
6+
7+
lp.js: lp.bc
8+
(cd Python-2.7.8/Lib/ && python ../../mapfiles.py .) > js/postJs.js
9+
cat js/postJs.js.in >> js/postJs.js
10+
emcc $(EMFLAGS) $(EMEXPORTS) $< -o $@
11+
12+
#lp.bc:
13+
# TODO
14+
15+
prep:
16+
cd tcl && git apply ../hacks.patch
17+
#TODO
18+
#cd tcl/unix && emconfigure ./configure --disable-threads --disable-load --disable-shared
19+
20+
#===================
21+
#
22+
#/usr/include/x86_64-linux-gnu
23+
#============
24+
25+
#sudo apt-get install gcc-multilib
26+
#
27+
#CONFFLAGS=""
28+
#CONFFLAGS="$CONFFLAGS --without-threads --disable-shared --without-signal-module --disable-ipv6"
29+
#
30+
#./configure
31+
#make Parser/pgen python
32+
#cp Makefile ../Makefile.native
33+
##cp Parser/pgen ../pgen.native
34+
#cp python ../python.native
35+
#make clean
36+
#git clean -f -x -d
37+
#
38+
#(export CFLAGS=-m32 && export LDFLAGS=-m32 && emconfigure ./configure $CONFFLAGS)
39+
#git apply ../hacks.patch
40+
#emmake make
41+
##cp ../pgen.native Parser/pgen
42+
#cp ../python.native python
43+
##chmod +x Parser/pgen
44+
#chmod +x python
45+
#emmake make
46+
#

hacks.patch

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/pyconfig.h.orig b/pyconfig.h
2+
--- a/pyconfig.h
3+
+++ b/pyconfig.h
4+
@@ -269,7 +269,7 @@
5+
6+
/* Define if we can use gcc inline assembler to get and set x87 control word
7+
*/
8+
-#define HAVE_GCC_ASM_FOR_X87 1
9+
+#undef HAVE_GCC_ASM_FOR_X87
10+
11+
/* Define if you have the getaddrinfo function. */
12+
#define HAVE_GETADDRINFO 1

js/postJs.js.in

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Module.FS = FS;
2+
Module.ENV = ENV;
3+
root['Module'] = Module;
4+
}
5+
};
6+
7+
root.empython();
8+
// Undo pollution of window
9+
delete window.Module;
10+
11+
// Init emscripten stuff
12+
root.Module.run();
13+
14+
root.Initialize = root.Module.cwrap('Py_Initialize', 'number', []);
15+
root.Run = root.Module.cwrap('PyRun_SimpleString', 'number', [
16+
'string' // string to eval
17+
]);
18+
19+
return root;
20+
21+
})();

js/preJs.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
this.empython = (function () {
2+
3+
var root = {
4+
empython: function () {
5+
var Module = {
6+
noInitialRun: true,
7+
noExitRuntime: true,
8+
preRun: [],
9+
postRun: []
10+
};

mapfiles.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python2
2+
3+
import os
4+
import sys
5+
6+
BASEDIR = '/lib/python2.7'
7+
8+
def main(root):
9+
os.chdir(root)
10+
commands = ['ENV["PYTHONHOME"] = "%s";' % (BASEDIR,)]
11+
commands.append('FS.createPath("%s", "%s", true, true);' % ('/', BASEDIR[1:]))
12+
for (dirpath, dirnames, filenames) in os.walk('.'):
13+
jsdir = os.path.abspath(os.path.join(BASEDIR, dirpath))
14+
for folder in dirnames:
15+
commands.append('FS.createFolder("%s", "%s", true, true);' % (jsdir, folder))
16+
for filename in filenames:
17+
if not filename.endswith('.py'): continue
18+
full_path = os.path.join(dirpath, filename)
19+
contents = ','.join(str(ord(i)) for i in open(full_path, 'rb').read())
20+
commands.append('FS.createDataFile("%s", "%s", [%s], true, true);' % (jsdir, filename, contents))
21+
22+
# Start out in a writeable folder.
23+
commands.append('FS.createFolder(".", "sandbox", true, true);')
24+
commands.append('FS.currentPath = "/sandbox";')
25+
26+
print '\n'.join(commands)
27+
28+
if __name__ == '__main__':
29+
if len(sys.argv) != 2:
30+
print 'Usage: %s root' % sys.argv[0]
31+
else:
32+
main('.')

0 commit comments

Comments
 (0)