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,16 @@
test-imports: func()
roundtrip-u8: func(a: u8) -> u8
roundtrip-s8: func(a: s8) -> s8
roundtrip-u16: func(a: u16) -> u16
roundtrip-s16: func(a: s16) -> s16
roundtrip-u32: func(a: u32) -> u32
roundtrip-s32: func(a: s32) -> s32
roundtrip-u64: func(a: u64) -> u64
roundtrip-s64: func(a: s64) -> s64
roundtrip-float32: func(a: float32) -> float32
roundtrip-float64: func(a: float64) -> float64
roundtrip-char: func(a: char) -> char
set-scalar: func(a: u32)
get-scalar: func() -> u32

View File

@@ -0,0 +1,106 @@
from exports.bindings import Exports
from imports.bindings import add_imports_to_linker, Imports
import math;
import sys
import wasmtime
class MyImports:
def roundtrip_u8(self, a: int) -> int:
return a
def roundtrip_s8(self, a: int) -> int:
return a
def roundtrip_u16(self, a: int) -> int:
return a
def roundtrip_s16(self, a: int) -> int:
return a
def roundtrip_u32(self, a: int) -> int:
return a
def roundtrip_s32(self, a: int) -> int:
return a
def roundtrip_u64(self, a: int) -> int:
return a
def roundtrip_s64(self, a: int) -> int:
return a
def roundtrip_float32(self, a: float) -> float:
return a
def roundtrip_float64(self, a: float) -> float:
return a
def roundtrip_char(self, a: str) -> str:
return a
def set_scalar(self, a: int) -> None:
self.scalar = a
def get_scalar(self) -> int:
return self.scalar
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_u8(store, 1) == 1)
assert(wasm.roundtrip_u8(store, (1 << 8) - 1) == (1 << 8) - 1)
assert(wasm.roundtrip_u16(store, 1) == 1)
assert(wasm.roundtrip_u16(store, (1 << 16) - 1) == (1 << 16) - 1)
assert(wasm.roundtrip_u32(store, 1) == 1)
assert(wasm.roundtrip_u32(store, (1 << 32) - 1) == (1 << 32) - 1)
assert(wasm.roundtrip_u64(store, 1) == 1)
assert(wasm.roundtrip_u64(store, (1 << 64) - 1) == (1 << 64) - 1)
assert(wasm.roundtrip_s8(store, 1) == 1)
assert(wasm.roundtrip_s8(store, (1 << (8 - 1) - 1)) == (1 << (8 - 1) - 1))
assert(wasm.roundtrip_s8(store, -(1 << (8 - 1))) == -(1 << (8 - 1)))
assert(wasm.roundtrip_s16(store, 1) == 1)
assert(wasm.roundtrip_s16(store, (1 << (16 - 1) - 1)) == (1 << (16 - 1) - 1))
assert(wasm.roundtrip_s16(store, -(1 << (16 - 1))) == -(1 << (16 - 1)))
assert(wasm.roundtrip_s32(store, 1) == 1)
assert(wasm.roundtrip_s32(store, (1 << (32 - 1) - 1)) == (1 << (32 - 1) - 1))
assert(wasm.roundtrip_s32(store, -(1 << (32 - 1))) == -(1 << (32 - 1)))
assert(wasm.roundtrip_s64(store, 1) == 1)
assert(wasm.roundtrip_s64(store, (1 << (64 - 1) - 1)) == (1 << (64 - 1) - 1))
assert(wasm.roundtrip_s64(store, -(1 << (64 - 1))) == -(1 << (64 - 1)))
inf = float('inf')
assert(wasm.roundtrip_float32(store, 1.0) == 1.0)
assert(wasm.roundtrip_float32(store, inf) == inf)
assert(wasm.roundtrip_float32(store, -inf) == -inf)
assert(math.isnan(wasm.roundtrip_float32(store, float('nan'))))
assert(wasm.roundtrip_float64(store, 1.0) == 1.0)
assert(wasm.roundtrip_float64(store, inf) == inf)
assert(wasm.roundtrip_float64(store, -inf) == -inf)
assert(math.isnan(wasm.roundtrip_float64(store, float('nan'))))
assert(wasm.roundtrip_char(store, 'a') == 'a')
assert(wasm.roundtrip_char(store, ' ') == ' ')
assert(wasm.roundtrip_char(store, '🚩') == '🚩')
wasm.set_scalar(store, 2)
assert(wasm.get_scalar(store) == 2)
wasm.set_scalar(store, 4)
assert(wasm.get_scalar(store) == 4)
if __name__ == '__main__':
run(sys.argv[1])

View File

@@ -0,0 +1,188 @@
use anyhow::Result;
wit_bindgen_wasmtime::export!("../../tests/runtime/numbers/imports.wit");
#[derive(Default)]
pub struct MyImports {
scalar: u32,
}
impl imports::Imports for MyImports {
fn roundtrip_u8(&mut self, val: u8) -> u8 {
val
}
fn roundtrip_s8(&mut self, val: i8) -> i8 {
val
}
fn roundtrip_u16(&mut self, val: u16) -> u16 {
val
}
fn roundtrip_s16(&mut self, val: i16) -> i16 {
val
}
fn roundtrip_u32(&mut self, val: u32) -> u32 {
val
}
fn roundtrip_s32(&mut self, val: i32) -> i32 {
val
}
fn roundtrip_u64(&mut self, val: u64) -> u64 {
val
}
fn roundtrip_s64(&mut self, val: i64) -> i64 {
val
}
fn roundtrip_float32(&mut self, val: f32) -> f32 {
val
}
fn roundtrip_float64(&mut self, val: f64) -> f64 {
val
}
fn roundtrip_char(&mut self, val: char) -> char {
val
}
fn set_scalar(&mut self, val: u32) {
self.scalar = val;
}
fn get_scalar(&mut self) -> u32 {
self.scalar
}
}
wit_bindgen_wasmtime::import!("../../tests/runtime/numbers/exports.wit");
fn run(wasm: &str) -> Result<()> {
let (exports, mut store) = crate::instantiate(
wasm,
|linker| imports::add_to_linker(linker, |cx| -> &mut MyImports { &mut cx.imports }),
|store, module, linker| {
exports::Exports::instantiate(store, module, linker, |cx| &mut cx.exports)
},
)?;
exports.test_imports(&mut store)?;
assert_eq!(exports.roundtrip_u8(&mut store, 1)?, 1);
assert_eq!(
exports.roundtrip_u8(&mut store, u8::min_value())?,
u8::min_value()
);
assert_eq!(
exports.roundtrip_u8(&mut store, u8::max_value())?,
u8::max_value()
);
assert_eq!(exports.roundtrip_s8(&mut store, 1)?, 1);
assert_eq!(
exports.roundtrip_s8(&mut store, i8::min_value())?,
i8::min_value()
);
assert_eq!(
exports.roundtrip_s8(&mut store, i8::max_value())?,
i8::max_value()
);
assert_eq!(exports.roundtrip_u16(&mut store, 1)?, 1);
assert_eq!(
exports.roundtrip_u16(&mut store, u16::min_value())?,
u16::min_value()
);
assert_eq!(
exports.roundtrip_u16(&mut store, u16::max_value())?,
u16::max_value()
);
assert_eq!(exports.roundtrip_s16(&mut store, 1)?, 1);
assert_eq!(
exports.roundtrip_s16(&mut store, i16::min_value())?,
i16::min_value()
);
assert_eq!(
exports.roundtrip_s16(&mut store, i16::max_value())?,
i16::max_value()
);
assert_eq!(exports.roundtrip_u32(&mut store, 1)?, 1);
assert_eq!(
exports.roundtrip_u32(&mut store, u32::min_value())?,
u32::min_value()
);
assert_eq!(
exports.roundtrip_u32(&mut store, u32::max_value())?,
u32::max_value()
);
assert_eq!(exports.roundtrip_s32(&mut store, 1)?, 1);
assert_eq!(
exports.roundtrip_s32(&mut store, i32::min_value())?,
i32::min_value()
);
assert_eq!(
exports.roundtrip_s32(&mut store, i32::max_value())?,
i32::max_value()
);
assert_eq!(exports.roundtrip_u64(&mut store, 1)?, 1);
assert_eq!(
exports.roundtrip_u64(&mut store, u64::min_value())?,
u64::min_value()
);
assert_eq!(
exports.roundtrip_u64(&mut store, u64::max_value())?,
u64::max_value()
);
assert_eq!(exports.roundtrip_s64(&mut store, 1)?, 1);
assert_eq!(
exports.roundtrip_s64(&mut store, i64::min_value())?,
i64::min_value()
);
assert_eq!(
exports.roundtrip_s64(&mut store, i64::max_value())?,
i64::max_value()
);
assert_eq!(exports.roundtrip_float32(&mut store, 1.0)?, 1.0);
assert_eq!(
exports.roundtrip_float32(&mut store, f32::INFINITY)?,
f32::INFINITY
);
assert_eq!(
exports.roundtrip_float32(&mut store, f32::NEG_INFINITY)?,
f32::NEG_INFINITY
);
assert!(exports.roundtrip_float32(&mut store, f32::NAN)?.is_nan());
assert_eq!(exports.roundtrip_float64(&mut store, 1.0)?, 1.0);
assert_eq!(
exports.roundtrip_float64(&mut store, f64::INFINITY)?,
f64::INFINITY
);
assert_eq!(
exports.roundtrip_float64(&mut store, f64::NEG_INFINITY)?,
f64::NEG_INFINITY
);
assert!(exports.roundtrip_float64(&mut store, f64::NAN)?.is_nan());
assert_eq!(exports.roundtrip_char(&mut store, 'a')?, 'a');
assert_eq!(exports.roundtrip_char(&mut store, ' ')?, ' ');
assert_eq!(exports.roundtrip_char(&mut store, '🚩')?, '🚩');
exports.set_scalar(&mut store, 2)?;
assert_eq!(exports.get_scalar(&mut store)?, 2);
exports.set_scalar(&mut store, 4)?;
assert_eq!(exports.get_scalar(&mut store)?, 4);
Ok(())
}

View File

@@ -0,0 +1,89 @@
import { addImportsToImports, Imports } from "./imports.js";
import { Exports } from "./exports.js";
import { getWasm, addWasiToImports } from "./helpers.js";
function assertEq(x: any, y: any) {
if (x !== y)
throw new Error(`${x} != ${y}`);
}
function assert(x: boolean) {
if (!x)
throw new Error("assert failed");
}
async function run() {
const importObj = {};
let scalar = 0;
addImportsToImports(importObj, {
roundtripU8(x) { return x; },
roundtripS8(x) { return x; },
roundtripU16(x) { return x; },
roundtripS16(x) { return x; },
roundtripU32(x) { return x; },
roundtripS32(x) { return x; },
roundtripU64(x) { return x; },
roundtripS64(x) { return x; },
roundtripFloat32(x) { return x; },
roundtripFloat64(x) { return x; },
roundtripChar(x) { return x; },
setScalar(x) { scalar = x; },
getScalar() { return scalar; },
});
const wasi = addWasiToImports(importObj);
const wasm = new Exports();
await wasm.instantiate(getWasm(), importObj);
wasi.start(wasm.instance);
wasm.testImports();
assertEq(wasm.roundtripU8(1), 1);
assertEq(wasm.roundtripU8((1 << 8) - 1), (1 << 8) - 1);
assertEq(wasm.roundtripS8(1), 1);
assertEq(wasm.roundtripS8((1 << 7) - 1), (1 << 7) - 1);
assertEq(wasm.roundtripS8(-(1 << 7)), -(1 << 7));
assertEq(wasm.roundtripU16(1), 1);
assertEq(wasm.roundtripU16((1 << 16) - 1), (1 << 16) - 1);
assertEq(wasm.roundtripS16(1), 1);
assertEq(wasm.roundtripS16((1 << 15) - 1), (1 << 15) - 1);
assertEq(wasm.roundtripS16(-(1 << 15)), -(1 << 15));
assertEq(wasm.roundtripU32(1), 1);
assertEq(wasm.roundtripU32(~0 >>> 0), ~0 >>> 0);
assertEq(wasm.roundtripS32(1), 1);
assertEq(wasm.roundtripS32(((1 << 31) - 1) >>> 0), ((1 << 31) - 1) >>> 0);
assertEq(wasm.roundtripS32(1 << 31), 1 << 31);
assertEq(wasm.roundtripU64(1n), 1n);
assertEq(wasm.roundtripU64((1n << 64n) - 1n), (1n << 64n) - 1n);
assertEq(wasm.roundtripS64(1n), 1n);
assertEq(wasm.roundtripS64((1n << 63n) - 1n), (1n << 63n) - 1n);
assertEq(wasm.roundtripS64(-(1n << 63n)), -(1n << 63n));
assertEq(wasm.roundtripFloat32(1), 1);
assertEq(wasm.roundtripFloat32(Infinity), Infinity);
assertEq(wasm.roundtripFloat32(-Infinity), -Infinity);
assert(Number.isNaN(wasm.roundtripFloat32(NaN)));
assertEq(wasm.roundtripFloat64(1), 1);
assertEq(wasm.roundtripFloat64(Infinity), Infinity);
assertEq(wasm.roundtripFloat64(-Infinity), -Infinity);
assert(Number.isNaN(wasm.roundtripFloat64(NaN)));
assertEq(wasm.roundtripChar('a'), 'a');
assertEq(wasm.roundtripChar(' '), ' ');
assertEq(wasm.roundtripChar('🚩'), '🚩');
wasm.setScalar(2);
assertEq(wasm.getScalar(), 2);
wasm.setScalar(4);
assertEq(wasm.getScalar(), 4);
}
await run()

View File

@@ -0,0 +1,14 @@
roundtrip-u8: func(a: u8) -> u8
roundtrip-s8: func(a: s8) -> s8
roundtrip-u16: func(a: u16) -> u16
roundtrip-s16: func(a: s16) -> s16
roundtrip-u32: func(a: u32) -> u32
roundtrip-s32: func(a: s32) -> s32
roundtrip-u64: func(a: u64) -> u64
roundtrip-s64: func(a: s64) -> s64
roundtrip-float32: func(a: float32) -> float32
roundtrip-float64: func(a: float64) -> float64
roundtrip-char: func(a: char) -> char
set-scalar: func(a: u32)
get-scalar: func() -> u32

View File

@@ -0,0 +1,113 @@
#include <assert.h>
#include <exports.h>
#include <imports.h>
#include <limits.h>
#include <math.h>
uint8_t exports_roundtrip_u8(uint8_t a) {
return a;
}
int8_t exports_roundtrip_s8(int8_t a) {
return a;
}
uint16_t exports_roundtrip_u16(uint16_t a) {
return a;
}
int16_t exports_roundtrip_s16(int16_t a) {
return a;
}
uint32_t exports_roundtrip_u32(uint32_t a) {
return a;
}
int32_t exports_roundtrip_s32(int32_t a) {
return a;
}
uint64_t exports_roundtrip_u64(uint64_t a) {
return a;
}
int64_t exports_roundtrip_s64(int64_t a) {
return a;
}
float exports_roundtrip_float32(float a) {
return a;
}
double exports_roundtrip_float64(double a) {
return a;
}
uint32_t exports_roundtrip_char(uint32_t a) {
return a;
}
static uint32_t SCALAR = 0;
void exports_set_scalar(uint32_t a) {
SCALAR = a;
}
uint32_t exports_get_scalar(void) {
return SCALAR;
}
void exports_test_imports() {
assert(imports_roundtrip_u8(1) == 1);
assert(imports_roundtrip_u8(0) == 0);
assert(imports_roundtrip_u8(UCHAR_MAX) == UCHAR_MAX);
assert(imports_roundtrip_s8(1) == 1);
assert(imports_roundtrip_s8(SCHAR_MIN) == SCHAR_MIN);
assert(imports_roundtrip_s8(SCHAR_MAX) == SCHAR_MAX);
assert(imports_roundtrip_u16(1) == 1);
assert(imports_roundtrip_u16(0) == 0);
assert(imports_roundtrip_u16(USHRT_MAX) == USHRT_MAX);
assert(imports_roundtrip_s16(1) == 1);
assert(imports_roundtrip_s16(SHRT_MIN) == SHRT_MIN);
assert(imports_roundtrip_s16(SHRT_MAX) == SHRT_MAX);
assert(imports_roundtrip_u32(1) == 1);
assert(imports_roundtrip_u32(0) == 0);
assert(imports_roundtrip_u32(UINT_MAX) == UINT_MAX);
assert(imports_roundtrip_s32(1) == 1);
assert(imports_roundtrip_s32(INT_MIN) == INT_MIN);
assert(imports_roundtrip_s32(INT_MAX) == INT_MAX);
assert(imports_roundtrip_u64(1) == 1);
assert(imports_roundtrip_u64(0) == 0);
assert(imports_roundtrip_u64(ULONG_MAX) == ULONG_MAX);
assert(imports_roundtrip_s64(1) == 1);
assert(imports_roundtrip_s64(LONG_MIN) == LONG_MIN);
assert(imports_roundtrip_s64(LONG_MAX) == LONG_MAX);
assert(imports_roundtrip_float32(1.0) == 1.0);
assert(imports_roundtrip_float32(INFINITY) == INFINITY);
assert(imports_roundtrip_float32(-INFINITY) == -INFINITY);
assert(isnan(imports_roundtrip_float32(NAN)));
assert(imports_roundtrip_float64(1.0) == 1.0);
assert(imports_roundtrip_float64(INFINITY) == INFINITY);
assert(imports_roundtrip_float64(-INFINITY) == -INFINITY);
assert(isnan(imports_roundtrip_float64(NAN)));
assert(imports_roundtrip_char('a') == 'a');
assert(imports_roundtrip_char(' ') == ' ');
assert(imports_roundtrip_char(U'🚩') == U'🚩');
imports_set_scalar(2);
assert(imports_get_scalar() == 2);
imports_set_scalar(4);
assert(imports_get_scalar() == 4);
}

View File

@@ -0,0 +1,116 @@
wit_bindgen_rust::import!("../../tests/runtime/numbers/imports.wit");
wit_bindgen_rust::export!("../../tests/runtime/numbers/exports.wit");
use imports::*;
use std::sync::atomic::{AtomicU32, Ordering::SeqCst};
struct Exports;
static SCALAR: AtomicU32 = AtomicU32::new(0);
impl exports::Exports for Exports {
fn test_imports() {
assert_eq!(roundtrip_u8(1), 1);
assert_eq!(roundtrip_u8(u8::min_value()), u8::min_value());
assert_eq!(roundtrip_u8(u8::max_value()), u8::max_value());
assert_eq!(roundtrip_s8(1), 1);
assert_eq!(roundtrip_s8(i8::min_value()), i8::min_value());
assert_eq!(roundtrip_s8(i8::max_value()), i8::max_value());
assert_eq!(roundtrip_u16(1), 1);
assert_eq!(roundtrip_u16(u16::min_value()), u16::min_value());
assert_eq!(roundtrip_u16(u16::max_value()), u16::max_value());
assert_eq!(roundtrip_s16(1), 1);
assert_eq!(roundtrip_s16(i16::min_value()), i16::min_value());
assert_eq!(roundtrip_s16(i16::max_value()), i16::max_value());
assert_eq!(roundtrip_u32(1), 1);
assert_eq!(roundtrip_u32(u32::min_value()), u32::min_value());
assert_eq!(roundtrip_u32(u32::max_value()), u32::max_value());
assert_eq!(roundtrip_s32(1), 1);
assert_eq!(roundtrip_s32(i32::min_value()), i32::min_value());
assert_eq!(roundtrip_s32(i32::max_value()), i32::max_value());
assert_eq!(roundtrip_u64(1), 1);
assert_eq!(roundtrip_u64(u64::min_value()), u64::min_value());
assert_eq!(roundtrip_u64(u64::max_value()), u64::max_value());
assert_eq!(roundtrip_s64(1), 1);
assert_eq!(roundtrip_s64(i64::min_value()), i64::min_value());
assert_eq!(roundtrip_s64(i64::max_value()), i64::max_value());
assert_eq!(roundtrip_float32(1.0), 1.0);
assert_eq!(roundtrip_float32(f32::INFINITY), f32::INFINITY);
assert_eq!(roundtrip_float32(f32::NEG_INFINITY), f32::NEG_INFINITY);
assert!(roundtrip_float32(f32::NAN).is_nan());
assert_eq!(roundtrip_float64(1.0), 1.0);
assert_eq!(roundtrip_float64(f64::INFINITY), f64::INFINITY);
assert_eq!(roundtrip_float64(f64::NEG_INFINITY), f64::NEG_INFINITY);
assert!(roundtrip_float64(f64::NAN).is_nan());
assert_eq!(roundtrip_char('a'), 'a');
assert_eq!(roundtrip_char(' '), ' ');
assert_eq!(roundtrip_char('🚩'), '🚩');
set_scalar(2);
assert_eq!(get_scalar(), 2);
set_scalar(4);
assert_eq!(get_scalar(), 4);
}
fn roundtrip_u8(a: u8) -> u8 {
a
}
fn roundtrip_s8(a: i8) -> i8 {
a
}
fn roundtrip_u16(a: u16) -> u16 {
a
}
fn roundtrip_s16(a: i16) -> i16 {
a
}
fn roundtrip_u32(a: u32) -> u32 {
a
}
fn roundtrip_s32(a: i32) -> i32 {
a
}
fn roundtrip_u64(a: u64) -> u64 {
a
}
fn roundtrip_s64(a: i64) -> i64 {
a
}
fn roundtrip_float32(a: f32) -> f32 {
a
}
fn roundtrip_float64(a: f64) -> f64 {
a
}
fn roundtrip_char(a: char) -> char {
a
}
fn set_scalar(val: u32) {
SCALAR.store(val, SeqCst)
}
fn get_scalar() -> u32 {
SCALAR.load(SeqCst)
}
}