Skip to content

Commit 9d4ebf0

Browse files
committed
Remove unused wasm functions
1 parent 48eea5d commit 9d4ebf0

File tree

3 files changed

+2
-174
lines changed

3 files changed

+2
-174
lines changed

lib/components_guide/rustler/math.ex

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,12 @@ defmodule ComponentsGuide.Rustler.Math do
2121
def reverse_string(_), do: error()
2222
def wasm_example_n_i32(_, _, _), do: error()
2323
def wasm_example_0(_, _), do: error()
24-
def wasm_example_1_i32(_, _, _), do: error()
25-
def wasm_example_2_i32(_, _, _, _), do: error()
26-
def wasm_buffer_2_i32(_, _, _, _), do: error()
2724
def wasm_string_2_i32(_, _, _, _), do: error()
2825

2926
def wasm_example(source, f), do: wasm_example_0(source, f)
3027
def wasm_example(source, f, a), do: wasm_example_n_i32(source, f, [a]) |> process_result()
3128
def wasm_example(source, f, a, b), do: wasm_example_n_i32(source, f, [a, b]) |> process_result()
3229

33-
def wasm_buffer(source, f, a, b) do
34-
wasm_buffer_2_i32(source, f, a, b) |> process_result()
35-
end
3630
def wasm_string(source, f, a, b), do: wasm_string_2_i32(source, f, a, b)
3731

3832
defp error, do: :erlang.nif_error(:nif_not_loaded)

native/componentsguide_rustler_math/src/lib.rs

Lines changed: 1 addition & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn reverse_string(a: String) -> String {
1818
fn wasm_example_n_i32(source: String, f: String, args: Vec<i32>) -> Result<Vec<i32>, Error> {
1919
// return Ok(5);
2020
//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) {
2222
Ok(v) => Ok(v),
2323
Err(e) => Err(Error::Term(Box::new(e.to_string())))
2424
}
@@ -71,67 +71,6 @@ fn wasm_example_0_internal(source: String, f: String) -> Result<i32, anyhow::Err
7171
return Ok(result);
7272
}
7373

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-
13574
#[rustler::nif]
13675
fn wasm_string_2_i32(wat_source: String, f: String, a: i32, b: i32) -> Result<String, Error> {
13776
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
14079
}
14180
}
14281

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-
24482
fn wasm_example_2_i32_string_internal(wat_source: String, f: String, a: i32, b: i32) -> Result<String, anyhow::Error> {
24583
let engine = Engine::default();
24684

@@ -354,8 +192,5 @@ rustler::init!("Elixir.ComponentsGuide.Rustler.Math", [
354192
reverse_string,
355193
wasm_example_n_i32,
356194
wasm_example_0,
357-
wasm_example_1_i32,
358-
wasm_example_2_i32,
359-
wasm_buffer_2_i32,
360195
wasm_string_2_i32
361196
]);

test/components_guide/rustler/math_test.exs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ defmodule ComponentsGuide.Rustler.MathTest do
5151
"""
5252

5353
assert Math.wasm_example(wasm_source, "add", 7, 5) == 12
54-
assert Math.wasm_buffer(wasm_source, "add", 7, 5) == 12
5554
end
5655

5756
test "wasm_example/4 multiplying two numbers" do
@@ -108,7 +107,7 @@ defmodule ComponentsGuide.Rustler.MathTest do
108107
)
109108
"""
110109

111-
assert Math.wasm_buffer(wasm_source, "main", 0, 0) == {256, 30}
110+
assert Math.wasm_example(wasm_source, "main", 0, 0) == {256, 30}
112111
assert Math.wasm_string(wasm_source, "main", 0, 0) == "Know the length of this string"
113112
end
114113

0 commit comments

Comments
 (0)