wrpc_runtime_wasmtime/
lib.rs

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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#![allow(clippy::type_complexity)] // TODO: https://github.com/bytecodealliance/wrpc/issues/2

use core::any::Any;
use core::borrow::Borrow;
use core::fmt;
use core::future::Future;
use core::iter::zip;
use core::pin::pin;
use core::time::Duration;

use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;

use anyhow::{anyhow, bail, Context as _};
use bytes::{Bytes, BytesMut};
use futures::future::try_join_all;
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt as _};
use tokio_util::codec::Encoder;
use tracing::{debug, instrument, trace, warn};
use uuid::Uuid;
use wasmtime::component::{types, Func, Resource, ResourceAny, ResourceType, Type, Val};
use wasmtime::{AsContextMut, Engine};
use wasmtime_wasi::{IoView, WasiView};
use wrpc_transport::Invoke;

use crate::bindings::rpc::context::Context;
use crate::bindings::rpc::error::Error;
use crate::bindings::rpc::transport::{IncomingChannel, Invocation, OutgoingChannel};

pub mod bindings;
mod codec;
mod polyfill;
pub mod rpc;
mod serve;

pub use codec::*;
pub use polyfill::*;
pub use serve::*;

// this returns the RPC name for a wasmtime function name.
// Unfortunately, the [`types::ComponentFunc`] does not include the kind information and we want to
// avoid (re-)parsing the WIT here.
fn rpc_func_name(name: &str) -> &str {
    if let Some(name) = name.strip_prefix("[constructor]") {
        name
    } else if let Some(name) = name.strip_prefix("[static]") {
        name
    } else if let Some(name) = name.strip_prefix("[method]") {
        name
    } else {
        name
    }
}

fn rpc_result_type<T: Borrow<Type>>(
    host_resources: &HashMap<Box<str>, HashMap<Box<str>, (ResourceType, ResourceType)>>,
    results_ty: impl IntoIterator<Item = T>,
) -> Option<Option<Type>> {
    let rpc_err_ty = host_resources
        .get("wrpc:rpc/error@0.1.0")
        .and_then(|instance| instance.get("error"));
    let mut results_ty = results_ty.into_iter();
    match (
        rpc_err_ty,
        results_ty.next().as_ref().map(Borrow::borrow),
        results_ty.next(),
    ) {
        (Some((guest_rpc_err_ty, host_rpc_err_ty)), Some(Type::Result(result_ty)), None)
            if *host_rpc_err_ty == ResourceType::host::<Error>()
                && result_ty.err() == Some(Type::Own(*guest_rpc_err_ty)) =>
        {
            Some(result_ty.ok())
        }
        _ => None,
    }
}

pub struct RemoteResource(pub Bytes);

/// A table of shared resources exported by the component
#[derive(Debug, Default)]
pub struct SharedResourceTable(HashMap<Uuid, ResourceAny>);

pub trait WrpcView: IoView + Send {
    type Invoke: Invoke;

    /// Returns context to use for invocation
    fn context(&self) -> <Self::Invoke as Invoke>::Context;

    /// Returns an [Invoke] implementation used to satisfy polyfilled imports
    fn client(&self) -> &Self::Invoke;

    /// Returns a table of shared exported resources
    fn shared_resources(&mut self) -> &mut SharedResourceTable;

    /// Optional invocation timeout, component will trap if invocation is not finished within the
    /// returned [Duration]. If this method returns [None], then no timeout will be used.
    fn timeout(&self) -> Option<Duration> {
        None
    }
}

impl<T: WrpcView> WrpcView for &mut T {
    type Invoke = T::Invoke;

    fn context(&self) -> <Self::Invoke as Invoke>::Context {
        (**self).context()
    }

    fn client(&self) -> &Self::Invoke {
        (**self).client()
    }

    fn shared_resources(&mut self) -> &mut SharedResourceTable {
        (**self).shared_resources()
    }

    fn timeout(&self) -> Option<Duration> {
        (**self).timeout()
    }
}

pub trait WrpcViewExt: WrpcView {
    fn push_invocation(
        &mut self,
        invocation: impl Future<
                Output = anyhow::Result<(
                    <Self::Invoke as Invoke>::Outgoing,
                    <Self::Invoke as Invoke>::Incoming,
                )>,
            > + Send
            + 'static,
    ) -> anyhow::Result<Resource<Invocation>> {
        self.table()
            .push(Invocation::Future(Box::pin(async move {
                let res = invocation.await;
                Box::new(res) as Box<dyn Any + Send>
            })))
            .context("failed to push invocation to table")
    }

    fn get_invocation_result(
        &mut self,
        invocation: &Resource<Invocation>,
    ) -> anyhow::Result<
        Option<
            &Box<
                anyhow::Result<(
                    <Self::Invoke as Invoke>::Outgoing,
                    <Self::Invoke as Invoke>::Incoming,
                )>,
            >,
        >,
    > {
        let invocation = self
            .table()
            .get(invocation)
            .context("failed to get invocation from table")?;
        match invocation {
            Invocation::Future(..) => Ok(None),
            Invocation::Ready(res) => {
                let res = res.downcast_ref().context("invalid invocation type")?;
                Ok(Some(res))
            }
        }
    }

    fn delete_invocation(
        &mut self,
        invocation: Resource<Invocation>,
    ) -> anyhow::Result<
        impl Future<
            Output = anyhow::Result<(
                <Self::Invoke as Invoke>::Outgoing,
                <Self::Invoke as Invoke>::Incoming,
            )>,
        >,
    > {
        let invocation = self
            .table()
            .delete(invocation)
            .context("failed to delete invocation from table")?;
        Ok(async move {
            let res = match invocation {
                Invocation::Future(fut) => fut.await,
                Invocation::Ready(res) => res,
            };
            let res = res
                .downcast()
                .map_err(|_| anyhow!("invalid invocation type"))?;
            *res
        })
    }

    fn push_outgoing_channel(
        &mut self,
        outgoing: <Self::Invoke as Invoke>::Outgoing,
    ) -> anyhow::Result<Resource<OutgoingChannel>> {
        self.table()
            .push(OutgoingChannel(Arc::new(std::sync::RwLock::new(Box::new(
                outgoing,
            )))))
            .context("failed to push outgoing channel to table")
    }

    fn delete_outgoing_channel(
        &mut self,
        outgoing: Resource<OutgoingChannel>,
    ) -> anyhow::Result<<Self::Invoke as Invoke>::Outgoing> {
        let OutgoingChannel(outgoing) = self
            .table()
            .delete(outgoing)
            .context("failed to delete outgoing channel from table")?;
        let outgoing =
            Arc::into_inner(outgoing).context("outgoing channel has an active stream")?;
        let Ok(outgoing) = outgoing.into_inner() else {
            bail!("lock poisoned");
        };
        let outgoing = outgoing
            .downcast()
            .map_err(|_| anyhow!("invalid outgoing channel type"))?;
        Ok(*outgoing)
    }

    fn push_incoming_channel(
        &mut self,
        incoming: <Self::Invoke as Invoke>::Incoming,
    ) -> anyhow::Result<Resource<IncomingChannel>> {
        self.table()
            .push(IncomingChannel(Arc::new(std::sync::RwLock::new(Box::new(
                incoming,
            )))))
            .context("failed to push incoming channel to table")
    }

    fn delete_incoming_channel(
        &mut self,
        incoming: Resource<IncomingChannel>,
    ) -> anyhow::Result<<Self::Invoke as Invoke>::Incoming> {
        let IncomingChannel(incoming) = self
            .table()
            .delete(incoming)
            .context("failed to delete incoming channel from table")?;
        let incoming =
            Arc::into_inner(incoming).context("incoming channel has an active stream")?;
        let Ok(incoming) = incoming.into_inner() else {
            bail!("lock poisoned");
        };
        let incoming = incoming
            .downcast()
            .map_err(|_| anyhow!("invalid incoming channel type"))?;
        Ok(*incoming)
    }

    fn push_error(&mut self, error: Error) -> anyhow::Result<Resource<Error>> {
        self.table()
            .push(error)
            .context("failed to push error to table")
    }

    fn get_error(&mut self, error: &Resource<Error>) -> anyhow::Result<&Error> {
        let error = self
            .table()
            .get(error)
            .context("failed to get error from table")?;
        Ok(error)
    }

    fn get_error_mut(&mut self, error: &Resource<Error>) -> anyhow::Result<&mut Error> {
        let error = self
            .table()
            .get_mut(error)
            .context("failed to get error from table")?;
        Ok(error)
    }

    fn delete_error(&mut self, error: Resource<Error>) -> anyhow::Result<Error> {
        let error = self
            .table()
            .delete(error)
            .context("failed to delete error from table")?;
        Ok(error)
    }

    fn push_context(
        &mut self,
        cx: <Self::Invoke as Invoke>::Context,
    ) -> anyhow::Result<Resource<Context>>
    where
        <Self::Invoke as Invoke>::Context: 'static,
    {
        self.table()
            .push(Context(Box::new(cx)))
            .context("failed to push context to table")
    }

    fn delete_context(
        &mut self,
        cx: Resource<Context>,
    ) -> anyhow::Result<<Self::Invoke as Invoke>::Context>
    where
        <Self::Invoke as Invoke>::Context: 'static,
    {
        let Context(cx) = self
            .table()
            .delete(cx)
            .context("failed to delete context from table")?;
        let cx = cx.downcast().map_err(|_| anyhow!("invalid context type"))?;
        Ok(*cx)
    }
}

impl<T: WrpcView> WrpcViewExt for T {}

/// Error type returned by [call]
pub enum CallError {
    Decode(anyhow::Error),
    Encode(anyhow::Error),
    Table(anyhow::Error),
    Call(anyhow::Error),
    TypeMismatch(anyhow::Error),
    Write(anyhow::Error),
    Flush(anyhow::Error),
    Deferred(anyhow::Error),
    PostReturn(anyhow::Error),
    Guest(Error),
}

impl core::error::Error for CallError {}

impl fmt::Debug for CallError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CallError::Decode(error)
            | CallError::Encode(error)
            | CallError::Table(error)
            | CallError::Call(error)
            | CallError::TypeMismatch(error)
            | CallError::Write(error)
            | CallError::Flush(error)
            | CallError::Deferred(error)
            | CallError::PostReturn(error) => error.fmt(f),
            CallError::Guest(error) => error.fmt(f),
        }
    }
}

impl fmt::Display for CallError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CallError::Decode(error)
            | CallError::Encode(error)
            | CallError::Table(error)
            | CallError::Call(error)
            | CallError::TypeMismatch(error)
            | CallError::Write(error)
            | CallError::Flush(error)
            | CallError::Deferred(error)
            | CallError::PostReturn(error) => error.fmt(f),
            CallError::Guest(error) => error.fmt(f),
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub async fn call<C, I, O>(
    mut store: C,
    rx: I,
    mut tx: O,
    guest_resources: &[ResourceType],
    host_resources: &HashMap<Box<str>, HashMap<Box<str>, (ResourceType, ResourceType)>>,
    params_ty: impl ExactSizeIterator<Item = &Type>,
    results_ty: &[Type],
    func: Func,
) -> Result<(), CallError>
where
    I: AsyncRead + wrpc_transport::Index<I> + Send + Sync + Unpin + 'static,
    O: AsyncWrite + wrpc_transport::Index<O> + Send + Sync + Unpin + 'static,
    C: AsContextMut,
    C::Data: WasiView + WrpcView,
{
    let mut params = vec![Val::Bool(false); params_ty.len()];
    let mut rx = pin!(rx);
    for (i, (v, ty)) in zip(&mut params, params_ty).enumerate() {
        read_value(&mut store, &mut rx, guest_resources, v, ty, &[i])
            .await
            .with_context(|| format!("failed to decode parameter value {i}"))
            .map_err(CallError::Decode)?;
    }
    let mut results = vec![Val::Bool(false); results_ty.len()];
    func.call_async(&mut store, &params, &mut results)
        .await
        .context("failed to call function")
        .map_err(CallError::Call)?;

    let mut buf = BytesMut::default();
    let mut deferred = vec![];
    match (
        &rpc_result_type(host_resources, results_ty),
        results.as_slice(),
    ) {
        (None, results) => {
            for (i, (v, ty)) in zip(results, results_ty).enumerate() {
                let mut enc = ValEncoder::new(store.as_context_mut(), ty, guest_resources);
                enc.encode(v, &mut buf)
                    .with_context(|| format!("failed to encode result value {i}"))
                    .map_err(CallError::Encode)?;
                deferred.push(enc.deferred);
            }
        }
        // `result<_, rpc-eror>`
        (Some(None), [Val::Result(Ok(None))]) => {}
        // `result<T, rpc-eror>`
        (Some(Some(ty)), [Val::Result(Ok(Some(v)))]) => {
            let mut enc = ValEncoder::new(store.as_context_mut(), ty, guest_resources);
            enc.encode(v, &mut buf)
                .context("failed to encode result value 0")
                .map_err(CallError::Encode)?;
            deferred.push(enc.deferred);
        }
        (Some(..), [Val::Result(Err(Some(err)))]) => {
            let Val::Resource(err) = &**err else {
                return Err(CallError::TypeMismatch(anyhow!(
                    "RPC result error value is not a resource"
                )));
            };
            let mut store = store.as_context_mut();
            let err = err
                .try_into_resource(&mut store)
                .context("RPC result error resource type mismatch")
                .map_err(CallError::TypeMismatch)?;
            let err = store
                .data_mut()
                .delete_error(err)
                .map_err(CallError::Table)?;
            return Err(CallError::Guest(err));
        }
        _ => return Err(CallError::TypeMismatch(anyhow!("RPC result type mismatch"))),
    }

    debug!("transmitting results");
    tx.write_all(&buf)
        .await
        .context("failed to transmit results")
        .map_err(CallError::Write)?;
    tx.flush()
        .await
        .context("failed to flush outgoing stream")
        .map_err(CallError::Flush)?;
    if let Err(err) = tx.shutdown().await {
        trace!(?err, "failed to shutdown outgoing stream");
    }
    try_join_all(
        zip(0.., deferred)
            .filter_map(|(i, f)| f.map(|f| (tx.index(&[i]), f)))
            .map(|(w, f)| async move {
                let w = w?;
                f(w).await
            }),
    )
    .await
    .map_err(CallError::Deferred)?;
    func.post_return_async(&mut store)
        .await
        .context("failed to perform post-return cleanup")
        .map_err(CallError::PostReturn)?;
    Ok(())
}

/// Recursively iterates the component item type and collects all exported resource types
#[instrument(level = "debug", skip_all)]
pub fn collect_item_resource_exports(
    engine: &Engine,
    ty: types::ComponentItem,
    resources: &mut impl Extend<types::ResourceType>,
) {
    match ty {
        types::ComponentItem::ComponentFunc(_)
        | types::ComponentItem::CoreFunc(_)
        | types::ComponentItem::Module(_)
        | types::ComponentItem::Type(_) => {}
        types::ComponentItem::Component(ty) => {
            collect_component_resource_exports(engine, &ty, resources)
        }

        types::ComponentItem::ComponentInstance(ty) => {
            collect_instance_resource_exports(engine, &ty, resources)
        }
        types::ComponentItem::Resource(ty) => {
            debug!(?ty, "collect resource export");
            resources.extend([ty])
        }
    }
}

/// Recursively iterates the instance type and collects all exported resource types
#[instrument(level = "debug", skip_all)]
pub fn collect_instance_resource_exports(
    engine: &Engine,
    ty: &types::ComponentInstance,
    resources: &mut impl Extend<types::ResourceType>,
) {
    for (name, ty) in ty.exports(engine) {
        trace!(name, ?ty, "collect instance item resource exports");
        collect_item_resource_exports(engine, ty, resources);
    }
}

/// Recursively iterates the component type and collects all exported resource types
#[instrument(level = "debug", skip_all)]
pub fn collect_component_resource_exports(
    engine: &Engine,
    ty: &types::Component,
    resources: &mut impl Extend<types::ResourceType>,
) {
    for (name, ty) in ty.exports(engine) {
        trace!(name, ?ty, "collect component item resource exports");
        collect_item_resource_exports(engine, ty, resources);
    }
}

/// Iterates the component type and collects all imported resource types
#[instrument(level = "debug", skip_all)]
pub fn collect_component_resource_imports(
    engine: &Engine,
    ty: &types::Component,
    resources: &mut BTreeMap<Box<str>, HashMap<Box<str>, types::ResourceType>>,
) {
    for (name, ty) in ty.imports(engine) {
        match ty {
            types::ComponentItem::ComponentFunc(..)
            | types::ComponentItem::CoreFunc(..)
            | types::ComponentItem::Module(..)
            | types::ComponentItem::Type(..)
            | types::ComponentItem::Component(..) => {}
            types::ComponentItem::ComponentInstance(ty) => {
                let instance = name;
                for (name, ty) in ty.exports(engine) {
                    if let types::ComponentItem::Resource(ty) = ty {
                        debug!(instance, name, ?ty, "collect instance resource import");
                        if let Some(resources) = resources.get_mut(instance) {
                            resources.insert(name.into(), ty);
                        } else {
                            resources.insert(instance.into(), HashMap::from([(name.into(), ty)]));
                        }
                    }
                }
            }
            types::ComponentItem::Resource(ty) => {
                debug!(name, "collect component resource import");
                if let Some(resources) = resources.get_mut("") {
                    resources.insert(name.into(), ty);
                } else {
                    resources.insert("".into(), HashMap::from([(name.into(), ty)]));
                }
            }
        }
    }
}