Skip to content

Commit

Permalink
test/example: add testing constructors and destructors
Browse files Browse the repository at this point in the history
  • Loading branch information
G4Vi committed Feb 23, 2024
1 parent 6d2e713 commit cc52a98
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions rust/examples/reactor-hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,16 @@ pub async fn main() -> anyhow::Result<()> {
f.call(&mut store, &[], &mut []).unwrap();
trace_ctx.shutdown().await;

// call __wasm_call_dtors
// Normally reactors don't emit __wasm_call_dtors, but
// reactor-hello.c.instr.wasm includes it as an example of specifying
// a cleanup function in a reactor.
{
let f = instance
.get_func(&mut store, "__wasm_call_dtors")
.expect("function exists");
f.call(&mut store, &[], &mut []).unwrap();
}

Ok(())
}
11 changes: 11 additions & 0 deletions test/constructor-destructor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>

__attribute__((constructor)) static void constructor(void) {
printf("constructor\n");
}

int main(void) {}

__attribute__((destructor)) static void destructor(void) {
printf("destructor\n");
}
Binary file added test/constructor-destructor.c.instr.wasm
Binary file not shown.
Binary file added test/constructor-destructor.c.wasm
Binary file not shown.
8 changes: 8 additions & 0 deletions test/reactor-hello.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#include <stdio.h>
#define EXPORT_AS(name) __attribute__((export_name(name)))

__attribute__((constructor)) static void constructor(void) {
printf("constructor\n");
}

EXPORT_AS("hello") void hello(void) { printf("hello world\n"); }

EXPORT_AS("__wasm_call_dtors") void __wasm_call_dtors(void) {
printf("real destructor\n");
}
Binary file modified test/reactor-hello.c.instr.wasm
Binary file not shown.
Binary file modified test/reactor-hello.c.wasm
Binary file not shown.

0 comments on commit cc52a98

Please sign in to comment.