File tree 5 files changed +29
-22
lines changed
examples/embedding/wasi-py-rs
5 files changed +29
-22
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ edition = "2021"
5
5
6
6
[dependencies ]
7
7
pyo3 = { version = " 0.19.0" , features = [" abi3-py311" ] }
8
- wlr-libpy = { git = " https://github.com/vmware-labs/webassembly-language-runtimes.git" , branch = " rust-py-example " , features = [" py_main" ] }
8
+ wlr-libpy = { git = " https://github.com/vmware-labs/webassembly-language-runtimes.git" , features = [" py_main" ] }
9
9
10
10
[build-dependencies ]
11
- wlr-libpy = { git = " https://github.com/vmware-labs/webassembly-language-runtimes.git" , branch = " rust-py-example " , features = [" build" ] }
11
+ wlr-libpy = { git = " https://github.com/vmware-labs/webassembly-language-runtimes.git" , features = [" build" ] }
Original file line number Diff line number Diff line change @@ -57,14 +57,18 @@ Take a look at [Cargo.toml](./Cargo.toml) to see how to add it as a build depend
57
57
58
58
``` toml
59
59
[build-dependencies ]
60
- wlr-libpy = { git = " https://github.com/vmware-labs/webassembly-language-runtimes.git" , branch = " rust-py-example " , features = [" build" ] }
60
+ wlr-libpy = { git = " https://github.com/vmware-labs/webassembly-language-runtimes.git" , features = [" build" ] }
61
61
```
62
62
63
63
Then, in the [ build.rs] ( ./build.rs ) file we only need to add this to the ` main ` method:
64
64
65
65
``` rs
66
+ fn main () {
67
+ // ...
66
68
use wlr_libpy :: bld_cfg :: configure_static_libs;
67
69
configure_static_libs (). unwrap (). emit_link_flags ();
70
+ // ...
71
+ }
68
72
```
69
73
70
74
This will ensure that all required libraries are downloaded and the linker is configured to use them.
Original file line number Diff line number Diff line change @@ -6,23 +6,7 @@ use wasi_py_rs::py_module::make_person_module;
6
6
pub fn main ( ) -> PyResult < ( ) > {
7
7
append_to_inittab ! ( make_person_module) ;
8
8
9
- let function_code = "def my_func(*args, **kwargs):
10
- import sys
11
- print(f'Hello from Python (libpython3.11.a / {sys.version}) in Wasm(Rust).\\ nargs=', args)
12
-
13
- import person
14
- people = []
15
- for name, age, tags in args:
16
- p = person.Person(name, age)
17
- for t in tags:
18
- p.add_tag(t)
19
- people.append(p)
20
-
21
- filtered = person.filter_by_tag(people, 'student')
22
- print(f'Original people: {people}')
23
- print(f'Filtered people by `student`: {filtered}')
24
- " ;
25
-
9
+ let function_code = include_str ! ( "py-func.py" ) ;
26
10
call_function (
27
11
"my_func" ,
28
12
function_code,
Original file line number Diff line number Diff line change
1
+ def my_func (* args , ** kwargs ):
2
+ import sys
3
+ print (f'Hello from Python (libpython3.11.a / { sys .version } ) in Wasm(Rust).\\ nargs=' , args )
4
+
5
+ import person
6
+ people = []
7
+ for name , age , tags in args :
8
+ p = person .Person (name , age )
9
+ for t in tags :
10
+ p .add_tag (t )
11
+ people .append (p )
12
+
13
+ filtered = person .filter_by_tag (people , 'student' )
14
+ print (f'Original people: { people } ' )
15
+ print (f'Filtered people by `student`: { filtered } ' )
Original file line number Diff line number Diff line change @@ -12,15 +12,17 @@ To use this feature add this to your Cargo.toml
12
12
13
13
``` toml
14
14
[build-dependencies ]
15
- wlr-libpy = { git = " https://github.com/vmware-labs/webassembly-language-runtimes.git" , branch = " rust-py-example " , features = [" build" ] }
15
+ wlr-libpy = { git = " https://github.com/vmware-labs/webassembly-language-runtimes.git" , features = [" build" ] }
16
16
```
17
17
18
18
Then, in the ` build.rs ` file of your project you only need to call ` configure_static_libs().unwrap().emit_link_flags() ` like this:
19
19
20
20
``` rs
21
21
fn main () {
22
+ // ...
22
23
use wlr_libpy :: bld_cfg :: configure_static_libs;
23
24
configure_static_libs (). unwrap (). emit_link_flags ();
25
+ // ...
24
26
}
25
27
```
26
28
@@ -39,12 +41,14 @@ To use this feature add this to your Cargo.toml
39
41
``` toml
40
42
[dependencies ]
41
43
...
42
- wlr-libpy = { git = " https://github.com/vmware-labs/webassembly-language-runtimes.git" , branch = " rust-py-example " , features = [" py_main" ] }
44
+ wlr-libpy = { git = " https://github.com/vmware-labs/webassembly-language-runtimes.git" , features = [" py_main" ] }
43
45
```
44
46
45
47
Then to call on the ` Py_Main ` method just do this:
46
48
47
49
``` rs
50
+ fn main () {
48
51
use wlr_libpy :: py_main :: py_main;
49
52
py_main (std :: env :: args (). collect ());
53
+ }
50
54
```
You can’t perform that action at this time.
0 commit comments