1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
///! The current module give a free access to an helper. With the goal of
///! providing an helper to read and write basic pointers in a WebAssembly
///! script builded from the [AssemblyScript compiler](https://www.assemblyscript.org).
///!
///! Thanks to wasmer and the [AssemblyScript Runtime](https://www.assemblyscript.org/garbage-collection.html#runtime-interface),
///! we can provide functions like `alloc`, `read` and `write` to interact with
///! a given webassembly instance.
///!
///! # Helpers
///!
///! For the moment this crate implement helpers for the ArrayBuffer and for strings.
///! Historically the ArrayBuffer is less tested than the string. But the both allow
///! you to interact with a wasmer instance.
///!
///! ```ignore
///! let wasm_bytes = include_bytes!(concat!(
///! env!("CARGO_MANIFEST_DIR"),
///! "/tests/runtime_exported.wat"
///! ));
///! let store = Store::default();
///! let module = Module::new(&store, wasm_bytes)?;
///!
///! let import_object = imports! {
///! "env" => {
///! "abort" => Function::new_native_with_env(&store, Env::default(), abort),
///! },
///! };
///!
///! let instance = Instance::new(&module, &import_object)?;
///! let memory = instance.exports.get_memory("memory").expect("get memory");
///!
///! let mut env = Env::default();
///! env.init(&instance)?;
///!
///! let get_string = instance
///! .exports
///! .get_native_function::<(), StringPtr>("getString")?;
///!
///! let str_ptr = get_string.call()?;
///! let string = str_ptr.read(memory)?;
///!
///! assert_eq!(string, "hello test");
///!
///! let str_ptr_2 = StringPtr::alloc(&"hello return".to_string(), &env)?;
///! let string = str_ptr_2.read(memory)?;
///! assert_eq!(string, "hello return");
///! ```
///!
///!
///!
///! # Unsafe note
///! This crate has a low-level access to your memory, it's often dangerous to
///! share memory between programs and you should consider this in your
///! project.
pub use BufferPtr;
pub use Env;
pub use StringPtr;
pub use abort;
use fmt;
use Memory;