@@ -18,7 +18,7 @@ fn reverse_string(a: String) -> String {
18
18
fn wasm_example_n_i32 ( source : String , f : String , args : Vec < i32 > ) -> Result < Vec < i32 > , Error > {
19
19
// return Ok(5);
20
20
//return Err(Error::Term(Box::new("hello")));
21
- return match wasm_example_n_i32_internal ( source, false , f, args) {
21
+ return match wasm_example_n_i32_internal ( source, true , f, args) {
22
22
Ok ( v) => Ok ( v) ,
23
23
Err ( e) => Err ( Error :: Term ( Box :: new ( e. to_string ( ) ) ) )
24
24
}
@@ -71,67 +71,6 @@ fn wasm_example_0_internal(source: String, f: String) -> Result<i32, anyhow::Err
71
71
return Ok ( result) ;
72
72
}
73
73
74
- #[ rustler:: nif]
75
- fn wasm_example_1_i32 ( source : String , f : String , a : i32 ) -> Result < i32 , Error > {
76
- return match wasm_example_1_i32_internal ( source, f, a) {
77
- Ok ( v) => Ok ( v) ,
78
- Err ( e) => Err ( Error :: Term ( Box :: new ( e. to_string ( ) ) ) )
79
- }
80
- }
81
-
82
- fn wasm_example_1_i32_internal ( source : String , f : String , a : i32 ) -> Result < i32 , anyhow:: Error > {
83
- let engine = Engine :: default ( ) ;
84
-
85
- // We start off by creating a `Module` which represents a compiled form
86
- // of our input wasm module. In this case it'll be JIT-compiled after
87
- // we parse the text format.
88
- let module = Module :: new ( & engine, source) ?;
89
-
90
- // A `Store` is what will own instances, functions, globals, etc. All wasm
91
- // items are stored within a `Store`, and it's what we'll always be using to
92
- // interact with the wasm world. Custom data can be stored in stores but for
93
- // now we just use `()`.
94
- let mut store = Store :: new ( & engine, ( ) ) ;
95
-
96
- // With a compiled `Module` we can then instantiate it, creating
97
- // an `Instance` which we can actually poke at functions on.
98
- let instance = Instance :: new ( & mut store, & module, & [ ] ) ?;
99
-
100
- // The `Instance` gives us access to various exported functions and items,
101
- // which we access here to pull out our `answer` exported function and
102
- // run it.
103
- let answer = instance
104
- . get_func ( & mut store, & f)
105
- . expect ( & format ! ( "{} was not an exported function" , f) ) ;
106
-
107
- // There's a few ways we can call the `answer` `Func` value. The easiest
108
- // is to statically assert its signature with `typed` (in this case
109
- // asserting it takes no arguments and returns one i32) and then call it.
110
- let answer = answer. typed :: < i32 , i32 > ( & store) ?;
111
-
112
- // And finally we can call our function! Note that the error propagation
113
- // with `?` is done to handle the case where the wasm function traps.
114
- let result = answer. call ( & mut store, a) ?;
115
-
116
- return Ok ( result) ;
117
- }
118
-
119
- #[ rustler:: nif]
120
- fn wasm_example_2_i32 ( source : String , f : String , a : i32 , b : i32 ) -> Result < i32 , Error > {
121
- return match wasm_example_2_i32_internal ( source, false , f, a, b) {
122
- Ok ( v) => Ok ( v) ,
123
- Err ( e) => Err ( Error :: Term ( Box :: new ( e. to_string ( ) ) ) )
124
- }
125
- }
126
-
127
- #[ rustler:: nif]
128
- fn wasm_buffer_2_i32 ( wat_source : String , f : String , a : i32 , b : i32 ) -> Result < Vec < i32 > , Error > {
129
- return match wasm_example_2_i32_n_internal ( wat_source, true , f, a, b) {
130
- Ok ( v) => Ok ( v) ,
131
- Err ( e) => Err ( Error :: Term ( Box :: new ( e. to_string ( ) ) ) )
132
- }
133
- }
134
-
135
74
#[ rustler:: nif]
136
75
fn wasm_string_2_i32 ( wat_source : String , f : String , a : i32 , b : i32 ) -> Result < String , Error > {
137
76
return match wasm_example_2_i32_string_internal ( wat_source, f, a, b) {
@@ -140,107 +79,6 @@ fn wasm_string_2_i32(wat_source: String, f: String, a: i32, b: i32) -> Result<St
140
79
}
141
80
}
142
81
143
- fn wasm_example_2_i32_internal ( wat_source : String , buffer : bool , f : String , a : i32 , b : i32 ) -> Result < i32 , anyhow:: Error > {
144
- let engine = Engine :: default ( ) ;
145
-
146
- // A `Store` is what will own instances, functions, globals, etc. All wasm
147
- // items are stored within a `Store`, and it's what we'll always be using to
148
- // interact with the wasm world. Custom data can be stored in stores but for
149
- // now we just use `()`.
150
- let mut store = Store :: new ( & engine, ( ) ) ;
151
- let mut linker = Linker :: new ( & engine) ;
152
-
153
- if buffer {
154
- let memory_ty = MemoryType :: new ( 1 , None ) ;
155
- let memory = Memory :: new ( & mut store, memory_ty) ?;
156
- linker. define ( & store, "env" , "buffer" , memory) ?;
157
- }
158
-
159
- // We start off by creating a `Module` which represents a compiled form
160
- // of our input wasm module. In this case it'll be JIT-compiled after
161
- // we parse the text format.
162
- let module = Module :: new ( & engine, wat_source) ?;
163
-
164
- // With a compiled `Module` we can then instantiate it, creating
165
- // an `Instance` which we can actually poke at functions on.
166
- // let instance = Instance::new(&mut store, &module, &[])?;
167
- let instance = linker. instantiate ( & mut store, & module) ?;
168
-
169
- // The `Instance` gives us access to various exported functions and items,
170
- // which we access here to pull out our `answer` exported function and
171
- // run it.
172
- let answer = instance
173
- . get_func ( & mut store, & f)
174
- . expect ( & format ! ( "{} was not an exported function" , f) ) ;
175
-
176
- // There's a few ways we can call the `answer` `Func` value. The easiest
177
- // is to statically assert its signature with `typed` (in this case
178
- // asserting it takes no arguments and returns one i32) and then call it.
179
- let answer = answer. typed :: < ( i32 , i32 ) , i32 > ( & store) ?;
180
-
181
- // And finally we can call our function! Note that the error propagation
182
- // with `?` is done to handle the case where the wasm function traps.
183
- let result = answer. call ( & mut store, ( a, b) ) ?;
184
-
185
- return Ok ( result) ;
186
- }
187
-
188
- fn wasm_example_2_i32_n_internal ( wat_source : String , buffer : bool , f : String , a : i32 , b : i32 ) -> Result < Vec < i32 > , anyhow:: Error > {
189
- let engine = Engine :: default ( ) ;
190
-
191
- // A `Store` is what will own instances, functions, globals, etc. All wasm
192
- // items are stored within a `Store`, and it's what we'll always be using to
193
- // interact with the wasm world. Custom data can be stored in stores but for
194
- // now we just use `()`.
195
- let mut store = Store :: new ( & engine, ( ) ) ;
196
- let mut linker = Linker :: new ( & engine) ;
197
-
198
- if buffer {
199
- let memory_ty = MemoryType :: new ( 1 , None ) ;
200
- let memory = Memory :: new ( & mut store, memory_ty) ?;
201
- linker. define ( & store, "env" , "buffer" , memory) ?;
202
- }
203
-
204
- // We start off by creating a `Module` which represents a compiled form
205
- // of our input wasm module. In this case it'll be JIT-compiled after
206
- // we parse the text format.
207
- let module = Module :: new ( & engine, wat_source) ?;
208
-
209
- // With a compiled `Module` we can then instantiate it, creating
210
- // an `Instance` which we can actually poke at functions on.
211
- // let instance = Instance::new(&mut store, &module, &[])?;
212
- let instance = linker. instantiate ( & mut store, & module) ?;
213
-
214
- // The `Instance` gives us access to various exported functions and items,
215
- // which we access here to pull out our `answer` exported function and
216
- // run it.
217
- let answer = instance
218
- . get_func ( & mut store, & f)
219
- . expect ( & format ! ( "{} was not an exported function" , f) ) ;
220
-
221
- let func_type = answer. ty ( & store) ;
222
- // There's a few ways we can call the `answer` `Func` value. The easiest
223
- // is to statically assert its signature with `typed` (in this case
224
- // asserting it takes no arguments and returns one i32) and then call it.
225
- // let answer = answer.typed::<(i32, i32), i32>(&store)?;
226
-
227
- // let args = vec![a, b];
228
- let args: & [ Val ] = & [ Val :: I32 ( a) , Val :: I32 ( b) ] ;
229
-
230
- let mut result: Vec < Val > = Vec :: with_capacity ( 16 ) ;
231
- // result.resize(2, Val::I32(0));
232
- let result_length = func_type. results ( ) . len ( ) ;
233
- result. resize ( result_length, Val :: I32 ( 0 ) ) ;
234
-
235
- // And finally we can call our function! Note that the error propagation
236
- // with `?` is done to handle the case where the wasm function traps.
237
- answer. call ( & mut store, & args, & mut result) ?;
238
-
239
- let result: Vec < _ > = result. iter ( ) . map ( |v| v. unwrap_i32 ( ) ) . collect ( ) ;
240
-
241
- return Ok ( result) ;
242
- }
243
-
244
82
fn wasm_example_2_i32_string_internal ( wat_source : String , f : String , a : i32 , b : i32 ) -> Result < String , anyhow:: Error > {
245
83
let engine = Engine :: default ( ) ;
246
84
@@ -354,8 +192,5 @@ rustler::init!("Elixir.ComponentsGuide.Rustler.Math", [
354
192
reverse_string,
355
193
wasm_example_n_i32,
356
194
wasm_example_0,
357
- wasm_example_1_i32,
358
- wasm_example_2_i32,
359
- wasm_buffer_2_i32,
360
195
wasm_string_2_i32
361
196
] ) ;
0 commit comments