Skip to content

Commit 3b454fd

Browse files
authored
WASM support (#1)
* WASM support * Enable debug-assertions in WASM tests * Fix up README
1 parent 8fdd1de commit 3b454fd

File tree

11 files changed

+436
-110
lines changed

11 files changed

+436
-110
lines changed

.github/workflows/rust.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ env:
1111

1212
jobs:
1313
build-std:
14-
name: Build and test (std)
14+
name: Build and test
1515
runs-on: ${{ matrix.os }}
1616
strategy:
1717
matrix:
@@ -28,7 +28,7 @@ jobs:
2828
- name: Run dylib-tests
2929
run: cargo test -p dylib-tests --features debug-assertions
3030
build-miri:
31-
name: Build and test (Miri)
31+
name: Miri
3232
runs-on: ubuntu-latest
3333
steps:
3434
- uses: actions/checkout@v4
@@ -37,6 +37,21 @@ jobs:
3737
components: miri
3838
- name: Build
3939
run: cargo +nightly miri test --features debug-assertions
40+
wasm:
41+
name: WASM
42+
runs-on: ubuntu-latest
43+
steps:
44+
- uses: actions/checkout@v4
45+
- name: Install wasm-pack
46+
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
47+
- name: Test stringleton-registry (Chrome)
48+
run: cd stringleton-registry && wasm-pack test --headless --chrome --features debug-assertions && cd ..
49+
- name: Test stringleton (Chrome)
50+
run: cd stringleton && wasm-pack test --headless --chrome --features debug-assertions && cd ..
51+
- name: Test stringleton-registry (Firefox)
52+
run: cd stringleton-registry && wasm-pack test --headless --firefox --features debug-assertions && cd ..
53+
- name: Test stringleton (Firefox)
54+
run: cd stringleton && wasm-pack test --headless --firefox --features debug-assertions && cd ..
4055
lint:
4156
name: Lint
4257
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 260 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ The latter can be supported by the `spin` and `critical-section` features:
181181

182182
Do not use these features unless you are familiar with the tradeoffs.
183183

184+
## WASM caveats
185+
186+
`stringleton` works in WASM binaries, but since the `wasm32-unknown-unknown`
187+
does not support static constructors, the `sym!(..)` macro will fall back to a
188+
slightly slower implementation that uses atomics and a single branch. (Note that
189+
WASM is normally single-threaded, so atomic operations have no overhead.)
190+
191+
Please note that it is *not* possible to pass a `Symbol` across a WASM boundary,
192+
because the host and the guest have different views of memory, and use separate
193+
registries. However, it is possible to pass an opaque `u64` representing the
194+
symbol across such a boundary using `Symbol::to_ffi()` and
195+
`Symbol::try_from_ffi()`. Getting the string representation of the symbol is
196+
only possible on the side that owns the symbol.
197+
184198
## Name
185199

186200
The name is a portmanteau of "string" and "singleton".

stringleton-registry/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ spin = { version = "0.9.8", optional = true, default-features = false, features
2525
"rwlock",
2626
] }
2727

28+
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
29+
wasm-bindgen-test = "0.3"
30+
2831
[features]
2932
default = ["std"]
3033
std = ["alloc"]

0 commit comments

Comments
 (0)