Initial reorg.
This is largely the same as #305, but updated for the current tree.
This commit is contained in:
1
crates/misc/py/examples/gcd/.gitignore
vendored
Normal file
1
crates/misc/py/examples/gcd/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
gcd.wasm
|
||||
15
crates/misc/py/examples/gcd/README.md
Normal file
15
crates/misc/py/examples/gcd/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Build example's file
|
||||
|
||||
To build `gcd.wasm` use rustc (nightly) for wasm32 target with debug information:
|
||||
|
||||
```
|
||||
rustc +nightly --target=wasm32-unknown-unknown -g gcd.rs --crate-type=cdylib
|
||||
```
|
||||
|
||||
# Run example
|
||||
|
||||
Point path to the built wasmtime_py library location when running python, e.g.
|
||||
|
||||
```
|
||||
PYTHONPATH=../../target/debug python3 run.py
|
||||
```
|
||||
19
crates/misc/py/examples/gcd/gcd.rs
Normal file
19
crates/misc/py/examples/gcd/gcd.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
#[inline(never)]
|
||||
#[no_mangle]
|
||||
pub extern fn gcd(m_: u32, n_: u32) -> u32
|
||||
{
|
||||
let mut m = m_;
|
||||
let mut n = n_;
|
||||
while m > 0 {
|
||||
let tmp = m;
|
||||
m = n % m;
|
||||
n = tmp;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn test() -> u32 {
|
||||
gcd(24, 9)
|
||||
}
|
||||
5
crates/misc/py/examples/gcd/run.py
Normal file
5
crates/misc/py/examples/gcd/run.py
Normal file
@@ -0,0 +1,5 @@
|
||||
import wasmtime
|
||||
import gcd
|
||||
|
||||
print("gcd(27, 6) =", gcd.gcd(27, 6))
|
||||
|
||||
3
crates/misc/py/examples/import/.gitignore
vendored
Normal file
3
crates/misc/py/examples/import/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import.wasm
|
||||
main.wasm
|
||||
__pycache__
|
||||
15
crates/misc/py/examples/import/README.md
Normal file
15
crates/misc/py/examples/import/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Build example's file
|
||||
|
||||
To build `demo.wasm` use rustc (nightly) for wasm32 target with debug information:
|
||||
|
||||
```
|
||||
rustc +nightly --target=wasm32-unknown-unknown demo.rs --crate-type=cdylib
|
||||
```
|
||||
|
||||
# Run example
|
||||
|
||||
Point path to the built `wasmtime_py` library location when running python, e.g.
|
||||
|
||||
```
|
||||
PYTHONPATH=../../target/debug python3 run.py
|
||||
```
|
||||
11
crates/misc/py/examples/import/demo.rs
Normal file
11
crates/misc/py/examples/import/demo.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
extern "C" {
|
||||
fn callback(s: *const u8, s_len: u32) -> u32;
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn test() {
|
||||
let msg = "Hello, world!";
|
||||
unsafe {
|
||||
callback(msg.as_ptr(), msg.len() as u32);
|
||||
}
|
||||
}
|
||||
10
crates/misc/py/examples/import/env.py
Normal file
10
crates/misc/py/examples/import/env.py
Normal file
@@ -0,0 +1,10 @@
|
||||
def callback(msg_p: 'i32', msg_len: 'i32') -> 'i32':
|
||||
print('callback:', msg_p, msg_len)
|
||||
|
||||
# global memory
|
||||
# mv = memoryview(memory)
|
||||
|
||||
# msg = bytes(mv[msg_p:(msg_p + msg_len)]).decode('utf-8')
|
||||
# print(msg)
|
||||
|
||||
return 42
|
||||
4
crates/misc/py/examples/import/run.py
Normal file
4
crates/misc/py/examples/import/run.py
Normal file
@@ -0,0 +1,4 @@
|
||||
import wasmtime
|
||||
import demo
|
||||
|
||||
demo.test()
|
||||
3
crates/misc/py/examples/two_modules/.gitignore
vendored
Normal file
3
crates/misc/py/examples/two_modules/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
one.wasm
|
||||
two.wasm
|
||||
__pycache__
|
||||
20
crates/misc/py/examples/two_modules/README.md
Normal file
20
crates/misc/py/examples/two_modules/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Build example's file
|
||||
|
||||
To build `one.wasm` use rustc (nightly) for wasm32 target with debug information:
|
||||
|
||||
```
|
||||
rustc +nightly --target=wasm32-unknown-unknown one.rs --crate-type=cdylib
|
||||
```
|
||||
|
||||
To build `two.wasm` use wabt.
|
||||
```
|
||||
wat2wasm two.wat -o two.wasm
|
||||
```
|
||||
|
||||
# Run example
|
||||
|
||||
Point path to the built wasmtime_py library location when running python, e.g.
|
||||
|
||||
```
|
||||
PYTHONPATH=../../target/debug python3 run.py
|
||||
```
|
||||
2
crates/misc/py/examples/two_modules/env.py
Normal file
2
crates/misc/py/examples/two_modules/env.py
Normal file
@@ -0,0 +1,2 @@
|
||||
def answer() -> 'i32':
|
||||
return 42
|
||||
17
crates/misc/py/examples/two_modules/one.rs
Normal file
17
crates/misc/py/examples/two_modules/one.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
extern "C" {
|
||||
fn answer() -> u32;
|
||||
}
|
||||
|
||||
// For the purpose of this wasm example, we don't worry about multi-threading,
|
||||
// and will be using the PLACE in unsafe manner below.
|
||||
static mut PLACE: u32 = 23;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn bar() -> *const u32 {
|
||||
unsafe {
|
||||
PLACE = answer();
|
||||
// Return a pointer to the exported memory.
|
||||
(&PLACE) as *const u32
|
||||
}
|
||||
}
|
||||
4
crates/misc/py/examples/two_modules/run.py
Normal file
4
crates/misc/py/examples/two_modules/run.py
Normal file
@@ -0,0 +1,4 @@
|
||||
import wasmtime
|
||||
import two
|
||||
|
||||
print("answer() returned", two.ask())
|
||||
11
crates/misc/py/examples/two_modules/two.wat
Normal file
11
crates/misc/py/examples/two_modules/two.wat
Normal file
@@ -0,0 +1,11 @@
|
||||
(module
|
||||
(import "one" "memory" (memory $memory 0))
|
||||
(import "one" "bar" (func $bar (result i32)))
|
||||
(export "ask" (func $foo))
|
||||
|
||||
(func $foo (result i32)
|
||||
call $bar
|
||||
;; Deference returned pointer to the value from imported memory
|
||||
i32.load
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user