Skip to content

Commit 8c9772f

Browse files
committed
Fix returned strings and lists in async wasmtime code
Fixes #141
1 parent 48f364d commit 8c9772f

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

crates/gen-wasmtime/src/lib.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,34 +1874,42 @@ impl Bindgen for FunctionBindgen<'_> {
18741874
self.gen.needs_copy_slice = true;
18751875
self.needs_functions
18761876
.insert(free.to_string(), NeededFunction::Free);
1877-
let (stringify, align) = match element {
1878-
Type::Char => (true, 1),
1879-
_ => (false, self.gen.sizes.align(element)),
1877+
let (stringify, align, el_size) = match element {
1878+
Type::Char => (true, 1, 1),
1879+
_ => (
1880+
false,
1881+
self.sizes().align(element),
1882+
self.sizes().size(element),
1883+
),
18801884
};
18811885
let tmp = self.tmp();
18821886
self.push_str(&format!("let ptr{} = {};\n", tmp, operands[0]));
18831887
self.push_str(&format!("let len{} = {};\n", tmp, operands[1]));
1884-
let result = format!(
1888+
self.push_str(&format!(
18851889
"
1886-
copy_slice(
1887-
&mut caller,
1888-
memory,
1889-
func_{},
1890-
ptr{tmp}, len{tmp}, {}
1891-
)?
1892-
",
1893-
free,
1890+
let data{tmp} = copy_slice(
1891+
&mut caller,
1892+
memory,
1893+
ptr{tmp}, len{tmp}, {}
1894+
)?;
1895+
",
18941896
align,
1895-
tmp = tmp
1897+
tmp = tmp,
1898+
));
1899+
self.call_intrinsic(
1900+
free,
1901+
// we use normal multiplication here as copy_slice has
1902+
// already verified that multiplied size fits i32
1903+
format!("(ptr{tmp}, len{tmp} * {}, {})", el_size, align, tmp = tmp),
18961904
);
18971905
if stringify {
18981906
results.push(format!(
1899-
"String::from_utf8({})
1907+
"String::from_utf8(data{})
19001908
.map_err(|_| wasmtime::Trap::new(\"invalid utf-8\"))?",
1901-
result
1909+
tmp,
19021910
));
19031911
} else {
1904-
results.push(result);
1912+
results.push(format!("data{}", tmp));
19051913
}
19061914
}
19071915
None => {

crates/wasmtime/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ pub mod rt {
137137
pub fn copy_slice<T: Endian>(
138138
store: impl AsContextMut,
139139
memory: &Memory,
140-
free: &TypedFunc<(i32, i32, i32), ()>,
141140
base: i32,
142141
len: i32,
143142
align: i32,
@@ -150,9 +149,7 @@ pub mod rt {
150149
.get(base as usize..)
151150
.and_then(|s| s.get(..size as usize))
152151
.ok_or_else(|| Trap::new("out of bounds read"))?;
153-
let result = Le::from_slice(slice).iter().map(|s| s.get()).collect();
154-
free.call(store, (base, size as i32, align))?;
155-
Ok(result)
152+
Ok(Le::from_slice(slice).iter().map(|s| s.get()).collect())
156153
}
157154

158155
macro_rules! as_traits {

0 commit comments

Comments
 (0)