wasmtime_c_api/
arrayref.rs1use crate::{WasmtimeStoreContextMut, wasmtime_anyref_t, wasmtime_array_type_t, wasmtime_eqref_t};
2use std::mem::MaybeUninit;
3use wasmtime::{ArrayRef, ArrayRefPre, OwnedRooted, RootScope};
4
5crate::anyref::ref_wrapper!({
6 wasmtime: ArrayRef,
7 capi: wasmtime_arrayref_t,
8 clone: wasmtime_arrayref_clone,
9 unroot: wasmtime_arrayref_unroot,
10});
11
12pub struct wasmtime_array_ref_pre_t {
13 pre: ArrayRefPre,
14}
15wasmtime_c_api_macros::declare_own!(wasmtime_array_ref_pre_t);
16
17#[unsafe(no_mangle)]
18pub extern "C" fn wasmtime_array_ref_pre_new(
19 cx: WasmtimeStoreContextMut<'_>,
20 ty: &wasmtime_array_type_t,
21) -> Box<wasmtime_array_ref_pre_t> {
22 let pre = ArrayRefPre::new(cx, ty.ty.clone());
23 Box::new(wasmtime_array_ref_pre_t { pre })
24}
25
26#[unsafe(no_mangle)]
27pub unsafe extern "C" fn wasmtime_arrayref_new(
28 mut cx: WasmtimeStoreContextMut<'_>,
29 pre: &wasmtime_array_ref_pre_t,
30 elem: &crate::wasmtime_val_t,
31 len: u32,
32 out: &mut MaybeUninit<wasmtime_arrayref_t>,
33) -> Option<Box<crate::wasmtime_error_t>> {
34 let mut scope = RootScope::new(&mut cx);
35 let val = elem.to_val(&mut scope);
36 match ArrayRef::new(&mut scope, &pre.pre, &val, len) {
37 Ok(arrayref) => {
38 let owned = arrayref
39 .to_owned_rooted(&mut scope)
40 .expect("just allocated");
41 crate::initialize(out, Some(owned).into());
42 None
43 }
44 Err(e) => {
45 crate::initialize(out, None::<OwnedRooted<ArrayRef>>.into());
46 Some(Box::new(e.into()))
47 }
48 }
49}
50
51#[unsafe(no_mangle)]
52pub unsafe extern "C" fn wasmtime_arrayref_to_anyref(
53 arrayref: Option<&wasmtime_arrayref_t>,
54 out: &mut MaybeUninit<wasmtime_anyref_t>,
55) {
56 let anyref = arrayref
57 .and_then(|a| a.as_wasmtime())
58 .map(|a| a.to_anyref());
59 crate::initialize(out, anyref.into());
60}
61
62#[unsafe(no_mangle)]
63pub unsafe extern "C" fn wasmtime_arrayref_to_eqref(
64 arrayref: Option<&wasmtime_arrayref_t>,
65 out: &mut MaybeUninit<wasmtime_eqref_t>,
66) {
67 let eqref = arrayref.and_then(|a| a.as_wasmtime()).map(|a| a.to_eqref());
68 crate::initialize(out, eqref.into());
69}
70
71#[unsafe(no_mangle)]
72pub unsafe extern "C" fn wasmtime_arrayref_len(
73 cx: WasmtimeStoreContextMut<'_>,
74 arrayref: Option<&wasmtime_arrayref_t>,
75 out: &mut MaybeUninit<u32>,
76) -> Option<Box<crate::wasmtime_error_t>> {
77 let arrayref = arrayref
78 .and_then(|a| a.as_wasmtime())
79 .expect("non-null arrayref required");
80 match arrayref.len(&cx) {
81 Ok(len) => {
82 crate::initialize(out, len);
83 None
84 }
85 Err(e) => Some(Box::new(e.into())),
86 }
87}
88
89#[unsafe(no_mangle)]
90pub unsafe extern "C" fn wasmtime_arrayref_get(
91 mut cx: WasmtimeStoreContextMut<'_>,
92 arrayref: Option<&wasmtime_arrayref_t>,
93 index: u32,
94 out: &mut MaybeUninit<crate::wasmtime_val_t>,
95) -> Option<Box<crate::wasmtime_error_t>> {
96 let arrayref = arrayref
97 .and_then(|a| a.as_wasmtime())
98 .expect("non-null arrayref required");
99 let mut scope = RootScope::new(&mut cx);
100 let rooted = arrayref.to_rooted(&mut scope);
101 match rooted.get(&mut scope, index) {
102 Ok(val) => {
103 let c_val = crate::wasmtime_val_t::from_val(&mut scope, val);
104 crate::initialize(out, c_val);
105 None
106 }
107 Err(e) => Some(Box::new(e.into())),
108 }
109}
110
111#[unsafe(no_mangle)]
112pub unsafe extern "C" fn wasmtime_arrayref_set(
113 mut cx: WasmtimeStoreContextMut<'_>,
114 arrayref: Option<&wasmtime_arrayref_t>,
115 index: u32,
116 val: &crate::wasmtime_val_t,
117) -> Option<Box<crate::wasmtime_error_t>> {
118 let arrayref = arrayref
119 .and_then(|a| a.as_wasmtime())
120 .expect("non-null arrayref required");
121 let mut scope = RootScope::new(&mut cx);
122 let rooted = arrayref.to_rooted(&mut scope);
123 let rust_val = val.to_val(&mut scope);
124 match rooted.set(&mut scope, index, rust_val) {
125 Ok(()) => None,
126 Err(e) => Some(Box::new(e.into())),
127 }
128}