|
3 | 3 | import java.lang.reflect.Method;
|
4 | 4 |
|
5 | 5 | public class WasmStore extends AbstractOpaquePtr {
|
| 6 | + // Store is !Send and !Sync in Rust, we will enforce that with a ThreadLocal |
| 7 | + // TODO: do we need stackable stores? |
| 8 | + private static final ThreadLocal<Long> STORE = new ThreadLocal<Long>(); |
| 9 | + |
6 | 10 | WasmStore(long ptr) {
|
7 |
| - super(ptr, WasmStore::freeStore); |
| 11 | + super(ptr, WasmStore::freeStoreChecked); |
| 12 | + if (STORE.get() != null) { |
| 13 | + throw new IllegalStateException("Previous STORE not cleared for this thread"); |
| 14 | + } |
| 15 | + |
| 16 | + STORE.set(ptr); |
8 | 17 | }
|
9 | 18 |
|
10 | 19 | private static native void freeStore(long ptr);
|
11 | 20 |
|
12 | 21 | private static native long newLinkerNtv(long store_ptr);
|
13 | 22 |
|
| 23 | + private static void verifyStore(long ptr) { |
| 24 | + if (STORE.get() != ptr) { |
| 25 | + throw new IllegalStateException(String.format("STORE expected %d got %d", STORE.get(), ptr)); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public long getPtr() { |
| 31 | + long ptr = super.getPtr(); |
| 32 | + verifyStore(ptr); |
| 33 | + |
| 34 | + return ptr; |
| 35 | + } |
| 36 | + |
14 | 37 | public WasmLinker newLinker() {
|
| 38 | + if (STORE.get() == null) { |
| 39 | + throw new IllegalStateException("STORE not available for this thread, did a new thread get started?"); |
| 40 | + } |
| 41 | + |
15 | 42 | return new WasmLinker(WasmStore.newLinkerNtv(this.getPtr()));
|
16 | 43 | }
|
| 44 | + |
| 45 | + public static void freeStoreChecked(long ptr) { |
| 46 | + if (STORE.get() != ptr) { |
| 47 | + throw new IllegalStateException(String.format("STORE expected %d got %d", STORE.get(), ptr)); |
| 48 | + } |
| 49 | + |
| 50 | + WasmStore.freeStore(ptr); |
| 51 | + STORE.remove(); |
| 52 | + } |
17 | 53 | }
|
0 commit comments