Skip to content

Commit 463618f

Browse files
Hood Chathamrth
Hood Chatham
andauthored
Use rollup (pyodide#1575)
Co-authored-by: Roman Yurchak <[email protected]>
1 parent 4e9d108 commit 463618f

25 files changed

+340
-15
lines changed

Makefile

+6-7
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ build/pyodide.asm.js: \
4242
src/pystone.py \
4343
src/_testcapi.py \
4444
src/webbrowser.py \
45-
$(wildcard src/pyodide-py/pyodide/*.py) \
45+
$(wildcard src/py/pyodide/*.py) \
4646
$(CPYTHONLIB)
4747
date +"[%F %T] Building pyodide.asm.js..."
4848
[ -d build ] || mkdir build
@@ -52,8 +52,8 @@ build/pyodide.asm.js: \
5252
--preload-file src/webbrowser.py@/lib/python$(PYMINOR)/webbrowser.py \
5353
--preload-file src/_testcapi.py@/lib/python$(PYMINOR)/_testcapi.py \
5454
--preload-file src/pystone.py@/lib/python$(PYMINOR)/pystone.py \
55-
--preload-file src/pyodide-py/pyodide@/lib/python$(PYMINOR)/site-packages/pyodide \
56-
--preload-file src/pyodide-py/_pyodide@/lib/python$(PYMINOR)/site-packages/_pyodide \
55+
--preload-file src/py/pyodide@/lib/python$(PYMINOR)/site-packages/pyodide \
56+
--preload-file src/py/_pyodide@/lib/python$(PYMINOR)/site-packages/_pyodide \
5757
--exclude-file "*__pycache__*" \
5858
--exclude-file "*/test/*" \
5959
--exclude-file "*/tests/*" \
@@ -78,10 +78,9 @@ env:
7878

7979

8080
.PHONY: build/pyodide.js
81-
build/pyodide.js: src/pyodide.js
82-
cp $< $@
83-
sed -i -e 's#{{ PYODIDE_BASE_URL }}#$(PYODIDE_BASE_URL)#g' $@
84-
81+
build/pyodide.js: src/js/*.js
82+
npm install ./src/js
83+
npx rollup -c src/js/rollup.config.js
8584

8685
build/test.html: src/templates/test.html
8786
cp $< $@

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
str(base_dir),
1515
str(base_dir / "pyodide-build"),
1616
str(base_dir / "docs/sphinx_pyodide"),
17-
str(base_dir / "src/pyodide-py"),
17+
str(base_dir / "src/py"),
1818
str(base_dir / "packages/micropip/micropip"),
1919
]
2020
sys.path = path_dirs + sys.path

docs/development/maintainers.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ the latest release branch named `stable` (due to ReadTheDocs constraints).
1414
`https://cdn.jsdelivr.net/pyodide/dev/full/` in `./docs/` replace `dev` with
1515
the release version `vX.Y.Z` (note the presence of the leading `v`). This
1616
also applies to `docs/conf.py`
17-
2. Set version in `src/pyodide-py/pyodide/__init__.py`
17+
2. Set version in `src/py/pyodide/__init__.py`
1818
3. Make sure the change log is up to date.
1919
- Indicate the release date in the change log.
2020
- Generate the list of contributors for the release at the end of the
@@ -53,7 +53,7 @@ the latest release branch named `stable` (due to ReadTheDocs constraints).
5353
```
5454
where `BB` is the last version of the `pyodide-env` Docker image.
5555
7. Revert Step 1. and increment the version in
56-
`src/pyodide-py/pyodide/__init__.py` to the next version specified by
56+
`src/py/pyodide/__init__.py` to the next version specified by
5757
Semantic Versioning.
5858

5959
### Making a minor release

repository-structure.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
The Pyodide runtime consists of the following components, sorted in terms of
2+
initialization-time (or import-time) dependencies.
3+
4+
1. CPython
5+
2. The py/_pyodide package which is a Python package with pure Python code
6+
avaiable in the inner stage of the Pyodide bootstrap process.
7+
3. The core/pyodide code, implemented in a mix of C and Javascript, which embeds
8+
the CPython interpreter in an emscripten application. This relies on
9+
py/pyodide and js/pyodide at runtime. The final stage of initialization is to
10+
import py/pyodide.
11+
4. The py/pyodide package which has Python code that is needed for the outer
12+
stage of the Pyodide bootstrap process. py/pyodide relies on core/pyodide at
13+
import time and relies on js/pyodide at runtime.
14+
5. The js/pyodide package which defines the Javascript public API, sets up the
15+
process of loading the core/pyodide emscripten application + CPython
16+
interpreter, and then completes the bootstrap by injecting the js/pyodide
17+
API into the Python `sys.modules`.
18+
6. The packages directory, which contains a large number of CPython packages
19+
built to run in Pyodide.
20+
21+
One of our long-term organizational goals is to carefully organize core/pyodide,
22+
py/_pyodide, py/pyodide, and js/pyodide to clarify which functionality is not part of
23+
runtime dependencies that define Pyodide's core behavior.

src/core/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.

src/core/main.c

+6
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ main(int argc, char** argv)
116116
FATAL_ERROR("JsRef doesn't have the same size as int.");
117117
}
118118

119+
PyObject* _pyodide = PyImport_ImportModule("_pyodide");
120+
if (_pyodide == NULL) {
121+
FATAL_ERROR("Failed to import pyodide module");
122+
}
123+
Py_CLEAR(_pyodide);
124+
119125
PyObject* core_module = NULL;
120126
core_module = PyModule_Create(&core_module_def);
121127
if (core_module == NULL) {

src/js/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# js/pyodide
2+
3+
The Javascript code in this folder is responsible for the following tasks:
4+
5+
1. Defines the public Javascript API
6+
- Package loading code to allow loading of other Python packages.
7+
- Can load micropip.py to bootstrap loading of pure Python wheels
8+
2. Loads the CPython interpreter and the core/pyodide emscripten application
9+
which embeds the interpreter.
10+
3. Injects the js/pyodide Javascript API into sys.modules. This is the final
11+
runtime dependency for core/pyodide & py/pyodide, so after this step the
12+
interpreter is fully up and running.

src/js/package-lock.json

+196
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/package.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name" : "pyodide",
3+
"version": "0.18.0dev0",
4+
"dependencies": {
5+
"prettier": "^2.2.1",
6+
"rollup-plugin-terser": "^7.0.2"
7+
}
8+
}

src/pyodide.js src/js/pyodide.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ globalThis.loadPyodide = async function (config = {}) {
3838
// Note: PYODIDE_BASE_URL is an environment variable replaced in
3939
// in this template in the Makefile. It's recommended to always set
4040
// indexURL in any case.
41-
let baseURL = config.indexURL || "{{ PYODIDE_BASE_URL }}";
41+
if (!config.indexURL) {
42+
throw new Error("Please provide indexURL parameter to loadPyodide");
43+
}
44+
let baseURL = config.indexURL;
4245
if (baseURL.endsWith(".js")) {
4346
baseURL = baseURL.substr(0, baseURL.lastIndexOf("/"));
4447
}

src/js/rollup.config.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { terser } from "rollup-plugin-terser";
2+
3+
function config({ input, format, minify, ext = "js" }) {
4+
const dir = `build/`;
5+
// const minifierSuffix = minify ? ".min" : "";
6+
const minifierSuffix = "";
7+
return {
8+
input: `./src/js/${input}.js`,
9+
output: {
10+
name: "loadPyodide",
11+
file: `${dir}/${input}${minifierSuffix}.${ext}`,
12+
format,
13+
sourcemap: true,
14+
},
15+
plugins: [
16+
minify
17+
? terser({
18+
compress: true,
19+
mangle: false,
20+
})
21+
: undefined,
22+
].filter(Boolean),
23+
};
24+
}
25+
26+
export default [
27+
// { input: "pyodide", format: "esm", minify: false, ext: "mjs" },
28+
{ input: "pyodide", format: "esm", minify: true, ext: "mjs" },
29+
// { input: "pyodide", format: "umd", minify: false },
30+
{ input: "pyodide", format: "umd", minify: true },
31+
].map(config);

0 commit comments

Comments
 (0)