diff --git a/Cargo.toml b/Cargo.toml index fbfb507..aac43f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "wasm3" version = "0.5.0" authors = ["Lukas Tobias Wirth "] -edition = "2018" +edition = "2021" description = "Rust bindings for wasm3" homepage = "https://github.com/wasm3/wasm3-rs" repository = "https://github.com/wasm3/wasm3-rs" @@ -15,12 +15,9 @@ exclude = ["examples/*", "tests/*"] members = ["wasm3-sys"] [features] -default = ["wasi", "std", "use-32bit-slots"] - +default = ["wasi", "use-32bit-slots"] wasi = ["ffi/wasi"] -std = [] use-32bit-slots = ["ffi/use-32bit-slots"] - build-bindgen = ["ffi/build-bindgen"] [dependencies] diff --git a/src/environment.rs b/src/environment.rs index e3e060b..643e224 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -57,7 +57,7 @@ impl Environment { impl core::cmp::Eq for Environment {} impl core::cmp::PartialEq for Environment { - fn eq(&self, &Environment(ref other): &Environment) -> bool { + fn eq(&self, Environment(other): &Environment) -> bool { alloc::rc::Rc::ptr_eq(&self.0, other) } } diff --git a/src/error.rs b/src/error.rs index 7c08cfa..91822f2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -60,8 +60,7 @@ impl cmp::PartialEq for Trap { } } -#[cfg(feature = "std")] -impl std::error::Error for Trap {} +impl core::error::Error for Trap {} impl fmt::Display for Trap { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(unsafe { cstr_to_str(self.as_ptr()) }, f) @@ -85,8 +84,7 @@ impl cmp::PartialEq for Wasm3Error { } } -#[cfg(feature = "std")] -impl std::error::Error for Wasm3Error {} +impl core::error::Error for Wasm3Error {} impl fmt::Debug for Wasm3Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(unsafe { cstr_to_str(self.0) }, f) @@ -129,8 +127,7 @@ impl Error { } } -#[cfg(feature = "std")] -impl std::error::Error for Error {} +impl core::error::Error for Error {} impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { diff --git a/src/function.rs b/src/function.rs index c5534a3..a128879 100644 --- a/src/function.rs +++ b/src/function.rs @@ -67,14 +67,14 @@ pub struct Function<'rt, Args, Ret> { _pd: PhantomData<*const (Args, Ret)>, } -impl<'rt, Args, Ret> Eq for Function<'rt, Args, Ret> {} -impl<'rt, Args, Ret> PartialEq for Function<'rt, Args, Ret> { +impl Eq for Function<'_, Args, Ret> {} +impl PartialEq for Function<'_, Args, Ret> { fn eq(&self, other: &Self) -> bool { self.raw == other.raw } } -impl<'rt, Args, Ret> Hash for Function<'rt, Args, Ret> { +impl Hash for Function<'_, Args, Ret> { fn hash(&self, state: &mut H) { self.raw.hash(state); } @@ -173,7 +173,7 @@ macro_rules! func_call_impl { } func_call_impl!(A, B, C, D, E, F, G, H, J, K, L, M, N, O, P, Q); -impl<'rt, ARG, Ret> Function<'rt, ARG, Ret> +impl Function<'_, ARG, Ret> where Ret: WasmType, ARG: WasmArg, @@ -188,7 +188,7 @@ where } } -impl<'rt, Ret> Function<'rt, (), Ret> +impl Function<'_, (), Ret> where Ret: WasmType, { diff --git a/src/lib.rs b/src/lib.rs index 4f97f8e..41c5196 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,22 +1,21 @@ -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] #![warn(missing_docs)] //! A rust wrapper for [WASM3](https://github.com/wasm3/wasm3). extern crate alloc; -pub mod error; - mod environment; -pub use self::environment::Environment; +pub mod error; mod function; -pub use self::function::{CallContext, Function, RawCall}; mod macros; -pub use self::macros::*; mod module; -pub use self::module::{Module, ParsedModule}; mod runtime; -pub use self::runtime::Runtime; mod ty; -pub use self::ty::{WasmArg, WasmArgs, WasmType}; mod utils; + +pub use self::environment::Environment; +pub use self::function::{CallContext, Function, RawCall}; +pub use self::module::{Module, ParsedModule}; +pub use self::runtime::Runtime; +pub use self::ty::{WasmArg, WasmArgs, WasmType}; pub use ffi as wasm3_sys; diff --git a/src/module.rs b/src/module.rs index 43f36b9..3deac90 100644 --- a/src/module.rs +++ b/src/module.rs @@ -84,7 +84,7 @@ impl<'rt> Module<'rt> { /// Links the given function to the corresponding module and function name. /// This allows linking a more verbose function, as it gets access to the unsafe - /// runtime parts. For easier use the [`make_func_wrapper`] should be used to create + /// runtime parts. For easier use the [`crate::make_func_wrapper`] should be used to create /// the unsafe facade for your function that then can be passed to this. /// /// For a simple API see [`link_closure`] which takes a closure instead. @@ -221,6 +221,11 @@ impl<'rt> Module<'rt> { unsafe { cstr_to_str(ffi::m3_GetModuleName(self.raw)) } } + /// Compile all functions in the module. + pub fn compile(&mut self) -> Result<()> { + unsafe { Error::from_ffi_res(ffi::m3_CompileModule(self.raw)) } + } + /// Links wasi to this module. #[cfg(feature = "wasi")] pub fn link_wasi(&mut self) -> Result<()> { diff --git a/src/ty.rs b/src/ty.rs index 86a3b64..afe602c 100644 --- a/src/ty.rs +++ b/src/ty.rs @@ -242,7 +242,7 @@ macro_rules! args_impl { }; (@do_impl) => {/* catch the () case, since its implementation differs slightly */}; (@do_impl $($types:ident,)*) => { - #[allow(clippy::eval_order_dependence)] + #[allow(clippy::mixed_read_write_in_expression)] #[allow(unused_assignments)] impl<$($types,)*> WasmArgs for ($($types,)*) where $($types: WasmArg,)* { diff --git a/src/utils.rs b/src/utils.rs index f923a39..d899a0c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,5 @@ use alloc::vec::Vec; +use core::{slice, str}; pub unsafe fn bytes_till_null<'a>(ptr: *const cty::c_char) -> &'a [u8] { if ptr.is_null() { @@ -11,15 +12,15 @@ pub unsafe fn bytes_till_null<'a>(ptr: *const cty::c_char) -> &'a [u8] { ptr = ptr.add(1); len += 1; } - core::slice::from_raw_parts(start, len) + slice::from_raw_parts(start, len) } pub unsafe fn cstr_to_str<'a>(ptr: *const cty::c_char) -> &'a str { - core::str::from_utf8_unchecked(bytes_till_null(ptr)) + str::from_utf8_unchecked(bytes_till_null(ptr)) } pub fn str_to_cstr_owned(str: &str) -> Vec { - let mut cstr = Vec::with_capacity(str.as_bytes().len() + 1); + let mut cstr = Vec::with_capacity(str.len() + 1); cstr.extend(str.bytes().map(|c| c as cty::c_char)); cstr.push(0 as cty::c_char); cstr diff --git a/wasm3-sys/Cargo.toml b/wasm3-sys/Cargo.toml index 2b9adc5..5e404c1 100644 --- a/wasm3-sys/Cargo.toml +++ b/wasm3-sys/Cargo.toml @@ -2,7 +2,7 @@ name = "wasm3-sys" version = "0.5.0" authors = ["Lukas Tobias Wirth "] -edition = "2018" +edition = "2021" description = "Raw ffi bindings for wasm3" homepage = "https://github.com/wasm3/wasm3-rs" repository = "https://github.com/wasm3/wasm3-rs" @@ -22,10 +22,10 @@ cty = "0.2" [build-dependencies] cc = "1" -shlex = "0.1.1" +shlex = "1.3.0" [build-dependencies.bindgen] -version = "0.59" +version = "0.71.1" optional = true [package.metadata.docs.rs] diff --git a/wasm3-sys/build.rs b/wasm3-sys/build.rs index c4ead44..d19ca00 100644 --- a/wasm3-sys/build.rs +++ b/wasm3-sys/build.rs @@ -27,28 +27,28 @@ fn gen_wrapper(out_path: &Path) -> PathBuf { #[cfg(not(feature = "build-bindgen"))] fn gen_bindings() { + use std::process::Command; let out_path = PathBuf::from(&env::var("OUT_DIR").unwrap()); - let wrapper_file = gen_wrapper(&out_path); - - let mut bindgen = std::process::Command::new("bindgen"); - bindgen - .arg(wrapper_file) - .arg("--use-core") - .arg("--ctypes-prefix") - .arg("cty") - .arg("--no-layout-tests") - .arg("--default-enum-style=moduleconsts") - .arg("--no-doc-comments") - .arg("--whitelist-function") - .arg(WHITELIST_REGEX_FUNCTION) - .arg("--whitelist-type") - .arg(WHITELIST_REGEX_TYPE) - .arg("--whitelist-var") - .arg(WHITELIST_REGEX_VAR) - .arg("--no-derive-debug"); - for &ty in PRIMITIVES.iter() { - bindgen.arg("--blacklist-type").arg(ty); + let mut bindgen = Command::new("bindgen"); + bindgen.arg(wrapper_file); + bindgen.args([ + "--use-core", + "--ctypes-prefix", + "cty", + "--no-layout-tests", + "--default-enum-style=moduleconsts", + "--no-doc-comments", + "--allowlist-function", + WHITELIST_REGEX_FUNCTION, + "--allowlist-type", + WHITELIST_REGEX_TYPE, + "--allowlist-var", + WHITELIST_REGEX_VAR, + "--no-derive-debug", + ]); + for &ty in PRIMITIVES { + bindgen.arg("--blocklist-type").arg(ty); } bindgen .arg("-o") @@ -57,26 +57,23 @@ fn gen_bindings() { .arg("--") .arg(format!( "-Dd_m3Use32BitSlots={}", - if cfg!(feature = "use-32bit-slots") { - 1 - } else { - 0 - } + cfg!(feature = "use-32bit-slots") as u8, )) .arg("-Dd_m3LogOutput=0") .arg("-Iwasm3/source"); - let status = bindgen.status().expect("Unable to generate bindings"); + let status = match bindgen.status() { + Ok(status) => status, + Err(error) => panic!("wasm3: unable to generate bindings: {error}"), + }; if !status.success() { - panic!("Failed to run bindgen: {:?}", status); + panic!("wasm3: failed to run bindgen: {:?}", status); } } #[cfg(feature = "build-bindgen")] fn gen_bindings() { let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - let wrapper_file = gen_wrapper(&out_path); - let mut bindgen = bindgen::builder() .header(wrapper_file.to_str().unwrap()) .use_core() @@ -91,26 +88,22 @@ fn gen_bindings() { bindgen = PRIMITIVES .iter() .fold(bindgen, |bindgen, ty| bindgen.blocklist_type(ty)); + let bindgen = bindgen.clang_args( + [ + &format!( + "-Dd_m3Use32BitSlots={}", + cfg!(feature = "use-32bit-slots") as u8, + ), + "-Dd_m3LogOutput=0", + "-Iwasm3/source", + ] + .iter(), + ); bindgen - .clang_args( - [ - &format!( - "-Dd_m3Use32BitSlots={}", - if cfg!(feature = "use-32bit-slots") { - 1 - } else { - 0 - } - ), - "-Dd_m3LogOutput=0", - "-Iwasm3/source", - ] - .iter(), - ) .generate() - .expect("Failed to generate bindings") + .unwrap_or_else(|error| panic!("bindgen: failed to generate bindings: {error}")) .write_to_file(out_path.join("bindings.rs").to_str().unwrap()) - .expect("Failed to write bindings"); + .unwrap_or_else(|error| panic!("bindgen: failed to write bindings: {error}")); } fn main() { @@ -120,7 +113,7 @@ fn main() { cfg.files( fs::read_dir(WASM3_SOURCE) - .unwrap_or_else(|_| panic!("failed to read {} directory", WASM3_SOURCE)) + .unwrap_or_else(|error| panic!("failed to read {WASM3_SOURCE} directory: {error}")) .filter_map(Result::ok) .map(|entry| entry.path()) .filter(|p| p.extension().and_then(OsStr::to_str) == Some("c")), @@ -150,11 +143,10 @@ fn main() { cfg.define( "d_m3Use32BitSlots", - if cfg!(feature = "use-32bit-slots") { - Some("1") - } else { - Some("0") - }, + match cfg!(feature = "use-32bit-slots") { + true => Some("1"), + false => Some("0"), + } ); cfg.compile("wasm3"); } diff --git a/wasm3-sys/wasm3 b/wasm3-sys/wasm3 index 6b8bcb1..139076a 160000 --- a/wasm3-sys/wasm3 +++ b/wasm3-sys/wasm3 @@ -1 +1 @@ -Subproject commit 6b8bcb1e07bf26ebef09a7211b0a37a446eafd52 +Subproject commit 139076a98b8321b67f850a844f558b5e91b5ac83