325 Commits

Author SHA1 Message Date
Thierry Berger
a9f06701c7 add cargo config alternative to set unstable rustflags (#3905) 2024-04-01 08:53:46 +02:00
battmdpkq
e22f754e06 fix some comments (#3875) 2024-03-08 11:16:05 +01:00
daxpedda
983ec579a3 Add NonNull<T> as parameter (#3857) 2024-03-02 09:11:13 +01:00
Doug A
002307746e Update passing-rust-closures-to-js.md (#3859) 2024-02-28 15:59:05 +01:00
daxpedda
c80bf9a323 Add support for Option<*const T>, Option<*mut T> and NonNull<T> (#3852)
Co-authored-by: Liam Murphy <43807659+Liamolucko@users.noreply.github.com>
2024-02-26 11:49:55 +01:00
daxpedda
3e469b2654 Deprecate --weak-refs in favor of run-time detection (#3822)
* Deprecate `--weak-refs` in favor of run-time detection

* Generate empty functions instead

* Fix integration tests

* Improve `Closure::into_js_value()` docs
2024-02-05 09:01:50 +01:00
Martin
9e699ac45d Add deployment note for first wasm-pack build --target web example (#3819) 2024-02-02 19:44:39 +01:00
Akiomi Kamakura
fb518d3b19 Fix example code in guide (#3813) 2024-01-26 10:20:47 +01:00
Gabriel Grant
034fb355c3 Add step to update CHANGELOG in publishing.md (#3765) 2024-01-25 09:11:35 +01:00
daxpedda
0979fea92b Add support for testing in more worker types (#3804)
Co-authored-by: Liam Murphy <43807659+Liamolucko@users.noreply.github.com>
2024-01-23 10:35:26 +01:00
Yann Dirson
bb9456735e Point to component model instead of host bindings (#3746)
* Point to component model instead of host bindings (fixes #3143)

The original https://github.com/WebAssembly/host-bindings link in the doc
is indeed redirected to https://github.com/WebAssembly/interface-types,
which is the inactive one. There the "explainer" sheet says work moved to
https://github.com/WebAssembly/component-model.
2023-12-17 10:14:17 +01:00
Chris Couzens
45039ff8c1 Correct code formatting contribution instructions (#3711) 2023-11-20 11:50:02 +01:00
kazuhiko kikuchi
bfe644fe13 rename main -> start on wasm-bindgen(start) example (#3648) 2023-10-11 09:40:58 +02:00
flumm
ba244b92e1 guide: add missing and fix incorrect testing info (#3629)
* guide: add missing and fix incorrect testing info

in the contributing section.

Some tests need `WASM_BINDGEN_SPLIT_LINKED_MODULES` set to run through,
and the macro ui-tests are not a workspace package anymore

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>

* guide: improve macro test invocation

by using `-p wasm-bindgen-macro` instead of changing dir to `crates/macro`

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>

---------

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2023-09-21 19:37:14 +10:00
Liam Murphy
51e89ebee5 Make wasm-bindgen compatible with the standard C ABI (#3595)
* Make `wasm-bindgen` compatible with the standard C ABI

* Update the guide

* Fix reference tests

For some reason a couple of functions got switched around; no actual
meaningful changes.

* Add changelog entry

* Add spacing

* Mention `WasmAbi` in the changelog

* fix a tiny typo
2023-09-12 22:24:36 +10:00
Liam Murphy
4d4851d452 Allow creating Vecs of and implement TryFrom<JsValue> for strings and exported Rust types (#3554)
* Enable passing String vectors and boxed slices across ABI

This is accomplished via conversion of the Strings to/from JsValues.

* Enable passing custom struct vectors over ABI

This was done by converting the structs to/from JsValues. It was
necessary to change the way relevant traits (e.g. WasmDescribe,
IntoWasmAbi etc.) are implemented. It's impossible to implement these
for `Box<[#name]>` in codegen.rs because implementing traits on generic
types is only allowed in the crate in which the trait is defined.
Naively adding a blanket implementation on the wasm-bindgen side doesn't
work either because it conflicts with the implementation for JsObjects.
The solution was to create traits like VectorIntoWasmAbi etc. that are
defined on the concrete type and contain the implementation for
IntoWasmAbi etc. for vectors of that type. JsObjects are blanket
implemented as before, and struct types are implemented in codegen.rs.
Due to the way these traits are defined, Rust requires implementing
types to be Sized, so they can't be used for the existing String
implementations.

Converting structs from JsValues was accomplished by adding an unwrap
function to the generated JavaScript class, and calling that from Rust.

* Remove unneeded require

* Move uses out of if_std

* Add documentation

* Move incorrect use statements

* Fix mistake in comment

* Throw on invalid array elements instead of silently removing them

I put some work into making sure that you can tell what function the error message is coming from. You still have to dig into the call stack of the error message to see it, but hopefully that's not too big an ask?

* Get rid of `JsValueVector`

The main reason for this, which I didn't mention before, is that I found it really confusing when I was originally reviewing this PR what the difference was between `JsValueVector` and `Vector{From,Into}WasmAbi`, since it really looks like another conversion trait at first glance.

* Respect the configured `wasm_bindgen` crate path

* Change the error type for String and rust structs' TryFrom<JsValue> impls to JsValue

* test string vecs too

* Refactor `String` impls

I moved the `TryFrom<JsValue>` impl out of convert/slices.rs, it doesn't
really belong there, and also got rid of the `js_value_vectors!` macro
in favour of just implementing it for `String` directly; there's not
much point in a macro you only use for one type.

* Don't require manual `OptionVector{From,Into}WasmAbi` impls

I noticed that strings and rust structs weren't implementing
`OptionVectorFromWasmAbi`, so I tried to make a failing test and... it
worked.

That threw me for a loop for a bit until I realised that it was because
I'd used `Vec<T>`, and `Vec<T>`'s impls of `Option{From,Into}WasmAbi`
didn't actually rely on `Box<[T]>` implementing the traits: they just
required that it implemented `{From,Into}WasmAbi` with an ABI of
`WasmSlice`, since from there the element type doesn't matter. So then
I thought 'well, why not do that for `Box<[T]>` too?

so that's how this commit's pile of new tests came to be.

* fix clippy

* Fix generated typescript

Since vecs of strings and rust structs were describing themselves as `Box<[JsValue]>`, they got typed as such - as `any[]`. This fixes that by using `NAMED_EXTERNREF` instead of just plain `EXTERNREF` with the type we want.

This is maybe _slightly_ sketchy, since `NAMED_EXTERNREF` is meant for imported JS types, but ehhh it's fine. You can already put arbitrary TypeScript in there with `typescript_type`.

* reorder some impls

This is the nitpickiest of nitpicks, but this is my PR goddammit and I can do what I want :)

* Update schema hash

I didn't actually bump the schema version because it didn't change. If you don't use the `TryFrom<JsValue>` impl for Rust structs (or pass a `Vec` of them from JS to Rust), using an old CLI version will work fine; if you do though, you get a bit of a cryptic error message:

```
error: import of `__wbg_foo_unwrap` doesn't have an adapter listed
```

(That's from trying to pass a `Vec<Foo>` from JS to Rust.)

So idk, maybe that's worth bumping the schema version over anyway?

* undo some unnecessary refactors

* don't pointlessly use assert.deepStrictEqual for numbers

* Update the guide

* update reference tests

* add WASI check

* Extremely nitpicky tweaks

---------

Co-authored-by: Billy Bradley <billy@squeno.com>
Co-authored-by: Billy Bradley <billy.jack.bradley@gmail.com>
2023-09-04 17:38:22 +10:00
Oliver
e7cfba5a14 Remove class wrap for constructors in Rust exports (#3562)
* Remove class wrap for constructors in Rust exports

After #1594 constructors of Rust exported structs started using class
wrapping when generating JS shims. Wrapping erases prototype information
from the object instance in JS and as a result it is not possible to
override methods (via inheritance) of the generated class.

Additionally, some checks to ensure constructors always return an
instance of `Self` were lost.

This PR is rebased from #3561, it passes the constructor information
from the `Builder` into the instruction translation function which
is then used to modify the generated bindings.

The `return` statement is also removed instead the value is directly
attached to the instance.

Signed-off-by: Oliver T <geronimooliver00@gmail.com>

* Fix typo

Co-authored-by: Liam Murphy <liampm32@gmail.com>

* Disallow returning JS primitives from constructors

Signed-off-by: Oliver T <geronimooliver00@gmail.com>

* Added missing String in match

Signed-off-by: Oliver T <geronimooliver00@gmail.com>

* Handle nested descriptors

Signed-off-by: Oliver T <geronimooliver00@gmail.com>

---------

Signed-off-by: Oliver T <geronimooliver00@gmail.com>
Co-authored-by: Liam Murphy <liampm32@gmail.com>
2023-08-26 09:23:28 +10:00
Liam Murphy
4a19be6055 Add missing guide pages to SUMMARY.md (#3521)
There were several pages that had been added to the guide, but were inaccessible due to not being listed in `SUMMARY.md` (which is how you specify the layout for the left sidebar). This PR fixes that.

I also noticed that there were two pages for the same `typscript_type` attribute: one in the 'on JS imports' section (which was inaccessible) and one in the 'on Rust exports' section. These aren't for two different versions of the attribute; they're both for the same attribute which can only be applied to JS imports.

The one in the 'on JS imports' section was much less detailed, so I got rid of it and moved the other one into its place.

Closes #2426 (an issue that was opened ages ago about the `typescript_type` page being in the wrong section). There was a bit of discussion there about whether it does actually belong in the 'on Rust exports' section, since despite being directly applied to JS imports its usage is to change the TypeScript output for Rust exports, but I think that the site where it's applied is more important.
2023-07-22 17:16:27 +10:00
Stefan Hanke
7e576ec5fe docs: replace WASM with Wasm 2023-07-03 12:41:47 +02:00
daxpedda
a2ab2d5169 Take alignment into consideration during malloc (#3463)
* Take alignment into consideration during `malloc`

* Use smallest possible alignment

Co-Authored-By: Liam Murphy <43807659+Liamolucko@users.noreply.github.com>

* Rework `DeferCallCore` to `DeferFree`

Co-Authored-By: Liam Murphy <43807659+Liamolucko@users.noreply.github.com>

* Address review

Co-Authored-By: Liam Murphy <43807659+Liamolucko@users.noreply.github.com>

---------

Co-authored-by: Liam Murphy <43807659+Liamolucko@users.noreply.github.com>
2023-06-06 22:10:45 +10:00
Lukas Lihotzki
5600b159bd Remove outdated browser support caveats (#3352) 2023-05-08 10:20:44 -05:00
Daniel Bevenius
ac96e60cbb docs: fix typo in imported-js-types.md (#3403)
Signed-off-by: Daniel Bevenius <daniel.bevenius@gmail.com>
2023-05-08 10:14:56 -05:00
WhizSid
0753bec4c6 Worker tests (#3351)
* Running tests on workers without displaying errors

* Initializing the wasm bindgen only in worker

* Adding tests to run in workers

* Format

* Adding webdriver.json file

* Removed memory module from worker tests

* Keeping original JsValue without overwriting

* Updated the documentation
2023-04-26 19:09:15 +05:00
daxpedda
153a6aa9c7 Introduce #[wasm_bindgen(main)] (#3299) 2023-04-12 23:51:54 +10:00
Lukas Lihotzki
e7c3f52e15 Add caveat of --target no-modules (#3354) 2023-03-15 09:32:11 +11:00
jneem
5bb3520d7e Add a skip_jsdoc attribute. (#3338) 2023-03-10 19:05:53 +11:00
Chris Ohk
0b72e391fd Correct several minor typos (#3346) 2023-03-10 19:00:57 +11:00
Nam Se Hyun
5d22f5b9d6 Append features to web-sys Cargo.toml by wasm-bindgen-webidl (#3343) 2023-03-08 18:40:02 +11:00
Tom Ballinger
3d9fe53270 Fix link in deployment docs. (#3334) 2023-02-27 15:45:51 +11:00
Vincent Esche
0f6621a9b4 Support for specifying a default js_namespace on an extern "C" { … } block (#3280)
* Add support for specifying a default `js_namespace` on an `extern "C" { … }` block

* Improve wording in `js_namespace` section of guide

Co-authored-by: Liam Murphy <liampm32@gmail.com>

---------

Co-authored-by: Liam Murphy <liampm32@gmail.com>
2023-02-10 12:11:25 +11:00
Liam Murphy
46ea32f20e Revert #3248's changes to the wasm-in-web-worker example (#3278)
The start function still gets called on every thread in that example because it doesn't actually use wasm multithreading; each thread's instance has it's own separate memory pool and is completely unaware of the other thread. So, attempting to switch it to use a regular start function caused the worker thread to spawn another one, which does likewise, quickly spiralling out of control.

It also ended up panicking because, thinking it was the main thread, the code was trying to access `window` inside of a worker.
2023-02-02 09:29:11 -06:00
daxpedda
3a939c4047 Remove faulty location.href fallback and disable --split-linked-modules by default (#3279)
* Remove faulty `location.href` fallback

* Disable `--split-linked-modules` in cli-support
2023-02-02 23:26:51 +11:00
daxpedda
5f6e651ea1 Adjust examples and docs to thread-aware start (#3248) 2023-02-02 20:17:41 +11:00
Lukas Lihotzki
e1b44b7570 Add support for linked modules (#3069)
* Add support for linked modules

* Use `wasm_bindgen::link_to!` in wasm-bindgen-futures

* Fix tests

* Update schema

* Return `String` instead of `Result<String, JsValue>`

* Add documentation

* Add tests

* Refactor: Return Diagnostic from module_from_opts

* Refactor: Use Option::filter

* Fix inline_js offsets

* Fully-qualified names in quote

* Return absolute URLs and add node tests

* Fix message

* Enable compile doctest for example

* Disallow module paths in `link_to!`

* Fix documentation

* Opt-in to linked modules in wasm-bindgen with `--allow-links`

* Fix tests

* Support embedding for local modules

* Remove linked module embed limit

* Fix escaping

* Add and refer to the documentation

* Rename option and improve documentation

* Improve documentation

* Update crates/macro/src/lib.rs

Co-authored-by: Liam Murphy <liampm32@gmail.com>

* Add paragraph break in docs
2023-02-01 09:53:41 +11:00
daxpedda
e218b7d9a8 #[wasm_bindgen(start)] shouldn't require pub (#3243)
* `#[wasm_bindgen(start)]` shouldn't require `pub`

* Adjust examples and tests
2023-01-19 12:42:07 +11:00
즈눅
17874c4cfc guide: typo (#3191) 2022-12-10 10:25:51 +11:00
ivanschuetz
4d07b9278d Show conversion to JsValue (#3158) 2022-11-21 11:08:33 -06:00
Sam Estep
f5b2ef8c5c Add an option to keep LLD exports (#3147) 2022-11-14 16:56:55 -06:00
Ethan D Twardy
0ebfbb6395 guide: Add documentation for use of static in extern blocks (#3127)
This commit helps address #2820 by providing additional documentation
for accessing exports of JS snippets and modules directly.
2022-10-31 10:19:27 -05:00
Liam Murphy
c890dc3509 Document that error types other than JsValue are supported (#3054)
Resolves #1004

#2710 added support for returning `Result<T, impl Into<JsValue>>` rather than just `Result<T, JsValue>`, but the `wasm-bindgen` guide still claims that only the latter is supported.

This fixes that, and also fixes a mistake in the table for what forms `Result` can be returned in (it previously claimed that only `Option<Result<...>>` was supported, when in fact only a plain `Result<...>` is supported).
2022-09-01 10:17:28 -05:00
Gunnlaugur Thor Briem
2e1b344cba doc: mention inferior type support of JSON approach (#3047) 2022-08-30 21:42:01 +10:00
Liam Murphy
823f57698c Deprecate JsValue::from_serde and JsValue::into_serde (#3031)
* Deprecate `JsValue::from_serde` and `JsValue::into_serde`

I've listed `serde-wasm-bindgen` as the replacement, and changed the section of the guide that talks about Serde to talk about `serde-wasm-bindgen` instead of the deprecated methods.

I didn't remove it entirely because I can imagine someone remembering it and trying to look it back up, only to find that it no longer exists, which would quite frustrating. I also added a footnote about the deprecated methods in case someone remembers the old way and wants to know what happened.

There were several examples using `from_serde`/`into_serde`, which I updated to use `serde-wasm-bindgen` or not use `serde` altogether.

The `fetch` example was a bit weird, in that it took a JS value, parsed it into a Rust value, only to serialize it back into a JS value. I removed that entirely in favour of just passing the original JS value directly. I suppose it behaves slightly differently in that it loses the extra validation, but a panic isn't all that much better than a JS runtime error.

* fmt

* Mention JSON as an alternative to `serde-wasm-bindgen`

* Use `gloo-utils` instead of raw `JSON`

I was considering leaving the examples using `JSON` directly and mentioning `gloo-utils` as an aside, but that has the major footgun that `JSON.stringify(undefined) === undefined`, causing a panic when deserializing `undefined` since the return type of `JSON::stringify` isn't optional. `gloo-utils` works around this, so I recommended it instead.

* Mention `gloo-utils` in API docs

* Rephrase section about deprecated methods
2022-08-29 23:17:27 -05:00
Willem Wyndham
f292973346 fix: fixes #3044 (#3045) 2022-08-25 06:00:50 +10:00
Lukas Lihotzki
5c3f6ed46a Fix links to WASM audio worklet example (#3030) 2022-08-14 09:17:45 +10:00
Lukas Lihotzki
d881d9da64 Add example with WASM audio worklet (#3017) 2022-08-13 20:12:17 +10:00
Lukas Lihotzki
d759c668ad Update docs of raytrace-parallel (#3026) 2022-08-10 07:58:57 +10:00
Liam Murphy
1790a28f77 Add a bit more detail about how non-primitive types are represented in FFI (#2999)
This is a bit of a follow-on from #2997, adding some more detail on the system that replaced `Stack`.
2022-07-18 08:22:01 -05:00
jneem
1bb1ab1ea1 Remove obsolete documentation about Stack. (#2997) 2022-07-17 17:39:01 +10:00
Jeff Mendez
bc95e13485 chore(guide): fix entry to raytracing example (#2988) 2022-07-11 09:15:45 -05:00
Zach
426c6a99f0 Add dependency to without-a-bundler guide (#2982)
Fixes #2981
2022-07-06 21:56:31 +05:00