Skip to content

Commit 6b34255

Browse files
Make wasmtime_component_instance_t non-opaque
1 parent 42ab913 commit 6b34255

File tree

6 files changed

+19
-35
lines changed

6 files changed

+19
-35
lines changed

crates/c-api/include/wasmtime/component/instance.h

+13-10
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
extern "C" {
1010
#endif
1111

12-
typedef struct wasmtime_component_instance_t wasmtime_component_instance_t;
13-
14-
/**
15-
* \brief Deletes a #wasmtime_component_instance_t created by
16-
* #wasmtime_component_linker_instantiate
17-
*
18-
* \param instance the #wasmtime_component_instance_t to delete
19-
*/
20-
WASM_API_EXTERN void
21-
wasmtime_component_instance_delete(wasmtime_component_instance_t *instance);
12+
/// \brief Representation of a instance in Wasmtime.
13+
///
14+
/// Instances are represented with a 64-bit identifying integer in Wasmtime.
15+
/// They do not have any destructor associated with them. Instances cannot
16+
/// interoperate between #wasmtime_store_t instances and if the wrong instance
17+
/// is passed to the wrong store then it may trigger an assertion to abort the
18+
/// process.
19+
typedef struct wasmtime_component_instance {
20+
/// Internal identifier of what store this belongs to, never zero.
21+
uint64_t store_id;
22+
/// Internal index within the store.
23+
size_t index;
24+
} wasmtime_component_instance_t;
2225

2326
#ifdef __cplusplus
2427
} // extern "C"

crates/c-api/include/wasmtime/component/linker.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ WASM_API_EXTERN wasmtime_error_t *wasmtime_component_linker_instance(
6767
WASM_API_EXTERN wasmtime_error_t *wasmtime_component_linker_instantiate(
6868
const wasmtime_component_linker_t *linker, wasmtime_context_t *context,
6969
const wasmtime_component_t *component,
70-
wasmtime_component_instance_t **instance_out);
70+
wasmtime_component_instance_t *instance_out);
7171

7272
/**
7373
* \brief Deletes a #wasmtime_component_linker_t created by

crates/c-api/src/component/instance.rs

-12
This file was deleted.

crates/c-api/src/component/linker.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::ffi::{c_char, CStr};
22

33
use anyhow::Context;
4-
use wasmtime::component::{Linker, LinkerInstance};
4+
use wasmtime::component::{Instance, Linker, LinkerInstance};
55

66
use crate::{wasm_engine_t, wasmtime_error_t, WasmtimeStoreContextMut, WasmtimeStoreData};
77

8-
use super::{wasmtime_component_instance_t, wasmtime_component_t};
8+
use super::wasmtime_component_t;
99

1010
#[repr(transparent)]
1111
pub struct wasmtime_component_linker_t {
@@ -49,12 +49,10 @@ pub unsafe extern "C" fn wasmtime_component_linker_instantiate(
4949
linker: &wasmtime_component_linker_t,
5050
context: WasmtimeStoreContextMut<'_>,
5151
component: &wasmtime_component_t,
52-
instance_out: &mut *mut wasmtime_component_instance_t,
52+
instance_out: &mut Instance,
5353
) -> Option<Box<wasmtime_error_t>> {
5454
let result = linker.linker.instantiate(context, &component.component);
55-
crate::handle_result(result, |instance| {
56-
*instance_out = Box::into_raw(Box::new(wasmtime_component_instance_t { instance }));
57-
})
55+
crate::handle_result(result, |instance| *instance_out = instance)
5856
}
5957

6058
#[unsafe(no_mangle)]

crates/c-api/src/component/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
mod component;
2-
mod instance;
32
mod linker;
43

54
pub use component::*;
6-
pub use instance::*;
75
pub use linker::*;

crates/c-api/tests/component/instantiate.cc

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,10 @@ TEST(component, instantiate) {
3838

3939
wasmtime_component_linker_instance_delete(linker_instance);
4040

41-
wasmtime_component_instance_t *instance = nullptr;
41+
wasmtime_component_instance_t instance = {};
4242
error = wasmtime_component_linker_instantiate(linker, context, component,
4343
&instance);
4444
EXPECT_EQ(error, nullptr);
45-
EXPECT_NE(instance, nullptr);
46-
47-
wasmtime_component_instance_delete(instance);
4845

4946
wasmtime_component_linker_delete(linker);
5047

0 commit comments

Comments
 (0)