Skip to content

Commit 0e4bfde

Browse files
Merge pull request #14 from arnaudgolfouse/tests
Tests
2 parents aa695aa + 64acfca commit 0e4bfde

File tree

6 files changed

+273
-102
lines changed

6 files changed

+273
-102
lines changed

examples/hello_c/hello.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ EMSCRIPTEN_KEEPALIVE void wasm_minimal_protocol_free_byte_buffer(uint8_t *ptr,
3030

3131
EMSCRIPTEN_KEEPALIVE
3232
int32_t hello(void) {
33-
const char static_message[] = "Hello world !";
33+
const char static_message[] = "Hello from wasm!!!";
3434
const size_t length = sizeof(static_message);
3535
char *message = malloc(length);
3636
memcpy((void *)message, (void *)static_message, length);

examples/hello_rust/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn hello() -> Vec<u8> {
99

1010
#[wasm_func]
1111
pub fn double_it(arg: &[u8]) -> Vec<u8> {
12-
[arg, b".", arg].concat()
12+
[arg, arg].concat()
1313
}
1414

1515
#[wasm_func]

examples/hello_zig/hello.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export fn wasm_minimal_protocol_free_byte_buffer(ptr: [*]u8, len: usize) void {
1717
// ===
1818

1919
export fn hello() i32 {
20-
const message = "Hello world !";
20+
const message = "Hello from wasm!!!";
2121
var result = allocator.alloc(u8, message.len) catch return 1;
2222
@memcpy(result, message);
2323
wasm_minimal_protocol_send_result_to_host(result.ptr, result.len);

examples/host-wasmi/src/lib.rs

+41-30
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ impl<'a> ReturnedData<'a> {
3030

3131
impl Drop for ReturnedData<'_> {
3232
fn drop(&mut self) {
33-
self.free_function
34-
.call(
35-
&mut *self.context_mut,
36-
&[Value::I32(self.ptr as _), Value::I32(self.len as _)],
37-
&mut [],
38-
)
39-
.unwrap();
33+
if self.ptr != 0 {
34+
self.free_function
35+
.call(
36+
&mut *self.context_mut,
37+
&[Value::I32(self.ptr as _), Value::I32(self.len as _)],
38+
&mut [],
39+
)
40+
.unwrap();
41+
}
4042
}
4143
}
4244

@@ -123,36 +125,40 @@ impl PluginInstance {
123125
})
124126
}
125127

126-
fn write(&mut self, args: &[&[u8]]) {
127-
self.store.data_mut().arg_buffer = args.concat();
128-
}
129-
130-
pub fn call(&mut self, function: &str, args: &[&[u8]]) -> Result<ReturnedData, String> {
131-
self.write(args);
128+
pub fn call<'a>(
129+
&mut self,
130+
function: &str,
131+
args: impl IntoIterator<Item = &'a [u8]>,
132+
) -> Result<ReturnedData, String> {
133+
self.store.data_mut().result_ptr = 0;
134+
self.store.data_mut().result_len = 0;
135+
136+
let mut result_args = Vec::new();
137+
let arg_buffer = &mut self.store.data_mut().arg_buffer;
138+
arg_buffer.clear();
139+
for arg in args {
140+
result_args.push(Value::I32(arg.len() as _));
141+
arg_buffer.extend_from_slice(arg);
142+
}
132143

133144
let (_, function) = self
134145
.functions
135146
.iter()
136147
.find(|(s, _)| s == function)
137-
.ok_or(format!("Plugin doesn't have the method: {function}"))?;
138-
139-
let result_args = args
140-
.iter()
141-
.map(|a| Value::I32(a.len() as _))
142-
.collect::<Vec<_>>();
148+
.ok_or(format!("plugin doesn't have the method: {function}"))?;
143149

144-
let mut code = [Value::I32(2)];
145-
let is_err = function
146-
.call(&mut self.store, &result_args, &mut code)
147-
.is_err();
148-
let code = if is_err {
149-
Value::I32(2)
150-
} else {
151-
code.first().cloned().unwrap_or(Value::I32(3)) // if the function returns nothing
152-
};
150+
let mut code = Value::I32(2);
151+
let ty = function.ty(&self.store);
152+
if ty.params().len() != result_args.len() {
153+
return Err("incorrect number of arguments".to_string());
154+
}
153155

156+
let call_result = function.call(
157+
&mut self.store,
158+
&result_args,
159+
std::array::from_mut(&mut code),
160+
);
154161
let (ptr, len) = (self.store.data().result_ptr, self.store.data().result_len);
155-
156162
let result = ReturnedData {
157163
memory: self.memory,
158164
ptr,
@@ -161,13 +167,18 @@ impl PluginInstance {
161167
context_mut: &mut self.store,
162168
};
163169

170+
match call_result {
171+
Ok(()) => {}
172+
Err(wasmi::Error::Trap(_)) => return Err("plugin panicked".to_string()),
173+
Err(_) => return Err("plugin did not respect the protocol".to_string()),
174+
};
175+
164176
match code {
165177
Value::I32(0) => Ok(result),
166178
Value::I32(1) => Err(match std::str::from_utf8(result.get()) {
167179
Ok(err) => format!("plugin errored with: '{}'", err,),
168180
Err(_) => String::from("plugin errored and did not return valid UTF-8"),
169181
}),
170-
Value::I32(2) => Err("plugin panicked".to_string()),
171182
_ => Err("plugin did not respect the protocol".to_string()),
172183
}
173184
}

examples/test-runner/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ edition = "2021"
77
anyhow = "1.0.71"
88
cfg-if = "1.0.0"
99
host-wasmi = { path = "../host-wasmi" }
10-
wasi-stub = { path = "../../wasi-stub", optional = true }
10+
wasi-stub = { path = "../../wasi-stub" }
1111

1212

1313
[features]
1414
default = []
15-
wasi = ["dep:wasi-stub"]
15+
wasi = []

0 commit comments

Comments
 (0)