Skip to content

Commit

Permalink
Shared memory support (#9507)
Browse files Browse the repository at this point in the history
* Trying to add shared memory :)

* Adding initial support for shared memory in the memory type and
the C-API

* Fixing dummies

* Remving the SharedMemory(CMemoryType) variant from the wasm_extern_* types

* Removing ExternType::SharedMemory as well! Not needed.

* * Using MemoryTypeBuilder as suggested.
* moving dummy shared memory

* Fixing clang format errors

* Try again to appease clang-format

---------

Co-authored-by: Alex Crichton <[email protected]>
  • Loading branch information
atilag and alexcrichton authored Nov 1, 2024
1 parent fd384cb commit 288c151
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
7 changes: 3 additions & 4 deletions crates/c-api/include/wasmtime/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ extern "C" {
* Note that this function is preferred over #wasm_memorytype_new for
* compatibility with the memory64 proposal.
*/
WASM_API_EXTERN wasm_memorytype_t *wasmtime_memorytype_new(uint64_t min,
bool max_present,
uint64_t max,
bool is_64);
WASM_API_EXTERN wasm_memorytype_t *
wasmtime_memorytype_new(uint64_t min, bool max_present, uint64_t max,
bool is_64, bool shared);

/**
* \brief Returns the minimum size, in pages, of the specified memory type.
Expand Down
4 changes: 3 additions & 1 deletion crates/c-api/src/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub extern "C" fn wasm_extern_kind(e: &wasm_extern_t) -> wasm_externkind_t {
Extern::Global(_) => crate::WASM_EXTERN_GLOBAL,
Extern::Table(_) => crate::WASM_EXTERN_TABLE,
Extern::Memory(_) => crate::WASM_EXTERN_MEMORY,
Extern::SharedMemory(_) => todo!(),
Extern::SharedMemory(_) => panic!(
"Shared Memory no implemented for wasm_* types. Please use wasmtime_* types instead"
),
}
}

Expand Down
21 changes: 12 additions & 9 deletions crates/c-api/src/types/memory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{wasm_externtype_t, wasm_limits_t, CExternType};
use std::cell::OnceCell;
use std::convert::TryFrom;
use wasmtime::MemoryType;
use wasmtime::{MemoryType, MemoryTypeBuilder};

#[repr(transparent)]
#[derive(Clone)]
Expand Down Expand Up @@ -71,20 +71,23 @@ pub extern "C" fn wasmtime_memorytype_new(
maximum_specified: bool,
maximum: u64,
memory64: bool,
shared: bool,
) -> Box<wasm_memorytype_t> {
let maximum = if maximum_specified {
Some(maximum)
} else {
None
};
Box::new(wasm_memorytype_t::new(if memory64 {
MemoryType::new64(minimum, maximum)
} else {
MemoryType::new(
u32::try_from(minimum).unwrap(),
maximum.map(|i| u32::try_from(i).unwrap()),
)
}))

Box::new(wasm_memorytype_t::new(
MemoryTypeBuilder::default()
.min(minimum)
.max(maximum)
.memory64(memory64)
.shared(shared)
.build()
.unwrap(),
))
}

#[no_mangle]
Expand Down
1 change: 1 addition & 0 deletions crates/fuzzing/src/oracles/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub fn dummy_memory<T>(store: &mut Store<T>, ty: MemoryType) -> Result<Memory> {

#[cfg(test)]
mod tests {

use super::*;

fn store() -> Store<()> {
Expand Down

0 comments on commit 288c151

Please sign in to comment.