Skip to content

Instantly share code, notes, and snippets.

@Mec-iS
Last active May 24, 2019 11:21
Show Gist options
  • Save Mec-iS/7ac190d7b46dad71e4a5691eb70e5ab9 to your computer and use it in GitHub Desktop.
Save Mec-iS/7ac190d7b46dad71e4a5691eb70e5ab9 to your computer and use it in GitHub Desktop.
Requirements for python-ext-wasmer

Ideas for python-ext-wasmer

Guideines to adoption/contribution

  • Read the Instance.rs documentation to understand how WASM binaries are ported to Python modules via pyo3 Rust crate.
  • Simply put the workflow looks something like:
  1. Developer defines Rust functions and compile them via wasm32-unknown-unknown target
  2. WASM binary (.wasm) is placed in a Python project
  3. With the tools imported via pip install wasmer, developer can load the WASM binary and use the exported functions. This way Rust-defined functions can be called transparently from Python. Great Wasmer!

Ideas for a stable interface

Definitions for snippets below:

  • in the context of Python snippets: module is the Python module and function is the exported function (exported functions are the ones defined in a Rust library somewhere and compiled to WASM biaries)
  • in the context of pyo3 snippets: a Python module is a type PyModule. It can be declared in different ways

[VERSION 1]

    1. (hide wasmer internals) Pythonic imports:
from module import function

# `module` should be wasmer Instance and `function`
# should be the exported function

[ADDITION]

    1. (as per 1. plus improve wasmer PyModule) Implement PyObjectProtocol for
#[pymodule]
fn wasmer(_py: Python, module: &PyModule) -> PyResult<()> { ... }

In alternative to 1. the main module can be used as entrypoint for the functions so, for example:

from wasmer import function

This may be achieved by changing the current definition from:

/// src/lib.rs
#[pymodule]
fn wasmer(_py: Python, module: &PyModule) -> PyResult<()> { ... }

to using #[pymodinit] as here so to take advantage of add_function and the new pyo3::wrap_pyfunction and pyo3::wrap_pymodule

[ADDITION]

    1. (abstract away loading binary) Autodiscover functionality for binary. User should not be asked to explicitly point and load the .wasm file; on the contrary python-ext-wasmer should look and find the file in project's sub-directories or in a dedicated directory.

P.S. For now the format of the list can be:

  • (objective) Implementation
@Mec-iS
Copy link
Author

Mec-iS commented May 23, 2019

I will keep that in mind thanks. As first step I will try to change as few as possible, basically reusing Instance to be a pyo3.PyModule, with the very same approach to namespace-memory binding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment