1use crate::{
2 WasmtimeStoreContextMut, wasmtime_anyref_t, wasmtime_arrayref_t, wasmtime_structref_t,
3};
4use std::mem::MaybeUninit;
5use wasmtime::{ArrayRef, EqRef, I31, OwnedRooted, RootScope, StructRef};
6
7crate::anyref::ref_wrapper!({
8 wasmtime: EqRef,
9 capi: wasmtime_eqref_t,
10 clone: wasmtime_eqref_clone,
11 unroot: wasmtime_eqref_unroot,
12});
13
14#[unsafe(no_mangle)]
15pub unsafe extern "C" fn wasmtime_eqref_to_anyref(
16 eqref: Option<&wasmtime_eqref_t>,
17 out: &mut MaybeUninit<wasmtime_anyref_t>,
18) {
19 let anyref = eqref.and_then(|e| e.as_wasmtime()).map(|e| e.to_anyref());
20 crate::initialize(out, anyref.into());
21}
22
23#[unsafe(no_mangle)]
24pub extern "C" fn wasmtime_eqref_from_i31(
25 cx: WasmtimeStoreContextMut<'_>,
26 val: u32,
27 out: &mut MaybeUninit<wasmtime_eqref_t>,
28) {
29 let mut scope = RootScope::new(cx);
30 let eqref = EqRef::from_i31(&mut scope, I31::wrapping_u32(val));
31 let eqref = eqref.to_owned_rooted(&mut scope).expect("in scope");
32 crate::initialize(out, Some(eqref).into())
33}
34
35#[unsafe(no_mangle)]
36pub unsafe extern "C" fn wasmtime_eqref_is_i31(
37 cx: WasmtimeStoreContextMut<'_>,
38 eqref: Option<&wasmtime_eqref_t>,
39) -> bool {
40 match eqref.and_then(|e| e.as_wasmtime()) {
41 Some(eqref) => eqref.is_i31(&cx).expect("OwnedRooted always in scope"),
42 None => false,
43 }
44}
45
46#[unsafe(no_mangle)]
47pub unsafe extern "C" fn wasmtime_eqref_i31_get_u(
48 cx: WasmtimeStoreContextMut<'_>,
49 eqref: Option<&wasmtime_eqref_t>,
50 dst: &mut MaybeUninit<u32>,
51) -> bool {
52 let mut scope = RootScope::new(cx);
53 if let Some(eqref) = eqref.and_then(|e| e.as_wasmtime()) {
54 if let Some(val) = eqref.as_i31(&mut scope).expect("in scope") {
55 crate::initialize(dst, val.get_u32());
56 return true;
57 }
58 }
59 false
60}
61
62#[unsafe(no_mangle)]
63pub unsafe extern "C" fn wasmtime_eqref_i31_get_s(
64 cx: WasmtimeStoreContextMut<'_>,
65 eqref: Option<&wasmtime_eqref_t>,
66 dst: &mut MaybeUninit<i32>,
67) -> bool {
68 let mut scope = RootScope::new(cx);
69 if let Some(eqref) = eqref.and_then(|e| e.as_wasmtime()) {
70 if let Some(val) = eqref.as_i31(&mut scope).expect("in scope") {
71 crate::initialize(dst, val.get_i32());
72 return true;
73 }
74 }
75 false
76}
77
78#[unsafe(no_mangle)]
79pub unsafe extern "C" fn wasmtime_eqref_is_struct(
80 cx: WasmtimeStoreContextMut<'_>,
81 eqref: Option<&wasmtime_eqref_t>,
82) -> bool {
83 match eqref.and_then(|e| e.as_wasmtime()) {
84 Some(eqref) => eqref.is_struct(&cx).expect("OwnedRooted always in scope"),
85 None => false,
86 }
87}
88
89#[unsafe(no_mangle)]
90pub unsafe extern "C" fn wasmtime_eqref_as_struct(
91 mut cx: WasmtimeStoreContextMut<'_>,
92 eqref: Option<&wasmtime_eqref_t>,
93 out: &mut MaybeUninit<wasmtime_structref_t>,
94) -> bool {
95 if let Some(eqref) = eqref.and_then(|e| e.as_wasmtime()) {
96 let mut scope = RootScope::new(&mut cx);
97 let rooted = eqref.to_rooted(&mut scope);
98 if let Ok(Some(structref)) = rooted.as_struct(&scope) {
99 let owned = structref.to_owned_rooted(&mut scope).expect("in scope");
100 crate::initialize(out, Some(owned).into());
101 return true;
102 }
103 }
104 crate::initialize(out, None::<OwnedRooted<StructRef>>.into());
105 false
106}
107
108#[unsafe(no_mangle)]
109pub unsafe extern "C" fn wasmtime_eqref_is_array(
110 cx: WasmtimeStoreContextMut<'_>,
111 eqref: Option<&wasmtime_eqref_t>,
112) -> bool {
113 match eqref.and_then(|e| e.as_wasmtime()) {
114 Some(eqref) => eqref.is_array(&cx).expect("OwnedRooted always in scope"),
115 None => false,
116 }
117}
118
119#[unsafe(no_mangle)]
120pub unsafe extern "C" fn wasmtime_eqref_as_array(
121 mut cx: WasmtimeStoreContextMut<'_>,
122 eqref: Option<&wasmtime_eqref_t>,
123 out: &mut MaybeUninit<wasmtime_arrayref_t>,
124) -> bool {
125 if let Some(eqref) = eqref.and_then(|e| e.as_wasmtime()) {
126 let mut scope = RootScope::new(&mut cx);
127 let rooted = eqref.to_rooted(&mut scope);
128 if let Ok(Some(arrayref)) = rooted.as_array(&scope) {
129 let owned = arrayref.to_owned_rooted(&mut scope).expect("just created");
130 crate::initialize(out, Some(owned).into());
131 return true;
132 }
133 }
134 crate::initialize(out, None::<OwnedRooted<ArrayRef>>.into());
135 false
136}