|
| 1 | +# core/pyodide |
| 2 | + |
| 3 | +The C and Javascript code in this package is responsible for embedding the |
| 4 | +Python interpreter in our emscripten js/wasm application. The primary purpose of |
| 5 | +this code is to define the foreign function interface between Python and |
| 6 | +Javascript. Once this foreign function interface is defined, more complex |
| 7 | +behaviors are better defined in Python when possible for easier development and |
| 8 | +debugging. |
| 9 | + |
| 10 | +In particular, when possible logic should be moved from core/pyodide to |
| 11 | +py/_pyodide or to py/pyodide. |
| 12 | + |
| 13 | +The core/pyodide code is responsible for the following main steps: |
| 14 | + |
| 15 | +1. Initialize the CPython interpreter |
| 16 | +2. Import py/_pyodide |
| 17 | +3. Initialize `_pyodide_core` which is a Python C extension that we use to make |
| 18 | + functionality available to py/pyodide. |
| 19 | +4. Set up functionality to automatically convert functions from Javascript to |
| 20 | + CPython calling conventions (`error_handling.h`). |
| 21 | +5. Set up the "hiwire" side table to hold references to Javascript objects -- |
| 22 | + necessary because wasm variables can only hold numbers (`hiwire.c`). |
| 23 | +6. Set up type conversions of basic types between Python and Javascript |
| 24 | + (`js2python.c` and `python2js.c`). |
| 25 | +7. Set up Proxying of remaining types between Python and Javascript (`jsproxy.c` |
| 26 | + and `pyproxy.c`). This is the most complicated part of the Pyodide runtime |
| 27 | + and involves careful conversion between [abstract Javascript object |
| 28 | + protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) |
| 29 | + (see also [Javascript Iteration |
| 30 | + Protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) |
| 31 | + ) |
| 32 | + and [Python object protocols](https://docs.python.org/3/c-api/abstract.html). |
| 33 | +8. Add `_pyodide_core` to `sys.modules` and import py/pyodide. |
0 commit comments