wasmtime_c_api/
structref.rs1use crate::{WasmtimeStoreContextMut, wasmtime_anyref_t, wasmtime_eqref_t, wasmtime_struct_type_t};
2use std::mem::MaybeUninit;
3use wasmtime::{OwnedRooted, RootScope, StructRef, StructRefPre, Val};
4
5crate::anyref::ref_wrapper!({
6 wasmtime: StructRef,
7 capi: wasmtime_structref_t,
8 clone: wasmtime_structref_clone,
9 unroot: wasmtime_structref_unroot,
10});
11
12pub struct wasmtime_struct_ref_pre_t {
13 pre: StructRefPre,
14}
15wasmtime_c_api_macros::declare_own!(wasmtime_struct_ref_pre_t);
16
17#[unsafe(no_mangle)]
18pub extern "C" fn wasmtime_struct_ref_pre_new(
19 cx: WasmtimeStoreContextMut<'_>,
20 ty: &wasmtime_struct_type_t,
21) -> Box<wasmtime_struct_ref_pre_t> {
22 let pre = StructRefPre::new(cx, ty.ty.clone());
23 Box::new(wasmtime_struct_ref_pre_t { pre })
24}
25
26#[unsafe(no_mangle)]
27pub unsafe extern "C" fn wasmtime_structref_new(
28 mut cx: WasmtimeStoreContextMut<'_>,
29 pre: &wasmtime_struct_ref_pre_t,
30 fields: *const crate::wasmtime_val_t,
31 nfields: usize,
32 out: &mut MaybeUninit<wasmtime_structref_t>,
33) -> Option<Box<crate::wasmtime_error_t>> {
34 let c_fields = crate::slice_from_raw_parts(fields, nfields);
35 let mut scope = RootScope::new(&mut cx);
36 let vals: Vec<Val> = c_fields.iter().map(|v| v.to_val(&mut scope)).collect();
37 match StructRef::new(&mut scope, &pre.pre, &vals) {
38 Ok(structref) => {
39 let owned = structref
40 .to_owned_rooted(&mut scope)
41 .expect("just allocated");
42 crate::initialize(out, Some(owned).into());
43 None
44 }
45 Err(e) => {
46 crate::initialize(out, None::<OwnedRooted<StructRef>>.into());
47 Some(Box::new(e.into()))
48 }
49 }
50}
51
52#[unsafe(no_mangle)]
53pub unsafe extern "C" fn wasmtime_structref_to_anyref(
54 structref: Option<&wasmtime_structref_t>,
55 out: &mut MaybeUninit<wasmtime_anyref_t>,
56) {
57 let anyref = structref
58 .and_then(|s| s.as_wasmtime())
59 .map(|s| s.to_anyref());
60 crate::initialize(out, anyref.into());
61}
62
63#[unsafe(no_mangle)]
64pub unsafe extern "C" fn wasmtime_structref_to_eqref(
65 structref: Option<&wasmtime_structref_t>,
66 out: &mut MaybeUninit<wasmtime_eqref_t>,
67) {
68 let eqref = structref
69 .and_then(|s| s.as_wasmtime())
70 .map(|s| s.to_eqref());
71 crate::initialize(out, eqref.into());
72}
73
74#[unsafe(no_mangle)]
75pub unsafe extern "C" fn wasmtime_structref_field(
76 mut cx: WasmtimeStoreContextMut<'_>,
77 structref: Option<&wasmtime_structref_t>,
78 index: usize,
79 out: &mut MaybeUninit<crate::wasmtime_val_t>,
80) -> Option<Box<crate::wasmtime_error_t>> {
81 let structref = structref
82 .and_then(|s| s.as_wasmtime())
83 .expect("non-null structref required");
84 let mut scope = RootScope::new(&mut cx);
85 let rooted = structref.to_rooted(&mut scope);
86 match rooted.field(&mut scope, index) {
87 Ok(val) => {
88 let c_val = crate::wasmtime_val_t::from_val(&mut scope, val);
89 crate::initialize(out, c_val);
90 None
91 }
92 Err(e) => Some(Box::new(e.into())),
93 }
94}
95
96#[unsafe(no_mangle)]
97pub unsafe extern "C" fn wasmtime_structref_set_field(
98 mut cx: WasmtimeStoreContextMut<'_>,
99 structref: Option<&wasmtime_structref_t>,
100 index: usize,
101 val: &crate::wasmtime_val_t,
102) -> Option<Box<crate::wasmtime_error_t>> {
103 let structref = structref
104 .and_then(|s| s.as_wasmtime())
105 .expect("non-null structref required");
106 let mut scope = RootScope::new(&mut cx);
107 let rooted = structref.to_rooted(&mut scope);
108 let rust_val = val.to_val(&mut scope);
109 match rooted.set_field(&mut scope, index, rust_val) {
110 Ok(()) => None,
111 Err(e) => Some(Box::new(e.into())),
112 }
113}