pyodide_webassembly_runtime_layer/
externref.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
use std::{any::Any, sync::Arc};

use pyo3::prelude::*;
use wasm_runtime_layer::backend::{AsContextMut, WasmExternRef};

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

/// Extern host reference type.
#[derive(Debug)]
pub struct ExternRef {
    /// The inner extern ref object, for host access, optional
    host: Option<Arc<AnyExternRef>>,
    /// The inner extern ref object, for guest access, opaque
    guest: Py<PyAny>,
}

impl Clone for ExternRef {
    fn clone(&self) -> Self {
        Python::with_gil(|py| Self {
            host: self.host.clone(),
            guest: self.guest.clone_ref(py),
        })
    }
}

impl WasmExternRef<Engine> for ExternRef {
    fn new<T: 'static + Send + Sync>(_ctx: impl AsContextMut<Engine>, object: T) -> Self {
        Python::with_gil(|py| -> Result<Self, PyErr> {
            let object: Arc<AnyExternRef> = Arc::new(object);

            let guest = Bound::new(
                py,
                PyExternRef {
                    object: Arc::clone(&object),
                },
            )?;
            let guest = py_to_js_proxy(guest)?;

            Ok(Self {
                host: Some(object),
                guest: guest.unbind(),
            })
        })
        .expect("ExternRef::new should not fail")
    }

    fn downcast<'a, 's: 'a, T: 'static, S: 's>(
        &'a self,
        _ctx: StoreContext<'s, S>,
    ) -> anyhow::Result<&'a T> {
        // Check if we have a host-accessible non-opaque reference to the data
        let Some(object) = self.host.as_ref() else {
            anyhow::bail!("extern ref is from a different source");
        };

        let Some(object) = object.downcast_ref() else {
            anyhow::bail!("incorrect extern ref type");
        };

        Ok(object)
    }
}

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

impl ExternRef {
    /// Creates a new extern ref from a Python value
    pub(crate) fn from_exported_externref(object: Bound<PyAny>) -> Self {
        // Check if this ExternRef comes from this source,
        //  if not, return an opaque ExternRef
        let Ok(host): Result<Bound<PyExternRef>, _> = object.extract() else {
            return Self {
                host: None,
                guest: object.unbind(),
            };
        };

        let host = Arc::clone(&host.get().object);

        Self {
            host: Some(host),
            guest: object.unbind(),
        }
    }
}

type AnyExternRef = dyn 'static + Any + Send + Sync;

#[pyclass(frozen)]
struct PyExternRef {
    /// The inner extern ref data
    object: Arc<AnyExternRef>,
}