pyodide_webassembly_runtime_layer/
func.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
use std::{
    any::TypeId,
    marker::PhantomData,
    sync::{Arc, Weak},
};

use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyTuple, PyTypeInfo};
use pyo3_error::PyErrChain;
use wasm_runtime_layer::{
    backend::{AsContext, AsContextMut, Value, WasmFunc, WasmStoreContext},
    FuncType,
};
use wobbly::sync::Wobbly;

use crate::{
    conversion::{py_to_js_proxy, ToPy, ValueExt},
    store::StoreContextMut,
    Engine,
};

/// A bound function, which may be an export from a WASM [`Instance`] or a host
/// function.
///
/// [`Instance`]: crate::instance::Instance
#[derive(Debug)]
pub struct Func {
    /// The inner function
    func: Py<PyAny>,
    /// The function signature
    ty: FuncType,
    /// The user state type of the context
    user_state: Option<TypeId>,
}

impl Clone for Func {
    fn clone(&self) -> Self {
        Python::with_gil(|py| Self {
            func: self.func.clone_ref(py),
            ty: self.ty.clone(),
            user_state: self.user_state,
        })
    }
}

impl WasmFunc<Engine> for Func {
    fn new<T>(
        mut ctx: impl AsContextMut<Engine, UserState = T>,
        ty: FuncType,
        func: impl 'static
            + Send
            + Sync
            + Fn(StoreContextMut<T>, &[Value<Engine>], &mut [Value<Engine>]) -> anyhow::Result<()>,
    ) -> Self {
        Python::with_gil(|py| -> Result<Self, PyErr> {
            #[cfg(feature = "tracing")]
            tracing::debug!("Func::new");

            let mut store: StoreContextMut<T> = ctx.as_context_mut();

            let weak_store = store.as_weak_proof();

            let user_state = non_static_type_id(store.data());
            let ty_clone = ty.clone();

            let func = Arc::new(move |args: Bound<PyTuple>| -> Result<Py<PyAny>, PyErr> {
                let py = args.py();

                let Some(mut strong_store) = Weak::upgrade(&weak_store) else {
                    return Err(PyRuntimeError::new_err(
                        "host func called after free of its associated store",
                    ));
                };

                // Safety:
                //
                // - The proof is constructed from a mutable store context
                // - Calling a host function (from the host or from WASM) provides that call
                //   with a mutable reborrow of the store context
                let store = unsafe { StoreContextMut::from_proof_unchecked(&mut strong_store) };

                let ty = &ty_clone;

                let args = ty
                    .params()
                    .iter()
                    .zip(args.iter())
                    .map(|(ty, arg)| Value::from_py_typed(arg, *ty))
                    .collect::<Result<Vec<_>, _>>()?;
                let mut results = vec![Value::I32(0); ty.results().len()];

                #[cfg(feature = "tracing")]
                let _span = tracing::debug_span!("call_host", ?args, ?ty).entered();

                match func(store, &args, &mut results) {
                    Ok(()) => {
                        #[cfg(feature = "tracing")]
                        tracing::debug!(?results, "result");
                    },
                    Err(err) => {
                        #[cfg(feature = "tracing")]
                        tracing::error!("{err:?}");
                        return Err(PyErrChain::pyerr_from_err(py, err));
                    },
                }

                let results = match results.as_slice() {
                    [] => py.None(),
                    [res] => res.to_py(py),
                    results => PyTuple::new(py, results.iter().map(|res| res.to_py(py)))?
                        .into_any()
                        .unbind(),
                };

                Ok(results)
            });

            let func = Bound::new(
                py,
                PyHostFunc {
                    func: store.register_host_func(func),
                    #[cfg(feature = "tracing")]
                    ty: ty.clone(),
                },
            )?;
            let func = py_to_js_proxy(func)?;

            Ok(Self {
                func: func.unbind(),
                ty,
                user_state: Some(user_state),
            })
        })
        .expect("Func::new should not fail")
    }

    fn ty(&self, _ctx: impl AsContext<Engine>) -> FuncType {
        self.ty.clone()
    }

    fn call<T>(
        &self,
        mut ctx: impl AsContextMut<Engine>,
        args: &[Value<Engine>],
        results: &mut [Value<Engine>],
    ) -> anyhow::Result<()> {
        Python::with_gil(|py| {
            let store: StoreContextMut<_> = ctx.as_context_mut();

            if let Some(user_state) = self.user_state {
                assert_eq!(user_state, non_static_type_id(store.data()));
            }

            #[cfg(feature = "tracing")]
            let _span = tracing::debug_span!("call_guest", ?args, ?self.ty).entered();

            // https://webassembly.github.io/spec/js-api/#exported-function-exotic-objects
            assert_eq!(self.ty.params().len(), args.len());
            assert_eq!(self.ty.results().len(), results.len());

            let args = args.iter().map(|arg| arg.to_py(py));
            let args = PyTuple::new(py, args)?;

            let res = self.func.bind(py).call1(args)?;

            #[cfg(feature = "tracing")]
            tracing::debug!(%res, ?self.ty);

            match (self.ty.results(), results) {
                ([], []) => (),
                ([ty], [result]) => *result = Value::from_py_typed(res, *ty)?,
                (tys, results) => {
                    let res: Bound<PyTuple> = PyTuple::type_object(py).call1((res,))?.extract()?;

                    // https://webassembly.github.io/spec/js-api/#exported-function-exotic-objects
                    assert_eq!(tys.len(), res.len());

                    for ((ty, result), value) in self
                        .ty
                        .results()
                        .iter()
                        .zip(results.iter_mut())
                        .zip(res.iter())
                    {
                        *result = Value::from_py_typed(value, *ty)?;
                    }
                },
            }

            Ok(())
        })
    }
}

impl ToPy for Func {
    fn to_py(&self, py: Python) -> Py<PyAny> {
        self.func.clone_ref(py)
    }
}

impl Func {
    /// Creates a new function from a Python value
    pub(crate) fn from_exported_function(func: Bound<PyAny>, ty: FuncType) -> anyhow::Result<Self> {
        if !func.is_callable() {
            anyhow::bail!("expected WebAssembly.Function but found {func:?} which is not callable");
        }

        #[cfg(feature = "tracing")]
        tracing::debug!(%func, ?ty, "Func::from_exported_function");

        Ok(Self {
            func: func.unbind(),
            ty,
            user_state: None,
        })
    }
}

pub type PyHostFuncFn = dyn 'static + Send + Sync + Fn(Bound<PyTuple>) -> Result<Py<PyAny>, PyErr>;

#[pyclass(frozen)]
struct PyHostFunc {
    func: Wobbly<PyHostFuncFn>,
    #[cfg(feature = "tracing")]
    ty: FuncType,
}

#[pymethods]
impl PyHostFunc {
    #[pyo3(signature = (*args))]
    fn __call__(&self, args: Bound<PyTuple>) -> Result<Py<PyAny>, PyErr> {
        #[cfg(feature = "tracing")]
        let _span = tracing::debug_span!("call_trampoline", ?self.ty, args = %args).entered();

        let Some(func) = self.func.upgrade() else {
            return Err(PyRuntimeError::new_err(
                "weak host func called after free of its associated store",
            ));
        };

        func(args)
    }
}

// Courtesy of David Tolnay:
// https://github.com/rust-lang/rust/issues/41875#issuecomment-317292888
fn non_static_type_id<T: ?Sized>(_x: &T) -> TypeId {
    trait NonStaticAny {
        fn get_type_id(&self) -> TypeId
        where
            Self: 'static;
    }

    impl<T: ?Sized> NonStaticAny for PhantomData<T> {
        fn get_type_id(&self) -> TypeId
        where
            Self: 'static,
        {
            TypeId::of::<T>()
        }
    }

    let phantom_data = PhantomData::<T>;
    NonStaticAny::get_type_id(unsafe {
        core::mem::transmute::<&dyn NonStaticAny, &(dyn NonStaticAny + 'static)>(&phantom_data)
    })
}