Created
August 23, 2025 00:43
-
-
Save alexcrichton/446dcd503253bc6c7795693bf11e5e26 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
commit b1bf88344905ac4813ec4a7bce58e475678b175f | |
Author: Alex Crichton <[email protected]> | |
Date: Fri Aug 22 17:15:17 2025 -0700 | |
wip | |
diff --git a/examples/min-platform/embedding/src/lib.rs b/examples/min-platform/embedding/src/lib.rs | |
index 460ea5d2c8..9448c3785e 100644 | |
--- a/examples/min-platform/embedding/src/lib.rs | |
+++ b/examples/min-platform/embedding/src/lib.rs | |
@@ -4,7 +4,7 @@ | |
extern crate alloc; | |
use alloc::string::ToString; | |
-use anyhow::Result; | |
+use anyhow::{Result, ensure}; | |
use core::ptr; | |
use wasmtime::{Engine, Instance, Linker, Module, Store}; | |
@@ -29,6 +29,8 @@ pub unsafe extern "C" fn run( | |
simple_add_size: usize, | |
simple_host_fn_module: *const u8, | |
simple_host_fn_size: usize, | |
+ simple_floats_module: *const u8, | |
+ simple_floats_size: usize, | |
) -> usize { | |
unsafe { | |
let buf = core::slice::from_raw_parts_mut(error_buf, error_size); | |
@@ -36,7 +38,8 @@ pub unsafe extern "C" fn run( | |
let simple_add = core::slice::from_raw_parts(simple_add_module, simple_add_size); | |
let simple_host_fn = | |
core::slice::from_raw_parts(simple_host_fn_module, simple_host_fn_size); | |
- match run_result(smoke, simple_add, simple_host_fn) { | |
+ let simple_floats = core::slice::from_raw_parts(simple_floats_module, simple_floats_size); | |
+ match run_result(smoke, simple_add, simple_host_fn, simple_floats) { | |
Ok(()) => 0, | |
Err(e) => { | |
let msg = format!("{e:?}"); | |
@@ -52,10 +55,12 @@ fn run_result( | |
smoke_module: &[u8], | |
simple_add_module: &[u8], | |
simple_host_fn_module: &[u8], | |
+ simple_floats_module: &[u8], | |
) -> Result<()> { | |
smoke(smoke_module)?; | |
simple_add(simple_add_module)?; | |
simple_host_fn(simple_host_fn_module)?; | |
+ simple_floats(simple_floats_module)?; | |
Ok(()) | |
} | |
@@ -78,7 +83,7 @@ fn simple_add(module: &[u8]) -> Result<()> { | |
let mut store = Store::new(&engine, ()); | |
let instance = Linker::new(&engine).instantiate(&mut store, &module)?; | |
let func = instance.get_typed_func::<(u32, u32), u32>(&mut store, "add")?; | |
- assert_eq!(func.call(&mut store, (2, 3))?, 5); | |
+ ensure!(func.call(&mut store, (2, 3))? == 5); | |
Ok(()) | |
} | |
@@ -93,7 +98,20 @@ fn simple_host_fn(module: &[u8]) -> Result<()> { | |
let mut store = Store::new(&engine, ()); | |
let instance = linker.instantiate(&mut store, &module)?; | |
let func = instance.get_typed_func::<(u32, u32, u32), u32>(&mut store, "add_and_mul")?; | |
- assert_eq!(func.call(&mut store, (2, 3, 4))?, 10); | |
+ ensure!(func.call(&mut store, (2, 3, 4))? == 10); | |
+ Ok(()) | |
+} | |
+ | |
+fn simple_floats(module: &[u8]) -> Result<()> { | |
+ let engine = Engine::default(); | |
+ let module = match deserialize(&engine, module)? { | |
+ Some(module) => module, | |
+ None => panic!(), | |
+ }; | |
+ let mut store = Store::new(&engine, ()); | |
+ let instance = Linker::new(&engine).instantiate(&mut store, &module)?; | |
+ let func = instance.get_typed_func::<(f32, f32), f32>(&mut store, "frob")?; | |
+ ensure!(func.call(&mut store, (1.4, 3.2))? == 5.); | |
Ok(()) | |
} | |
diff --git a/examples/min-platform/src/main.rs b/examples/min-platform/src/main.rs | |
index fd1867c6d5..7de14846da 100644 | |
--- a/examples/min-platform/src/main.rs | |
+++ b/examples/min-platform/src/main.rs | |
@@ -95,6 +95,16 @@ fn main() -> Result<()> { | |
) | |
"#, | |
)?; | |
+ let simple_floats = engine.precompile_module( | |
+ br#" | |
+ (module | |
+ (func (export "frob") (param f32 f32) (result f32) | |
+ (f32.ceil (local.get 0)) | |
+ (f32.floor (local.get 0)) | |
+ f32.add) | |
+ ) | |
+ "#, | |
+ )?; | |
// Next is an example of running this embedding, which also serves as test | |
// that basic functionality actually works. | |
@@ -134,6 +144,8 @@ fn main() -> Result<()> { | |
usize, | |
*const u8, | |
usize, | |
+ *const u8, | |
+ usize, | |
) -> usize, | |
> = lib | |
.get(b"run") | |
@@ -149,6 +161,8 @@ fn main() -> Result<()> { | |
simple_add.len(), | |
simple_host_fn.as_ptr(), | |
simple_host_fn.len(), | |
+ simple_floats.as_ptr(), | |
+ simple_floats.len(), | |
); | |
error_buf.set_len(len); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment