Skip to content

Commit 69c01c5

Browse files
c-api: component-model: Resource table, WASI (#11055)
* c-api: component-model: Resource table, WASI * WASIP2 context builder * Add include * Rename function * Add a simple test * Add comments prtest:full
1 parent ef7a296 commit 69c01c5

File tree

10 files changed

+187
-0
lines changed

10 files changed

+187
-0
lines changed

crates/c-api/include/wasmtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
#include <wasmtime/val.h>
206206
#include <wasmtime/async.h>
207207
#include <wasmtime/component.h>
208+
#include <wasmtime/wasip2.h>
208209
#include <wasmtime/wat.h>
209210
// IWYU pragma: end_exports
210211
// clang-format on

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ WASM_API_EXTERN wasmtime_error_t *wasmtime_component_linker_instance_add_func(
136136
size_t name_len, wasmtime_component_func_callback_t callback, void *data,
137137
void (*finalizer)());
138138

139+
#ifdef WASMTIME_FEATURE_WASI
140+
141+
/**
142+
* \brief Add all WASI interfaces into the \p linker provided.
143+
*/
144+
WASM_API_EXTERN wasmtime_error_t *
145+
wasmtime_component_linker_add_wasip2(wasmtime_component_linker_t *linker);
146+
147+
#endif // WASMTIME_FEATURE_WASI
148+
139149
/**
140150
* \brief Deletes a #wasmtime_component_linker_instance_t
141151
*

crates/c-api/include/wasmtime/store.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <wasm.h>
1212
#include <wasmtime/conf.h>
1313
#include <wasmtime/error.h>
14+
#include <wasmtime/wasip2.h>
1415

1516
#ifdef __cplusplus
1617
extern "C" {
@@ -192,6 +193,22 @@ wasmtime_context_get_fuel(const wasmtime_context_t *context, uint64_t *fuel);
192193
WASM_API_EXTERN wasmtime_error_t *
193194
wasmtime_context_set_wasi(wasmtime_context_t *context, wasi_config_t *wasi);
194195

196+
#ifdef WASMTIME_FEATURE_COMPONENT_MODEL
197+
198+
/**
199+
* \brief Set the WASIP2 config for this store.
200+
*
201+
* This function is required if #wasmtime_component_linker_add_wasip2 is called.
202+
*
203+
* This function takes ownership of \p config. The caller should no longer use
204+
* \p config after calling this function.
205+
*/
206+
WASM_API_EXTERN void
207+
wasmtime_context_set_wasip2(wasmtime_context_t *context,
208+
wasmtime_wasip2_config_t *config);
209+
210+
#endif // WASMTIME_FEATURE_COMPONENT_MODEL
211+
195212
#endif // WASMTIME_FEATURE_WASI
196213

197214
/**
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/// \file wasmtime/wasip2.h
2+
3+
#ifndef WASMTIME_WASIP2_H
4+
#define WASMTIME_WASIP2_H
5+
6+
#include <wasm.h>
7+
#include <wasmtime/conf.h>
8+
9+
#ifdef WASMTIME_FEATURE_WASI
10+
#ifdef WASMTIME_FEATURE_COMPONENT_MODEL
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
/// Config for the WASIP2 context.
17+
typedef struct wasmtime_wasip2_config_t wasmtime_wasip2_config_t;
18+
19+
/**
20+
* \brief Create a #wasmtime_wasip2_config_t
21+
*/
22+
WASM_API_EXTERN wasmtime_wasip2_config_t *wasmtime_wasip2_config_new();
23+
24+
/**
25+
* \brief Configures this context's stdout stream to write to the host process's
26+
* stdout.
27+
*/
28+
WASM_API_EXTERN void
29+
wasmtime_wasip2_config_inherit_stdout(wasmtime_wasip2_config_t *config);
30+
31+
/**
32+
* \brief Delete a #wasmtime_wasip2_config_t
33+
*
34+
* \note This is not needed if the config is passed to
35+
* #wasmtime_component_linker_add_wasip2
36+
*/
37+
WASM_API_EXTERN void
38+
wasmtime_wasip2_config_delete(wasmtime_wasip2_config_t *config);
39+
40+
#ifdef __cplusplus
41+
} // extern "C"
42+
#endif
43+
44+
#endif // WASMTIME_FEATURE_COMPONENT_MODEL
45+
#endif // WASMTIME_FEATURE_WASI
46+
47+
#endif // WASMTIME_WASIP2_H

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ pub unsafe extern "C" fn wasmtime_component_linker_instance_add_func(
152152
crate::handle_result(result, |_| ())
153153
}
154154

155+
#[unsafe(no_mangle)]
156+
#[cfg(feature = "wasi")]
157+
pub unsafe extern "C" fn wasmtime_component_linker_add_wasip2(
158+
linker: &mut wasmtime_component_linker_t,
159+
) -> Option<Box<wasmtime_error_t>> {
160+
let result = wasmtime_wasi::p2::add_to_linker_sync(&mut linker.linker);
161+
crate::handle_result(result, |_| ())
162+
}
163+
155164
#[unsafe(no_mangle)]
156165
pub unsafe extern "C" fn wasmtime_component_linker_instance_delete(
157166
_linker_instance: Box<wasmtime_component_linker_instance_t>,

crates/c-api/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ mod wasi;
6666
#[cfg(feature = "wasi")]
6767
pub use crate::wasi::*;
6868

69+
#[cfg(all(feature = "component-model", feature = "wasi"))]
70+
mod wasip2;
71+
#[cfg(all(feature = "component-model", feature = "wasi"))]
72+
pub use crate::wasip2::*;
73+
6974
#[cfg(feature = "wat")]
7075
mod wat2wasm;
7176
#[cfg(feature = "wat")]

crates/c-api/src/store.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ pub struct WasmtimeStoreData {
9595

9696
/// Limits for the store.
9797
pub store_limits: StoreLimits,
98+
99+
#[cfg(feature = "component-model")]
100+
pub(crate) resource_table: wasmtime::component::ResourceTable,
101+
102+
#[cfg(all(feature = "component-model", feature = "wasi"))]
103+
pub(crate) wasip2: Option<wasmtime_wasi::p2::WasiCtx>,
104+
}
105+
106+
#[cfg(all(feature = "component-model", feature = "wasi"))]
107+
impl wasmtime_wasi::p2::IoView for WasmtimeStoreData {
108+
fn table(&mut self) -> &mut wasmtime_wasi::ResourceTable {
109+
&mut self.resource_table
110+
}
111+
}
112+
113+
#[cfg(all(feature = "component-model", feature = "wasi"))]
114+
impl wasmtime_wasi::p2::WasiView for WasmtimeStoreData {
115+
fn ctx(&mut self) -> &mut wasmtime_wasi::p2::WasiCtx {
116+
self.wasip2.as_mut().unwrap()
117+
}
98118
}
99119

100120
#[unsafe(no_mangle)]
@@ -113,6 +133,10 @@ pub extern "C" fn wasmtime_store_new(
113133
hostcall_val_storage: Vec::new(),
114134
wasm_val_storage: Vec::new(),
115135
store_limits: StoreLimits::default(),
136+
#[cfg(feature = "component-model")]
137+
resource_table: wasmtime::component::ResourceTable::default(),
138+
#[cfg(all(feature = "component-model", feature = "wasi"))]
139+
wasip2: None,
116140
},
117141
),
118142
})
@@ -217,6 +241,15 @@ pub extern "C" fn wasmtime_context_set_wasi(
217241
})
218242
}
219243

244+
#[cfg(all(feature = "component-model", feature = "wasi"))]
245+
#[unsafe(no_mangle)]
246+
pub unsafe extern "C" fn wasmtime_context_set_wasip2(
247+
mut context: WasmtimeStoreContextMut<'_>,
248+
mut config: Box<crate::wasmtime_wasip2_config_t>,
249+
) {
250+
context.data_mut().wasip2 = Some(config.builder.build());
251+
}
252+
220253
#[unsafe(no_mangle)]
221254
pub extern "C" fn wasmtime_context_gc(mut context: WasmtimeStoreContextMut<'_>) {
222255
context.gc(None);

crates/c-api/src/wasip2.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[repr(transparent)]
2+
pub struct wasmtime_wasip2_config_t {
3+
pub(crate) builder: wasmtime_wasi::p2::WasiCtxBuilder,
4+
}
5+
6+
#[unsafe(no_mangle)]
7+
pub unsafe extern "C" fn wasmtime_wasip2_config_new() -> Box<wasmtime_wasip2_config_t> {
8+
Box::new(wasmtime_wasip2_config_t {
9+
builder: wasmtime_wasi::p2::WasiCtxBuilder::new(),
10+
})
11+
}
12+
13+
#[unsafe(no_mangle)]
14+
pub unsafe extern "C" fn wasmtime_wasip2_config_inherit_stdout(
15+
config: &mut wasmtime_wasip2_config_t,
16+
) {
17+
config.builder.inherit_stdout();
18+
}
19+
20+
#[unsafe(no_mangle)]
21+
pub unsafe extern "C" fn wasmtime_wasip2_config_delete(_: Box<wasmtime_wasip2_config_t>) {}

crates/c-api/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ add_capi_test(tests FILES
5050
memory.cc
5151
instance.cc
5252
linker.cc
53+
wasip2.cc
5354
)
5455

5556
# Create a list of all wasmtime headers with `GLOB_RECURSE`, then emit a file

crates/c-api/tests/wasip2.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "component/utils.h"
2+
3+
#include <gtest/gtest.h>
4+
#include <wasmtime.h>
5+
6+
TEST(wasip2, smoke) {
7+
static constexpr auto component_text = std::string_view{
8+
R"END(
9+
(component)
10+
)END",
11+
};
12+
const auto engine = wasm_engine_new();
13+
EXPECT_NE(engine, nullptr);
14+
15+
const auto store = wasmtime_store_new(engine, nullptr, nullptr);
16+
const auto context = wasmtime_store_context(store);
17+
18+
const auto cfg = wasmtime_wasip2_config_new();
19+
wasmtime_wasip2_config_inherit_stdout(cfg);
20+
wasmtime_context_set_wasip2(context, cfg);
21+
22+
wasmtime_component_t *component = nullptr;
23+
24+
auto err = wasmtime_component_new(
25+
engine, reinterpret_cast<const uint8_t *>(component_text.data()),
26+
component_text.size(), &component);
27+
28+
CHECK_ERR(err);
29+
30+
const auto linker = wasmtime_component_linker_new(engine);
31+
32+
wasmtime_component_linker_add_wasip2(linker);
33+
34+
wasmtime_component_instance_t instance = {};
35+
err = wasmtime_component_linker_instantiate(linker, context, component,
36+
&instance);
37+
CHECK_ERR(err);
38+
39+
wasmtime_component_linker_delete(linker);
40+
41+
wasmtime_store_delete(store);
42+
wasm_engine_delete(engine);
43+
}

0 commit comments

Comments
 (0)