feat: add a histrical wit-bindgen
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
test-imports: func()
|
||||
|
||||
roundtrip-option: func(a: option<float32>) -> option<u8>
|
||||
roundtrip-result: func(a: expected<u32, float32>) -> expected<float64, u8>
|
||||
|
||||
enum e1 { a, b }
|
||||
roundtrip-enum: func(a: e1) -> e1
|
||||
|
||||
invert-bool: func(a: bool) -> bool
|
||||
|
||||
variant c1 { a(s32), b(s64) }
|
||||
variant c2 { a(s32), b(float32) }
|
||||
variant c3 { a(s32), b(float64) }
|
||||
variant c4 { a(s64), b(float32) }
|
||||
variant c5 { a(s64), b(float64) }
|
||||
variant c6 { a(float32), b(float64) }
|
||||
type casts = tuple<c1, c2, c3, c4, c5, c6>
|
||||
variant-casts: func(a: casts) -> casts
|
||||
|
||||
variant z1 { a(s32), b }
|
||||
variant z2 { a(s64), b }
|
||||
variant z3 { a(float32), b }
|
||||
variant z4 { a(float64), b }
|
||||
type zeros = tuple<z1, z2, z3, z4>
|
||||
variant-zeros: func(a: zeros) -> zeros
|
||||
|
||||
type option-typedef = option<u32>
|
||||
type bool-typedef = bool
|
||||
type result-typedef = expected<u32, unit>
|
||||
variant-typedefs: func(a: option-typedef, b: bool-typedef, c: result-typedef)
|
||||
@@ -0,0 +1,114 @@
|
||||
from exports.bindings import Exports
|
||||
from imports.bindings import add_imports_to_linker, Imports
|
||||
from typing import Optional, Tuple
|
||||
import exports.bindings as e
|
||||
import imports.bindings as i
|
||||
import sys
|
||||
import wasmtime
|
||||
|
||||
class MyImports:
|
||||
def roundtrip_option(self, a: Optional[float]) -> Optional[int]:
|
||||
if a:
|
||||
return int(a)
|
||||
return None
|
||||
|
||||
def roundtrip_result(self, a: i.Expected[int, float]) -> i.Expected[float, int]:
|
||||
if isinstance(a, i.Ok):
|
||||
return i.Ok(float(a.value))
|
||||
return i.Err(int(a.value))
|
||||
|
||||
def roundtrip_enum(self, a: i.E1) -> i.E1:
|
||||
return a
|
||||
|
||||
def invert_bool(self, a: bool) -> bool:
|
||||
return not a
|
||||
|
||||
def variant_casts(self, a: i.Casts) -> i.Casts:
|
||||
return a
|
||||
|
||||
def variant_zeros(self, a: i.Zeros) -> i.Zeros:
|
||||
return a
|
||||
|
||||
def variant_typedefs(self, a: i.OptionTypedef, b: i.BoolTypedef, c: i.ResultTypedef) -> None:
|
||||
pass
|
||||
|
||||
def variant_enums(self, a: bool, b: i.Expected[None, None], c: i.MyErrno) -> Tuple[bool, i.Expected[None, None], i.MyErrno]:
|
||||
assert(a)
|
||||
assert(isinstance(b, i.Ok))
|
||||
assert(c == i.MyErrno.SUCCESS)
|
||||
return (False, i.Err(None), i.MyErrno.A)
|
||||
|
||||
def run(wasm_file: str) -> None:
|
||||
store = wasmtime.Store()
|
||||
module = wasmtime.Module.from_file(store.engine, wasm_file)
|
||||
linker = wasmtime.Linker(store.engine)
|
||||
linker.define_wasi()
|
||||
wasi = wasmtime.WasiConfig()
|
||||
wasi.inherit_stdout()
|
||||
wasi.inherit_stderr()
|
||||
store.set_wasi(wasi)
|
||||
|
||||
imports = MyImports()
|
||||
add_imports_to_linker(linker, store, imports)
|
||||
wasm = Exports(store, linker, module)
|
||||
|
||||
wasm.test_imports(store)
|
||||
|
||||
assert(wasm.roundtrip_option(store, 1.) == 1)
|
||||
assert(wasm.roundtrip_option(store, None) == None)
|
||||
assert(wasm.roundtrip_option(store, 2.) == 2)
|
||||
assert(wasm.roundtrip_result(store, e.Ok(2)) == e.Ok(2))
|
||||
assert(wasm.roundtrip_result(store, e.Ok(4)) == e.Ok(4))
|
||||
assert(wasm.roundtrip_result(store, e.Err(5)) == e.Err(5))
|
||||
|
||||
assert(wasm.roundtrip_enum(store, e.E1.A) == e.E1.A)
|
||||
assert(wasm.roundtrip_enum(store, e.E1.B) == e.E1.B)
|
||||
|
||||
assert(wasm.invert_bool(store, True) == False)
|
||||
assert(wasm.invert_bool(store, False) == True)
|
||||
|
||||
a1, a2, a3, a4, a5, a6 = wasm.variant_casts(store, (
|
||||
e.C1A(1),
|
||||
e.C2A(2),
|
||||
e.C3A(3),
|
||||
e.C4A(4),
|
||||
e.C5A(5),
|
||||
e.C6A(6.),
|
||||
))
|
||||
assert(a1 == e.C1A(1))
|
||||
assert(a2 == e.C2A(2))
|
||||
assert(a3 == e.C3A(3))
|
||||
assert(a4 == e.C4A(4))
|
||||
assert(a5 == e.C5A(5))
|
||||
assert(a6 == e.C6A(6))
|
||||
|
||||
b1, b2, b3, b4, b5, b6 = wasm.variant_casts(store, (
|
||||
e.C1B(1),
|
||||
e.C2B(2),
|
||||
e.C3B(3),
|
||||
e.C4B(4),
|
||||
e.C5B(5),
|
||||
e.C6B(6.),
|
||||
))
|
||||
assert(b1 == e.C1B(1))
|
||||
assert(b2 == e.C2B(2))
|
||||
assert(b3 == e.C3B(3))
|
||||
assert(b4 == e.C4B(4))
|
||||
assert(b5 == e.C5B(5))
|
||||
assert(b6 == e.C6B(6))
|
||||
|
||||
z1, z2, z3, z4 = wasm.variant_zeros(store, (
|
||||
e.Z1A(1),
|
||||
e.Z2A(2),
|
||||
e.Z3A(3.),
|
||||
e.Z4A(4.),
|
||||
))
|
||||
assert(z1 == e.Z1A(1))
|
||||
assert(z2 == e.Z2A(2))
|
||||
assert(z3 == e.Z3A(3))
|
||||
assert(z4 == e.Z4A(4))
|
||||
|
||||
wasm.variant_typedefs(store, None, False, e.Err(None))
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(sys.argv[1])
|
||||
@@ -0,0 +1,119 @@
|
||||
use anyhow::Result;
|
||||
|
||||
wit_bindgen_wasmtime::export!("../../tests/runtime/variants/imports.wit");
|
||||
|
||||
use imports::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MyImports;
|
||||
|
||||
impl Imports for MyImports {
|
||||
fn roundtrip_option(&mut self, a: Option<f32>) -> Option<u8> {
|
||||
a.map(|x| x as u8)
|
||||
}
|
||||
|
||||
fn roundtrip_result(&mut self, a: Result<u32, f32>) -> Result<f64, u8> {
|
||||
match a {
|
||||
Ok(a) => Ok(a.into()),
|
||||
Err(b) => Err(b as u8),
|
||||
}
|
||||
}
|
||||
|
||||
fn roundtrip_enum(&mut self, a: E1) -> E1 {
|
||||
assert_eq!(a, a);
|
||||
a
|
||||
}
|
||||
|
||||
fn invert_bool(&mut self, a: bool) -> bool {
|
||||
!a
|
||||
}
|
||||
|
||||
fn variant_casts(&mut self, a: Casts) -> Casts {
|
||||
a
|
||||
}
|
||||
|
||||
fn variant_zeros(&mut self, a: Zeros) -> Zeros {
|
||||
a
|
||||
}
|
||||
|
||||
fn variant_typedefs(&mut self, _: Option<u32>, _: bool, _: Result<u32, ()>) {}
|
||||
|
||||
fn variant_enums(
|
||||
&mut self,
|
||||
a: bool,
|
||||
b: Result<(), ()>,
|
||||
c: MyErrno,
|
||||
) -> (bool, Result<(), ()>, MyErrno) {
|
||||
assert_eq!(a, true);
|
||||
assert_eq!(b, Ok(()));
|
||||
assert_eq!(c, MyErrno::Success);
|
||||
(false, Err(()), MyErrno::A)
|
||||
}
|
||||
}
|
||||
|
||||
wit_bindgen_wasmtime::import!("../../tests/runtime/variants/exports.wit");
|
||||
|
||||
fn run(wasm: &str) -> Result<()> {
|
||||
use exports::*;
|
||||
|
||||
let (exports, mut store) = crate::instantiate(
|
||||
wasm,
|
||||
|linker| imports::add_to_linker(linker, |cx| -> &mut MyImports { &mut cx.imports }),
|
||||
|store, module, linker| Exports::instantiate(store, module, linker, |cx| &mut cx.exports),
|
||||
)?;
|
||||
|
||||
exports.test_imports(&mut store)?;
|
||||
|
||||
assert_eq!(exports.roundtrip_option(&mut store, Some(1.0))?, Some(1));
|
||||
assert_eq!(exports.roundtrip_option(&mut store, None)?, None);
|
||||
assert_eq!(exports.roundtrip_option(&mut store, Some(2.0))?, Some(2));
|
||||
assert_eq!(exports.roundtrip_result(&mut store, Ok(2))?, Ok(2.0));
|
||||
assert_eq!(exports.roundtrip_result(&mut store, Ok(4))?, Ok(4.0));
|
||||
assert_eq!(exports.roundtrip_result(&mut store, Err(5.3))?, Err(5));
|
||||
|
||||
assert_eq!(exports.roundtrip_enum(&mut store, E1::A)?, E1::A);
|
||||
assert_eq!(exports.roundtrip_enum(&mut store, E1::B)?, E1::B);
|
||||
|
||||
assert_eq!(exports.invert_bool(&mut store, true)?, false);
|
||||
assert_eq!(exports.invert_bool(&mut store, false)?, true);
|
||||
|
||||
let (a1, a2, a3, a4, a5, a6) = exports.variant_casts(
|
||||
&mut store,
|
||||
(C1::A(1), C2::A(2), C3::A(3), C4::A(4), C5::A(5), C6::A(6.0)),
|
||||
)?;
|
||||
assert!(matches!(a1, C1::A(1)));
|
||||
assert!(matches!(a2, C2::A(2)));
|
||||
assert!(matches!(a3, C3::A(3)));
|
||||
assert!(matches!(a4, C4::A(4)));
|
||||
assert!(matches!(a5, C5::A(5)));
|
||||
assert!(matches!(a6, C6::A(b) if b == 6.0));
|
||||
|
||||
let (a1, a2, a3, a4, a5, a6) = exports.variant_casts(
|
||||
&mut store,
|
||||
(
|
||||
C1::B(1),
|
||||
C2::B(2.0),
|
||||
C3::B(3.0),
|
||||
C4::B(4.0),
|
||||
C5::B(5.0),
|
||||
C6::B(6.0),
|
||||
),
|
||||
)?;
|
||||
assert!(matches!(a1, C1::B(1)));
|
||||
assert!(matches!(a2, C2::B(b) if b == 2.0));
|
||||
assert!(matches!(a3, C3::B(b) if b == 3.0));
|
||||
assert!(matches!(a4, C4::B(b) if b == 4.0));
|
||||
assert!(matches!(a5, C5::B(b) if b == 5.0));
|
||||
assert!(matches!(a6, C6::B(b) if b == 6.0));
|
||||
|
||||
let (a1, a2, a3, a4) =
|
||||
exports.variant_zeros(&mut store, (Z1::A(1), Z2::A(2), Z3::A(3.0), Z4::A(4.0)))?;
|
||||
assert!(matches!(a1, Z1::A(1)));
|
||||
assert!(matches!(a2, Z2::A(2)));
|
||||
assert!(matches!(a3, Z3::A(b) if b == 3.0));
|
||||
assert!(matches!(a4, Z4::A(b) if b == 4.0));
|
||||
|
||||
exports.variant_typedefs(&mut store, None, false, Err(()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
import { addImportsToImports, Imports, MyErrno } from "./imports.js";
|
||||
import { Exports } from "./exports.js";
|
||||
import * as exports from "./exports.js";
|
||||
import { getWasm, addWasiToImports } from "./helpers.js";
|
||||
// @ts-ignore
|
||||
import * as assert from 'assert';
|
||||
|
||||
async function run() {
|
||||
const importObj = {};
|
||||
const imports: Imports = {
|
||||
roundtripOption(x) { return x; },
|
||||
roundtripResult(x) {
|
||||
if (x.tag == 'ok') {
|
||||
return { tag: 'ok', val: x.val };
|
||||
} else {
|
||||
return { tag: 'err', val: Math.round(x.val) };
|
||||
}
|
||||
},
|
||||
roundtripEnum(x) { return x; },
|
||||
invertBool(x) { return !x; },
|
||||
variantCasts(x) { return x; },
|
||||
variantZeros(x) { return x; },
|
||||
variantTypedefs(x, y, z) {},
|
||||
variantEnums(a, b, c) {
|
||||
assert.deepStrictEqual(a, true);
|
||||
assert.deepStrictEqual(b, { tag: 'ok', val: undefined });
|
||||
assert.deepStrictEqual(c, "success");
|
||||
return [
|
||||
false,
|
||||
{ tag: 'err', val: undefined },
|
||||
"a",
|
||||
];
|
||||
},
|
||||
};
|
||||
let instance: WebAssembly.Instance;
|
||||
addImportsToImports(importObj, imports, name => instance.exports[name]);
|
||||
const wasi = addWasiToImports(importObj);
|
||||
|
||||
const wasm = new Exports();
|
||||
await wasm.instantiate(getWasm(), importObj);
|
||||
wasi.start(wasm.instance);
|
||||
instance = wasm.instance;
|
||||
|
||||
wasm.testImports();
|
||||
assert.deepStrictEqual(wasm.roundtripOption(1), 1);
|
||||
assert.deepStrictEqual(wasm.roundtripOption(null), null);
|
||||
assert.deepStrictEqual(wasm.roundtripOption(2), 2);
|
||||
assert.deepStrictEqual(wasm.roundtripResult({ tag: 'ok', val: 2 }), { tag: 'ok', val: 2 });
|
||||
assert.deepStrictEqual(wasm.roundtripResult({ tag: 'ok', val: 4 }), { tag: 'ok', val: 4 });
|
||||
const f = Math.fround(5.2);
|
||||
assert.deepStrictEqual(wasm.roundtripResult({ tag: 'err', val: f }), { tag: 'err', val: 5 });
|
||||
|
||||
assert.deepStrictEqual(wasm.roundtripEnum("a"), "a");
|
||||
assert.deepStrictEqual(wasm.roundtripEnum("b"), "b");
|
||||
|
||||
assert.deepStrictEqual(wasm.invertBool(true), false);
|
||||
assert.deepStrictEqual(wasm.invertBool(false), true);
|
||||
|
||||
{
|
||||
const [a1, a2, a3, a4, a5, a6] = wasm.variantCasts([
|
||||
{ tag: 'a', val: 1 },
|
||||
{ tag: 'a', val: 2 },
|
||||
{ tag: 'a', val: 3 },
|
||||
{ tag: 'a', val: 4n },
|
||||
{ tag: 'a', val: 5n },
|
||||
{ tag: 'a', val: 6 },
|
||||
]);
|
||||
assert.deepStrictEqual(a1, { tag: 'a', val: 1 });
|
||||
assert.deepStrictEqual(a2, { tag: 'a', val: 2 });
|
||||
assert.deepStrictEqual(a3, { tag: 'a', val: 3 });
|
||||
assert.deepStrictEqual(a4, { tag: 'a', val: 4n });
|
||||
assert.deepStrictEqual(a5, { tag: 'a', val: 5n });
|
||||
assert.deepStrictEqual(a6, { tag: 'a', val: 6 });
|
||||
}
|
||||
{
|
||||
const [b1, b2, b3, b4, b5, b6] = wasm.variantCasts([
|
||||
{ tag: 'b', val: 1n },
|
||||
{ tag: 'b', val: 2 },
|
||||
{ tag: 'b', val: 3 },
|
||||
{ tag: 'b', val: 4 },
|
||||
{ tag: 'b', val: 5 },
|
||||
{ tag: 'b', val: 6 },
|
||||
]);
|
||||
assert.deepStrictEqual(b1, { tag: 'b', val: 1n });
|
||||
assert.deepStrictEqual(b2, { tag: 'b', val: 2 });
|
||||
assert.deepStrictEqual(b3, { tag: 'b', val: 3 });
|
||||
assert.deepStrictEqual(b4, { tag: 'b', val: 4 });
|
||||
assert.deepStrictEqual(b5, { tag: 'b', val: 5 });
|
||||
assert.deepStrictEqual(b6, { tag: 'b', val: 6 });
|
||||
}
|
||||
|
||||
{
|
||||
const [a1, a2, a3, a4] = wasm.variantZeros([
|
||||
{ tag: 'a', val: 1 },
|
||||
{ tag: 'a', val: 2n },
|
||||
{ tag: 'a', val: 3 },
|
||||
{ tag: 'a', val: 4 },
|
||||
]);
|
||||
assert.deepStrictEqual(a1, { tag: 'a', val: 1 });
|
||||
assert.deepStrictEqual(a2, { tag: 'a', val: 2n });
|
||||
assert.deepStrictEqual(a3, { tag: 'a', val: 3 });
|
||||
assert.deepStrictEqual(a4, { tag: 'a', val: 4 });
|
||||
}
|
||||
|
||||
wasm.variantTypedefs(null, false, { tag: 'err', val: undefined });
|
||||
}
|
||||
|
||||
await run()
|
||||
@@ -0,0 +1,31 @@
|
||||
roundtrip-option: func(a: option<float32>) -> option<u8>
|
||||
roundtrip-result: func(a: expected<u32, float32>) -> expected<float64, u8>
|
||||
|
||||
enum e1 { a, b }
|
||||
roundtrip-enum: func(a: e1) -> e1
|
||||
|
||||
invert-bool: func(a: bool) -> bool
|
||||
|
||||
variant c1 { a(s32), b(s64) }
|
||||
variant c2 { a(s32), b(float32) }
|
||||
variant c3 { a(s32), b(float64) }
|
||||
variant c4 { a(s64), b(float32) }
|
||||
variant c5 { a(s64), b(float64) }
|
||||
variant c6 { a(float32), b(float64) }
|
||||
type casts = tuple<c1, c2, c3, c4, c5, c6>
|
||||
variant-casts: func(a: casts) -> casts
|
||||
|
||||
variant z1 { a(s32), b }
|
||||
variant z2 { a(s64), b }
|
||||
variant z3 { a(float32), b }
|
||||
variant z4 { a(float64), b }
|
||||
type zeros = tuple<z1, z2, z3, z4>
|
||||
variant-zeros: func(a: zeros) -> zeros
|
||||
|
||||
type option-typedef = option<u32>
|
||||
type bool-typedef = bool
|
||||
type result-typedef = expected<u32, unit>
|
||||
variant-typedefs: func(a: option-typedef, b: bool-typedef, c: result-typedef)
|
||||
|
||||
enum my-errno { success, a, b }
|
||||
variant-enums: func(a: bool, b: expected<unit, unit>, c: my-errno) -> tuple<bool, expected<unit, unit>, my-errno>
|
||||
@@ -0,0 +1,208 @@
|
||||
#include <assert.h>
|
||||
#include <imports.h>
|
||||
#include <exports.h>
|
||||
|
||||
void exports_test_imports() {
|
||||
{
|
||||
imports_option_float32_t a;
|
||||
uint8_t r;
|
||||
a.is_some = true;
|
||||
a.val = 1;
|
||||
assert(imports_roundtrip_option(&a, &r) && r == 1);
|
||||
assert(r == 1);
|
||||
a.is_some = false;
|
||||
assert(!imports_roundtrip_option(&a, &r));
|
||||
a.is_some = true;
|
||||
a.val = 2;
|
||||
assert(imports_roundtrip_option(&a, &r) && r == 2);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
imports_expected_u32_float32_t a;
|
||||
imports_expected_float64_u8_t b;
|
||||
|
||||
a.is_err = false;
|
||||
a.val.ok = 2;
|
||||
imports_roundtrip_result(&a, &b);
|
||||
assert(!b.is_err);
|
||||
assert(b.val.ok == 2.0);
|
||||
|
||||
a.val.ok = 4;
|
||||
imports_roundtrip_result(&a, &b);
|
||||
assert(!b.is_err);
|
||||
assert(b.val.ok == 4);
|
||||
|
||||
a.is_err = true;
|
||||
a.val.err = 5.3;
|
||||
imports_roundtrip_result(&a, &b);
|
||||
assert(b.is_err);
|
||||
assert(b.val.err == 5);
|
||||
}
|
||||
|
||||
assert(imports_roundtrip_enum(IMPORTS_E1_A) == IMPORTS_E1_A);
|
||||
assert(imports_roundtrip_enum(IMPORTS_E1_B) == IMPORTS_E1_B);
|
||||
|
||||
assert(imports_invert_bool(true) == false);
|
||||
assert(imports_invert_bool(false) == true);
|
||||
|
||||
{
|
||||
imports_casts_t c;
|
||||
imports_c1_t r1;
|
||||
imports_c2_t r2;
|
||||
imports_c3_t r3;
|
||||
imports_c4_t r4;
|
||||
imports_c5_t r5;
|
||||
imports_c6_t r6;
|
||||
c.f0.tag = IMPORTS_C1_A;
|
||||
c.f0.val.a = 1;
|
||||
c.f1.tag = IMPORTS_C2_A;
|
||||
c.f1.val.a = 2;
|
||||
c.f2.tag = IMPORTS_C3_A;
|
||||
c.f2.val.a = 3;
|
||||
c.f3.tag = IMPORTS_C4_A;
|
||||
c.f3.val.a = 4;
|
||||
c.f4.tag = IMPORTS_C5_A;
|
||||
c.f4.val.a = 5;
|
||||
c.f5.tag = IMPORTS_C6_A;
|
||||
c.f5.val.a = 6;
|
||||
imports_variant_casts(&c, &r1, &r2, &r3, &r4, &r5, &r6);
|
||||
assert(r1.tag == IMPORTS_C1_A && r1.val.a == 1);
|
||||
assert(r2.tag == IMPORTS_C2_A && r2.val.a == 2);
|
||||
assert(r3.tag == IMPORTS_C3_A && r3.val.a == 3);
|
||||
assert(r4.tag == IMPORTS_C4_A && r4.val.a == 4);
|
||||
assert(r5.tag == IMPORTS_C5_A && r5.val.a == 5);
|
||||
assert(r6.tag == IMPORTS_C6_A && r6.val.a == 6);
|
||||
}
|
||||
|
||||
{
|
||||
imports_casts_t c;
|
||||
imports_c1_t r1;
|
||||
imports_c2_t r2;
|
||||
imports_c3_t r3;
|
||||
imports_c4_t r4;
|
||||
imports_c5_t r5;
|
||||
imports_c6_t r6;
|
||||
c.f0.tag = IMPORTS_C1_B;
|
||||
c.f0.val.b = 1;
|
||||
c.f1.tag = IMPORTS_C2_B;
|
||||
c.f1.val.b = 2;
|
||||
c.f2.tag = IMPORTS_C3_B;
|
||||
c.f2.val.b = 3;
|
||||
c.f3.tag = IMPORTS_C4_B;
|
||||
c.f3.val.b = 4;
|
||||
c.f4.tag = IMPORTS_C5_B;
|
||||
c.f4.val.b = 5;
|
||||
c.f5.tag = IMPORTS_C6_B;
|
||||
c.f5.val.b = 6;
|
||||
imports_variant_casts(&c, &r1, &r2, &r3, &r4, &r5, &r6);
|
||||
assert(r1.tag == IMPORTS_C1_B && r1.val.b == 1);
|
||||
assert(r2.tag == IMPORTS_C2_B && r2.val.b == 2);
|
||||
assert(r3.tag == IMPORTS_C3_B && r3.val.b == 3);
|
||||
assert(r4.tag == IMPORTS_C4_B && r4.val.b == 4);
|
||||
assert(r5.tag == IMPORTS_C5_B && r5.val.b == 5);
|
||||
assert(r6.tag == IMPORTS_C6_B && r6.val.b == 6);
|
||||
}
|
||||
|
||||
{
|
||||
imports_zeros_t c;
|
||||
imports_z1_t r1;
|
||||
imports_z2_t r2;
|
||||
imports_z3_t r3;
|
||||
imports_z4_t r4;
|
||||
c.f0.tag = IMPORTS_Z1_A;
|
||||
c.f0.val.a = 1;
|
||||
c.f1.tag = IMPORTS_Z2_A;
|
||||
c.f1.val.a = 2;
|
||||
c.f2.tag = IMPORTS_Z3_A;
|
||||
c.f2.val.a = 3;
|
||||
c.f3.tag = IMPORTS_Z4_A;
|
||||
c.f3.val.a = 4;
|
||||
imports_variant_zeros(&c, &r1, &r2, &r3, &r4);
|
||||
assert(r1.tag == IMPORTS_Z1_A && r1.val.a == 1);
|
||||
assert(r2.tag == IMPORTS_Z2_A && r2.val.a == 2);
|
||||
assert(r3.tag == IMPORTS_Z3_A && r3.val.a == 3);
|
||||
assert(r4.tag == IMPORTS_Z4_A && r4.val.a == 4);
|
||||
}
|
||||
|
||||
{
|
||||
imports_zeros_t c;
|
||||
imports_z1_t r1;
|
||||
imports_z2_t r2;
|
||||
imports_z3_t r3;
|
||||
imports_z4_t r4;
|
||||
c.f0.tag = IMPORTS_Z1_B;
|
||||
c.f1.tag = IMPORTS_Z2_B;
|
||||
c.f2.tag = IMPORTS_Z3_B;
|
||||
c.f3.tag = IMPORTS_Z4_B;
|
||||
imports_variant_zeros(&c, &r1, &r2, &r3, &r4);
|
||||
assert(r1.tag == IMPORTS_Z1_B);
|
||||
assert(r2.tag == IMPORTS_Z2_B);
|
||||
assert(r3.tag == IMPORTS_Z3_B);
|
||||
assert(r4.tag == IMPORTS_Z4_B);
|
||||
}
|
||||
|
||||
{
|
||||
imports_option_typedef_t a;
|
||||
a.is_some = false;
|
||||
bool b = false;
|
||||
imports_result_typedef_t c;
|
||||
c.is_err = true;
|
||||
imports_variant_typedefs(&a, b, &c);
|
||||
}
|
||||
|
||||
{
|
||||
bool a;
|
||||
imports_expected_unit_unit_t b;
|
||||
imports_my_errno_t c;
|
||||
b.is_err = false;
|
||||
imports_variant_enums(true, &b, IMPORTS_MY_ERRNO_SUCCESS, &a, &b, &c);
|
||||
assert(a == false);
|
||||
assert(b.is_err);
|
||||
assert(c == IMPORTS_MY_ERRNO_A);
|
||||
}
|
||||
}
|
||||
|
||||
bool exports_roundtrip_option(exports_option_float32_t *a, uint8_t *ret0) {
|
||||
if (a->is_some) {
|
||||
*ret0 = a->val;
|
||||
}
|
||||
return a->is_some;
|
||||
}
|
||||
|
||||
void exports_roundtrip_result(exports_expected_u32_float32_t *a, exports_expected_float64_u8_t *ret0) {
|
||||
ret0->is_err = a->is_err;
|
||||
if (a->is_err) {
|
||||
ret0->val.err = a->val.err;
|
||||
} else {
|
||||
ret0->val.ok = a->val.ok;
|
||||
}
|
||||
}
|
||||
|
||||
exports_e1_t exports_roundtrip_enum(exports_e1_t a) {
|
||||
return a;
|
||||
}
|
||||
|
||||
bool exports_invert_bool(bool a) {
|
||||
return !a;
|
||||
}
|
||||
|
||||
void exports_variant_casts(exports_casts_t *a, exports_c1_t *ret0, exports_c2_t *ret1, exports_c3_t *ret2, exports_c4_t *ret3, exports_c5_t *ret4, exports_c6_t *ret5) {
|
||||
*ret0 = a->f0;
|
||||
*ret1 = a->f1;
|
||||
*ret2 = a->f2;
|
||||
*ret3 = a->f3;
|
||||
*ret4 = a->f4;
|
||||
*ret5 = a->f5;
|
||||
}
|
||||
|
||||
void exports_variant_zeros(exports_zeros_t *a, exports_z1_t *ret0, exports_z2_t *ret1, exports_z3_t *ret2, exports_z4_t *ret3) {
|
||||
*ret0 = a->f0;
|
||||
*ret1 = a->f1;
|
||||
*ret2 = a->f2;
|
||||
*ret3 = a->f3;
|
||||
}
|
||||
|
||||
void exports_variant_typedefs(exports_option_typedef_t *a, exports_bool_typedef_t b, exports_result_typedef_t *c) {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
wit_bindgen_rust::import!("../../tests/runtime/variants/imports.wit");
|
||||
wit_bindgen_rust::export!("../../tests/runtime/variants/exports.wit");
|
||||
|
||||
use exports::*;
|
||||
|
||||
struct Exports;
|
||||
|
||||
impl exports::Exports for Exports {
|
||||
fn test_imports() {
|
||||
use imports::*;
|
||||
|
||||
assert_eq!(roundtrip_option(Some(1.0)), Some(1));
|
||||
assert_eq!(roundtrip_option(None), None);
|
||||
assert_eq!(roundtrip_option(Some(2.0)), Some(2));
|
||||
assert_eq!(roundtrip_result(Ok(2)), Ok(2.0));
|
||||
assert_eq!(roundtrip_result(Ok(4)), Ok(4.0));
|
||||
assert_eq!(roundtrip_result(Err(5.3)), Err(5));
|
||||
|
||||
assert_eq!(roundtrip_enum(E1::A), E1::A);
|
||||
assert_eq!(roundtrip_enum(E1::B), E1::B);
|
||||
|
||||
assert_eq!(invert_bool(true), false);
|
||||
assert_eq!(invert_bool(false), true);
|
||||
|
||||
let (a1, a2, a3, a4, a5, a6) =
|
||||
variant_casts((C1::A(1), C2::A(2), C3::A(3), C4::A(4), C5::A(5), C6::A(6.0)));
|
||||
assert!(matches!(a1, C1::A(1)));
|
||||
assert!(matches!(a2, C2::A(2)));
|
||||
assert!(matches!(a3, C3::A(3)));
|
||||
assert!(matches!(a4, C4::A(4)));
|
||||
assert!(matches!(a5, C5::A(5)));
|
||||
assert!(matches!(a6, C6::A(b) if b == 6.0));
|
||||
|
||||
let (a1, a2, a3, a4, a5, a6) = variant_casts((
|
||||
C1::B(1),
|
||||
C2::B(2.0),
|
||||
C3::B(3.0),
|
||||
C4::B(4.0),
|
||||
C5::B(5.0),
|
||||
C6::B(6.0),
|
||||
));
|
||||
assert!(matches!(a1, C1::B(1)));
|
||||
assert!(matches!(a2, C2::B(b) if b == 2.0));
|
||||
assert!(matches!(a3, C3::B(b) if b == 3.0));
|
||||
assert!(matches!(a4, C4::B(b) if b == 4.0));
|
||||
assert!(matches!(a5, C5::B(b) if b == 5.0));
|
||||
assert!(matches!(a6, C6::B(b) if b == 6.0));
|
||||
|
||||
let (a1, a2, a3, a4) = variant_zeros((Z1::A(1), Z2::A(2), Z3::A(3.0), Z4::A(4.0)));
|
||||
assert!(matches!(a1, Z1::A(1)));
|
||||
assert!(matches!(a2, Z2::A(2)));
|
||||
assert!(matches!(a3, Z3::A(b) if b == 3.0));
|
||||
assert!(matches!(a4, Z4::A(b) if b == 4.0));
|
||||
|
||||
let (a1, a2, a3, a4) = variant_zeros((Z1::B, Z2::B, Z3::B, Z4::B));
|
||||
assert!(matches!(a1, Z1::B));
|
||||
assert!(matches!(a2, Z2::B));
|
||||
assert!(matches!(a3, Z3::B));
|
||||
assert!(matches!(a4, Z4::B));
|
||||
|
||||
variant_typedefs(None, false, Err(()));
|
||||
|
||||
assert_eq!(
|
||||
variant_enums(true, Ok(()), MyErrno::Success),
|
||||
(false, Err(()), MyErrno::A)
|
||||
);
|
||||
}
|
||||
|
||||
fn roundtrip_option(a: Option<f32>) -> Option<u8> {
|
||||
a.map(|x| x as u8)
|
||||
}
|
||||
|
||||
fn roundtrip_result(a: Result<u32, f32>) -> Result<f64, u8> {
|
||||
match a {
|
||||
Ok(a) => Ok(a.into()),
|
||||
Err(b) => Err(b as u8),
|
||||
}
|
||||
}
|
||||
|
||||
fn roundtrip_enum(a: E1) -> E1 {
|
||||
assert_eq!(a, a);
|
||||
a
|
||||
}
|
||||
|
||||
fn invert_bool(a: bool) -> bool {
|
||||
!a
|
||||
}
|
||||
|
||||
fn variant_casts(a: Casts) -> Casts {
|
||||
a
|
||||
}
|
||||
|
||||
fn variant_zeros(a: Zeros) -> Zeros {
|
||||
a
|
||||
}
|
||||
|
||||
fn variant_typedefs(_: Option<u32>, _: bool, _: Result<u32, ()>) {}
|
||||
}
|
||||
Reference in New Issue
Block a user