Path: blob/main/crates/c-api/src/component/component.rs
1692 views
use std::ffi::{CStr, c_char};12use anyhow::Context;3use wasmtime::component::{Component, ComponentExportIndex};45use crate::{wasm_byte_vec_t, wasm_config_t, wasm_engine_t, wasmtime_error_t};67#[unsafe(no_mangle)]8pub unsafe extern "C" fn wasmtime_config_wasm_component_model_set(9c: &mut wasm_config_t,10enable: bool,11) {12c.config.wasm_component_model(enable);13}1415#[derive(Clone)]16#[repr(transparent)]17pub struct wasmtime_component_t {18pub(crate) component: Component,19}2021#[unsafe(no_mangle)]22#[cfg(any(feature = "cranelift", feature = "winch"))]23pub unsafe extern "C" fn wasmtime_component_new(24engine: &wasm_engine_t,25buf: *const u8,26len: usize,27component_out: &mut *mut wasmtime_component_t,28) -> Option<Box<wasmtime_error_t>> {29let bytes = unsafe { crate::slice_from_raw_parts(buf, len) };30crate::handle_result(Component::new(&engine.engine, bytes), |component| {31*component_out = Box::into_raw(Box::new(wasmtime_component_t { component }));32})33}3435#[unsafe(no_mangle)]36#[cfg(any(feature = "cranelift", feature = "winch"))]37pub unsafe extern "C" fn wasmtime_component_serialize(38component: &wasmtime_component_t,39ret: &mut wasm_byte_vec_t,40) -> Option<Box<wasmtime_error_t>> {41crate::handle_result(component.component.serialize(), |buffer| {42ret.set_buffer(buffer);43})44}4546#[unsafe(no_mangle)]47pub unsafe extern "C" fn wasmtime_component_deserialize(48engine: &wasm_engine_t,49buf: *const u8,50len: usize,51component_out: &mut *mut wasmtime_component_t,52) -> Option<Box<wasmtime_error_t>> {53let binary = unsafe { crate::slice_from_raw_parts(buf, len) };54crate::handle_result(55unsafe { Component::deserialize(&engine.engine, binary) },56|component| {57*component_out = Box::into_raw(Box::new(wasmtime_component_t { component }));58},59)60}6162#[unsafe(no_mangle)]63pub unsafe extern "C" fn wasmtime_component_deserialize_file(64engine: &wasm_engine_t,65path: *const c_char,66component_out: &mut *mut wasmtime_component_t,67) -> Option<Box<wasmtime_error_t>> {68let path = unsafe { CStr::from_ptr(path) };69let result = path70.to_str()71.context("input path is not valid utf-8")72.and_then(|path| unsafe { Component::deserialize_file(&engine.engine, path) });73crate::handle_result(result, |component| {74*component_out = Box::into_raw(Box::new(wasmtime_component_t { component }));75})76}7778#[unsafe(no_mangle)]79pub unsafe extern "C" fn wasmtime_component_clone(80component: &wasmtime_component_t,81) -> Box<wasmtime_component_t> {82Box::new(component.clone())83}8485#[unsafe(no_mangle)]86pub unsafe extern "C" fn wasmtime_component_delete(_component: Box<wasmtime_component_t>) {}8788#[repr(transparent)]89pub struct wasmtime_component_export_index_t {90pub(crate) export_index: ComponentExportIndex,91}9293#[unsafe(no_mangle)]94pub unsafe extern "C" fn wasmtime_component_get_export_index(95component: &wasmtime_component_t,96instance_export_index: *const wasmtime_component_export_index_t,97name: *const u8,98name_len: usize,99) -> Option<Box<wasmtime_component_export_index_t>> {100let name = unsafe { std::slice::from_raw_parts(name, name_len) };101let Ok(name) = std::str::from_utf8(name) else {102return None;103};104105let instance_export_index = if instance_export_index.is_null() {106None107} else {108Some((*instance_export_index).export_index)109};110111component112.component113.get_export_index(instance_export_index.as_ref(), &name)114.map(|export_index| Box::new(wasmtime_component_export_index_t { export_index }))115}116117#[unsafe(no_mangle)]118pub unsafe extern "C" fn wasmtime_component_export_index_delete(119_export_index: Box<wasmtime_component_export_index_t>,120) {121}122123124