Skip to content

Commit

Permalink
chore: handle ref/non-ref case with rigor (#608)
Browse files Browse the repository at this point in the history
Match numeric runtime vals on ref/non-ref.
  • Loading branch information
Zeeshan Lakhani committed Mar 8, 2024
1 parent b44d0d5 commit 422cca1
Show file tree
Hide file tree
Showing 16 changed files with 13,896 additions and 6 deletions.
6 changes: 5 additions & 1 deletion .config/hakari.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ platforms = [
exact-versions = true

[traversal-excludes]
workspace-members = ["homestar-functions-add", "homestar-functions-test"]
workspace-members = [
"homestar-functions-add",
"homestar-functions-subtract",
"homestar-functions-test",
]

third-party = [{ name = "libsqlite3-sys" }]
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@
wasm-tools print homestar-wasm/fixtures/example_add_component.wasm -o homestar-wasm/fixtures/example_add_component.wat
'';

wasmSubtract = pkgs.writeScriptBin "wasm-ex-subtract" ''
#!${pkgs.stdenv.shell}
cargo component build -p homestar-functions-subtract --profile release-wasm-fn
cp target/wasm32-wasi/release-wasm-fn/homestar_functions_subtract.wasm homestar-wasm/fixtures/example_subtract_cargo_component_wasi.wasm
wasm-tools print homestar-wasm/fixtures/example_subtract_cargo_component_wasi.wasm -o homestar-wasm/fixtures/example_subtract_cargo_component_wasi.wat
cargo build -p homestar-functions-subtract --target wasm32-unknown-unknown --profile release-wasm-fn
cp target/wasm32-unknown-unknown/release-wasm-fn/homestar_functions_subtract.wasm homestar-wasm/fixtures/example_subtract.wasm
wasm-tools component new homestar-wasm/fixtures/example_subtract.wasm -o homestar-wasm/fixtures/example_subtract_component.wasm
wasm-tools print homestar-wasm/fixtures/example_subtract.wasm -o homestar-wasm/fixtures/example_subtract.wat
wasm-tools print homestar-wasm/fixtures/example_subtract_component.wasm -o homestar-wasm/fixtures/example_subtract_component.wat
'';

runIpfs = pkgs.writeScriptBin "run-ipfs" ''
#!${pkgs.stdenv.shell}
ipfs --repo-dir ./.ipfs --offline daemon
Expand Down Expand Up @@ -289,6 +301,7 @@
testCleanup
wasmTest
wasmAdd
wasmSubtract
];
in {
devShells.default = pkgs.mkShell {
Expand Down
2 changes: 1 addition & 1 deletion homestar-functions/add/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ wit-bindgen-rt = "0.20.0"
crate-type = ["cdylib"]

[package.metadata.component]
package = "component:add"
package = "component:homestar-functions-add"
15 changes: 15 additions & 0 deletions homestar-functions/subtract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "homestar-functions-subtract"
publish = false
version = "0.1.0"
edition = { workspace = true }
rust-version = { workspace = true }

[dependencies]
wit-bindgen-rt = "0.20.0"

[lib]
crate-type = ["cdylib"]

[package.metadata.component]
package = "component:homestar-functions-subtract"
93 changes: 93 additions & 0 deletions homestar-functions/subtract/src/bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Generated by `wit-bindgen` 0.20.0. DO NOT EDIT!
// Options used:

#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn _export_subtract_cabi<T: Guest>(arg0: f64, arg1: f64) -> f64 {
let result0 = T::subtract(arg0, arg1);
_rt::as_f64(result0)
}
pub trait Guest {
fn subtract(a: f64, b: f64) -> f64;
}
#[doc(hidden)]

macro_rules! __export_world_subtract_cabi{
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = {


#[export_name = "subtract"]
unsafe extern "C" fn export_subtract(arg0: f64,arg1: f64,) -> f64 {
$($path_to_types)*::_export_subtract_cabi::<$ty>(arg0, arg1)
}
};);
}
#[doc(hidden)]
pub(crate) use __export_world_subtract_cabi;
mod _rt {

pub fn as_f64<T: AsF64>(t: T) -> f64 {
t.as_f64()
}

pub trait AsF64 {
fn as_f64(self) -> f64;
}

impl<'a, T: Copy + AsF64> AsF64 for &'a T {
fn as_f64(self) -> f64 {
(*self).as_f64()
}
}

impl AsF64 for f64 {
#[inline]
fn as_f64(self) -> f64 {
self as f64
}
}
}

/// Generates `#[no_mangle]` functions to export the specified type as the
/// root implementation of all generated traits.
///
/// For more information see the documentation of `wit_bindgen::generate!`.
///
/// ```rust
/// # macro_rules! export{ ($($t:tt)*) => (); }
/// # trait Guest {}
/// struct MyType;
///
/// impl Guest for MyType {
/// // ...
/// }
///
/// export!(MyType);
/// ```
#[allow(unused_macros)]
#[doc(hidden)]

macro_rules! __export_subtract_impl {
($ty:ident) => (self::export!($ty with_types_in self););
($ty:ident with_types_in $($path_to_types_root:tt)*) => (
$($path_to_types_root)*::__export_world_subtract_cabi!($ty with_types_in $($path_to_types_root)*);
)
}
#[doc(inline)]
pub(crate) use __export_subtract_impl as export;

#[cfg(target_arch = "wasm32")]
#[link_section = "component-type:wit-bindgen:0.20.0:subtract:encoded world"]
#[doc(hidden)]
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 197] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07G\x01A\x02\x01A\x02\x01\
@\x02\x01au\x01bu\0u\x04\0\x08subtract\x01\0\x04\x01$homestar-functions:subtract\
/subtract\x04\0\x0b\x0e\x01\0\x08subtract\x03\0\0\0G\x09producers\x01\x0cprocess\
ed-by\x02\x0dwit-component\x070.201.0\x10wit-bindgen-rust\x060.20.0";

#[inline(never)]
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
pub fn __link_custom_section_describing_imports() {
wit_bindgen_rt::maybe_link_cabi_realloc();
}
15 changes: 15 additions & 0 deletions homestar-functions/subtract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[allow(clippy::all)]
#[rustfmt::skip]
mod bindings;

use bindings::Guest;

struct Component;

impl Guest for Component {
fn subtract(a: f64, b: f64) -> f64 {
a - b
}
}

bindings::export!(Component with_types_in bindings);
5 changes: 5 additions & 0 deletions homestar-functions/subtract/wit/world.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package homestar-functions:subtract;

world subtract {
export subtract: func(a: float64, b: float64) -> float64;
}
Binary file added homestar-wasm/fixtures/example_subtract.wasm
Binary file not shown.
Loading

0 comments on commit 422cca1

Please sign in to comment.