Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a GC function to the C API #2052

Merged
merged 3 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/c-api/include/wasmtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,17 @@ WASM_API_EXTERN own wasmtime_error_t* wasmtime_wat2wasm(
own wasm_byte_vec_t *ret
);

/**
* \brief Perform garbage collection within the given store.
*
* Garbage collects `externref`s that are used within this store. Any
* `externref`s that are discovered to be unreachable by other code or objects
* will have their finalizers run.
*
* The `store` argument must not be NULL.
*/
WASM_API_EXTERN void wasmtime_store_gc(wasm_store_t* store);

/**
* \typedef wasmtime_linker_t
* \brief Convenience alias for #wasmtime_linker_t
Expand Down
5 changes: 5 additions & 0 deletions crates/c-api/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pub extern "C" fn wasm_store_new(engine: &wasm_engine_t) -> Box<wasm_store_t> {
})
}

#[no_mangle]
pub extern "C" fn wasmtime_store_gc(store: &wasm_store_t) {
store.store.gc();
}

#[repr(C)]
pub struct wasmtime_interrupt_handle_t {
handle: InterruptHandle,
Expand Down
8 changes: 8 additions & 0 deletions examples/externref.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ int main() {
printf("Creating new `externref`...\n");

// Create a new `externref` value.
//
// Note that if you need clean up for after the externref is reclaimed, you
// can use `wasmtime_externref_new_with_finalizer`.
wasm_val_t externref;
wasmtime_externref_new("Hello, World!", &externref);
assert(externref.kind == WASM_ANYREF);
Expand Down Expand Up @@ -148,6 +151,11 @@ int main() {
assert(results[0].kind == WASM_ANYREF);
assert(wasm_ref_same(results[0].of.ref, externref.of.ref));

// We can GC any now-unused references to our externref that the store is
// holding.
printf("GCing within the store...\n");
wasmtime_store_gc(store);

// Clean up after ourselves at this point
printf("All finished!\n");
ret = 0;
Expand Down
3 changes: 3 additions & 0 deletions examples/externref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ fn main() -> Result<()> {
assert!(ret.is_some());
assert!(ret.unwrap().ptr_eq(&externref));

println!("GCing within the store...");
store.gc();

println!("Done.");
Ok(())
}