Skip to content

Commit 6aaeaea

Browse files
committed
fix rust guest to actually mutate shared state
1 parent 9a45c2f commit 6aaeaea

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

test-programs/src/bin/test.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@ wit_bindgen::generate!({
88
}"
99
});
1010

11-
const IS_INITIALIZED: Cell<bool> = Cell::new(false);
11+
// rustc won't allow using a a Cell in a static, but since this is a component (which is always
12+
// single-threaded) we can ignore this and mark it send and sync.
13+
struct MakeSendSync<T>(T);
14+
unsafe impl<T> Send for MakeSendSync<T> {}
15+
unsafe impl<T> Sync for MakeSendSync<T> {}
16+
17+
static IS_INITIALIZED: MakeSendSync<Cell<bool>> = MakeSendSync(Cell::new(false));
1218

1319
struct S;
1420
impl Guest for S {
1521
fn component_init() {
16-
let before = IS_INITIALIZED.replace(true);
22+
let before = IS_INITIALIZED.0.replace(true);
1723
assert!(!before, "component should only be initialized once");
1824
}
1925
}
2026

2127
export!(S);
2228

2329
fn main() {
24-
let initialized = IS_INITIALIZED.get();
30+
let initialized = IS_INITIALIZED.0.get();
2531
assert!(initialized, "component was not initialized")
2632
}

wasmtime/tests/rust.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,9 @@ async fn init_rust() -> Result<()> {
8484

8585
let initialized_component = component_init_wasmtime::initialize(&component).await?;
8686

87-
/*
88-
// TODO: execute `component`. it should not trap.
8987
execute(&initialized_component)
9088
.await
9189
.context("execute initialized component")?;
92-
*/
9390

9491
Ok(())
9592
}

0 commit comments

Comments
 (0)