Skip to content

Commit

Permalink
chore: handle incoming as Wit integer, but argument is float (#609)
Browse files Browse the repository at this point in the history
Includes:

- string -> list<u8> is no longer a possibility after discussion with
@hugomrdias.
  • Loading branch information
Zeeshan Lakhani committed Mar 8, 2024
1 parent 422cca1 commit ebac601
Show file tree
Hide file tree
Showing 14 changed files with 363 additions and 214 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ represents the `Homestar` runtime. We recommend diving into each package's own
the input interface for working with Ipvm's standard Wasm tasks.

You can find the spec for translating between IPLD and WIT runtime values
based on WIT types [here](./homestar-wasm/README.md##interpreting-between-ipld-and-wit).
based on WIT types [here](./homestar-wasm/README.md#interpreting-between-ipld-and-wit).

- [homestar-workflow](./homestar-workflow)

Expand Down
94 changes: 89 additions & 5 deletions homestar-functions/subtract/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ pub unsafe fn _export_subtract_cabi<T: Guest>(arg0: f64, arg1: f64) -> f64 {
let result0 = T::subtract(arg0, arg1);
_rt::as_f64(result0)
}

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

Expand All @@ -20,6 +28,11 @@ macro_rules! __export_world_subtract_cabi{
unsafe extern "C" fn export_subtract(arg0: f64,arg1: f64,) -> f64 {
$($path_to_types)*::_export_subtract_cabi::<$ty>(arg0, arg1)
}

#[export_name = "subtract-int"]
unsafe extern "C" fn export_subtract_int(arg0: i32,arg1: i32,) -> i32 {
$($path_to_types)*::_export_subtract_int_cabi::<$ty>(arg0, arg1)
}
};);
}
#[doc(hidden)]
Expand All @@ -46,6 +59,76 @@ mod _rt {
self as f64
}
}

pub fn as_i32<T: AsI32>(t: T) -> i32 {
t.as_i32()
}

pub trait AsI32 {
fn as_i32(self) -> i32;
}

impl<'a, T: Copy + AsI32> AsI32 for &'a T {
fn as_i32(self) -> i32 {
(*self).as_i32()
}
}

impl AsI32 for i32 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for u32 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for i16 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for u16 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for i8 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for u8 {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for char {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}

impl AsI32 for usize {
#[inline]
fn as_i32(self) -> i32 {
self as i32
}
}
}

/// Generates `#[no_mangle]` functions to export the specified type as the
Expand Down Expand Up @@ -79,11 +162,12 @@ 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";
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 225] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07c\x01A\x02\x01A\x04\x01\
@\x02\x01au\x01bu\0u\x04\0\x08subtract\x01\0\x01@\x02\x01a~\x01b~\0~\x04\0\x0csu\
btract-int\x01\x01\x04\x01$homestar-functions:subtract/subtract\x04\0\x0b\x0e\x01\
\0\x08subtract\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x07\
0.201.0\x10wit-bindgen-rust\x060.20.0";

#[inline(never)]
#[doc(hidden)]
Expand Down
4 changes: 4 additions & 0 deletions homestar-functions/subtract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ impl Guest for Component {
fn subtract(a: f64, b: f64) -> f64 {
a - b
}

fn subtract_int(a: i8, b: i8) -> i8 {
a - b
}
}

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

world subtract {
export subtract: func(a: float64, b: float64) -> float64;
export subtract-int: func(a: s8, b: s8) -> s8;
}
58 changes: 17 additions & 41 deletions homestar-wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ ty ::= 'u8' | 'u16' | 'u32' | 'u64'
Conversely, when an integer value (not a float) is returned from a WIT
function, it can be translated back into an `Ipld::Integer`.

**IPLD Schema Definitions**:
*IPLD Schema Definitions*:

```ipldschme
type IPLDIntegerAsWit union {
Expand Down Expand Up @@ -173,7 +173,7 @@ ty ::= 'float32' | 'float64'

- **IPLD to WIT Translation**:

When a WIT function expects a float input, an `Ipld::Float` value is
Typically, when a WIT function expects a float input, an `Ipld::Float` value is
mapped to a float WIT runtime value. Casting is done to convert from `f32` to
`f64` if necessary.

Expand All @@ -194,6 +194,11 @@ ty ::= 'float32' | 'float64'
`1.0` is converted into an `Ipld::Float`, which is then translated and
passed into `fn` as a float argument (`f64`).

**Note**: However, if the input argument to the WIT interface is one of the
WIT interger types, but the incoming value is an `Ipld::Integer`, then the
IPLD value will be cast to that integer type, and remain as one for the rest
of the computation.

- **WIT to IPLD Translation**:

Conversely, when a `float32` or `float64` value is returned from a WIT
Expand All @@ -203,7 +208,7 @@ ty ::= 'float32' | 'float64'
the default precision for [IPLD][ipld-float], precision will be lost.
**The interpreter will use decimal precision in this conversion**.

**IPLD Schema Definitions**:
*IPLD Schema Definitions*:

```ipldsch
type IPLDFloatAsWit union {
Expand All @@ -221,7 +226,7 @@ type WitAsIpldFloat union {

This section outlines the translation process between IPLD string values
(`Ipld::String`) and various [WIT runtime values][wit-val]. A `Ipld::String` value can be
interpreted as one of a `string`, `char`, `list<u8>`, or an `enum` discriminant
interpreted as one of a `string`, `char`, or an `enum` discriminant
(which has no payload).

- `string`
Expand Down Expand Up @@ -282,35 +287,6 @@ interpreted as one of a `string`, `char`, `list<u8>`, or an `enum` discriminant
Conversely, when a char value is returned from a WIT function, it is
translated back into an `Ipld::String`.

- `list<u8>`

* **IPLD to WIT Translation**

When a WIT function expects a `list<u8>` input, an `Ipld::String` value is
mapped to a `list<u8>` WIT runtime value.

**Example**:

```wit
export fn: func(a: list<u8>) -> list<u8>;
```

Given a JSON input for this function:

```json
{
"args": ["aGVsbDA"]
}
```

`"aGVsbDA"` is converted into an `Ipld::String`, which is then translated
into bytes and passed into `fn` as a `list<u8>` argument.

* **WIT to IPLD Translation**:

**Here, when a `list<u8>` value is returned from a WIT function, it is
translated into an `Ipld::Bytes` value, which is the proper type**.

- [`enum`][wit-enum]:

An enum statement defines a new type which is semantically equivalent to a
Expand Down Expand Up @@ -350,7 +326,7 @@ interpreted as one of a `string`, `char`, `list<u8>`, or an `enum` discriminant
Conversely, when an enum value is returned from a WIT function, it can be
translated back into an `Ipld::String` value.

**IPLD Schema Definitions**:
*IPLD Schema Definitions*:

``` ipldsch
type Enum enum {
Expand Down Expand Up @@ -440,7 +416,7 @@ can be interpreted either as a `list<u8>` or `string`.
translated into an `Ipld::String` value, because we can't determine if it
was originally `bytes`**.

**IPLD Schema Definitions**:
*IPLD Schema Definitions*:

``` ipldsch
type IPLDBytesAsWit union {
Expand Down Expand Up @@ -491,7 +467,7 @@ below.
Conversely, when a `string` value of `"null"` is returned from a WIT function,
it can be translated into an `Ipld::Null` value.

**IPLD Schema Definitions**:
*IPLD Schema Definitions*:

``` ipldsch
type None unit representation null
Expand Down Expand Up @@ -543,7 +519,7 @@ interpreted as a `string` in WIT, and vice versa.
can be converted to a Cid, it can then be translated into an `Ipld::Link`
value.

**IPLD Schema Definitions**:
*IPLD Schema Definitions*:

``` ipldsch
type IPLDLinkAsWit &String link
Expand Down Expand Up @@ -674,7 +650,7 @@ possibilities here**.
translated back into an `Ipld::List` value.


**IPLD Schema Definitions**:
*IPLD Schema Definitions*:

``` ipldsch
type IPLDListAsWit union {
Expand Down Expand Up @@ -810,7 +786,7 @@ a `list` of two-element `tuples`.
Conversely, when a `list` of two-element `tuples` is returned from a WIT
function, it can be translated back into an `Ipld::Map` value.

**IPLD Schema Definitions**:
*IPLD Schema Definitions*:

``` ipldsch
type TupleAsMap {string:any} representation listpairs
Expand Down Expand Up @@ -882,7 +858,7 @@ as either a `Ipld::Null` or of any other IPLD value.
translated back into an `Ipld::Null` value if it's the `None`/`Unit` case, or
any other IPLD value if it's the `Some` case.

**IPLD Schema Definitions**:
*IPLD Schema Definitions*:

``` ipldsch
type IpldAsWitOption union {
Expand Down Expand Up @@ -1024,7 +1000,7 @@ A [`result`][wit-result] can be interpreted as one of these patterns:
`[null, 1]` internally, which signifies the `Err` (error) case, with
the `1` payload discarded.**

**IPLD Schema Definitions**:
*IPLD Schema Definitions*:

``` ipldsch
type Null unit representation null
Expand Down
Binary file modified homestar-wasm/fixtures/example_subtract.wasm
Binary file not shown.
Loading

0 comments on commit ebac601

Please sign in to comment.