Skip to content

Commit b2523c7

Browse files
authored
Extracting parts from #855 (#858)
* better random token generation for caller hashmap * open up github workflow runs to all pushes and adding some install steps to elixir-ci * fun with CI * take back workflow/GH actions changes. i'm not smart enough for this
1 parent 5b8e368 commit b2523c7

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

.github/workflows/elixir-ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,16 @@ jobs:
120120
run: |
121121
rustup target add wasm32-unknown-unknown
122122
rustup target add wasm32-wasip1
123+
rustup target add wasm32-wasip2
123124
124125
- name: "Install cargo-component"
125126
run: cargo install --locked cargo-component
126127
shell: bash
127128

129+
- name: "Install wasm-tools"
130+
run: cargo install --locked wasm-tools
131+
shell: bash
132+
128133
- uses: actions/cache@v4
129134
with:
130135
path: |

.github/workflows/rust-ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ on:
77
push:
88
branches:
99
- main
10+
paths:
11+
- "native/wasmex/**"
1012

1113
defaults:
1214
run:

native/wasmex/src/caller.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,23 @@ pub(crate) fn get_caller_mut(token: &mut i32) -> Option<&mut Caller<'_, StoreDat
3030

3131
pub(crate) fn set_caller(caller: Caller<StoreData>) -> i32 {
3232
let mut map = CALLER_MAP.lock().unwrap();
33-
// TODO: prevent duplicates by throwing the dice again when the id is already known
34-
let token = rand::random();
33+
let token = generate_unique_random_token(&map);
34+
3535
let caller =
3636
unsafe { std::mem::transmute::<Caller<'_, StoreData>, Caller<'static, StoreData>>(caller) };
3737
map.insert(token, caller);
3838
token
3939
}
4040

41+
fn generate_unique_random_token(map: &HashMap<i32, Caller<'_, StoreData>>) -> i32 {
42+
loop {
43+
let token: i32 = rand::random();
44+
if !map.contains_key(&token) {
45+
break token;
46+
}
47+
}
48+
}
49+
4150
pub(crate) fn remove_caller(token: i32) {
4251
let mut map = CALLER_MAP.lock().unwrap();
4352
map.remove(&token);

0 commit comments

Comments
 (0)