Giter VIP home page Giter VIP logo

wasmer-python's Introduction

Wasmer logo Wasmer Python PyPI version Wasmer Python Documentation Wasmer PyPI downloads Wasmer Slack #python Channel

A complete and mature WebAssembly runtime for Python based on Wasmer.

Features

  • Secure by default. No file, network, or environment access, unless explicitly enabled.
  • Fast. Run WebAssembly at near-native speeds.
  • Compliant with latest WebAssembly Proposals (SIMD, Reference Types, Threads, ...)

Documentation: browse the detailed API documentation full of examples.

Examples and tutorials: browse the examples/ directory, it's the best place for a complete introduction!

Install

To install the wasmer Python package, and let's say the wasmer_compiler_cranelift compiler, just run those commands in your shell:

$ pip install wasmer wasmer_compiler_cranelift

Usage

from wasmer import engine, Store, Module, Instance

store = Store()

# Let's compile the module to be able to execute it!
module = Module(store, """
(module
  (type (func (param i32 i32) (result i32)))
  (func (export "sum") (type 0) (param i32) (param i32) (result i32)
    local.get 0
    local.get 1
    i32.add))
""")

# Now the module is compiled, we can instantiate it.
instance = Instance(module)

# Call the exported `sum` function.
result = instance.exports.sum(5, 37)

print(result) # 42!

And then, finally, enjoy by running:

$ python examples/appendices/simple.py

We highly recommend to read the examples/ directory, which contains a sequence of examples/tutorials. It's the best place to learn by reading examples.

Quick Introduction

The wasmer package brings the required API to execute WebAssembly modules. In a nutshell, wasmer compiles the WebAssembly module into compiled code, and then executes it. wasmer is designed to work in various environments and platforms: From nano single-board computers to large and powerful servers, including more exotic ones. To address those requirements, Wasmer provides 2 engines and 3 compilers.

Succinctly, an engine is responsible to drive the compilation and the execution of a WebAssembly module. By extension, a headless engine can only execute a WebAssembly module, i.e. a module that has previously been compiled, or compiled, serialized and deserialized. By default, the wasmer package comes with 2 headless engines:

  1. wasmer.engine.Universal, the compiled machine code lives in memory,
  2. wasmer.engine.Native, the compiled machine code lives in a shared object file (.so, .dylib, or .dll), and is natively executed.

Because wasmer does not embed compilers in its package, engines are headless, i.e. they can't compile WebAssembly module; they can only execute them. Compilers live in their own standalone packages. Let's briefly introduce them:

Compiler package Description PyPi
wasmer_compiler_singlepass Super fast compilation times, slower execution times. Not prone to JIT-bombs. Ideal for blockchains On PyPi Downloads
wasmer_compiler_cranelift Fast compilation times, fast execution times. Ideal for development On PyPi Downloads
wasmer_compiler_llvm Slow compilation times, very fast execution times (close to native, sometimes faster). Ideal for Production On PyPi Downloads

We generally recommend wasmer_compiler_cranelift for development purposes and wasmer_compiler_llvm in production.

Learn more by reading the documentation of the wasmer.engine submodule.

Supported platforms

We try to provide wheels for as many platforms and architectures as possible. For the moment, here are the supported platforms and architectures:

Platform Architecture Triple Packages
Linux amd64 x86_64-unknown-linux-gnu wasmer
wasmer_compiler_singlepass
wasmer_compiler_cranelift
wasmer_compiler_llvm
aarch64 aarch64-unknown-linux-gnu wasmer
wasmer_compiler_singlepass 1
wasmer_compiler_cranelift
wasmer_compiler_llvm
Darwin amd64 x86_64-apple-darwin wasmer
wasmer_compiler_singlepass
wasmer_compiler_cranelift
wasmer_compiler_llvm
Windows amd64 x86_64-pc-windows-msvc wasmer
wasmer_compiler_singlepass
wasmer_compiler_cranelift
wasmer_compiler_llvm 2

Notes:

  • 1 wasmer_compiler_singlepass does not support aarch64 for the moment
  • 2 wasmer_compiler_llvm is not packaging properly on Windows for the moment

Wheels are all built for the following Python versions:

  • Python 3.7,
  • Python 3.8.
  • Python 3.9.
  • Python 3.10,
Learn about the “fallback” py3-none-any wheel

py3-none-any.whl

A special wasmer-$(version)-py3-none-any wheel is built as a fallback. The wasmer library will be installable, but it will raise an ImportError exception saying that “Wasmer is not available on this system”.

This wheel will be installed if none matches before (learn more by reading the PEP 425, Compatibility Tags for Built Distributions).

Development

The Python extension is written in Rust, with pyo3 and maturin.

First, you need to install Rust and Python. We will not make you the affront to explain to you how to install Python (if you really need, check pyenv). For Rust though, we advise to use rustup, then:

$ rustup install stable

To set up your environment, you'll need just, and then, install the prelude of this project:

$ cargo install just
$ just --list # to learn about all the available recipes
$ just prelude

It will install pyo3 and maturin for Python and for Rust. It will also install virtualenv.

Then, simply run:

$ source .env/bin/activate
$ just build api
$ just build compiler-cranelift
$ python examples/appendices/simple.py

Testing

To build all tests you'll need LLVM 12.0 in your system. We recommend either installing prepackaged libraries with llvm.sh or building it yourself with llvmenv.

Build all the packages and run the tests:

$ just build-all
$ just test

What is WebAssembly?

Quoting the WebAssembly site:

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.

About speed:

WebAssembly aims to execute at native speed by taking advantage of common hardware capabilities available on a wide range of platforms.

About safety:

WebAssembly describes a memory-safe, sandboxed execution environment […].

License

The entire project is under the MIT License. Please read the LICENSE file.

wasmer-python's People

Contributors

amanieu avatar ayys avatar bors[bot] avatar brunokim avatar chmp avatar dependabot-preview[bot] avatar dependabot[bot] avatar hywan avatar jubianchi avatar jvs avatar kianmeng avatar mec-is avatar ods avatar simonw avatar syrusakbary avatar urhengulas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wasmer-python's Issues

Type signature for i64

Thanks for proposing a new feature!

Motivation

I need to create the type signature for a function returning i64. I am auto-generating code for these functions so its very inconvenient to treat i64 specially.

Proposed solution

def HPyLong_AsLongLong(runtime_context, ctx: int, num_handle: int) -> Value.i64:

AND

def HPyLong_AsLongLong(runtime_context, ctx: int, num_handle: int) -> Type.I64:

Should both do it.

Additional context

Also:

def returns_nothing() -> None

should be treated identically to:

def returns_nothing()

As it is in the rest of Python.

Dependabot can't resolve your Rust dependency files

Dependabot can't resolve your Rust dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

info: syncing channel updates for 'nightly-x86_64-unknown-linux-gnu'
info: latest update on 2020-06-12, rust version 1.46.0-nightly (a37c32e2d 2020-06-11)
warning: Force-skipping unavailable component 'rustfmt-x86_64-unknown-linux-gnu'
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
error: failed to parse manifest at `/home/dependabot/dependabot-updater/dependabot_tmp_dir/Cargo.toml`

Caused by:
  readme file with name 'README.md' was not found

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

View the update logs.

Transfering strings (and other high level objects) between host and module

Motivation

Currently, Wasmer supports viewing raw memory and accessing functions with numeric parameters and returns. However, it is not (easily) possible to transfer/access strings, arrays/lists/dictionaries and other high level objects.

Proposed solution

Ideally, WebAssembly specs should be updated to support strings and other high level objects :)

More realistic approach is to provide some wrapper functions both to host and WebAssembly module to easily store strings and other objects into memory using currently supported data types and then later easily view/access them.

Some other tools (which are mostly for JS in browsers) already provide similar things. Some of them are Bindgen, Nbind and Embind, there are also some JS functions for this and Emscripten also provides some high level wrappers. You can get others if you just search something like "webassembly strings." Wasmer's implementation of this could be compatible with some of existing implementations for JS to make it easier to write such modules.

Alternatives

It is already possible that function returns pointer to a string and you then manually view memory and assemble the string, but this is not really very practical.

ImportError: cannot import name 'Instance' from 'wasmer'

I'm totally new to wasmer and python but I need to use them anyway.
So I installed python-ext-wasm using "pip3 install wasmer" on macOs and CentOs
I'm using python 3.6 on both
when I try to run my code I get this error :
ImportError: cannot import name 'Instance' from 'wasmer'

How to install compiler packages?

The compiler packages mentioned in the README aren't available on PyPI yet. But I was able to set up wasmer-python using the development instructions and run the examples, and I see that the examples don't require any of the compiler packages.

Will compiler packages be available with the next wasmer-python release?

Thank you for your work on wasmer!

` ExportedFunction` to ease introspection

The __call__ method in ExportedFunction should be factorised into methods (similar to getters?).

Motivation

This is important as when debugging Wasmer functions on the Python side there are no introspection tools. So to allow better fine-grained access to information in wasmer_runtime_core::instance::dyn_func underlying the exported function, like signature parameters etc. Developers may need to know how wasmer_runtime_core::instance::ExportedFunction represent Python function's signature in a way similar to what Python's inspect module provides. Also having a help method that prints this information on the Python side could be very useful.

Proposed solution

I am preparing a PR to implement methods like:

// returns the dynamic function object. To be used by __call__ and other methods
fn get_runtime_func_obj(&self) -> Result<DynFunc, PyErr> {...}

// other convenience methods
fn get_runtime_signature(&self) -> ... {...}
fn get_runtime_arguments(&self) -> ... {...}
...

// Plus any code required to turn the return values into pyo3 PyObject

Please provide feedback.

Provide wheels for Raspberry Pi with piwheels

Motivation

Wheels are only provided for classic 64-bit Linux systems. This means that is is currently not possible to easily use Python Wasmer library on Raspberry Pi, as well as other ARM-based systems.

Proposed solution

You should provide wheels for Raspberry Pi. However, you shouldn't publish them on PyPI, as Raspberry Pi armv7l can be uncompatible with other ARM-based systems. Instead, they should be published on piwheels which is a Python package repository providing Arm platform wheels specifically for the Raspberry Pi.

Normally, wheels are automatically built and published by piwheels servers, but as this library requires extra building steps (and actually even doesn't publish source code to PyPI), this doesn't work.

You will probably need to contact @bennuttall or @waveform80 to set up build dependencies on piwheels servers and provide instructions for building library.

Publish the any wheel

Summary

In PR #62 a wheel was created that installs anywhere and fails on import. It was published with version 0.3 but not with version 0.4.1. This breaks @syrusakbary snapshottest for me. Could please publish that wheel? It seems that code needed to build the wheel is still there.

Import “ImportObject” from python “wasmer” doesn't work for 0.4.1

I posted it as a question here and on stackOverflow, and nobody answered, so now it's clear it's a bug.

pip install wasmer
python -c 'from wasmer import ImportObject'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'ImportObject' from 'wasmer' (/usr/lib/python3.8/site-packages/wasmer.cpython-38-x86_64-linux-gnu.so

"ImportObject" class is described in readme and presented in examples. I've checked that proper 0.4.1 version of "wasmer" was installed. Also I checked the newest tagged version of "wasmer" which fails with the same error. What can be wrong?

generate_import_object() function doesn't work also:

python -c "from wasmer import Module; Module.generate_import_object()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: type object 'Module' has no attribute 'generate_import_object'

This code gives the same result:

wasm_bytes = open('binary.wasm', 'rb').read()
module = Module(wasm_bytes)
import_object = module.generate_import_object()

Provide wasmer core version in resulting Python object

Motivation

As done in #27 for __version__ method, it can be useful to have a __core_version__ method that returns the wasmer_runtime version.

Proposed solution

I don't know exactly if there is another way of doing introspection into linked libraries (I suppose not due to mangling), the only solution I can imagine is to parse Cargo.toml using toml crate and then read the wasmer_runtime values. Any better way?

Additional context

We should put as much "meta-information" as possible about the WASM environment in the resulting Python object.

Example of TableType FUNC_REF

Thanks for proposing a new feature!

Motivation

I want to import a FUNC_REF table into my module.

Proposed solution

I would like to see an example like this in the docs and tests:

    table = Table(store, TableType(Type.FUNC_REF, minimum=0, maximum=10 ), INITIAL_VALUE)

What is INITIAL_VALUE?

Roadmap to v1.0 interface

Motivation

As proposed in #37, it would be nice to collect ideas about how a stable interface should look like from the Python side and how to make it possible using the latest additions to pyo3.

Proposed solution

Collecting notes and comments in this gist. Please keep this issue open to keep track of progress.

Are there plans to release for Python 3.9 ?

Summary

Currently installing the package with Python 3.9 on macOS doesn't give errors but actually only installs an __init__,py with:

raise ImportError("Wasmer is not available on this system")

now that Python 3.9 is released as stable, is there a roadmap to release for it?

Documentation and user experience

Motivation

The library needs some documents to guide developers through adoption/usage/contribution, so to bring them to a "what to do next" stage after trying the example in the README.

Proposed solution

We are working out some stuff in a section of this gist. Please leave this issue open to track progress.

Additional context

It would be great in the future to have an online playground where user can load their own WASM-compiled binary and then try some scripting in Python in an interactive way (like Skulpt)

How to set the buffer allocated by WASM. from Python?

some pseudo code:

string_to_set = np.frombuffer(b"hello", dtype=uint8)
str_len = len(string_to_set)
ptr= instance.exports.alloc(str_len )


# this is what can be done in JS
arr= UInt8Array.new(instance.exports.memory.buffer(), prt, str_len)
arr.set(string_to_set )

OK, I managed to make it like this:

for i, u in enumerate(string_to_set):
    instance.exports.memory.uint8_view()[i] = int(u)

Still, I think this should be included in the example_memory.py

Publish the new release: 1.0.0-alpha1

I'm writing this issue to provide a summary of where we are at publishing the 1.0.0-alpha1 version.

The rewrite is over. We are going to publish the new release. To do so, we need to fix an issue with LLVM.

LLVM is required for the wasmer_compiler_llvm crate. So far, we use the official LLVM builts on Linux and macOS. On Windows, we are using a custom built from ours (see https://github.com/wasmerio/llvm-build/).

However, the official LLVM builts come with ncurse as a dependency. It breaks the manylinux rule of Python wheels. Our own builts have ncurse turned off, so it is likely to fix that issue! However, we have another issue: Our own builts do not build clang, which is required by the native engine.

This last problem is being tested in wasmerio/llvm-build#1 for the moment.

Once it is done, we will see if our custom builts will fix the issue with Python wheels.

Unable to find attribute 'exports'

Hi! I've tried running the example to see how wasmer runs, and encountered the error:
'libwasmer.Instance' object has no attribute 'exports'

wasmer: 0.1.4
python: 3.7.3
wasm: build with cargo wasm.

The wasm files seems working when I load it with the below code:

fetch('rusty.wasm').then(response =>
        response.arrayBuffer()
      ).then(bytes =>
        WebAssembly.instantiate(bytes, {})
      ).then(results => {
        alert(results.instance.exports.add_one(41));
      });

Register as Python `Importer`

Motivation

Way to use this project is simple, but by micking Javascipt API, it's not pythonic nor transparent to users.

Proposed solution

Implement Python Importers protocol so WebAssembly modules can be imported in Python code as if they were Python modules, with plain from blah import foo statements.

LICENSE file seems to be missing

README says the project is BSD licensed and refers to the LICENSE file, but I can’t seem to find said file in the repository.

AttributeError: 'Module' object has no attribute 'custom_section_names'

Is the pip release out of sync with the README or something?

The following fails with a fresh install from pip on python 3.7, same goes with just about anything except the Module and Instance constructor;

from wasmer import Module

wasm_bytes = open('a.out', 'rb').read()
wasm_module = Module(wasm_bytes)
print(wasm_module.custom_section_names)

Troubleshooting: Arguments increased on `ExportedFunction`

Summary

I have compiled a library to wrap in python using webassembly but the wrapped function requires a different number of arguments from the original rust code.

Additional details

To reproduce the issue:

install wasmer from pip

pip install wasmer

Clone the repo and build wrapper lib into the target wasm32-unknown-unknown

git clone https://github.com/hskang9/schnorrkel-py.git
cd schnorrkel-py
cargo build --target wasm32-unknown-unknown

Run python-ext-wasm

from wasmer import Instance
from wasmer import Uint8Array
wasm_bytes = open('target/wasm32-unknown-unknown/release/schnorrkel_py.wasm', 'rb').read()
instance = Instance(wasm_bytes)

result = instance.exports.secret_from_seed([1, 2])

print(result)

The error shows that the exported function requires 3 arguments when the original rust code required one pointer to UInt8Array.

here is the error log:

Traceback (most recent call last):
RuntimeError: Missing 2 argument(s) when calling `secret_from_seed`: Expect 3 argument(s), given 1.

Here is the rust code compiled to wasm:

#[no_mangle]
pub extern fn secret_from_seed(seed: &[u8]) -> Vec<u8> {
	__secret_from_seed(seed).to_vec()
}

pub fn __secret_from_seed(seed: &[u8]) -> [u8; SECRET_KEY_LENGTH] {
	let secret = keypair_from_seed(seed).secret.to_bytes();
	let mut s = [0u8; SECRET_KEY_LENGTH];
	s.copy_from_slice(&secret);
	s
}

My question is:

  1. How did the argument increase?
  2. Did I compile the rust code to the right target?

WASI support

Motivation

I'd like to be able to (easily) use WASI with Wasmer's Python package in order to publish a native dependency of my application on PyPI. Wasmtime-py, which is what I'm currently using, does provide this.

Proposed solution

Expose WASI functions (perhaps from wasi-common?) through the Python interface in a way that allows easily linking them to WASM modules.

Additional context

The dependency I'm packaging is here.

"free" function is absent though "malloc" exists

Hi. I have a function in WebAssembly which gets an image matrix as a parameter. The image format is "unsigned char*" pointer. To pass it I allocate the memory with "dataPtr = instance.exports.malloc(dataLen)" call, fill the data under this pointer with an image and use "dataPtr" as a parameter to my function. In C++ world I usually need to free memory afterwards. I don't have such function here. And after some number of runs I finally get a memory out-of-bounds access, and I need to restart python. Is there any way to free the memory in WebAssembly from python?

Below is the code to reproduce the problem:

image_list = bytearray(image.flatten().tolist())            # convert numpy matrix to byte array
dataLen = len(image_list)                                   # amount of memory we need for image
dataPtr = instance.exports.malloc(dataLen)                  # allocate memory with malloc
uint8_view = instance.memory.uint8_view(offset = dataPtr)   # get "uint8_view" for pointer
uint8_view[:dataLen] = image_list[:]                        # fill data under pointer with bytes

instance.exports.setImage(dataPtr, imageWidth, imageHeight) # call WebAssembly function
# instance.exports.free(dataPtr)                            # absent function to free memory after call

This code works normally except of the aforementioned memory leak which leads to runtime error after some runs:

<ipython-input-49-35a9fe36b2bc> in func(image)
      2     image_list = bytearray(image.flatten().tolist())
      3     dataLen = len(image_list)
----> 4     dataPtr = instance.exports.malloc(dataLen)
      5     uint8_view = instance.memory.uint8_view(offset = dataPtr)
      6     uint8_view[:dataLen] = image_list[:]

RuntimeError: Call error: Error when calling invoke: A `memory out-of-bounds access` trap was thrown at code offset 76788

I didn't find anything about memory freeing in the documentation (including "malloc" function, which is not documented feature).

Thank you for your help

`wat2wasm` feature

That would be super useful to expose the wat2wasm crate directly in Python.

Possibility to call native ctype library code from python-ext-wasm

Summary

Assume there is a function foo, which is exposed to python via loading a native C compiled library via ctypes. Is it possible to call this ctypes defined / imported library from wasm using this library?

Additional details

In my usecase, we have a lot of numerical code from C++/C wrapped exposed to python. To speed up the execution, it would be great to move some of the code from python to wasm. For prototype reasons, writing a first version in python is preferred.

The closest I could find is issue #28. However, this is not about importing python but native code, where the (ctypes) are assumed to be known.

is this lib only support rust's wasm?

Summary

is this lib only support rust's wasm?

I can't test wasm build by cpp.

Additional details

index.cc

#include <emscripten/bind.h>

using namespace emscripten;

int add(int x,int y){
    return x+y;
} 

EMSCRIPTEN_BINDINGS(module){
    function("add",&add);
}

and command to build

emcc index.cc --std=c++11 --bind -s WASM=1

and load into python

from wasmer import Instance

# Get the Wasm module as bytes.
wasm_bytes = open('index.wasm', 'rb').read()

# Instantiate the Wasm module.
instance = Instance(wasm_bytes)

# Call a function on it.
result = instance.exports.add(1, 2)

//not work .

and use Module also not work too.

ImportError: Wasmer is not available on this system

Describe the bug

Trying to install and use wasmer in windows 10, using (mini)conda environment with py3.7(tried with 3.8 and 3.6 also), installed wasmer "successfully" via: pip install wasmer

First import fails with the following output:

(py37) C:\tmp>python test_wasm.py
Traceback (most recent call last):
File "test_wasm.py", line 1, in
from wasmer import Instance
File "C:\Users\kha\AppData\Local\Continuum\miniconda3\envs\py37\lib\site-packages\wasmer_init_.py", line 1, in
raise ImportError("Wasmer is not available on this system")
ImportError: Wasmer is not available on this system

NOTE: Exact same procedure works fine when performed in a WSL(1) Ubuntu instance on the same machine

Example/greet.py doesn't work well

Example/greet.py doesn't work well.

Describe

Executing example/greet.py fails with the following message.

$ python examples/greet.py
Traceback (most recent call last):
  File "examples/greet.py", line 19, in <module>
    memory[0:length_of_subject] = subject
TypeError: 'slice' object cannot be interpreted as an integer

If I modify the part memory[0:length_of_subject] = subject to the following code, it works well.

for i in range(length_of_subject-1):
  memory[i] = subject[i]

However, slice assignment should work well based on the implementation __setitem__ in impl PyMappingProtocol for Uint8Array.

I'm not sure if it relates to this issue but I found the external issue (PyO3/pyo3#611).

Inferring multiple return values

Thanks for proposing a new feature!

Motivation

Wasmer could support type inferencing of multi-valued return types using Python's typing.Tuple or simple tuples:

from typing import Tuple
from wasmer import Function, Store

store = Store()


def func(foo: int) -> Tuple[int, int]:
    return (foo, foo)


myfunc = Function(store, func)

Proposed solution

I would like the code above to infer FunctionType { params: [I32], results: [I32, I32] })

Alternatives

I can of course supply an explicit declaration

Other spellings that might make sense (more inferencing is better than less, so I'd suggest all be supported, if effort is available):

(int, int)

class Point(NamedTuple):
   x: int
   y: int

def makePoint()->Point:...

Wheels with LLVM backend

Motivation

The Wasmer LLVM backend provides support for a lot more platforms than Cranelift. At the moment if I am depending on Wasmer I would need to ask users on platforms other than x86_64/AArch64 to build Wasmer manually, which is a significant hurdle.

Proposed solution

It would be nice if the Wasmer Python extension had wheels published with the LLVM backend enabled.

Provide built-ins to access memory views

Motivation

Beside the exported functions (the custom functions written by the user and compiled to Wasm), we should provide built-in functions that provide a "mini standard library" for the user to leverage on the Python side. For example, regarding this issue, some functions to handle memory views should be included in the Python interface to allow reading/writing to memory views.

Proposed solution

At the moment I use these utilities to access memory views from my Python scripts:

def get_memory_view(instance, size=8):
    """Get a view of `size` from a Wasmer an instance.
    Returns a memory view"""
    mem = instance.memory
    funcname = f'uint{size}_view'
    return getattr(mem, funcname)()

def write_to_view(bytestr, mem_view, offset=0):
    """Fill memory with values from a bytestring starting at `offset`.
    Returns a memory view"""
    for i, c in enumerate(bytestr):
        mem_view[offset + i] = c
    return mem_view

These should be embedded on the Rust side with pyo3 in the Wasmer Instance, so the user can leverage them as he/she leverages exports. So as we have

from instance.exports import some_custom_function

we should have something like:

from instance.builtins import get_memory_view

To achieve this we can add to Instance:

#[pyclass]
pub struct Instance {
    pub(crate) exports: Py<ExportedFunctions>,
    pub(crate) memory: Py<Memory>,
    // to be added
    pub(crate) builtins: Py<BuiltIns>,   
}
...
#[pyclass]
pub struct BuiltIns {
    pub(crate) instance: Rc<runtime::Instance>,
}

#[pymethods]
impl BuiltIns {
    #[getter]
    fn get_memory_view ...

    #[setter]
    fn write_memory_view ...
}

Additional context

As a reference, see what is already present in examples/greet.rs for the host machine. This feature proposes to have those functions also for the Wasm Instance as built-ins.

In addition, as a note, similar to this feature we may want to have a "toolkit" of Rust functions that collects this kind of utilities (as the one in examples/greet.rs; C FFI and similar) and provide them in some sort of wasmer-dev-toolkit crate.

python 3.8 wheel packaging

Thanks for proposing a new feature!

Motivation

currently installing wasmer on python 3.8 results in a empty package installation

>>> import wasmer                                            
Traceback (most recent call last):                        
  File "<stdin>", line 1, in <module>                             
  File "/Users/kapilt/.pyenv/versions/pywasm/lib/python3.8/site-packages/wasmer/__init__.py", line 1, in <module>
    raise ImportError("Wasmer is not available on this system")
ImportError: Wasmer is not available on this system

Proposed solution

updating the build / release to use python 3.8 for artifacts published would resolve.

support providing imports / host functions

Thanks for proposing a new feature!

Motivation

I was trying to run a wasm file generated by https://github.com/open-policy-agent/opa

>>> i = Instance(wasm_bytes)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Failed to instantiate the module:
    7 link errors: (1 of 7) Import not found, namespace: env, name: opa_abort (2 of 7) Import not found, namespace: env, name: opa_builtin0 (3 of 7) Import not found, namespace: env, name: opa_builtin1 (4 of 7) Import not found, namespace: env, name: opa_builtin2 (5 of 7) Import not found, namespace: env, name: opa_builtin3 (6 of 7) Import not found, namespace: env, name: opa_builtin4 (7 of 7) Import not found, namespace: env, name: memory

it looks like opa expects as part of its integration to provider some imports, here's an example in js

https://github.com/open-policy-agent/npm-opa-wasm/blob/master/src/opa.js#L95

Proposed solution

allow the user to pass in an imports mapping

at the moment it looks as though that its always set to an empty import object

        let imports = imports! {};

        // Instantiate the module.
        let instance = match self.module.instantiate(&imports) {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.