| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,14 +9,23 @@ | |||
| 9 | 9 | * offered as-is, without any warranty. | |
| 10 | 10 | */ | |
| 11 | 11 | ||
| 12 | - #include <emscripten.h> | ||
| 13 | 12 | #include <Python.h> | |
| 14 | 13 | PyMODINIT_FUNC initemscripten(void); | |
| 15 | 14 | ||
| 16 | 15 | int main(int argc, char**argv) { | |
| 17 | - Py_OptimizeFlag = 2; // look for .pyo rather than .pyc | ||
| 18 | - Py_InitializeEx(0); // get rid of 'Calling stub instead of sigaction()' | ||
| 16 | + Py_OptimizeFlag = 2; // look for .pyo rather than .pyc | ||
| 17 | + Py_FrozenFlag = 1; // drop <exec_prefix> warnings | ||
| 18 | + Py_VerboseFlag = 1; // trace modules loading | ||
| 19 | + Py_InitializeEx(0); // 0 = get rid of 'Calling stub instead of sigaction()' | ||
| 19 | 20 | static struct _inittab builtins[] = { {"emscripten", initemscripten}, }; | |
| 20 | 21 | PyImport_ExtendInittab(builtins); | |
| 21 | 22 | return 0; | |
| 22 | 23 | } | |
| 24 | + | ||
| 25 | + // Run a line *and* display the result | ||
| 26 | + // PyRun_StringFlags only returns a non-None object with Py_eval_input (no 'print' support) | ||
| 27 | + // PyRun_InteractiveOne always reads stdin even with another 'fp', so we redirect stdin | ||
| 28 | + void pyruni() { | ||
| 29 | + freopen("/tmp/input.py", "rb", stdin); | ||
| 30 | + PyRun_InteractiveOne(stdin, "<stdin>"); | ||
| 31 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -64,7 +64,7 @@ | |||
| 64 | 64 | ||
| 65 | 65 | <hr/> | |
| 66 | 66 | <textarea class="emscripten" id="output" rows="8"></textarea> | |
| 67 | - <input type="text" class="emscripten" id="input" name="text" value="print({'hello':'world'})"></input> | ||
| 67 | + <input type="text" class="emscripten" id="input" name="text" value="{'hello':'world'}"></input> | ||
| 68 | 68 | <hr> | |
| 69 | 69 | ||
| 70 | 70 | <script type='text/javascript'> | |
@@ -160,26 +160,29 @@ | |||
| 160 | 160 | if (ev.key === 'Enter') { | |
| 161 | 161 | Module.print('>>> ' + input.value); | |
| 162 | 162 | ||
| 163 | - // Execute Python code | ||
| 164 | - Module.cwrap('PyRun_SimpleString', 'number', ['string'])(input.value); | ||
| 163 | + // Execute Python code | ||
| 164 | + // Module.cwrap('PyRun_SimpleString', 'number', ['string'])(input.value); | ||
| 165 | 165 | // or: Module.ccall('PyRun_SimpleString', 'number', ['string'], [input.value]); | |
| 166 | + FS.writeFile('/tmp/input.py', input.value + '\n') | ||
| 167 | + Module.ccall('pyruni'); | ||
| 166 | 168 | ||
| 167 | 169 | hist[hist.length-1] = input.value; | |
| 168 | - hist.push(''); | ||
| 169 | - hist_index = hist.length - 1; | ||
| 170 | + hist.push(''); | ||
| 171 | + hist_index = hist.length - 1; | ||
| 170 | 172 | input.value = ''; | |
| 171 | 173 | } else if (ev.key === 'ArrowUp') { | |
| 172 | 174 | hist[hist_index] = input.value; | |
| 173 | - hist_index = (hist_index > 0) ? hist_index-1 : hist_index; | ||
| 175 | + hist_index = (hist_index > 0) ? hist_index-1 : hist_index; | ||
| 174 | 176 | input.value = hist[hist_index]; | |
| 175 | 177 | } else if (ev.key === 'ArrowDown') { | |
| 176 | 178 | hist[hist_index] = input.value; | |
| 177 | - hist_index = (hist_index < hist.length-1) ? hist_index+1 : hist_index; | ||
| 179 | + hist_index = (hist_index < hist.length-1) ? hist_index+1 : hist_index; | ||
| 178 | 180 | input.value = hist[hist_index]; | |
| 179 | 181 | } | |
| 180 | 182 | }; | |
| 181 | 183 | input.focus(); | |
| 182 | 184 | </script> | |
| 185 | + <script type='text/javascript' src="pythonhome-data.js"></script> | ||
| 183 | 186 | ||
| 184 | 187 | {{{ SCRIPT }}} | |
| 185 | 188 | </body> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,13 +19,18 @@ mkdir -p $BUILD | |||
| 19 | 19 | ||
| 20 | 20 | cython ../emscripten.pyx -o $BUILD/emscripten.c | |
| 21 | 21 | emcc $BUILD/emscripten.c -o $BUILD/emscripten.bc -I $INSTALLDIR/include/python2.7 | |
| 22 | + PREFIX=$INSTALLDIR OUTDIR=$BUILD ./package-pythonhome.sh | ||
| 22 | 23 | emcc -o $BUILD/webprompt.html \ | |
| 23 | 24 | webprompt-main.c $BUILD/emscripten.c \ | |
| 24 | 25 | -I$INSTALLDIR/include/python2.7 -L$INSTALLDIR/lib -lpython2.7 \ | |
| 25 | 26 | -s EMULATE_FUNCTION_POINTER_CASTS=1 \ | |
| 26 | 27 | -s USE_ZLIB=1 \ | |
| 27 | 28 | -s TOTAL_MEMORY=256MB \ | |
| 28 | - --preload-file $INSTALLDIR/lib/python2.7@/lib/python2.7 \ | ||
| 29 | + -s FORCE_FILESYSTEM=1 \ | ||
| 29 | 30 | --shell-file webprompt-shell.html \ | |
| 30 | - -s EXPORTED_FUNCTIONS="['_main', '_malloc', '_Py_Initialize', '_PyRun_SimpleString']" \ | ||
| 31 | + -s EXPORTED_FUNCTIONS="['_main', '_malloc', '_Py_Initialize', '_PyRun_SimpleString', '_pyruni']" \ | ||
| 31 | 32 | -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' | |
| 33 | + | ||
| 34 | + # cython ../mock/emscripten.pyx -o t/mock.c | ||
| 35 | + # gcc -g -I build/hostpython/include/python2.7 -L build/hostpython/lib/ t/mock.c webprompt-main.c -lpython2.7 -ldl -lm -lutil -lz | ||
| 36 | + # PYTHONHOME=build/hostpython/ ./a.out | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,6 +11,8 @@ Usage: | |||
| 11 | 11 | ||
| 12 | 12 | See for instance [RenPyWeb](https://github.com/renpy/renpyweb). | |
| 13 | 13 | ||
| 14 | + Web demo: <https://www.beuc.net/python-emscripten/demo/> | ||
| 15 | + | ||
| 14 | 16 | Mirrors: | |
| 15 | 17 | ||
| 16 | 18 | - <https://gitlab.com/python-emscripten/python> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments