Path: blob/main/crates/c-api/src/component/val.rs
1692 views
use wasmtime::component::Val;12use crate::wasm_name_t;34use std::mem;5use std::mem::MaybeUninit;6use std::ptr;7use std::slice;89crate::declare_vecs! {10(11name: wasmtime_component_vallist_t,12ty: wasmtime_component_val_t,13new: wasmtime_component_vallist_new,14empty: wasmtime_component_vallist_new_empty,15uninit: wasmtime_component_vallist_new_uninit,16copy: wasmtime_component_vallist_copy,17delete: wasmtime_component_vallist_delete,18)19(20name: wasmtime_component_valrecord_t,21ty: wasmtime_component_valrecord_entry_t,22new: wasmtime_component_valrecord_new,23empty: wasmtime_component_valrecord_new_empty,24uninit: wasmtime_component_valrecord_new_uninit,25copy: wasmtime_component_valrecord_copy,26delete: wasmtime_component_valrecord_delete,27)28(29name: wasmtime_component_valtuple_t,30ty: wasmtime_component_val_t,31new: wasmtime_component_valtuple_new,32empty: wasmtime_component_valtuple_new_empty,33uninit: wasmtime_component_valtuple_new_uninit,34copy: wasmtime_component_valtuple_copy,35delete: wasmtime_component_valtuple_delete,36)37(38name: wasmtime_component_valflags_t,39ty: wasm_name_t,40new: wasmtime_component_valflags_new,41empty: wasmtime_component_valflags_new_empty,42uninit: wasmtime_component_valflags_new_uninit,43copy: wasmtime_component_valflags_copy,44delete: wasmtime_component_valflags_delete,45)46}4748impl From<&wasmtime_component_vallist_t> for Vec<Val> {49fn from(value: &wasmtime_component_vallist_t) -> Self {50value.as_slice().iter().map(Val::from).collect()51}52}5354impl From<&[Val]> for wasmtime_component_vallist_t {55fn from(value: &[Val]) -> Self {56value57.iter()58.map(wasmtime_component_val_t::from)59.collect::<Vec<_>>()60.into()61}62}6364#[derive(Clone)]65#[repr(C)]66pub struct wasmtime_component_valrecord_entry_t {67name: wasm_name_t,68val: wasmtime_component_val_t,69}7071impl Default for wasmtime_component_valrecord_entry_t {72fn default() -> Self {73Self {74name: wasm_name_t::from_name(String::new()),75val: Default::default(),76}77}78}7980impl From<&wasmtime_component_valrecord_entry_t> for (String, Val) {81fn from(value: &wasmtime_component_valrecord_entry_t) -> Self {82(83String::from_utf8(value.name.clone().take()).unwrap(),84Val::from(&value.val),85)86}87}8889impl From<&(String, Val)> for wasmtime_component_valrecord_entry_t {90fn from((name, val): &(String, Val)) -> Self {91Self {92name: wasm_name_t::from_name(name.clone()),93val: wasmtime_component_val_t::from(val),94}95}96}9798impl From<&wasmtime_component_valrecord_t> for Vec<(String, Val)> {99fn from(value: &wasmtime_component_valrecord_t) -> Self {100value.as_slice().iter().map(Into::into).collect()101}102}103104impl From<&[(String, Val)]> for wasmtime_component_valrecord_t {105fn from(value: &[(String, Val)]) -> Self {106value107.iter()108.map(wasmtime_component_valrecord_entry_t::from)109.collect::<Vec<_>>()110.into()111}112}113114impl From<&wasmtime_component_valtuple_t> for Vec<Val> {115fn from(value: &wasmtime_component_valtuple_t) -> Self {116value.as_slice().iter().map(Val::from).collect()117}118}119120impl From<&[Val]> for wasmtime_component_valtuple_t {121fn from(value: &[Val]) -> Self {122value123.iter()124.map(wasmtime_component_val_t::from)125.collect::<Vec<_>>()126.into()127}128}129130impl From<&wasmtime_component_valflags_t> for Vec<String> {131fn from(value: &wasmtime_component_valflags_t) -> Self {132value133.clone()134.take()135.into_iter()136.map(|mut x| String::from_utf8(x.take()))137.collect::<Result<Vec<_>, _>>()138.unwrap()139}140}141142impl From<&[String]> for wasmtime_component_valflags_t {143fn from(value: &[String]) -> Self {144value145.iter()146.map(|x| wasm_name_t::from_name(x.clone()))147.collect::<Vec<_>>()148.into()149}150}151152#[repr(C)]153#[derive(Clone)]154pub struct wasmtime_component_valvariant_t {155discriminant: wasm_name_t,156val: Option<Box<wasmtime_component_val_t>>,157}158159impl From<(&String, &Option<Box<Val>>)> for wasmtime_component_valvariant_t {160fn from((discriminant, value): (&String, &Option<Box<Val>>)) -> Self {161Self {162discriminant: wasm_name_t::from_name(discriminant.clone()),163val: value164.as_ref()165.map(|x| Box::new(wasmtime_component_val_t::from(x.as_ref()))),166}167}168}169170impl From<&wasmtime_component_valvariant_t> for (String, Option<Box<Val>>) {171fn from(value: &wasmtime_component_valvariant_t) -> Self {172(173String::from_utf8(value.discriminant.clone().take()).unwrap(),174value.val.as_ref().map(|x| Box::new(Val::from(x.as_ref()))),175)176}177}178179#[repr(C)]180#[derive(Clone)]181pub struct wasmtime_component_valresult_t {182is_ok: bool,183val: Option<Box<wasmtime_component_val_t>>,184}185186impl From<&wasmtime_component_valresult_t> for Result<Option<Box<Val>>, Option<Box<Val>>> {187fn from(value: &wasmtime_component_valresult_t) -> Self {188let val = value.val.as_ref().map(|x| Box::new(Val::from(x.as_ref())));189190match value.is_ok {191true => Ok(val),192false => Err(val),193}194}195}196197impl From<&Result<Option<Box<Val>>, Option<Box<Val>>>> for wasmtime_component_valresult_t {198fn from(value: &Result<Option<Box<Val>>, Option<Box<Val>>>) -> Self {199let (Ok(x) | Err(x)) = value;200201Self {202is_ok: value.is_ok(),203val: x204.as_ref()205.map(|x| Box::new(wasmtime_component_val_t::from(x.as_ref()))),206}207}208}209210#[repr(C, u8)]211#[derive(Clone)]212pub enum wasmtime_component_val_t {213Bool(bool),214S8(i8),215U8(u8),216S16(i16),217U16(u16),218S32(i32),219U32(u32),220S64(i64),221U64(u64),222F32(f32),223F64(f64),224Char(u32),225String(wasm_name_t),226List(wasmtime_component_vallist_t),227Record(wasmtime_component_valrecord_t),228Tuple(wasmtime_component_valtuple_t),229Variant(wasmtime_component_valvariant_t),230Enum(wasm_name_t),231Option(Option<Box<Self>>),232Result(wasmtime_component_valresult_t),233Flags(wasmtime_component_valflags_t),234}235236impl Default for wasmtime_component_val_t {237fn default() -> Self {238Self::Bool(false)239}240}241242impl From<&wasmtime_component_val_t> for Val {243fn from(value: &wasmtime_component_val_t) -> Self {244match value {245wasmtime_component_val_t::Bool(x) => Val::Bool(*x),246wasmtime_component_val_t::S8(x) => Val::S8(*x),247wasmtime_component_val_t::U8(x) => Val::U8(*x),248wasmtime_component_val_t::S16(x) => Val::S16(*x),249wasmtime_component_val_t::U16(x) => Val::U16(*x),250wasmtime_component_val_t::S32(x) => Val::S32(*x),251wasmtime_component_val_t::U32(x) => Val::U32(*x),252wasmtime_component_val_t::S64(x) => Val::S64(*x),253wasmtime_component_val_t::U64(x) => Val::U64(*x),254wasmtime_component_val_t::F32(x) => Val::Float32(*x),255wasmtime_component_val_t::F64(x) => Val::Float64(*x),256wasmtime_component_val_t::Char(x) => Val::Char(char::from_u32(*x).unwrap()),257wasmtime_component_val_t::String(x) => {258Val::String(String::from_utf8(x.clone().take()).unwrap())259}260wasmtime_component_val_t::List(x) => Val::List(x.into()),261wasmtime_component_val_t::Record(x) => Val::Record(x.into()),262wasmtime_component_val_t::Tuple(x) => Val::Tuple(x.into()),263wasmtime_component_val_t::Variant(x) => {264let (a, b) = x.into();265Val::Variant(a, b)266}267wasmtime_component_val_t::Enum(x) => {268Val::Enum(String::from_utf8(x.clone().take()).unwrap())269}270wasmtime_component_val_t::Option(x) => {271Val::Option(x.as_ref().map(|x| Box::new(Val::from(x.as_ref()))))272}273wasmtime_component_val_t::Result(x) => Val::Result(x.into()),274wasmtime_component_val_t::Flags(x) => Val::Flags(x.into()),275}276}277}278279impl From<&Val> for wasmtime_component_val_t {280fn from(value: &Val) -> Self {281match value {282Val::Bool(x) => wasmtime_component_val_t::Bool(*x),283Val::S8(x) => wasmtime_component_val_t::S8(*x),284Val::U8(x) => wasmtime_component_val_t::U8(*x),285Val::S16(x) => wasmtime_component_val_t::S16(*x),286Val::U16(x) => wasmtime_component_val_t::U16(*x),287Val::S32(x) => wasmtime_component_val_t::S32(*x),288Val::U32(x) => wasmtime_component_val_t::U32(*x),289Val::S64(x) => wasmtime_component_val_t::S64(*x),290Val::U64(x) => wasmtime_component_val_t::U64(*x),291Val::Float32(x) => wasmtime_component_val_t::F32(*x),292Val::Float64(x) => wasmtime_component_val_t::F64(*x),293Val::Char(x) => wasmtime_component_val_t::Char(*x as _),294Val::String(x) => wasmtime_component_val_t::String(wasm_name_t::from_name(x.clone())),295Val::List(x) => wasmtime_component_val_t::List(x.as_slice().into()),296Val::Record(x) => wasmtime_component_val_t::Record(x.as_slice().into()),297Val::Tuple(x) => wasmtime_component_val_t::Tuple(x.as_slice().into()),298Val::Variant(discriminant, val) => {299wasmtime_component_val_t::Variant((discriminant, val).into())300}301Val::Enum(x) => wasmtime_component_val_t::Enum(wasm_name_t::from_name(x.clone())),302Val::Option(x) => wasmtime_component_val_t::Option(303x.as_ref()304.map(|x| Box::new(wasmtime_component_val_t::from(x.as_ref()))),305),306Val::Result(x) => wasmtime_component_val_t::Result(x.into()),307Val::Flags(x) => wasmtime_component_val_t::Flags(x.as_slice().into()),308Val::Resource(_resource_any) => todo!(),309Val::Future(_) => todo!(),310Val::Stream(_) => todo!(),311Val::ErrorContext(_) => todo!(),312}313}314}315316#[unsafe(no_mangle)]317pub unsafe extern "C" fn wasmtime_component_val_new() -> Box<wasmtime_component_val_t> {318Box::new(wasmtime_component_val_t::default())319}320321#[unsafe(no_mangle)]322pub unsafe extern "C" fn wasmtime_component_val_delete(value: *mut wasmtime_component_val_t) {323unsafe {324std::ptr::drop_in_place(value);325}326}327328329