Skip to content

Commit f071c7e

Browse files
authored
Added support for the wasm32v1-none target (#4277)
This PR adds support for the `wasm32v1-none` target and should enable all features previously behind `cfg(feature = "std")` for both `wasm32-unknown-unknown` and `wasm32v1-none`. Additionally, now `js-sys`, `web-sys`, `wasm-bindgen-futures` and `wasm-bindgen-test` support `no_std`. Changes: - Use `any(target_os = "unknown", target_os = "none")` instead of `target_os = "unknown"`. - For globals: - Continue to use `once_cell::sync::Lazy` when targeting `std`. - Use `Send + Sync` wrapped `once_cell::unsync::Lazy` when targeting `no_std` without `target_feature = "atomics"`. - Use `once_cell/critical_section` when targeting `no_std` with `target_feature = "atomics"`. As recommended by `critical_section`, this is something the user has to enable. Notable this only affects `link_to!`, any other feature will work as expected with no additional work required from the user. - For thread locals: - Continue to use `std::thread_local!` when targeting `std`. - Use `static mut` when targeting `no_std` without `target_feature = "atomics"`. - Use `#[thread_local]` when targeting `no_std` with `target_feature = "atomics"`. - Add `std` crate feature to `js-sys`, `web-sys`, `wasm-bindgen-futures` and `wasm-bindgen-test` and enable by default. This makes it possible to use these crates with `no_std` for both targets as well. - Inlined [`console_error_panic_hook`](https://crates.io/crates/console_error_panic_hook) and a `no_std` version of [`scoped_tls`](https://crates.io/crates/scoped-tls). Missing: - Add `wasm32v1-none` to CI. Probably in a follow-up when its stable, ergo Rust v1.84.
1 parent fe5d89f commit f071c7e

File tree

575 files changed

+2254
-1565
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

575 files changed

+2254
-1565
lines changed

.cargo/config.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[target.wasm32-unknown-unknown]
1+
[target.'cfg(target_arch = "wasm32")']
22
runner = 'cargo run -p wasm-bindgen-cli --bin wasm-bindgen-test-runner --'
33

44
[target.'cfg(all())']

.github/workflows/main.yml

+31-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,37 @@ jobs:
9292
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p js-sys -- -D warnings
9393
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p web-sys -- -D warnings
9494

95+
# Run `cargo clippy` over crates that support `no_std`
96+
clippy_no_std:
97+
name: Clippy `no_std`
98+
runs-on: ubuntu-latest
99+
steps:
100+
- uses: actions/checkout@v4
101+
- run: rustup update --no-self-update stable && rustup default stable
102+
- run: rustup target add wasm32-unknown-unknown
103+
- run: cargo clippy --no-deps --no-default-features --target wasm32-unknown-unknown -p wasm-bindgen -- -D warnings
104+
- run: cargo clippy --no-deps --no-default-features --target wasm32-unknown-unknown -p js-sys -- -D warnings
105+
- run: cargo clippy --no-deps --no-default-features --target wasm32-unknown-unknown -p web-sys -- -D warnings
106+
- run: cargo clippy --no-deps --no-default-features --target wasm32-unknown-unknown -p wasm-bindgen-futures -- -D warnings
107+
- run: cargo clippy --no-deps --no-default-features --target wasm32-unknown-unknown -p wasm-bindgen-test -- -D warnings
108+
109+
# Run `cargo clippy` over crates that support `no_std` with `target_feature = "atomics"` support.
110+
clippy_no_std_atomics:
111+
name: Clippy `no_std` with `atomics`
112+
runs-on: ubuntu-latest
113+
env:
114+
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUSTFLAGS: -Ctarget-feature=+atomics,+bulk-memory
115+
steps:
116+
- uses: actions/checkout@v4
117+
- run: rustup default nightly-2024-07-06
118+
- run: rustup target add wasm32-unknown-unknown
119+
- run: rustup component add clippy rust-src
120+
- run: cargo clippy --no-deps --no-default-features --target wasm32-unknown-unknown -p wasm-bindgen -Zbuild-std=core,alloc -- -D warnings
121+
- run: cargo clippy --no-deps --no-default-features --target wasm32-unknown-unknown -p js-sys -Zbuild-std=core,alloc -- -D warnings
122+
- run: cargo clippy --no-deps --no-default-features --target wasm32-unknown-unknown -p web-sys -Zbuild-std=core,alloc -- -D warnings
123+
- run: cargo clippy --no-deps --no-default-features --target wasm32-unknown-unknown -p wasm-bindgen-futures --features once_cell/critical-section -Zbuild-std=core,alloc -- -D warnings
124+
- run: cargo clippy --no-deps --no-default-features --target wasm32-unknown-unknown -p wasm-bindgen-test --features once_cell/critical-section -Zbuild-std=core,alloc -- -D warnings
125+
95126
# Run `cargo clippy` over the project
96127
clippy_project:
97128
name: Clippy (project)
@@ -100,7 +131,6 @@ jobs:
100131
- uses: actions/checkout@v4
101132
- run: rustup update --no-self-update stable && rustup default stable
102133
- run: rustup target add wasm32-unknown-unknown
103-
- run: cargo clippy --no-deps --no-default-features --target wasm32-unknown-unknown -- -D warnings
104134
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -- -D warnings
105135
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown --tests -- -D warnings
106136
- run: for i in examples/*/; do cd "$i"; cargo +stable clippy --no-deps --all-features --target wasm32-unknown-unknown -- -D warnings || exit 1; cd ../..; done

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
* Added WASM ABI support for `u128` and `i128`
2424
[#4222](https://github.com/rustwasm/wasm-bindgen/pull/4222)
2525

26+
* Added support for the `wasm32v1-none` target.
27+
[#4277](https://github.com/rustwasm/wasm-bindgen/pull/4277)
28+
29+
* Added support for `no_std` to `js-sys`, `web-sys`, `wasm-bindgen-futures` and `wasm-bindgen-test`.
30+
[#4277](https://github.com/rustwasm/wasm-bindgen/pull/4277)
31+
32+
* Added support for `no_std` to `link_to!`, `static_string` (via `thread_local_v2`) and `throw`.
33+
[#4277](https://github.com/rustwasm/wasm-bindgen/pull/4277)
34+
2635
### Changed
2736

2837
* String enums now generate private TypeScript types but only if used.
@@ -46,6 +55,9 @@
4655
* `wasm-bindgen-test-runner` now tries to restart the WebDriver on failure, instead of spending its timeout period trying to connect to a non-existing WebDriver.
4756
[#4267](https://github.com/rustwasm/wasm-bindgen/pull/4267)
4857

58+
* Deprecated `#[wasm_bindgen(thread_local)]` in favor of `#[wasm_bindgen(thread_local_v2)]`, which creates a `wasm_bindgen::JsThreadLocal`. It is similar to `std::thread::LocalKey` but supports `no_std`.
59+
[#4277](https://github.com/rustwasm/wasm-bindgen/pull/4277)
60+
4961
### Fixed
5062

5163
* Fixed methods with `self: &Self` consuming the object.

Cargo.toml

+8-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ default = ["spans", "std"]
2626
enable-interning = ["std"]
2727
serde-serialize = ["serde", "serde_json", "std"]
2828
spans = ["wasm-bindgen-macro/spans"]
29-
std = []
29+
std = ["wasm-bindgen-macro/std", "once_cell/std"]
3030

3131
# Whether or not the `#[wasm_bindgen]` macro is strict and generates an error on
3232
# all unused attributes
@@ -42,10 +42,15 @@ xxx_debug_only_print_generated_code = ["wasm-bindgen-macro/xxx_debug_only_print_
4242

4343
[dependencies]
4444
cfg-if = "1.0.0"
45-
once_cell = "1.12"
45+
once_cell = { version = "1.12", default-features = false }
4646
serde = { version = "1.0", optional = true }
4747
serde_json = { version = "1.0", optional = true }
48-
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.95" }
48+
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.95", default-features = false }
49+
50+
[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"), target_feature = "atomics"))'.dependencies]
51+
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.95", default-features = false, features = [
52+
"atomics",
53+
] }
4954

5055
[dev-dependencies]
5156
wasm-bindgen-test = { path = 'crates/test' }

crates/backend/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ rust-version = "1.57"
1414
version = "0.2.95"
1515

1616
[features]
17+
atomics = []
18+
default = ["std"]
1719
extra-traits = ["syn/extra-traits"]
1820
spans = []
21+
std = []
1922

2023
[dependencies]
2124
bumpalo = "3.0.0"

crates/backend/src/ast.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,17 @@ pub struct ImportStatic {
275275
pub js_name: String,
276276
/// Path to wasm_bindgen
277277
pub wasm_bindgen: Path,
278-
/// [`true`] if using the new `thread_local` representation.
279-
pub thread_local: bool,
278+
/// Version of `thread_local`, if any.
279+
pub thread_local: Option<ThreadLocal>,
280+
}
281+
282+
/// Which version of the `thread_local` attribute is enabled.
283+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
284+
pub enum ThreadLocal {
285+
/// V1.
286+
V1,
287+
/// V2.
288+
V2,
280289
}
281290

282291
/// The type of a static string being imported
@@ -297,6 +306,8 @@ pub struct ImportString {
297306
pub js_sys: Path,
298307
/// The string to export.
299308
pub string: String,
309+
/// Version of `thread_local`.
310+
pub thread_local: ThreadLocal,
300311
}
301312

302313
/// The metadata for a type being imported

crates/backend/src/codegen.rs

+66-26
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl TryToTokens for ast::Program {
163163
let prefix_json_bytes = syn::LitByteStr::new(&prefix_json_bytes, Span::call_site());
164164

165165
(quote! {
166-
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
166+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
167167
#[automatically_derived]
168168
const _: () = {
169169
use #wasm_bindgen::__rt::{flat_len, flat_byte_slices};
@@ -201,9 +201,10 @@ impl TryToTokens for ast::LinkToModule {
201201
#program
202202
#extern_fn
203203

204-
static __VAL: #wasm_bindgen::__rt::Lazy<String> = #wasm_bindgen::__rt::Lazy::new(|| unsafe {
205-
<#wasm_bindgen::__rt::alloc::string::String as #wasm_bindgen::convert::FromWasmAbi>::from_abi(#name().join())
206-
});
204+
static __VAL: #wasm_bindgen::__rt::once_cell::sync::Lazy<#wasm_bindgen::__rt::alloc::string::String> =
205+
#wasm_bindgen::__rt::once_cell::sync::Lazy::new(|| unsafe {
206+
<#wasm_bindgen::__rt::alloc::string::String as #wasm_bindgen::convert::FromWasmAbi>::from_abi(#name().join())
207+
});
207208

208209
#wasm_bindgen::__rt::alloc::string::String::clone(&__VAL)
209210
}
@@ -275,12 +276,12 @@ impl ToTokens for ast::Struct {
275276
let ptr = #wasm_bindgen::convert::IntoWasmAbi::into_abi(value);
276277

277278
#[link(wasm_import_module = "__wbindgen_placeholder__")]
278-
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
279+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
279280
extern "C" {
280281
fn #new_fn(ptr: u32) -> u32;
281282
}
282283

283-
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
284+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
284285
unsafe fn #new_fn(_: u32) -> u32 {
285286
panic!("cannot convert to JsValue outside of the Wasm target")
286287
}
@@ -292,7 +293,7 @@ impl ToTokens for ast::Struct {
292293
}
293294
}
294295

295-
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
296+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
296297
#[automatically_derived]
297298
const _: () = {
298299
#[no_mangle]
@@ -381,12 +382,12 @@ impl ToTokens for ast::Struct {
381382
let idx = #wasm_bindgen::convert::IntoWasmAbi::into_abi(&value);
382383

383384
#[link(wasm_import_module = "__wbindgen_placeholder__")]
384-
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
385+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
385386
extern "C" {
386387
fn #unwrap_fn(ptr: u32) -> u32;
387388
}
388389

389-
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
390+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
390391
unsafe fn #unwrap_fn(_: u32) -> u32 {
391392
panic!("cannot convert from JsValue outside of the Wasm target")
392393
}
@@ -493,7 +494,7 @@ impl ToTokens for ast::StructField {
493494
(quote! {
494495
#[automatically_derived]
495496
const _: () = {
496-
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), no_mangle)]
497+
#[cfg_attr(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), no_mangle)]
497498
#[doc(hidden)]
498499
#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
499500
pub unsafe extern "C" fn #getter(js: u32)
@@ -532,7 +533,7 @@ impl ToTokens for ast::StructField {
532533
let (args, names) = splat(wasm_bindgen, &Ident::new("val", rust_name.span()), &abi);
533534

534535
(quote! {
535-
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
536+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
536537
#[automatically_derived]
537538
const _: () = {
538539
#[no_mangle]
@@ -791,7 +792,7 @@ impl TryToTokens for ast::Export {
791792
const _: () = {
792793
#(#attrs)*
793794
#[cfg_attr(
794-
all(target_arch = "wasm32", target_os = "unknown"),
795+
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
795796
export_name = #export_name,
796797
)]
797798
#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
@@ -1066,11 +1067,11 @@ impl ToTokens for ast::ImportType {
10661067
impl JsCast for #rust_name {
10671068
fn instanceof(val: &JsValue) -> bool {
10681069
#[link(wasm_import_module = "__wbindgen_placeholder__")]
1069-
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
1070+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
10701071
extern "C" {
10711072
fn #instanceof_shim(val: u32) -> u32;
10721073
}
1073-
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
1074+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
10741075
unsafe fn #instanceof_shim(_: u32) -> u32 {
10751076
panic!("cannot check instanceof on non-wasm targets");
10761077
}
@@ -1675,14 +1676,15 @@ impl ToTokens for ast::ImportStatic {
16751676
fn to_tokens(&self, into: &mut TokenStream) {
16761677
let ty = &self.ty;
16771678

1678-
if self.thread_local {
1679+
if let Some(thread_local) = self.thread_local {
16791680
thread_local_import(
16801681
&self.vis,
16811682
&self.rust_name,
16821683
&self.wasm_bindgen,
16831684
ty,
16841685
ty,
16851686
&self.shim,
1687+
thread_local,
16861688
)
16871689
.to_tokens(into)
16881690
} else {
@@ -1695,7 +1697,7 @@ impl ToTokens for ast::ImportStatic {
16951697

16961698
into.extend(quote! {
16971699
#[automatically_derived]
1698-
#[deprecated = "use with `#[wasm_bindgen(thread_local)]` instead"]
1700+
#[deprecated = "use with `#[wasm_bindgen(thread_local_v2)]` instead"]
16991701
});
17001702
into.extend(
17011703
quote_spanned! { name.span() => #vis static #name: #wasm_bindgen::JsStatic<#ty> = {
@@ -1735,6 +1737,7 @@ impl ToTokens for ast::ImportString {
17351737
&actual_ty,
17361738
&self.ty,
17371739
&self.shim,
1740+
self.thread_local,
17381741
)
17391742
.to_tokens(into);
17401743
}
@@ -1747,15 +1750,52 @@ fn thread_local_import(
17471750
actual_ty: &syn::Type,
17481751
ty: &syn::Type,
17491752
shim_name: &Ident,
1753+
thread_local: ast::ThreadLocal,
17501754
) -> TokenStream {
17511755
let init = static_init(wasm_bindgen, ty, shim_name);
17521756

1753-
quote! {
1754-
thread_local! {
1755-
#[automatically_derived]
1756-
#vis static #name: #actual_ty = {
1757-
#init
1757+
match thread_local {
1758+
ast::ThreadLocal::V1 => quote! {
1759+
thread_local! {
1760+
#[automatically_derived]
1761+
#[deprecated = "use with `#[wasm_bindgen(thread_local_v2)]` instead"]
1762+
#vis static #name: #actual_ty = {
1763+
#init
1764+
};
1765+
}
1766+
},
1767+
ast::ThreadLocal::V2 => {
1768+
#[cfg(feature = "std")]
1769+
let inner = quote! {
1770+
thread_local!(static _VAL: #actual_ty = init(););
1771+
#wasm_bindgen::JsThreadLocal {
1772+
__inner: &_VAL,
1773+
}
1774+
};
1775+
#[cfg(all(not(feature = "std"), not(feature = "atomics")))]
1776+
let inner = quote! {
1777+
static _VAL: #wasm_bindgen::__rt::LazyCell<#actual_ty> = #wasm_bindgen::__rt::LazyCell::new(init);
1778+
#wasm_bindgen::JsThreadLocal {
1779+
__inner: &_VAL,
1780+
}
1781+
};
1782+
#[cfg(all(not(feature = "std"), feature = "atomics"))]
1783+
let inner = quote! {
1784+
#[thread_local]
1785+
static _VAL: #wasm_bindgen::__rt::LazyCell<#actual_ty> = #wasm_bindgen::__rt::LazyCell::new(init);
1786+
#wasm_bindgen::JsThreadLocal {
1787+
__inner: || unsafe { #wasm_bindgen::__rt::LazyCell::force(&_VAL) as *const #actual_ty },
1788+
}
17581789
};
1790+
1791+
quote! {
1792+
#vis static #name: #wasm_bindgen::JsThreadLocal<#actual_ty> = {
1793+
fn init() -> #actual_ty {
1794+
#init
1795+
}
1796+
#inner
1797+
};
1798+
}
17591799
}
17601800
}
17611801
}
@@ -1766,12 +1806,12 @@ fn static_init(wasm_bindgen: &syn::Path, ty: &syn::Type, shim_name: &Ident) -> T
17661806
};
17671807
quote! {
17681808
#[link(wasm_import_module = "__wbindgen_placeholder__")]
1769-
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
1809+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
17701810
extern "C" {
17711811
fn #shim_name() -> #abi_ret;
17721812
}
17731813

1774-
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
1814+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
17751815
unsafe fn #shim_name() -> #abi_ret {
17761816
panic!("cannot access imported statics on non-wasm targets")
17771817
}
@@ -1818,7 +1858,7 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
18181858
let attrs = &self.attrs;
18191859
let wasm_bindgen = &self.wasm_bindgen;
18201860
(quote! {
1821-
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
1861+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
18221862
#[automatically_derived]
18231863
const _: () = {
18241864
#(#attrs)*
@@ -1845,14 +1885,14 @@ fn extern_fn(
18451885
abi_ret: TokenStream,
18461886
) -> TokenStream {
18471887
quote! {
1848-
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
1888+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
18491889
#(#attrs)*
18501890
#[link(wasm_import_module = "__wbindgen_placeholder__")]
18511891
extern "C" {
18521892
fn #import_name(#(#abi_arguments),*) -> #abi_ret;
18531893
}
18541894

1855-
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
1895+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
18561896
unsafe fn #import_name(#(#abi_arguments),*) -> #abi_ret {
18571897
#(
18581898
drop(#abi_argument_names);

crates/cli/tests/reference/anyref-import-catch.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
(type (;2;) (func (result i32 i32)))
55
(type (;3;) (func (param i32)))
66
(import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0)))
7-
(func $__externref_table_dealloc (;1;) (type 3) (param i32))
8-
(func $__externref_table_alloc (;2;) (type 1) (result i32))
9-
(func $__wbindgen_exn_store (;3;) (type 3) (param i32))
7+
(func $__wbindgen_exn_store (;1;) (type 3) (param i32))
8+
(func $__externref_table_dealloc (;2;) (type 3) (param i32))
9+
(func $__externref_table_alloc (;3;) (type 1) (result i32))
1010
(func $"exported multivalue shim" (;4;) (type 2) (result i32 i32))
1111
(table (;0;) 128 externref)
1212
(memory (;0;) 17)

0 commit comments

Comments
 (0)