-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend C API with interfaces needed to use threads
- Loading branch information
Showing
12 changed files
with
302 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* \file wasmtime/sharedmemory.h | ||
* | ||
* Wasmtime API for interacting with wasm shared memories. | ||
*/ | ||
|
||
#ifndef WASMTIME_SHAREDMEMORY_H | ||
#define WASMTIME_SHAREDMEMORY_H | ||
|
||
#include <wasm.h> | ||
#include <wasmtime/error.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* \brief Interface for shared memories. | ||
* | ||
* For more information see the Rust documentation at: | ||
* https://docs.wasmtime.dev/api/wasmtime/struct.SharedMemory.html | ||
*/ | ||
typedef struct wasmtime_sharedmemory wasmtime_sharedmemory_t; | ||
|
||
/** | ||
* \brief Creates a new WebAssembly shared linear memory | ||
* | ||
* \param ty the type of the memory to create | ||
* \param ret where to store the returned memory | ||
* | ||
* If an error happens when creating the memory it's returned and owned by the | ||
* caller. If an error happens then `ret` is not filled in. | ||
*/ | ||
WASM_API_EXTERN wasmtime_error_t * | ||
wasmtime_sharedmemory_new(const wasm_engine_t *engine, | ||
const wasm_memorytype_t *ty, | ||
wasmtime_sharedmemory_t **ret); | ||
|
||
/** | ||
* \brief Deletes shared linear memory | ||
* | ||
* \param memory memory to be deleted | ||
*/ | ||
WASM_API_EXTERN void | ||
wasmtime_sharedmemory_delete(wasmtime_sharedmemory_t *memory); | ||
|
||
/** | ||
* \brief Returns the type of the shared memory specified | ||
*/ | ||
WASM_API_EXTERN wasm_memorytype_t * | ||
wasmtime_sharedmemory_type(const wasmtime_sharedmemory_t *memory); | ||
|
||
/** | ||
* \brief Returns the base pointer in memory where | ||
the shared linear memory starts. | ||
*/ | ||
WASM_API_EXTERN uint8_t * | ||
wasmtime_sharedmemory_data(const wasmtime_sharedmemory_t *memory); | ||
|
||
/** | ||
* \brief Returns the byte length of this shared linear memory. | ||
*/ | ||
WASM_API_EXTERN size_t | ||
wasmtime_sharedmemory_data_size(const wasmtime_sharedmemory_t *memory); | ||
|
||
/** | ||
* \brief Returns the length, in WebAssembly pages, of this shared linear memory | ||
*/ | ||
WASM_API_EXTERN uint64_t | ||
wasmtime_sharedmemory_size(const wasmtime_sharedmemory_t *memory); | ||
|
||
/** | ||
* \brief Attempts to grow the specified shared memory by `delta` pages. | ||
* | ||
* \param memory the memory to grow | ||
* \param delta the number of pages to grow by | ||
* \param prev_size where to store the previous size of memory | ||
* | ||
* If memory cannot be grown then `prev_size` is left unchanged and an error is | ||
* returned. Otherwise `prev_size` is set to the previous size of the memory, in | ||
* WebAssembly pages, and `NULL` is returned. | ||
*/ | ||
WASM_API_EXTERN wasmtime_error_t * | ||
wasmtime_sharedmemory_grow(const wasmtime_sharedmemory_t *memory, | ||
uint64_t delta, uint64_t *prev_size); | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif | ||
|
||
#endif // WASMTIME_SHAREDMEMORY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* \file wasmtime/wasictx.h | ||
* | ||
* Wasmtime API for creating WASI context object. | ||
*/ | ||
|
||
#ifndef WASMTIME_WASICTX_H | ||
#define WASMTIME_WASICTX_H | ||
|
||
#include <wasi.h> | ||
#include <wasm.h> | ||
#include <wasmtime/error.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* \brief Manually created WASI context object. | ||
*/ | ||
typedef struct wasmtime_wasictx wasmtime_wasictx_t; | ||
|
||
/** | ||
* \brief Deletes WASI context object. | ||
*/ | ||
WASM_API_EXTERN void wasmtime_wasictx_delete(wasmtime_wasictx_t *wasictx); | ||
|
||
/** | ||
* \brief Creates WASI context object from provided config. | ||
* | ||
* \param wasi WASI config to be used to configure created context | ||
* \param ret pointer to where created `wasmtime_wasictx_t*` will be written | ||
* | ||
* This function does take ownership of `wasi`, even when returning with error. | ||
* Space pointed to by `ret` is only written when returning without error. | ||
*/ | ||
WASM_API_EXTERN wasmtime_error_t * | ||
wasmtime_wasictx_new(wasi_config_t *wasi, wasmtime_wasictx_t **ret); | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif | ||
|
||
#endif // WASMTIME_WASICTX_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use crate::{handle_result, wasm_engine_t, wasm_memorytype_t, wasmtime_error_t}; | ||
use std::cell::UnsafeCell; | ||
use wasmtime::SharedMemory; | ||
|
||
type wasmtime_sharedmemory_t = SharedMemory; | ||
|
||
wasmtime_c_api_macros::declare_own!(wasmtime_sharedmemory_t); | ||
|
||
#[no_mangle] | ||
pub extern "C" fn wasmtime_sharedmemory_new( | ||
engine: &wasm_engine_t, | ||
ty: &wasm_memorytype_t, | ||
ret: &mut *mut wasmtime_sharedmemory_t, | ||
) -> Option<Box<wasmtime_error_t>> { | ||
handle_result( | ||
SharedMemory::new(&engine.engine, ty.ty().ty.clone()), | ||
|mem| *ret = Box::<wasmtime_sharedmemory_t>::into_raw(Box::new(mem)), | ||
) | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn wasmtime_sharedmemory_type( | ||
mem: &wasmtime_sharedmemory_t, | ||
) -> Box<wasm_memorytype_t> { | ||
Box::new(wasm_memorytype_t::new(mem.ty())) | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn wasmtime_sharedmemory_data( | ||
mem: &wasmtime_sharedmemory_t, | ||
) -> *const UnsafeCell<u8> { | ||
mem.data().as_ptr() | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn wasmtime_sharedmemory_data_size(mem: &wasmtime_sharedmemory_t) -> usize { | ||
mem.data().len() | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn wasmtime_sharedmemory_size(mem: &wasmtime_sharedmemory_t) -> u64 { | ||
mem.size() | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn wasmtime_sharedmemory_grow( | ||
mem: &wasmtime_sharedmemory_t, | ||
delta: u64, | ||
prev_size: &mut u64, | ||
) -> Option<Box<wasmtime_error_t>> { | ||
handle_result(mem.grow(delta), |prev| *prev_size = prev) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.