139ec36caSAlex Crichton use crate::{
239ec36caSAlex Crichton wasm_byte_vec_t, wasm_config_t, wasm_engine_t, wasmtime_component_type_t, wasmtime_error_t,
339ec36caSAlex Crichton };
439ec36caSAlex Crichton use std::ffi::{CStr, c_char};
5ce3c1a72SMangoPeachGrape use wasmtime::component::{Component, ComponentExportIndex};
6dc029724SNick Fitzgerald use wasmtime::error::Context;
76ba6e13bSMangoPeachGrape
86ba6e13bSMangoPeachGrape #[unsafe(no_mangle)]
wasmtime_config_wasm_component_model_set(c: &mut wasm_config_t, enable: bool)939ec36caSAlex Crichton pub extern "C" fn wasmtime_config_wasm_component_model_set(c: &mut wasm_config_t, enable: bool) {
106ba6e13bSMangoPeachGrape c.config.wasm_component_model(enable);
116ba6e13bSMangoPeachGrape }
126ba6e13bSMangoPeachGrape
13*1b59b579SYordis Prieto #[unsafe(no_mangle)]
wasmtime_config_wasm_component_model_map_set( c: &mut wasm_config_t, enable: bool, )14*1b59b579SYordis Prieto pub extern "C" fn wasmtime_config_wasm_component_model_map_set(
15*1b59b579SYordis Prieto c: &mut wasm_config_t,
16*1b59b579SYordis Prieto enable: bool,
17*1b59b579SYordis Prieto ) {
18*1b59b579SYordis Prieto c.config.wasm_component_model_map(enable);
19*1b59b579SYordis Prieto }
20*1b59b579SYordis Prieto
216ba6e13bSMangoPeachGrape #[derive(Clone)]
226ba6e13bSMangoPeachGrape #[repr(transparent)]
236ba6e13bSMangoPeachGrape pub struct wasmtime_component_t {
246ba6e13bSMangoPeachGrape pub(crate) component: Component,
256ba6e13bSMangoPeachGrape }
266ba6e13bSMangoPeachGrape
276ba6e13bSMangoPeachGrape #[unsafe(no_mangle)]
286ba6e13bSMangoPeachGrape #[cfg(any(feature = "cranelift", feature = "winch"))]
wasmtime_component_new( engine: &wasm_engine_t, buf: *const u8, len: usize, component_out: &mut *mut wasmtime_component_t, ) -> Option<Box<wasmtime_error_t>>296ba6e13bSMangoPeachGrape pub unsafe extern "C" fn wasmtime_component_new(
306ba6e13bSMangoPeachGrape engine: &wasm_engine_t,
316ba6e13bSMangoPeachGrape buf: *const u8,
326ba6e13bSMangoPeachGrape len: usize,
336ba6e13bSMangoPeachGrape component_out: &mut *mut wasmtime_component_t,
346ba6e13bSMangoPeachGrape ) -> Option<Box<wasmtime_error_t>> {
35930d3543SMangoPeachGrape let bytes = unsafe { crate::slice_from_raw_parts(buf, len) };
36930d3543SMangoPeachGrape crate::handle_result(Component::new(&engine.engine, bytes), |component| {
376ba6e13bSMangoPeachGrape *component_out = Box::into_raw(Box::new(wasmtime_component_t { component }));
38930d3543SMangoPeachGrape })
396ba6e13bSMangoPeachGrape }
406ba6e13bSMangoPeachGrape
416ba6e13bSMangoPeachGrape #[unsafe(no_mangle)]
426ba6e13bSMangoPeachGrape #[cfg(any(feature = "cranelift", feature = "winch"))]
wasmtime_component_serialize( component: &wasmtime_component_t, ret: &mut wasm_byte_vec_t, ) -> Option<Box<wasmtime_error_t>>4339ec36caSAlex Crichton pub extern "C" fn wasmtime_component_serialize(
446ba6e13bSMangoPeachGrape component: &wasmtime_component_t,
456ba6e13bSMangoPeachGrape ret: &mut wasm_byte_vec_t,
466ba6e13bSMangoPeachGrape ) -> Option<Box<wasmtime_error_t>> {
476ba6e13bSMangoPeachGrape crate::handle_result(component.component.serialize(), |buffer| {
486ba6e13bSMangoPeachGrape ret.set_buffer(buffer);
496ba6e13bSMangoPeachGrape })
506ba6e13bSMangoPeachGrape }
516ba6e13bSMangoPeachGrape
526ba6e13bSMangoPeachGrape #[unsafe(no_mangle)]
wasmtime_component_deserialize( engine: &wasm_engine_t, buf: *const u8, len: usize, component_out: &mut *mut wasmtime_component_t, ) -> Option<Box<wasmtime_error_t>>536ba6e13bSMangoPeachGrape pub unsafe extern "C" fn wasmtime_component_deserialize(
546ba6e13bSMangoPeachGrape engine: &wasm_engine_t,
556ba6e13bSMangoPeachGrape buf: *const u8,
566ba6e13bSMangoPeachGrape len: usize,
576ba6e13bSMangoPeachGrape component_out: &mut *mut wasmtime_component_t,
586ba6e13bSMangoPeachGrape ) -> Option<Box<wasmtime_error_t>> {
596ba6e13bSMangoPeachGrape let binary = unsafe { crate::slice_from_raw_parts(buf, len) };
606ba6e13bSMangoPeachGrape crate::handle_result(
616ba6e13bSMangoPeachGrape unsafe { Component::deserialize(&engine.engine, binary) },
626ba6e13bSMangoPeachGrape |component| {
636ba6e13bSMangoPeachGrape *component_out = Box::into_raw(Box::new(wasmtime_component_t { component }));
646ba6e13bSMangoPeachGrape },
656ba6e13bSMangoPeachGrape )
666ba6e13bSMangoPeachGrape }
676ba6e13bSMangoPeachGrape
686ba6e13bSMangoPeachGrape #[unsafe(no_mangle)]
wasmtime_component_deserialize_file( engine: &wasm_engine_t, path: *const c_char, component_out: &mut *mut wasmtime_component_t, ) -> Option<Box<wasmtime_error_t>>696ba6e13bSMangoPeachGrape pub unsafe extern "C" fn wasmtime_component_deserialize_file(
706ba6e13bSMangoPeachGrape engine: &wasm_engine_t,
716ba6e13bSMangoPeachGrape path: *const c_char,
726ba6e13bSMangoPeachGrape component_out: &mut *mut wasmtime_component_t,
736ba6e13bSMangoPeachGrape ) -> Option<Box<wasmtime_error_t>> {
746ba6e13bSMangoPeachGrape let path = unsafe { CStr::from_ptr(path) };
756ba6e13bSMangoPeachGrape let result = path
766ba6e13bSMangoPeachGrape .to_str()
776ba6e13bSMangoPeachGrape .context("input path is not valid utf-8")
786ba6e13bSMangoPeachGrape .and_then(|path| unsafe { Component::deserialize_file(&engine.engine, path) });
796ba6e13bSMangoPeachGrape crate::handle_result(result, |component| {
806ba6e13bSMangoPeachGrape *component_out = Box::into_raw(Box::new(wasmtime_component_t { component }));
816ba6e13bSMangoPeachGrape })
826ba6e13bSMangoPeachGrape }
836ba6e13bSMangoPeachGrape
846ba6e13bSMangoPeachGrape #[unsafe(no_mangle)]
wasmtime_component_type( component: &wasmtime_component_t, ) -> Box<wasmtime_component_type_t>8539ec36caSAlex Crichton pub extern "C" fn wasmtime_component_type(
8639ec36caSAlex Crichton component: &wasmtime_component_t,
8739ec36caSAlex Crichton ) -> Box<wasmtime_component_type_t> {
8839ec36caSAlex Crichton Box::new(component.component.component_type().into())
8939ec36caSAlex Crichton }
9039ec36caSAlex Crichton
9139ec36caSAlex Crichton #[unsafe(no_mangle)]
wasmtime_component_clone( component: &wasmtime_component_t, ) -> Box<wasmtime_component_t>9239ec36caSAlex Crichton pub extern "C" fn wasmtime_component_clone(
936ba6e13bSMangoPeachGrape component: &wasmtime_component_t,
946ba6e13bSMangoPeachGrape ) -> Box<wasmtime_component_t> {
956ba6e13bSMangoPeachGrape Box::new(component.clone())
966ba6e13bSMangoPeachGrape }
976ba6e13bSMangoPeachGrape
986ba6e13bSMangoPeachGrape #[unsafe(no_mangle)]
wasmtime_component_delete(_component: Box<wasmtime_component_t>)9939ec36caSAlex Crichton pub extern "C" fn wasmtime_component_delete(_component: Box<wasmtime_component_t>) {}
100ce3c1a72SMangoPeachGrape
101ce3c1a72SMangoPeachGrape #[repr(transparent)]
102ce3c1a72SMangoPeachGrape pub struct wasmtime_component_export_index_t {
103ce3c1a72SMangoPeachGrape pub(crate) export_index: ComponentExportIndex,
104ce3c1a72SMangoPeachGrape }
105ce3c1a72SMangoPeachGrape
106ce3c1a72SMangoPeachGrape #[unsafe(no_mangle)]
wasmtime_component_get_export_index( component: &wasmtime_component_t, instance_export_index: *const wasmtime_component_export_index_t, name: *const u8, name_len: usize, ) -> Option<Box<wasmtime_component_export_index_t>>107ce3c1a72SMangoPeachGrape pub unsafe extern "C" fn wasmtime_component_get_export_index(
108ce3c1a72SMangoPeachGrape component: &wasmtime_component_t,
109ce3c1a72SMangoPeachGrape instance_export_index: *const wasmtime_component_export_index_t,
110ce3c1a72SMangoPeachGrape name: *const u8,
111ce3c1a72SMangoPeachGrape name_len: usize,
112ce3c1a72SMangoPeachGrape ) -> Option<Box<wasmtime_component_export_index_t>> {
113ce3c1a72SMangoPeachGrape let name = unsafe { std::slice::from_raw_parts(name, name_len) };
114ce3c1a72SMangoPeachGrape let Ok(name) = std::str::from_utf8(name) else {
115ce3c1a72SMangoPeachGrape return None;
116ce3c1a72SMangoPeachGrape };
117ce3c1a72SMangoPeachGrape
118ce3c1a72SMangoPeachGrape let instance_export_index = if instance_export_index.is_null() {
119ce3c1a72SMangoPeachGrape None
120ce3c1a72SMangoPeachGrape } else {
121ce3c1a72SMangoPeachGrape Some((*instance_export_index).export_index)
122ce3c1a72SMangoPeachGrape };
123ce3c1a72SMangoPeachGrape
124ce3c1a72SMangoPeachGrape component
125ce3c1a72SMangoPeachGrape .component
126ce3c1a72SMangoPeachGrape .get_export_index(instance_export_index.as_ref(), &name)
127ce3c1a72SMangoPeachGrape .map(|export_index| Box::new(wasmtime_component_export_index_t { export_index }))
128ce3c1a72SMangoPeachGrape }
129ce3c1a72SMangoPeachGrape
130ce3c1a72SMangoPeachGrape #[unsafe(no_mangle)]
wasmtime_component_export_index_clone( export_index: &wasmtime_component_export_index_t, ) -> Box<wasmtime_component_export_index_t>13139ec36caSAlex Crichton pub extern "C" fn wasmtime_component_export_index_clone(
132579ec46bSAlex Crichton export_index: &wasmtime_component_export_index_t,
133579ec46bSAlex Crichton ) -> Box<wasmtime_component_export_index_t> {
134579ec46bSAlex Crichton Box::new(wasmtime_component_export_index_t {
135579ec46bSAlex Crichton export_index: export_index.export_index,
136579ec46bSAlex Crichton })
137579ec46bSAlex Crichton }
138579ec46bSAlex Crichton
139579ec46bSAlex Crichton #[unsafe(no_mangle)]
wasmtime_component_export_index_delete( _export_index: Box<wasmtime_component_export_index_t>, )14039ec36caSAlex Crichton pub extern "C" fn wasmtime_component_export_index_delete(
141ce3c1a72SMangoPeachGrape _export_index: Box<wasmtime_component_export_index_t>,
142ce3c1a72SMangoPeachGrape ) {
143ce3c1a72SMangoPeachGrape }
144