Skip to content

Commit a226ebf

Browse files
committed
Remove the explicit gc feature from the wasmtime dependency in the c-api crate
1 parent 97361cc commit a226ebf

15 files changed

Lines changed: 184 additions & 38 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ jobs:
409409
-p wasmtime-c-api --no-default-features
410410
-p wasmtime-c-api --no-default-features --features wat
411411
-p wasmtime-c-api --no-default-features --features wasi
412+
-p wasmtime-c-api --no-default-features --features gc
412413
413414
- name: wasmtime-wasi-http
414415
checks: |

crates/c-api/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ doctest = false
2121

2222
[dependencies]
2323
env_logger = { workspace = true, optional = true }
24-
wasmtime = { workspace = true, features = ['runtime', 'gc', 'std'] }
24+
wasmtime = { workspace = true, features = ['runtime', 'std'] }
2525
wasmtime-c-api-macros = { workspace = true }
2626
log = { workspace = true }
2727
tracing = { workspace = true }

crates/c-api/include/wasmtime/config.hh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,15 @@ class Config {
303303
wasmtime_config_wasm_tail_call_set(ptr.get(), enable);
304304
}
305305

306+
#ifdef WASMTIME_FEATURE_GC
306307
/// \brief Configures whether the WebAssembly reference types proposal is
307308
/// enabled
308309
///
309310
/// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_reference_types
310311
void wasm_reference_types(bool enable) {
311312
wasmtime_config_wasm_reference_types_set(ptr.get(), enable);
312313
}
314+
#endif // WASMTIME_FEATURE_GC
313315

314316
/// \brief Configures whether the WebAssembly simd proposal is enabled
315317
///
@@ -361,11 +363,13 @@ class Config {
361363
wasmtime_config_wasm_memory64_set(ptr.get(), enable);
362364
}
363365

366+
#ifdef WASMTIME_FEATURE_GC
364367
/// \brief Configures whether the WebAssembly Garbage Collection proposal will
365368
/// be enabled
366369
///
367370
/// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_gc
368371
void wasm_gc(bool enable) { wasmtime_config_wasm_gc_set(ptr.get(), enable); }
372+
#endif // WASMTIME_FEATURE_GC
369373

370374
/// \brief Configures whether the WebAssembly function references proposal
371375
/// will be enabled
@@ -383,13 +387,15 @@ class Config {
383387
wasmtime_config_wasm_wide_arithmetic_set(ptr.get(), enable);
384388
}
385389

390+
#ifdef WASMTIME_FEATURE_GC
386391
/// \brief Configures whether the WebAssembly exceptions proposal will be
387392
/// enabled
388393
///
389394
/// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.wasm_exceptions
390395
void wasm_exceptions(bool enable) {
391396
wasmtime_config_wasm_exceptions_set(ptr.get(), enable);
392397
}
398+
#endif // WASMTIME_FEATURE_GC
393399

394400
/// \brief Configures whether the WebAssembly custom-page-sizes proposal will
395401
/// be enabled

crates/c-api/include/wasmtime/func.hh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ NATIVE_WASM_TYPE(double, F64, f64)
8181

8282
#undef NATIVE_WASM_TYPE
8383

84+
#ifdef WASMTIME_FEATURE_GC
8485
/// Type information for `externref`, represented on the host as an optional
8586
/// `ExternRef`.
8687
template <> struct WasmType<std::optional<ExternRef>> {
@@ -102,8 +103,9 @@ template <> struct WasmType<std::optional<ExternRef>> {
102103
p->externref = 0;
103104
}
104105
}
106+
105107
static std::optional<ExternRef> load(Store::Context cx,
106-
wasmtime_val_raw_t *p) {
108+
wasmtime_val_raw_t *p) {
107109
if (p->externref == 0) {
108110
return std::nullopt;
109111
}
@@ -112,6 +114,7 @@ template <> struct WasmType<std::optional<ExternRef>> {
112114
return ExternRef(val);
113115
}
114116
};
117+
#endif // WASMTIME_FEATURE_GC
115118

116119
/// Type information for the `V128` host value used as a wasm value.
117120
template <> struct WasmType<V128> {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ WASM_API_EXTERN void wasmtime_context_set_data(wasmtime_context_t *context,
144144
*
145145
* The `context` argument must not be NULL.
146146
*/
147+
#ifdef WASMTIME_FEATURE_GC
147148
WASM_API_EXTERN wasmtime_error_t *
148149
wasmtime_context_gc(wasmtime_context_t *context);
150+
#endif
149151

150152
/**
151153
* \brief Set fuel to this context's store for wasm to consume while executing.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public:
8989
/// Creates a context referencing the provided `Caller`.
9090
Context(Caller *caller);
9191

92+
#ifdef WASMTIME_FEATURE_GC
9293
/// Runs a garbage collection pass in the referenced store to collect loose
9394
/// `externref` values, if any are available.
9495
Result<std::monostate> gc() {
@@ -98,6 +99,7 @@ public:
9899
}
99100
return std::monostate();
100101
}
102+
#endif
101103

102104
/// Injects fuel to be consumed within this store.
103105
///
@@ -231,9 +233,11 @@ public:
231233
/// Explicit function to acquire a `Context` from this store.
232234
Context context() { return this; }
233235

236+
#ifdef WASMTIME_FEATURE_GC
234237
/// Runs a garbage collection pass in the referenced store to collect loose
235238
/// GC-managed objects, if any are available.
236239
Result<std::monostate> gc() { return context().gc(); }
240+
#endif
237241

238242
private:
239243
template <typename F>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
extern "C" {
1616
#endif
1717

18+
1819
/**
1920
* \typedef wasmtime_anyref_t
2021
* \brief Convenience alias for #wasmtime_anyref
@@ -52,6 +53,7 @@ typedef struct wasmtime_anyref {
5253
void *__private3;
5354
} wasmtime_anyref_t;
5455

56+
#ifdef WASMTIME_FEATURE_GC
5557
/// \brief Helper function to initialize the `ref` provided to a null anyref
5658
/// value.
5759
static inline void wasmtime_anyref_set_null(wasmtime_anyref_t *ref) {
@@ -67,6 +69,7 @@ static inline bool wasmtime_anyref_is_null(const wasmtime_anyref_t *ref) {
6769
return ref->store_id == 0;
6870
}
6971

72+
7073
/**
7174
* \brief Creates a new reference pointing to the same data that `anyref`
7275
* points to (depending on the configured collector this might increase a
@@ -175,6 +178,8 @@ WASM_API_EXTERN bool wasmtime_anyref_i31_get_s(wasmtime_context_t *context,
175178
* `wasmtime_externref_set_null`. Null can be tested for with the
176179
* `wasmtime_externref_is_null` function.
177180
*/
181+
#endif // WASMTIME_FEATURE_GC
182+
178183
typedef struct wasmtime_externref {
179184
/// Internal metadata tracking within the store, embedders should not
180185
/// configure or modify these fields.
@@ -187,6 +192,7 @@ typedef struct wasmtime_externref {
187192
void *__private3;
188193
} wasmtime_externref_t;
189194

195+
#ifdef WASMTIME_FEATURE_GC
190196
/// \brief Helper function to initialize the `ref` provided to a null externref
191197
/// value.
192198
static inline void wasmtime_externref_set_null(wasmtime_externref_t *ref) {
@@ -287,6 +293,7 @@ WASM_API_EXTERN void wasmtime_externref_from_raw(wasmtime_context_t *context,
287293
*/
288294
WASM_API_EXTERN uint32_t wasmtime_externref_to_raw(
289295
wasmtime_context_t *context, const wasmtime_externref_t *ref);
296+
#endif // WASMTIME_FEATURE_GC
290297

291298
/// \brief Discriminant stored in #wasmtime_val::kind
292299
typedef uint8_t wasmtime_valkind_t;
@@ -314,6 +321,7 @@ typedef uint8_t wasmtime_valkind_t;
314321
/// stored in little-endian order.
315322
typedef uint8_t wasmtime_v128[16];
316323

324+
317325
/**
318326
* \typedef wasmtime_valunion_t
319327
* \brief Convenience alias for #wasmtime_valunion

crates/c-api/include/wasmtime/val.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace wasmtime {
1414

15+
#ifdef WASMTIME_FEATURE_GC
1516
/**
1617
* \brief Representation of a WebAssembly `externref` value.
1718
*
@@ -98,7 +99,10 @@ public:
9899
return wasmtime_externref_to_raw(cx.capi(), &val);
99100
}
100101
};
102+
#endif // WASMTIME_FEATURE_GC
101103

104+
105+
#ifdef WASMTIME_FEATURE_GC
102106
/**
103107
* \brief Representation of a WebAssembly `anyref` value.
104108
*/
@@ -174,6 +178,7 @@ public:
174178
return std::nullopt;
175179
}
176180
};
181+
#endif // WASMTIME_FEATURE_GC
177182

178183
/// \brief Container for the `v128` WebAssembly type.
179184
struct V128 {

crates/c-api/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ pub extern "C" fn wasmtime_config_wasm_tail_call_set(c: &mut wasm_config_t, enab
9090
}
9191

9292
#[unsafe(no_mangle)]
93+
#[cfg(feature = "gc")]
9394
pub extern "C" fn wasmtime_config_wasm_reference_types_set(c: &mut wasm_config_t, enable: bool) {
9495
c.config.wasm_reference_types(enable);
9596
}
9697

9798
#[unsafe(no_mangle)]
99+
#[cfg(feature = "gc")]
98100
pub extern "C" fn wasmtime_config_wasm_function_references_set(
99101
c: &mut wasm_config_t,
100102
enable: bool,
@@ -103,6 +105,7 @@ pub extern "C" fn wasmtime_config_wasm_function_references_set(
103105
}
104106

105107
#[unsafe(no_mangle)]
108+
#[cfg(feature = "gc")]
106109
pub extern "C" fn wasmtime_config_wasm_gc_set(c: &mut wasm_config_t, enable: bool) {
107110
c.config.wasm_gc(enable);
108111
}
@@ -476,6 +479,7 @@ pub extern "C" fn wasmtime_config_wasm_wide_arithmetic_set(c: &mut wasm_config_t
476479
}
477480

478481
#[unsafe(no_mangle)]
482+
#[cfg(feature = "gc")]
479483
pub extern "C" fn wasmtime_config_wasm_exceptions_set(c: &mut wasm_config_t, enable: bool) {
480484
c.config.wasm_exceptions(enable);
481485
}

crates/c-api/src/func.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ use std::mem::{self, MaybeUninit};
1010
use std::panic::{self, AssertUnwindSafe};
1111
use std::ptr;
1212
use std::str;
13+
#[cfg(feature = "gc")]
14+
use wasmtime::RootScope;
1315
use wasmtime::{
14-
AsContext, AsContextMut, Error, Extern, Func, Result, RootScope, StoreContext, StoreContextMut,
15-
Trap, Val, ValRaw,
16+
AsContext, AsContextMut, Error, Extern, Func, Result, StoreContext, StoreContextMut, Trap, Val,
17+
ValRaw,
1618
};
1719

1820
#[derive(Clone)]
@@ -351,13 +353,15 @@ pub unsafe extern "C" fn wasmtime_func_call(
351353
nresults: usize,
352354
trap_ret: &mut *mut wasm_trap_t,
353355
) -> Option<Box<wasmtime_error_t>> {
354-
let mut scope = RootScope::new(&mut store);
355-
let mut params = mem::take(&mut scope.as_context_mut().data_mut().wasm_val_storage);
356+
#[cfg(feature = "gc")]
357+
let mut store = RootScope::new(&mut store);
358+
let mut params = mem::take(&mut store.as_context_mut().data_mut().wasm_val_storage);
359+
356360
let (wt_params, wt_results) = translate_args(
357361
&mut params,
358362
crate::slice_from_raw_parts(args, nargs)
359363
.iter()
360-
.map(|i| i.to_val(&mut scope)),
364+
.map(|i| i.to_val(&mut store)),
361365
nresults,
362366
);
363367

@@ -366,16 +370,16 @@ pub unsafe extern "C" fn wasmtime_func_call(
366370
// can. As a result we catch panics here and transform them to traps to
367371
// allow the caller to have any insulation possible against Rust panics.
368372
let result = panic::catch_unwind(AssertUnwindSafe(|| {
369-
func.call(&mut scope, wt_params, wt_results)
373+
func.call(&mut store, wt_params, wt_results)
370374
}));
371375
match result {
372376
Ok(Ok(())) => {
373377
let results = crate::slice_from_raw_parts_mut(results, nresults);
374378
for (slot, val) in results.iter_mut().zip(wt_results.iter()) {
375-
crate::initialize(slot, wasmtime_val_t::from_val(&mut scope, *val));
379+
crate::initialize(slot, wasmtime_val_t::from_val(&mut store, *val));
376380
}
377381
params.truncate(0);
378-
scope.as_context_mut().data_mut().wasm_val_storage = params;
382+
store.as_context_mut().data_mut().wasm_val_storage = params;
379383
None
380384
}
381385
Ok(Err(trap)) => store_err(trap, trap_ret),

0 commit comments

Comments
 (0)