Path: blob/main/crates/c-api/src/component/component.rs
3082 views
use crate::{1wasm_byte_vec_t, wasm_config_t, wasm_engine_t, wasmtime_component_type_t, wasmtime_error_t,2};3use std::ffi::{CStr, c_char};4use wasmtime::component::{Component, ComponentExportIndex};5use wasmtime::error::Context;67#[unsafe(no_mangle)]8pub extern "C" fn wasmtime_config_wasm_component_model_set(c: &mut wasm_config_t, enable: bool) {9c.config.wasm_component_model(enable);10}1112#[derive(Clone)]13#[repr(transparent)]14pub struct wasmtime_component_t {15pub(crate) component: Component,16}1718#[unsafe(no_mangle)]19#[cfg(any(feature = "cranelift", feature = "winch"))]20pub unsafe extern "C" fn wasmtime_component_new(21engine: &wasm_engine_t,22buf: *const u8,23len: usize,24component_out: &mut *mut wasmtime_component_t,25) -> Option<Box<wasmtime_error_t>> {26let bytes = unsafe { crate::slice_from_raw_parts(buf, len) };27crate::handle_result(Component::new(&engine.engine, bytes), |component| {28*component_out = Box::into_raw(Box::new(wasmtime_component_t { component }));29})30}3132#[unsafe(no_mangle)]33#[cfg(any(feature = "cranelift", feature = "winch"))]34pub extern "C" fn wasmtime_component_serialize(35component: &wasmtime_component_t,36ret: &mut wasm_byte_vec_t,37) -> Option<Box<wasmtime_error_t>> {38crate::handle_result(component.component.serialize(), |buffer| {39ret.set_buffer(buffer);40})41}4243#[unsafe(no_mangle)]44pub unsafe extern "C" fn wasmtime_component_deserialize(45engine: &wasm_engine_t,46buf: *const u8,47len: usize,48component_out: &mut *mut wasmtime_component_t,49) -> Option<Box<wasmtime_error_t>> {50let binary = unsafe { crate::slice_from_raw_parts(buf, len) };51crate::handle_result(52unsafe { Component::deserialize(&engine.engine, binary) },53|component| {54*component_out = Box::into_raw(Box::new(wasmtime_component_t { component }));55},56)57}5859#[unsafe(no_mangle)]60pub unsafe extern "C" fn wasmtime_component_deserialize_file(61engine: &wasm_engine_t,62path: *const c_char,63component_out: &mut *mut wasmtime_component_t,64) -> Option<Box<wasmtime_error_t>> {65let path = unsafe { CStr::from_ptr(path) };66let result = path67.to_str()68.context("input path is not valid utf-8")69.and_then(|path| unsafe { Component::deserialize_file(&engine.engine, path) });70crate::handle_result(result, |component| {71*component_out = Box::into_raw(Box::new(wasmtime_component_t { component }));72})73}7475#[unsafe(no_mangle)]76pub extern "C" fn wasmtime_component_type(77component: &wasmtime_component_t,78) -> Box<wasmtime_component_type_t> {79Box::new(component.component.component_type().into())80}8182#[unsafe(no_mangle)]83pub extern "C" fn wasmtime_component_clone(84component: &wasmtime_component_t,85) -> Box<wasmtime_component_t> {86Box::new(component.clone())87}8889#[unsafe(no_mangle)]90pub extern "C" fn wasmtime_component_delete(_component: Box<wasmtime_component_t>) {}9192#[repr(transparent)]93pub struct wasmtime_component_export_index_t {94pub(crate) export_index: ComponentExportIndex,95}9697#[unsafe(no_mangle)]98pub unsafe extern "C" fn wasmtime_component_get_export_index(99component: &wasmtime_component_t,100instance_export_index: *const wasmtime_component_export_index_t,101name: *const u8,102name_len: usize,103) -> Option<Box<wasmtime_component_export_index_t>> {104let name = unsafe { std::slice::from_raw_parts(name, name_len) };105let Ok(name) = std::str::from_utf8(name) else {106return None;107};108109let instance_export_index = if instance_export_index.is_null() {110None111} else {112Some((*instance_export_index).export_index)113};114115component116.component117.get_export_index(instance_export_index.as_ref(), &name)118.map(|export_index| Box::new(wasmtime_component_export_index_t { export_index }))119}120121#[unsafe(no_mangle)]122pub extern "C" fn wasmtime_component_export_index_clone(123export_index: &wasmtime_component_export_index_t,124) -> Box<wasmtime_component_export_index_t> {125Box::new(wasmtime_component_export_index_t {126export_index: export_index.export_index,127})128}129130#[unsafe(no_mangle)]131pub extern "C" fn wasmtime_component_export_index_delete(132_export_index: Box<wasmtime_component_export_index_t>,133) {134}135136137