Skip to content

feat(extension) Replace rust-cpython with pyo3 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 137 additions & 33 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ name = "wasmer"
crate-type = ["cdylib"]

[dependencies]
cpython = { version = "0.2", features = ["extension-module"] }
wasmer-runtime = { git = "https://github.com/wasmerio/wasmer", branch = "master" }
wasmer-runtime = "0.3.0"
wasmer-runtime-core = "0.3.0"
pyo3 = { version = "0.6.0", features = ["extension-module"] }

[package.metadata.pyo3-pack]
classifier = [
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ from wasmer import Instance, Value

wasm_bytes = open('simple.wasm', 'rb').read()
instance = Instance(wasm_bytes)
result = instance.call('sum', [Value.i32(5), Value.i32(37)])
result = instance.exports.sum(5, 37)

print(result) # 42!
```
Expand Down Expand Up @@ -88,11 +88,18 @@ wasm_bytes = open('my_program.wasm', 'rb').read()
instance = Instance(wasm_bytes)

# Call a function on it.
result = instance.call('sum', [Value.i32(1), Value.i32(2)])
result = instance.exports.sum(1, 2)

print(result) # 3
```

All exported functions are accessible on the `exports` getter.
Arguments of these functions are automatically casted to WebAssembly
values. If one wants to explicitely pass a value of a particular type,
it is possible to use the `Value` class,
e.g. `instance.exports.sum(Value.i32(1), Value.i32(2))`. Note that for
most usecases, this is not necessary.

### The `Value` class

Builds WebAssembly values with the correct types:
Expand Down Expand Up @@ -141,7 +148,8 @@ All these classes share the same implementation. Taking the example of

```python
class Uint8MemoryView:
BYTES_PER_ELEMENT = 1
@property
def bytes_per_element()

def __len__()
def __getitem__(index)
Expand All @@ -160,7 +168,7 @@ wasm_bytes = open('my_program.wasm', 'rb').read()
instance = Instance(wasm_bytes)

# Call a function that returns a pointer to a string for instance.
pointer = instance.call('return_string')
pointer = instance.exports.return_string()

# Get the memory view, with the offset set to `pointer` (default is 0).
memory = instance.uint8_memory_view(pointer)
Expand Down
2 changes: 1 addition & 1 deletion examples/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

wasm_bytes = open(__dir__ + '/memory.wasm', 'rb').read()
instance = Instance(wasm_bytes)
pointer = instance.call('return_hello')
pointer = instance.exports.return_hello()

memory = instance.uint8_memory_view(pointer)
nth = 0;
Expand Down
5 changes: 3 additions & 2 deletions examples/simple.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from wasmer import Instance, Value
from wasmer import Instance, Value, Uint8MemoryView
import os

__dir__ = os.path.dirname(os.path.realpath(__file__))

wasm_bytes = open(__dir__ + '/simple.wasm', 'rb').read()
instance = Instance(wasm_bytes)
result = instance.call('sum', [Value.i32(5), Value.i32(37)])

result = instance.exports.sum(1, 2)

print(result) # 42!
12 changes: 6 additions & 6 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Install the environment to develop the extension.
prelude:
pip3 install pyo3-pack
cargo install pyo3-pack
pip3 install virtualenv
virtualenv -p $(which python3) .env
source .env/bin/activate
pip3 install pyo3-pack pytest pytest-benchmark

# Setup the environment to develop the extension.
wakeup:
Expand All @@ -17,19 +17,19 @@ sleep:
rust:
export PYTHON_SYS_EXECUTABLE=$(which python3)
cargo check
pyo3-pack develop --release --strip
pyo3-pack develop --binding_crate pyo3 --release --strip

# Run Python.
python-run file='':
.env/bin/python {{file}}
@python {{file}}

# Run the tests.
test:
@.env/bin/python tests/init.py
@py.test tests

# Inspect the `python-ext-wasm` extension.
inspect:
.env/bin/python -c "help('wasmer')"
@python -c "help('wasmer')"

# Local Variables:
# mode: makefile
Expand Down
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly
20 changes: 0 additions & 20 deletions src/error.rs

This file was deleted.

Loading