feat: add a histrical wit-bindgen

This commit is contained in:
2023-01-01 00:25:48 +08:00
parent 01e8f5a959
commit aa50d63aec
419 changed files with 45283 additions and 1 deletions

View File

@@ -0,0 +1,59 @@
test-imports: func()
/// A union of all of the integral types
union all-integers {
/// Bool is equivalent to a 1 bit integer
/// and is treated that way in some languages
bool,
u8, u16, u32, u64,
s8, s16, s32, s64
}
union all-floats {
float32, float64
}
union all-text {
char, string
}
// Returns the same case as the input but with 1 added
add-one-integer: func(num: all-integers) -> all-integers
// Returns the same case as the input but with 1 added
add-one-float: func(num: all-floats) -> all-floats
// Returns the same case as the input but with the first character replaced
replace-first-char: func(text: all-text, letter: char) -> all-text
// Returns the index of the case provided
identify-integer: func(num: all-integers) -> u8
// Returns the index of the case provided
identify-float: func(num: all-floats) -> u8
// Returns the index of the case provided
identify-text: func(text: all-text) -> u8
union duplicated-s32 {
/// The first s32
s32,
/// The second s32
s32,
/// The third s32
s32
}
// Returns the same case as the input but with 1 added
add-one-duplicated: func(num: duplicated-s32) -> duplicated-s32
// Returns the index of the case provided
identify-duplicated: func(num: duplicated-s32) -> u8
/// A type containing numeric types that are distinct in most languages
union distinguishable-num {
/// A Floating Point Number
float64,
/// A Signed Integer
s64
}
// Returns the same case as the input but with 1 added
add-one-distinguishable-num: func(num: distinguishable-num) -> distinguishable-num
// Returns the index of the case provided
identify-distinguishable-num: func(num: distinguishable-num) -> u8

View File

@@ -0,0 +1,237 @@
from exports.bindings import Exports
from imports.bindings import add_imports_to_linker, Imports
from typing import Union
import exports.bindings as e
import imports.bindings as i
import sys
import wasmtime
class MyImports:
# Simple uses of unions whose inner values all have the same Python representation
def add_one_integer(self, num: i.AllIntegers) -> i.AllIntegers:
# Bool
if isinstance(num, i.AllIntegers0):
assert num.value in (True, False)
return i.AllIntegers0(not num.value)
# The unsigned numbers
elif isinstance(num, i.AllIntegers1):
lower_limit = 0
upper_limit = 2**8
assert lower_limit <= num.value < upper_limit
return i.AllIntegers1(num.value + 1 % upper_limit)
elif isinstance(num, i.AllIntegers2):
lower_limit = 0
upper_limit = 2**16
assert lower_limit <= num.value < upper_limit
return i.AllIntegers2(num.value + 1 % upper_limit)
elif isinstance(num, i.AllIntegers3):
lower_limit = 0
upper_limit = 2**32
assert lower_limit <= num.value < upper_limit
return i.AllIntegers3(num.value + 1 % upper_limit)
elif isinstance(num, i.AllIntegers4):
lower_limit = 0
upper_limit = 2**64
assert lower_limit <= num.value < upper_limit
return i.AllIntegers4(num.value + 1 % upper_limit)
# The signed numbers
elif isinstance(num, i.AllIntegers5):
lower_limit = -2**7
upper_limit = 2**7
assert lower_limit <= num.value < upper_limit
return i.AllIntegers5(num.value + 1 % upper_limit)
elif isinstance(num, i.AllIntegers6):
lower_limit = -2**15
upper_limit = 2**15
assert lower_limit <= num.value < upper_limit
return i.AllIntegers6(num.value + 1 % upper_limit)
elif isinstance(num, i.AllIntegers7):
lower_limit = -2**31
upper_limit = 2**31
assert lower_limit <= num.value < upper_limit
return i.AllIntegers7(num.value + 1 % upper_limit)
elif isinstance(num, i.AllIntegers8):
lower_limit = -2**63
upper_limit = 2**63
assert lower_limit <= num.value < upper_limit
return i.AllIntegers8(num.value + 1 % upper_limit)
else:
raise ValueError("Invalid input value!")
def add_one_float(self, num: i.AllFloats) -> i.AllFloats:
if isinstance(num, i.AllFloats0):
return i.AllFloats0(num.value + 1)
if isinstance(num, i.AllFloats1):
return i.AllFloats1(num.value + 1)
else:
raise ValueError("Invalid input value!")
def replace_first_char(self, text: i.AllText, letter: str) -> i.AllText:
if isinstance(text, i.AllText0):
return i.AllText0(letter)
if isinstance(text, i.AllFloats1):
return i.AllText1(letter + text.value[1:])
else:
raise ValueError("Invalid input value!")
# Identify each case of unions whose inner values all have the same Python representation
def identify_integer(self, num: i.AllIntegers) -> int:
# Bool
if isinstance(num, i.AllIntegers0):
return 0
# The unsigned numbers
elif isinstance(num, i.AllIntegers1):
return 1
elif isinstance(num, i.AllIntegers2):
return 2
elif isinstance(num, i.AllIntegers3):
return 3
elif isinstance(num, i.AllIntegers4):
return 4
# The signed numbers
elif isinstance(num, i.AllIntegers5):
return 5
elif isinstance(num, i.AllIntegers6):
return 6
elif isinstance(num, i.AllIntegers7):
return 7
elif isinstance(num, i.AllIntegers8):
return 8
else:
raise ValueError("Invalid input value!")
def identify_float(self, num: i.AllFloats) -> int:
if isinstance(num, i.AllFloats0):
return 0
if isinstance(num, i.AllFloats1):
return 1
else:
raise ValueError("Invalid input value!")
def identify_text(self, text: i.AllText) -> int:
if isinstance(text, i.AllText0):
return 0
if isinstance(text, i.AllFloats1):
return 1
else:
raise ValueError("Invalid input value!")
# A simple use of a union which contains multiple entries of the same type
def add_one_duplicated(self, num: i.DuplicatedS32) -> i.DuplicatedS32:
if isinstance(num, i.DuplicatedS320):
return i.DuplicatedS320(num.value + 1)
if isinstance(num, i.DuplicatedS321):
return i.DuplicatedS321(num.value + 1)
if isinstance(num, i.DuplicatedS322):
return i.DuplicatedS322(num.value + 1)
else:
raise ValueError("Invalid input value!")
# Identify each case of unions which contains multiple entries of the same type
def identify_duplicated(self, num: i.DuplicatedS32) -> int:
if isinstance(num, i.DuplicatedS320):
return 0
if isinstance(num, i.DuplicatedS321):
return 1
if isinstance(num, i.DuplicatedS322):
return 2
else:
raise ValueError("Invalid input value!")
# A simple use of a union whose cases have distinct Python representations
def add_one_distinguishable_num(self, num: Union[float, int]) -> Union[float, int]:
return num + 1
# Identify each case of unions whose cases have distinct Python representations
def identify_distinguishable_num(self, num: i.DistinguishableNum) -> int:
if isinstance(num, float):
return 0
elif isinstance(num, int):
return 1
else:
raise ValueError("Invalid input value!")
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)
# All-Integers
# Booleans
assert wasm.add_one_integer(store, e.AllIntegers0(False)) == e.AllIntegers0(True)
assert wasm.add_one_integer(store, e.AllIntegers0(True)) == e.AllIntegers0(False)
# Unsigned integers
assert wasm.add_one_integer(store, e.AllIntegers1(0)) == e.AllIntegers1(1)
assert wasm.add_one_integer(store, e.AllIntegers1(2**8-1)) == e.AllIntegers1(0)
assert wasm.add_one_integer(store, e.AllIntegers2(0)) == e.AllIntegers2(1)
assert wasm.add_one_integer(store, e.AllIntegers2(2**16-1)) == e.AllIntegers2(0)
assert wasm.add_one_integer(store, e.AllIntegers3(0)) == e.AllIntegers3(1)
assert wasm.add_one_integer(store, e.AllIntegers3(2**32-1)) == e.AllIntegers3(0)
assert wasm.add_one_integer(store, e.AllIntegers4(0)) == e.AllIntegers4(1)
assert wasm.add_one_integer(store, e.AllIntegers4(2**64-1)) == e.AllIntegers4(0)
# Signed integers
assert wasm.add_one_integer(store, e.AllIntegers5(0)) == e.AllIntegers5(1)
assert wasm.add_one_integer(store, e.AllIntegers5(2**7-1)) == e.AllIntegers5(-2**7)
assert wasm.add_one_integer(store, e.AllIntegers6(0)) == e.AllIntegers6(1)
assert wasm.add_one_integer(store, e.AllIntegers6(2**15-1)) == e.AllIntegers6(-2**15)
assert wasm.add_one_integer(store, e.AllIntegers7(0)) == e.AllIntegers7(1)
assert wasm.add_one_integer(store, e.AllIntegers7(2**31-1)) == e.AllIntegers7(-2**31)
assert wasm.add_one_integer(store, e.AllIntegers8(0)) == e.AllIntegers8(1)
assert wasm.add_one_integer(store, e.AllIntegers8(2**63-1)) == e.AllIntegers8(-2**63)
# All-Floats
assert wasm.add_one_float(store, e.AllFloats0(0.0)) == e.AllFloats0(1.0)
assert wasm.add_one_float(store, e.AllFloats1(0.0)) == e.AllFloats1(1.0)
# All-Text
assert wasm.replace_first_char(store, e.AllText0('a'), 'z') == e.AllText0('z')
assert wasm.replace_first_char(store, e.AllText1('abc'), 'z') == e.AllText1('zbc')
# All-Integers
assert wasm.identify_integer(store, e.AllIntegers0(True)) == 0
assert wasm.identify_integer(store, e.AllIntegers1(0)) == 1
assert wasm.identify_integer(store, e.AllIntegers2(0)) == 2
assert wasm.identify_integer(store, e.AllIntegers3(0)) == 3
assert wasm.identify_integer(store, e.AllIntegers4(0)) == 4
assert wasm.identify_integer(store, e.AllIntegers5(0)) == 5
assert wasm.identify_integer(store, e.AllIntegers6(0)) == 6
assert wasm.identify_integer(store, e.AllIntegers7(0)) == 7
assert wasm.identify_integer(store, e.AllIntegers8(0)) == 8
# All-Floats
assert wasm.identify_float(store, e.AllFloats0(0.0)) == 0
assert wasm.identify_float(store, e.AllFloats1(0.0)) == 1
# All-Text
assert wasm.identify_text(store, e.AllText0('a')) == 0
assert wasm.identify_text(store, e.AllText1('abc')) == 1
# Duplicated
assert wasm.add_one_duplicated(store, e.DuplicatedS320(0)) == e.DuplicatedS320(1)
assert wasm.add_one_duplicated(store, e.DuplicatedS321(1)) == e.DuplicatedS321(2)
assert wasm.add_one_duplicated(store, e.DuplicatedS322(2)) == e.DuplicatedS322(3)
assert wasm.identify_duplicated(store, e.DuplicatedS320(0)) == 0
assert wasm.identify_duplicated(store, e.DuplicatedS321(0)) == 1
assert wasm.identify_duplicated(store, e.DuplicatedS322(0)) == 2
# Distinguishable
assert wasm.add_one_distinguishable_num(store, 0.0) == 1.0
assert wasm.add_one_distinguishable_num(store, 0) == 1
assert wasm.identify_distinguishable_num(store, 0.0) == 0
assert wasm.identify_distinguishable_num(store, 1) == 1
if __name__ == '__main__':
run(sys.argv[1])

View File

@@ -0,0 +1,57 @@
/// A union of all of the integral types
union all-integers {
/// Bool is equivalent to a 1 bit integer
/// and is treated that way in some languages
bool,
u8, u16, u32, u64,
s8, s16, s32, s64
}
union all-floats {
float32, float64
}
union all-text {
char, string
}
// Returns the same case as the input but with 1 added
add-one-integer: func(num: all-integers) -> all-integers
// Returns the same case as the input but with 1 added
add-one-float: func(num: all-floats) -> all-floats
// Returns the same case as the input but with the first character replaced
replace-first-char: func(text: all-text, letter: char) -> all-text
// Returns the index of the case provided
identify-integer: func(num: all-integers) -> u8
// Returns the index of the case provided
identify-float: func(num: all-floats) -> u8
// Returns the index of the case provided
identify-text: func(text: all-text) -> u8
union duplicated-s32 {
/// The first s32
s32,
/// The second s32
s32,
/// The third s32
s32
}
// Returns the same case as the input but with 1 added
add-one-duplicated: func(num: duplicated-s32) -> duplicated-s32
// Returns the index of the case provided
identify-duplicated: func(num: duplicated-s32) -> u8
/// A type containing numeric types that are distinct in most languages
union distinguishable-num {
/// A Floating Point Number
float64,
/// A Signed Integer
s64
}
// Returns the same case as the input but with 1 added
add-one-distinguishable-num: func(num: distinguishable-num) -> distinguishable-num
// Returns the index of the case provided
identify-distinguishable-num: func(num: distinguishable-num) -> u8

View File

@@ -0,0 +1,171 @@
wit_bindgen_rust::import!("../../tests/runtime/unions/imports.wit");
wit_bindgen_rust::export!("../../tests/runtime/unions/exports.wit");
use exports::*;
struct Exports;
impl exports::Exports for Exports {
fn test_imports() {
use imports::*;
// All-Integers
// Booleans
assert!(matches!(add_one_integer(AllIntegers::Bool(false)), AllIntegers::Bool(true)));
assert!(matches!(add_one_integer(AllIntegers::Bool(true)), AllIntegers::Bool(false)));
// Unsigned integers
assert!(matches!(add_one_integer(AllIntegers::U8(0)), AllIntegers::U8(1)));
assert!(matches!(add_one_integer(AllIntegers::U8(u8::MAX)), AllIntegers::U8(0)));
assert!(matches!(add_one_integer(AllIntegers::U16(0)), AllIntegers::U16(1)));
assert!(matches!(add_one_integer(AllIntegers::U16(u16::MAX)), AllIntegers::U16(0)));
assert!(matches!(add_one_integer(AllIntegers::U32(0)), AllIntegers::U32(1)));
assert!(matches!(add_one_integer(AllIntegers::U32(u32::MAX)), AllIntegers::U32(0)));
assert!(matches!(add_one_integer(AllIntegers::U64(0)), AllIntegers::U64(1)));
assert!(matches!(add_one_integer(AllIntegers::U64(u64::MAX)), AllIntegers::U64(0)));
// Signed integers
assert!(matches!(add_one_integer(AllIntegers::I8(0)), AllIntegers::I8(1)));
assert!(matches!(add_one_integer(AllIntegers::I8(i8::MAX)), AllIntegers::I8(i8::MIN)));
assert!(matches!(add_one_integer(AllIntegers::I16(0)), AllIntegers::I16(1)));
assert!(matches!(add_one_integer(AllIntegers::I16(i16::MAX)), AllIntegers::I16(i16::MIN)));
assert!(matches!(add_one_integer(AllIntegers::I32(0)), AllIntegers::I32(1)));
assert!(matches!(add_one_integer(AllIntegers::I32(i32::MAX)), AllIntegers::I32(i32::MIN)));
assert!(matches!(add_one_integer(AllIntegers::I64(0)), AllIntegers::I64(1)));
assert!(matches!(add_one_integer(AllIntegers::I64(i64::MAX)), AllIntegers::I64(i64::MIN)));
// All-Floats
assert!(matches!(add_one_float(AllFloats::F32(0.0)), AllFloats::F32(1.0)));
assert!(matches!(add_one_float(AllFloats::F64(0.0)), AllFloats::F64(1.0)));
// All-Text
assert!(matches!(replace_first_char(AllTextParam::Char('a'), 'z'), AllTextResult::Char('z')));
let rhs = "zbc".to_string();
assert!(matches!(replace_first_char(AllTextParam::String("abc"), 'z'), AllTextResult::String(rhs)));
// All-Integers
assert!(matches!(identify_integer(AllIntegers::Bool(true)), 0));
assert!(matches!(identify_integer(AllIntegers::U8(0)), 1));
assert!(matches!(identify_integer(AllIntegers::U16(0)), 2));
assert!(matches!(identify_integer(AllIntegers::U32(0)), 3));
assert!(matches!(identify_integer(AllIntegers::U64(0)), 4));
assert!(matches!(identify_integer(AllIntegers::I8(0)), 5));
assert!(matches!(identify_integer(AllIntegers::I16(0)), 6));
assert!(matches!(identify_integer(AllIntegers::I32(0)), 7));
assert!(matches!(identify_integer(AllIntegers::I64(0)), 8));
// All-Floats
assert!(matches!(identify_float(AllFloats::F32(0.0)), 0));
assert!(matches!(identify_float(AllFloats::F64(0.0)), 1));
// All-Text
assert!(matches!(identify_text(AllTextParam::Char('a')), 0));
assert!(matches!(identify_text(AllTextParam::String("abc")), 1));
// Duplicated
assert!(matches!(add_one_duplicated(DuplicatedS32::I320(0)), DuplicatedS32::I320(1)));
assert!(matches!(add_one_duplicated(DuplicatedS32::I321(1)), DuplicatedS32::I321(2)));
assert!(matches!(add_one_duplicated(DuplicatedS32::I322(2)), DuplicatedS32::I322(3)));
assert!(matches!(identify_duplicated(DuplicatedS32::I320(0)), 0));
assert!(matches!(identify_duplicated(DuplicatedS32::I321(0)), 1));
assert!(matches!(identify_duplicated(DuplicatedS32::I321(0)), 2));
// Distinguishable
assert!(matches!(add_one_distinguishable_num(DistinguishableNum::F64(0.0)), DistinguishableNum::F64(1.0)));
assert!(matches!(add_one_distinguishable_num(DistinguishableNum::I64(0)), DistinguishableNum::I64(1)));
assert!(matches!(identify_distinguishable_num(DistinguishableNum::F64(0.0)), 0));
assert!(matches!(identify_distinguishable_num(DistinguishableNum::I64(1)), 1));
}
fn add_one_integer(num: AllIntegers) -> AllIntegers {
match num {
// Boolean
AllIntegers::Bool(b) => AllIntegers::Bool(!b),
// Unsigneed Integers
AllIntegers::U8(n) => AllIntegers::U8(n.wrapping_add(1)),
AllIntegers::U16(n) => AllIntegers::U16(n.wrapping_add(1)),
AllIntegers::U32(n) => AllIntegers::U32(n.wrapping_add(1)),
AllIntegers::U64(n) => AllIntegers::U64(n.wrapping_add(1)),
// Signed Integers
AllIntegers::I8(n) => AllIntegers::I8(n.wrapping_add(1)),
AllIntegers::I16(n) => AllIntegers::I16(n.wrapping_add(1)),
AllIntegers::I32(n) => AllIntegers::I32(n.wrapping_add(1)),
AllIntegers::I64(n) => AllIntegers::I64(n.wrapping_add(1)),
}
}
fn add_one_float(num: AllFloats) -> AllFloats {
match num {
AllFloats::F32(n) => AllFloats::F32(n + 1.0),
AllFloats::F64(n) => AllFloats::F64(n + 1.0),
}
}
fn replace_first_char(text: AllText, letter: char) -> AllText {
match text {
AllText::Char(c) => AllText::Char(letter),
AllText::String(s) => AllText::String(format!("{}{}", letter, &s[1..]))
}
}
fn identify_integer(num: AllIntegers) -> u8 {
match num {
// Boolean
AllIntegers::Bool(_b) => 0,
// Unsigneed Integers
AllIntegers::U8(_n) => 1,
AllIntegers::U16(_n) => 2,
AllIntegers::U32(_n) => 3,
AllIntegers::U64(_n) => 4,
// Signed Integers
AllIntegers::I8(_n) => 5,
AllIntegers::I16(_n) => 6,
AllIntegers::I32(_n) => 7,
AllIntegers::I64(_n) => 8,
}
}
fn identify_float(num: AllFloats) -> u8 {
match num {
AllFloats::F32(_n) => 0,
AllFloats::F64(_n) => 1,
}
}
fn identify_text(text: AllText) -> u8 {
match text {
AllText::Char(_c) => 0,
AllText::String(_s) => 1
}
}
fn add_one_duplicated(num: DuplicatedS32) -> DuplicatedS32 {
match num {
DuplicatedS32::I320(n) => DuplicatedS32::I320(n.wrapping_add(1)),
DuplicatedS32::I321(n) => DuplicatedS32::I321(n.wrapping_add(1)),
DuplicatedS32::I322(n) => DuplicatedS32::I322(n.wrapping_add(1)),
}
}
fn identify_duplicated(num: DuplicatedS32) -> u8 {
match num {
DuplicatedS32::I320(_n) => 0,
DuplicatedS32::I321(_n) => 1,
DuplicatedS32::I322(_n) => 2,
}
}
fn add_one_distinguishable_num(num: DistinguishableNum) -> DistinguishableNum {
match num {
DistinguishableNum::F64(n) => DistinguishableNum::F64(n + 1.0),
DistinguishableNum::I64(n) => DistinguishableNum::I64(n.wrapping_add(1)),
}
}
fn identify_distinguishable_num(num: DistinguishableNum) -> u8 {
match num {
DistinguishableNum::F64(_n) => 0,
DistinguishableNum::I64(_n) => 1,
}
}
}