fix(c-api) wasm_limits_t contains Pages, not Bytes.

When building a `wasm_memorytype_t` with `wasm_memorytype_new`, we
pass a `wasm_limits_t`, where `min` and `max` represent `Pages`. This
semantics is set by `wasm_memorytype_new` itself where `min` and `max`
from `wasm_limits_t` are used to compute `Pages`, which are then passed
to `MemoryType`.

Then, in `wasm_memorytype_limits`, we expect to get the same
`wasm_limits_t` given to `wasm_memorytype_new`. But it's not!

The same `MemoryType` is read, good. The `minimum` and `maximum`
fields are `Pages`, good. Then, we compute the `min` and `max` values
for the resulting `wasm_limits_t`, which receive `Page.bytes().0`, not
good! We don't want the number of bytes, but the number of pages.

This patch fixes that.
This commit is contained in:
Ivan Enderlin
2020-10-08 17:14:45 +02:00
parent 70baded113
commit 5d19813e63

View File

@@ -39,6 +39,7 @@ pub unsafe extern "C" fn wasm_memorytype_new(limits: &wasm_limits_t) -> Box<wasm
} else {
Some(Pages(limits.max as _))
};
Box::new(wasm_memorytype_t {
extern_: wasm_externtype_t {
inner: ExternType::Memory(MemoryType::new(min_pages, max_pages, false)),
@@ -54,8 +55,9 @@ pub unsafe extern "C" fn wasm_memorytype_delete(_memorytype: Option<Box<wasm_mem
#[no_mangle]
pub unsafe extern "C" fn wasm_memorytype_limits(mt: &wasm_memorytype_t) -> *const wasm_limits_t {
let md = mt.as_memorytype();
Box::into_raw(Box::new(wasm_limits_t {
min: md.minimum.bytes().0 as _,
max: md.maximum.map(|max| max.bytes().0 as _).unwrap_or(0),
min: md.minimum.0 as _,
max: md.maximum.map(|max| max.0 as _).unwrap_or(0),
}))
}