1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use core::iter::zip;

use anyhow::{bail, ensure, Context as _};
use futures::stream;
use tracing::instrument;
use wasmtime::component::types::{Case, Field};
use wasmtime::component::{
    Enum, Flags, List, OptionVal, Record, ResourceType, ResultVal, Tuple, Type, Val, Variant,
};
use wasmtime::AsContextMut;
use wasmtime_wasi::preview2::{InputStream, Pollable, WasiView};

#[instrument(level = "trace", skip(store))]
pub fn to_wrpc_value<T: WasiView>(
    mut store: impl AsContextMut<Data = T>,
    val: &Val,
) -> anyhow::Result<wrpc_transport::Value> {
    let mut store = store.as_context_mut();
    match val {
        Val::Bool(val) => Ok(wrpc_transport::Value::Bool(*val)),
        Val::S8(val) => Ok(wrpc_transport::Value::S8(*val)),
        Val::U8(val) => Ok(wrpc_transport::Value::U8(*val)),
        Val::S16(val) => Ok(wrpc_transport::Value::S16(*val)),
        Val::U16(val) => Ok(wrpc_transport::Value::U16(*val)),
        Val::S32(val) => Ok(wrpc_transport::Value::S32(*val)),
        Val::U32(val) => Ok(wrpc_transport::Value::U32(*val)),
        Val::S64(val) => Ok(wrpc_transport::Value::S64(*val)),
        Val::U64(val) => Ok(wrpc_transport::Value::U64(*val)),
        Val::Float32(val) => Ok(wrpc_transport::Value::Float32(*val)),
        Val::Float64(val) => Ok(wrpc_transport::Value::Float64(*val)),
        Val::Char(val) => Ok(wrpc_transport::Value::Char(*val)),
        Val::String(val) => Ok(wrpc_transport::Value::String(val.to_string())),
        Val::List(val) => val
            .iter()
            .map(|val| to_wrpc_value(&mut store, val))
            .collect::<anyhow::Result<_>>()
            .map(wrpc_transport::Value::List),
        Val::Record(val) => val
            .fields()
            .map(|(_, val)| val)
            .map(|val| to_wrpc_value(&mut store, val))
            .collect::<anyhow::Result<_>>()
            .map(wrpc_transport::Value::Record),
        Val::Tuple(val) => val
            .values()
            .iter()
            .map(|val| to_wrpc_value(&mut store, val))
            .collect::<anyhow::Result<_>>()
            .map(wrpc_transport::Value::Tuple),
        Val::Variant(val) => {
            let discriminant = zip(0.., val.ty().cases())
                .find_map(|(i, Case { name, .. })| (name == val.discriminant()).then_some(i))
                .context("unknown variant discriminant")?;
            let nested = val
                .payload()
                .map(|val| to_wrpc_value(store, val))
                .transpose()?
                .map(Box::new);
            Ok(wrpc_transport::Value::Variant {
                discriminant,
                nested,
            })
        }
        Val::Enum(val) => zip(0.., val.ty().names())
            .find_map(|(i, name)| (name == val.discriminant()).then_some(i))
            .context("unknown enum discriminant")
            .map(wrpc_transport::Value::Enum),
        Val::Option(val) => {
            let val = val
                .value()
                .map(|val| to_wrpc_value(store, val))
                .transpose()?
                .map(Box::new);
            Ok(wrpc_transport::Value::Option(val))
        }
        Val::Result(val) => {
            let val = match val.value() {
                Ok(val) => {
                    let val = val
                        .map(|val| to_wrpc_value(store, val))
                        .transpose()?
                        .map(Box::new);
                    Ok(val)
                }
                Err(val) => {
                    let val = val
                        .map(|val| to_wrpc_value(store, val))
                        .transpose()?
                        .map(Box::new);
                    Err(val)
                }
            };
            Ok(wrpc_transport::Value::Result(val))
        }
        Val::Flags(val) => {
            let mut v = 0;
            for name in val.flags() {
                let i = zip(0.., val.ty().names())
                    .find_map(|(i, flag_name)| (name == flag_name).then_some(i))
                    .context("unknown flag")?;
                ensure!(
                    i < 64,
                    "flag discriminants over 64 currently cannot be represented"
                );
                v |= 1 << i
            }
            Ok(wrpc_transport::Value::Flags(v))
        }
        Val::Resource(resource) => {
            let ty = resource.ty();
            if ty == ResourceType::host::<InputStream>() {
                let _stream = resource
                    .try_into_resource::<InputStream>(&mut store)
                    .context("failed to downcast `wasi:io/input-stream`")?;
                //if stream.owned() {
                //    store
                //        .data_mut()
                //        .table()
                //        .delete(stream)
                //        .context("failed to delete input stream")?;
                //} else {
                //    store
                //        .data_mut()
                //        .table()
                //        .get_mut(&stream)
                //        .context("failed to get input stream")?;
                //};
                Ok(wrpc_transport::Value::Stream(Box::pin(stream::once(
                    async { bail!("`wasi:io/input-stream` not supported yet") },
                ))))
            } else if ty == ResourceType::host::<Pollable>() {
                let _pollable = resource
                    .try_into_resource::<Pollable>(&mut store)
                    .context("failed to downcast `wasi:io/pollable")?;
                //if pollable.owned() {
                //    store
                //        .data_mut()
                //        .table()
                //        .delete(pollable)
                //        .context("failed to delete pollable")?;
                //} else {
                //    store
                //        .data_mut()
                //        .table()
                //        .get_mut(&pollable)
                //        .context("failed to get pollable")?;
                //}
                Ok(wrpc_transport::Value::Future(Box::pin(async {
                    bail!("`wasi:io/pollable` not supported yet")
                })))
            } else {
                bail!("resources not supported yet")
            }
        }
    }
}

#[instrument(level = "trace", skip(val))]
pub fn from_wrpc_value(val: wrpc_transport::Value, ty: &Type) -> anyhow::Result<Val> {
    match (val, ty) {
        (wrpc_transport::Value::Bool(v), Type::Bool) => Ok(Val::Bool(v)),
        (wrpc_transport::Value::U8(v), Type::U8) => Ok(Val::U8(v)),
        (wrpc_transport::Value::U16(v), Type::U16) => Ok(Val::U16(v)),
        (wrpc_transport::Value::U32(v), Type::U32) => Ok(Val::U32(v)),
        (wrpc_transport::Value::U64(v), Type::U64) => Ok(Val::U64(v)),
        (wrpc_transport::Value::S8(v), Type::S8) => Ok(Val::S8(v)),
        (wrpc_transport::Value::S16(v), Type::S16) => Ok(Val::S16(v)),
        (wrpc_transport::Value::S32(v), Type::S32) => Ok(Val::S32(v)),
        (wrpc_transport::Value::S64(v), Type::S64) => Ok(Val::S64(v)),
        (wrpc_transport::Value::Float32(v), Type::Float32) => Ok(Val::Float32(v)),
        (wrpc_transport::Value::Float64(v), Type::Float64) => Ok(Val::Float64(v)),
        (wrpc_transport::Value::Char(v), Type::Char) => Ok(Val::Char(v)),
        (wrpc_transport::Value::String(v), Type::String) => Ok(Val::String(v.into())),
        (wrpc_transport::Value::List(vs), Type::List(ty)) => {
            let mut w_vs = Vec::with_capacity(vs.len());
            let el_ty = ty.ty();
            for v in vs {
                let v = from_wrpc_value(v, &el_ty).context("failed to convert list element")?;
                w_vs.push(v);
            }
            List::new(ty, w_vs.into()).map(Val::List)
        }
        (wrpc_transport::Value::Record(vs), Type::Record(ty)) => {
            let mut w_vs = Vec::with_capacity(vs.len());
            for (v, Field { name, ty }) in zip(vs, ty.fields()) {
                let v = from_wrpc_value(v, &ty).context("failed to convert record field")?;
                w_vs.push((name, v));
            }
            Record::new(ty, w_vs).map(Val::Record)
        }
        (wrpc_transport::Value::Tuple(vs), Type::Tuple(ty)) => {
            let mut w_vs = Vec::with_capacity(vs.len());
            for (v, ty) in zip(vs, ty.types()) {
                let v = from_wrpc_value(v, &ty).context("failed to convert tuple element")?;
                w_vs.push(v);
            }
            Tuple::new(ty, w_vs.into()).map(Val::Tuple)
        }
        (
            wrpc_transport::Value::Variant {
                discriminant,
                nested,
            },
            Type::Variant(ty),
        ) => {
            let discriminant = discriminant
                .try_into()
                .context("discriminant does not fit in usize")?;
            let Case { name, ty: case_ty } = ty
                .cases()
                .nth(discriminant)
                .context("variant discriminant not found")?;
            let v = if let Some(case_ty) = case_ty {
                let v = nested.context("nested value missing")?;
                let v = from_wrpc_value(*v, &case_ty).context("failed to convert variant value")?;
                Some(v)
            } else {
                None
            };
            Variant::new(ty, name, v).map(Val::Variant)
        }
        (wrpc_transport::Value::Enum(discriminant), Type::Enum(ty)) => {
            let discriminant = discriminant
                .try_into()
                .context("discriminant does not fit in usize")?;
            let name = ty
                .names()
                .nth(discriminant)
                .context("enum discriminant not found")?;
            Enum::new(ty, name).map(Val::Enum)
        }
        (wrpc_transport::Value::Option(v), Type::Option(ty)) => {
            let v = if let Some(v) = v {
                let v = from_wrpc_value(*v, &ty.ty()).context("failed to convert option value")?;
                Some(v)
            } else {
                None
            };
            OptionVal::new(ty, v).map(Val::Option)
        }
        (wrpc_transport::Value::Result(v), Type::Result(ty)) => {
            let v = match v {
                Ok(None) => Ok(None),
                Ok(Some(v)) => {
                    let ty = ty.ok().context("`result::ok` type missing")?;
                    let v =
                        from_wrpc_value(*v, &ty).context("failed to convert `result::ok` value")?;
                    Ok(Some(v))
                }
                Err(None) => Err(None),
                Err(Some(v)) => {
                    let ty = ty.err().context("`result::err` type missing")?;
                    let v = from_wrpc_value(*v, &ty)
                        .context("failed to convert `result::err` value")?;
                    Err(Some(v))
                }
            };
            ResultVal::new(ty, v).map(Val::Result)
        }
        (wrpc_transport::Value::Flags(v), Type::Flags(ty)) => {
            // NOTE: Currently flags are limited to 64
            let mut names = Vec::with_capacity(64);
            for (i, name) in zip(0..64, ty.names()) {
                if v & (1 << i) != 0 {
                    names.push(name)
                }
            }
            Flags::new(ty, &names).map(Val::Flags)
        }
        (wrpc_transport::Value::Future(_v), Type::Own(ty) | Type::Borrow(ty)) => {
            if *ty == ResourceType::host::<Pollable>() {
                // TODO: Implement once https://github.com/bytecodealliance/wasmtime/issues/7714
                // is addressed
                bail!("`wasi:io/pollable` not supported yet")
            } else {
                // TODO: Implement in preview3 or via a wasmCloud-specific interface
                bail!("dynamically-typed futures not supported yet")
            }
        }
        (wrpc_transport::Value::Stream(_v), Type::Own(ty) | Type::Borrow(ty)) => {
            if *ty == ResourceType::host::<InputStream>() {
                // TODO: Implement once https://github.com/bytecodealliance/wasmtime/issues/7714
                // is addressed
                bail!("`wasi:io/input-stream` not supported yet")
            } else {
                // TODO: Implement in preview3 or via a wasmCloud-specific interface
                bail!("dynamically-typed streams not supported yet")
            }
        }
        (wrpc_transport::Value::String(_), Type::Own(_ty) | Type::Borrow(_ty)) => {
            // TODO: Implement guest resource handling
            bail!("resources not supported yet")
        }
        _ => bail!("type mismatch"),
    }
}