Skip to content

Commit e8d6c9d

Browse files
authored
Remove the explicit gc feature from the wasmtime dependency in the c-api crate (bytecodealliance#12805)
* Remove the explicit gc feature from the wasmtime dependency in the c-api crate * Fixes thrownexcpetion import which is behind gc * Adds more wasmtime_feature_gc gate, and moves some definition outside the gate
1 parent 47dcd1c commit e8d6c9d

22 files changed

Lines changed: 201 additions & 44 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ jobs:
414414
-p wasmtime-c-api --no-default-features
415415
-p wasmtime-c-api --no-default-features --features wat
416416
-p wasmtime-c-api --no-default-features --features wasi
417+
-p wasmtime-c-api --no-default-features --features gc
417418
418419
- name: wasmtime-wasi-http
419420
checks: |

crates/c-api-macros/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,15 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
135135

136136
#[doc = #as_ref_docs]
137137
#[unsafe(no_mangle)]
138+
#[cfg(feature = "gc")]
138139
pub extern fn #as_ref(a: &#ty) -> Box<crate::wasm_ref_t> {
139140
eprintln!("`{}` is not implemented", stringify!(#as_ref));
140141
std::process::abort();
141142
}
142143

143144
#[doc = #as_ref_const_docs]
144145
#[unsafe(no_mangle)]
146+
#[cfg(feature = "gc")]
145147
pub extern fn #as_ref_const(a: &#ty) -> Box<crate::wasm_ref_t> {
146148
eprintln!("`{}` is not implemented", stringify!(#as_ref_const));
147149
std::process::abort();

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: 3 additions & 0 deletions
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,6 +103,7 @@ template <> struct WasmType<std::optional<ExternRef>> {
102103
p->externref = 0;
103104
}
104105
}
106+
105107
static std::optional<ExternRef> load(Store::Context cx,
106108
wasmtime_val_raw_t *p) {
107109
if (p->externref == 0) {
@@ -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/gc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#ifndef WASMTIME_GC_H
1212
#define WASMTIME_GC_H
1313

14+
#ifdef WASMTIME_FEATURE_GC
15+
1416
#include <wasmtime/val.h>
1517

1618
#ifdef __cplusplus
@@ -593,4 +595,5 @@ WASM_API_EXTERN bool wasmtime_anyref_as_array(wasmtime_context_t *context,
593595
} // extern "C"
594596
#endif
595597

598+
#endif // WASMTIME_FEATURE_GC
596599
#endif // WASMTIME_GC_H

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#ifndef WASMTIME_GC_HH
88
#define WASMTIME_GC_HH
99

10+
#include <wasmtime/config.hh>
11+
12+
#ifdef WASMTIME_FEATURE_GC
1013
#include <vector>
1114
#include <wasmtime/gc.h>
1215
#include <wasmtime/val.hh>
@@ -476,4 +479,6 @@ inline std::optional<ArrayRef> AnyRef::as_array(Store::Context cx) const {
476479

477480
} // namespace wasmtime
478481

482+
#endif // WASMTIME_FEATURE_GC
483+
479484
#endif // WASMTIME_GC_HH

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
@@ -94,6 +94,7 @@ public:
9494
/// Creates a context referencing the provided `Caller`.
9595
Context(Caller *caller);
9696

97+
#ifdef WASMTIME_FEATURE_GC
9798
/// Runs a garbage collection pass in the referenced store to collect loose
9899
/// `externref` values, if any are available.
99100
Result<std::monostate> gc() {
@@ -103,6 +104,7 @@ public:
103104
}
104105
return std::monostate();
105106
}
107+
#endif
106108

107109
/// Injects fuel to be consumed within this store.
108110
///
@@ -253,9 +255,11 @@ public:
253255
/// Explicit function to acquire a `Context` from this store.
254256
Context context() { return this; }
255257

258+
#ifdef WASMTIME_FEATURE_GC
256259
/// Runs a garbage collection pass in the referenced store to collect loose
257260
/// GC-managed objects, if any are available.
258261
Result<std::monostate> gc() { return context().gc(); }
262+
#endif
259263

260264
private:
261265
template <typename F>

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

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

18+
#ifdef WASMTIME_FEATURE_GC
1819
struct wasmtime_eqref;
1920
/// Convenience alias for #wasmtime_eqref
2021
typedef struct wasmtime_eqref wasmtime_eqref_t;
@@ -201,6 +202,7 @@ WASM_API_EXTERN bool wasmtime_anyref_i31_get_s(wasmtime_context_t *context,
201202
* `wasmtime_externref_set_null`. Null can be tested for with the
202203
* `wasmtime_externref_is_null` function.
203204
*/
205+
204206
typedef struct wasmtime_externref {
205207
/// Internal metadata tracking within the store, embedders should not
206208
/// configure or modify these fields.
@@ -367,6 +369,7 @@ WASM_API_EXTERN void wasmtime_exnref_clone(const wasmtime_exnref_t *ref,
367369
* After this call, `ref` is left in an undefined state and should not be used.
368370
*/
369371
WASM_API_EXTERN void wasmtime_exnref_unroot(wasmtime_exnref_t *ref);
372+
#endif // WASMTIME_FEATURE_GC
370373

371374
/// \brief Discriminant stored in #wasmtime_val::kind
372375
typedef uint8_t wasmtime_valkind_t;
@@ -416,6 +419,7 @@ typedef union wasmtime_valunion {
416419
float32_t f32;
417420
/// Field used if #wasmtime_val_t::kind is #WASMTIME_F64
418421
float64_t f64;
422+
#ifdef WASMTIME_FEATURE_GC
419423
/// Field used if #wasmtime_val_t::kind is #WASMTIME_ANYREF
420424
wasmtime_anyref_t anyref;
421425
/// Field used if #wasmtime_val_t::kind is #WASMTIME_EXTERNREF
@@ -427,6 +431,7 @@ typedef union wasmtime_valunion {
427431
/// Use `wasmtime_funcref_is_null` to test whether this is a null function
428432
/// reference.
429433
wasmtime_func_t funcref;
434+
#endif // WASMTIME_FEATURE_GC
430435
/// Field used if #wasmtime_val_t::kind is #WASMTIME_V128
431436
wasmtime_v128 v128;
432437
} wasmtime_valunion_t;

0 commit comments

Comments
 (0)