Export PATH and ERRNO_CODES from Emscripten (#2582)

BrowserFS can mount custom filesystems into Emscripten. 
However it requires the PATH and ERRNO_CODES exports from
Emscripten in addition to FS.

This exports `PATH` and `ERRNO_CODES` from `Module` into the `pyodide`
Javascript API so they can be used with BrowserFS.
This commit is contained in:
Kyle Cutler 2022-05-19 11:02:52 -07:00 committed by GitHub
parent 829023c9af
commit 7d3c724665
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 8 deletions

View File

@ -113,7 +113,8 @@ export MAIN_MODULE_LDFLAGS= $(LDFLAGS_BASE) \
--exclude-file "*/tests/*" \
--exclude-file "*/distutils/*" \
--pre-js src/core/pre.js \
--pre-js src/js/_pyodide.out.js
--pre-js src/js/_pyodide.out.js \
--post-js src/core/post.js
export SIDE_MODULE_CXXFLAGS = $(CXXFLAGS_BASE)

View File

@ -86,6 +86,9 @@ substitutions:
`clear_interval`, `add_event_listener` and `remove_event_listener` for the corresponding JavaScript functions.
{pr}`2456`
- {{ Enhancement }} Pyodide now directly exposes the Emscripten `PATH` and `ERRNO_CODES` APIs.
{pr}`2582`
### micropip
- {{ Fix }} micropip now correctly handles package names that include dashes

View File

@ -90,13 +90,6 @@ static struct PyModuleDef core_module_def = {
int
main(int argc, char** argv)
{
EM_ASM({
// For some reason emscripten doesn't make UTF8ToString available on Module
// by default...
Module.UTF8ToString = UTF8ToString;
Module.wasmTable = wasmTable;
});
// This exits and prints a message to stderr on failure,
// no status code to check.
initialize_python();

5
src/core/post.js Normal file
View File

@ -0,0 +1,5 @@
// Emscripten doesn't make UTF8ToString or wasmTable available on Module by default...
Module.UTF8ToString = UTF8ToString;
Module.wasmTable = wasmTable;
// Emscripten has a bug where it accidentally exposes an empty object as Module.ERRNO_CODES
Module.ERRNO_CODES = ERRNO_CODES;

View File

@ -449,6 +449,8 @@ export function checkInterrupt() {
export type PyodideInterface = {
globals: typeof globals;
FS: typeof FS;
PATH: typeof PATH;
ERRNO_CODES: typeof ERRNO_CODES;
pyodide_py: typeof pyodide_py;
version: typeof version;
loadPackage: typeof loadPackage;
@ -486,14 +488,32 @@ export type PyodideInterface = {
*/
export let FS: any;
/**
* An alias to the `Emscripten Path API
* <https://github.com/emscripten-core/emscripten/blob/main/src/library_path.js>`_.
*
* This provides a variety of operations for working with file system paths, such as
* ``dirname``, ``normalize``, and ``splitPath``.
*/
export let PATH: any;
/**
* An alias to the Emscripten ERRNO_CODES map of standard error codes.
*/
export let ERRNO_CODES: any;
/**
* @private
*/
API.makePublicAPI = function (): PyodideInterface {
FS = Module.FS;
PATH = Module.PATH;
ERRNO_CODES = Module.ERRNO_CODES;
let namespace = {
globals,
FS,
PATH,
ERRNO_CODES,
pyodide_py,
version,
loadPackage,

View File

@ -25,3 +25,24 @@ describe("FS", () => {
chai.assert.isTrue(result);
});
});
describe("PATH", () => {
it("exists", async () => {
chai.assert.exists(pyodide.PATH);
});
it("has expected keys", async () => {
chai.assert.exists(pyodide.PATH.dirname);
chai.assert.exists(pyodide.PATH.normalize);
});
});
describe("ERRNO_CODES", () => {
it("exists", async () => {
chai.assert.exists(pyodide.ERRNO_CODES);
});
it("has expected keys", async () => {
chai.assert.exists(pyodide.ERRNO_CODES.ENOENT);
chai.assert.exists(pyodide.ERRNO_CODES.EPERM);
chai.assert.exists(pyodide.ERRNO_CODES.EEXIST);
});
});