Skip to content

Commit 2569780

Browse files
committed
Enables conditional compilation with gc
- Remove gc from default on c-api - Adds fallback implementation for when gc is not enabled
1 parent 0bc0afd commit 2569780

8 files changed

Lines changed: 366 additions & 71 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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ futures = { workspace = true, optional = true }
4242

4343
[features]
4444
# WASMTIME_FEATURE_LIST
45-
default = ["gc"]
4645
async = ['wasmtime/async', 'futures']
4746
profiling = ["wasmtime/profiling"]
4847
cache = ["wasmtime/cache"]

crates/c-api/src/config.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,32 @@ pub extern "C" fn wasmtime_config_wasm_tail_call_set(c: &mut wasm_config_t, enab
9191

9292
#[unsafe(no_mangle)]
9393
pub extern "C" fn wasmtime_config_wasm_reference_types_set(c: &mut wasm_config_t, enable: bool) {
94+
#[cfg(feature = "gc")]
9495
c.config.wasm_reference_types(enable);
96+
97+
#[cfg(not(feature = "gc"))]
98+
let _ = (c, enable);
9599
}
96100

97101
#[unsafe(no_mangle)]
98102
pub extern "C" fn wasmtime_config_wasm_function_references_set(
99103
c: &mut wasm_config_t,
100104
enable: bool,
101105
) {
106+
#[cfg(feature = "gc")]
102107
c.config.wasm_function_references(enable);
108+
109+
#[cfg(not(feature = "gc"))]
110+
let _ = (c, enable);
103111
}
104112

105113
#[unsafe(no_mangle)]
106114
pub extern "C" fn wasmtime_config_wasm_gc_set(c: &mut wasm_config_t, enable: bool) {
115+
#[cfg(feature = "gc")]
107116
c.config.wasm_gc(enable);
117+
118+
#[cfg(not(feature = "gc"))]
119+
let _ = (c, enable);
108120
}
109121

110122
#[unsafe(no_mangle)]
@@ -477,7 +489,11 @@ pub extern "C" fn wasmtime_config_wasm_wide_arithmetic_set(c: &mut wasm_config_t
477489

478490
#[unsafe(no_mangle)]
479491
pub extern "C" fn wasmtime_config_wasm_exceptions_set(c: &mut wasm_config_t, enable: bool) {
492+
#[cfg(feature = "gc")]
480493
c.config.wasm_exceptions(enable);
494+
495+
#[cfg(not(feature = "gc"))]
496+
let _ = (c, enable);
481497
}
482498

483499
#[unsafe(no_mangle)]

crates/c-api/src/func.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ use std::panic::{self, AssertUnwindSafe};
1111
use std::ptr;
1212
use std::str;
1313
use wasmtime::{
14-
AsContext, AsContextMut, Error, Extern, Func, Result, RootScope, StoreContext, StoreContextMut,
14+
AsContext, AsContextMut, Error, Extern, Func, Result, StoreContext, StoreContextMut,
1515
Trap, Val, ValRaw,
1616
};
17+
#[cfg(feature = "gc")]
18+
use wasmtime::RootScope;
1719

1820
#[derive(Clone)]
1921
#[repr(transparent)]
@@ -351,6 +353,8 @@ 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>> {
356+
#[cfg(feature = "gc")]
357+
{
354358
let mut scope = RootScope::new(&mut store);
355359
let mut params = mem::take(&mut scope.as_context_mut().data_mut().wasm_val_storage);
356360
let (wt_params, wt_results) = translate_args(
@@ -385,6 +389,40 @@ pub unsafe extern "C" fn wasmtime_func_call(
385389
None
386390
}
387391
}
392+
}
393+
394+
#[cfg(not(feature = "gc"))]
395+
{
396+
let mut params = mem::take(&mut store.data_mut().wasm_val_storage);
397+
let (wt_params, wt_results) = translate_args(
398+
&mut params,
399+
crate::slice_from_raw_parts(args, nargs)
400+
.iter()
401+
.map(|i| i.to_val_unscoped(&mut store)),
402+
nresults,
403+
);
404+
405+
let result = panic::catch_unwind(AssertUnwindSafe(|| {
406+
func.call(&mut store, wt_params, wt_results)
407+
}));
408+
match result {
409+
Ok(Ok(())) => {
410+
let results = crate::slice_from_raw_parts_mut(results, nresults);
411+
for (slot, val) in results.iter_mut().zip(wt_results.iter()) {
412+
crate::initialize(slot, wasmtime_val_t::from_val_unscoped(&mut store, *val));
413+
}
414+
params.truncate(0);
415+
store.data_mut().wasm_val_storage = params;
416+
None
417+
}
418+
Ok(Err(trap)) => store_err(trap, trap_ret),
419+
Err(panic) => {
420+
let err = error_from_panic(panic);
421+
*trap_ret = Box::into_raw(Box::new(wasm_trap_t::new(err)));
422+
None
423+
}
424+
}
425+
}
388426
}
389427

390428
#[unsafe(no_mangle)]

crates/c-api/src/global.rs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use crate::{
33
wasm_store_t, wasm_val_t, wasmtime_error_t, wasmtime_val_t,
44
};
55
use std::mem::MaybeUninit;
6-
use wasmtime::{Extern, Global, RootScope};
6+
#[cfg(feature = "gc")]
7+
use wasmtime::RootScope;
8+
use wasmtime::{Extern, Global};
79

810
#[derive(Clone)]
911
#[repr(transparent)]
@@ -84,12 +86,24 @@ pub unsafe extern "C" fn wasmtime_global_new(
8486
val: &wasmtime_val_t,
8587
ret: &mut Global,
8688
) -> Option<Box<wasmtime_error_t>> {
87-
let mut scope = RootScope::new(&mut store);
88-
let val = val.to_val(&mut scope);
89-
let global = Global::new(scope, gt.ty().ty.clone(), val);
90-
handle_result(global, |global| {
91-
*ret = global;
92-
})
89+
#[cfg(feature = "gc")]
90+
{
91+
let mut scope = RootScope::new(&mut store);
92+
let val = val.to_val(&mut scope);
93+
let global = Global::new(scope, gt.ty().ty.clone(), val);
94+
handle_result(global, |global| {
95+
*ret = global;
96+
})
97+
}
98+
99+
#[cfg(not(feature = "gc"))]
100+
{
101+
let val = val.to_val_unscoped(&mut store);
102+
let global = Global::new(&mut store, gt.ty().ty.clone(), val);
103+
handle_result(global, |global| {
104+
*ret = global;
105+
})
106+
}
93107
}
94108

95109
#[unsafe(no_mangle)]
@@ -106,9 +120,20 @@ pub extern "C" fn wasmtime_global_get(
106120
global: &Global,
107121
val: &mut MaybeUninit<wasmtime_val_t>,
108122
) {
109-
let mut scope = RootScope::new(store);
110-
let gval = global.get(&mut scope);
111-
crate::initialize(val, wasmtime_val_t::from_val(&mut scope, gval))
123+
let mut store = store;
124+
125+
#[cfg(feature = "gc")]
126+
{
127+
let mut scope = RootScope::new(&mut store);
128+
let gval = global.get(&mut scope);
129+
crate::initialize(val, wasmtime_val_t::from_val(&mut scope, gval))
130+
}
131+
132+
#[cfg(not(feature = "gc"))]
133+
{
134+
let gval = global.get(&mut store);
135+
crate::initialize(val, wasmtime_val_t::from_val_unscoped(&mut store, gval))
136+
}
112137
}
113138

114139
#[unsafe(no_mangle)]
@@ -117,7 +142,16 @@ pub unsafe extern "C" fn wasmtime_global_set(
117142
global: &Global,
118143
val: &wasmtime_val_t,
119144
) -> Option<Box<wasmtime_error_t>> {
120-
let mut scope = RootScope::new(&mut store);
121-
let val = val.to_val(&mut scope);
122-
handle_result(global.set(scope, val), |()| {})
145+
#[cfg(feature = "gc")]
146+
{
147+
let mut scope = RootScope::new(&mut store);
148+
let val = val.to_val(&mut scope);
149+
handle_result(global.set(scope, val), |()| {})
150+
}
151+
152+
#[cfg(not(feature = "gc"))]
153+
{
154+
let val = val.to_val_unscoped(&mut store);
155+
handle_result(global.set(&mut store, val), |()| {})
156+
}
123157
}

0 commit comments

Comments
 (0)