721 Commits

Author SHA1 Message Date
Liam Murphy
88f8917efb Fix Rust values getting GC'd while still borrowed and not getting GC'd if created via. constructor (#3940)
* Add a failing test

* Force tests to run sequentially

At first I tried fixing them so that they didn't need to (giving each of them
their own `dropCount`), but running multiple GCs in parallel seems to be flaky.

* Add objects created via. constructors to the FinalizationRegistry

* Add a failing test

* Fix exported Rust types being GC'd while still borrowed

I also discovered and fixed an extra bug while working on this, which
was that `LongRefFromWasmAbi` wasn't getting used for `self`: this bug
didn't cause any problems before, because the only type that had a
different `LongRefFromWasmAbi` impl than its `RefFromWasmAbi` impl was
`JsValue` which can't be the type of `self`.

It became a problem here because I made the new `LongRefFromWasmAbi`
impl for exported Rust types clone the `Rc`, whereas the
`RefFromWasmAbi` impl doesn't because garbage collection can't occur
during the synchronous call that the value has to live for.

I might actually change it so that both of the impls behave like the
current `LongRefFromWasmAbi` impl, though: cloning an `Rc` isn't
expensive and so having the second different impl just makes things more
complicated for no good reason. I just left it in this commit as
explanation for how I discovered the `LongRefFromWasmAbi` issue.

* Unify RefFromWasmAbi and LongRefFromWasmAbi impls

* Get rid of needless looping

* Get rid of outdated `borrow_mut`

Now that borrowing a Rust value always clones its `Rc`, `Rc::into_inner`
is a sufficient check that the value isn't borrowed.

* Get rid of needless `mut`

For some reason I was getting errors before without it, but that seems
to be fixed now. (It's probably something to do with having removed the
`borrow_mut`, but that only takes `&self`, so I still don't get it.)

* Update reference tests

* Add changelog entry

* Update schema hash

* Use Rc::try_unwrap instead of Rc::into_inner

* Skip GC tests

They seem to be far flakier in CI than locally for some reason, and I
don't see any way to solve it; so just turn them off. :(

I also got rid of the weird GC warmup hack because it doesn't do
anything anymore; I could've sworn it was a reproducible effect before,
but it seems to make no difference now.
2024-06-01 22:18:14 +10:00
Nicklas Warming Jacobsen
5767be9ea0 Fixes #3929 Allow exporting functions named "default". (#3930) 2024-05-28 06:38:45 +10:00
David Huculak
9b37613bba Improve the performance of 'string enums' (#3915) 2024-05-21 10:04:15 +02:00
Liam Murphy
ad251c06a6 Fix CI failures introduced by Rust 1.78 (except clippy::empty_docs) (#3949)
* Fix clippy/cargo warnings introduced by Rust 1.78

* Update reference tests
2024-05-07 20:30:12 +10:00
ZGY
7d0b11c80e Support expr when use typescript_custom_section attribute (#3901)
* feat: support expr when use `typescript_custom_section` attribute

* test: update typescript-tests

* chore: update "APPROVED_SCHEMA_FILE_HASH" of shared lib

* chore: cargo fmt

* Apply suggestions from code review

include fix typo and adding whitespace to ensure consistent code style.

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

* chore(backend): fix typo

* chore(typescript-tests): rename custom_section_type to custom_section_type.d.ts

* fix(backend/codegen): change method flat_slices to flat_byte_slices in order to avoid unsafe code

* fix(backend/codegen): use dynamic wasm_bindgen path as import entry

* chore(typescript-tests): ignore *.d.ts file when test

* chore(shared/program): rename CustomSection to LitOrExpr

* doc(shared/lib): add doc for program[typescript_custom_sections], explain why there are different types of LitOrExpr when encoding and decoding

* chore(shared): update "APPROVED_SCHEMA_FILE_HASH" of shared lib

* doc: add docs for method encode_u32_to_fixed_len_bytes

* refactor(backend/encode): rename method shared_typescript_custom_section to shared_lit_or_expr

* refactor(__rt): extract methods from nested mod directly into `__rt`

* chore: cargo fmt

* chore(__rt): remove unnecessary TODO

* chore(changelog): update change log
Support Expressions when using the `typescript_custom_section` attribute[#3901]

* Update CHANGELOG.md
2024-04-10 16:43:15 +10:00
Liam Murphy
88efe468e6 Implement Into<JsValue> for Vec (#3630)
* Implement `Into<JsValue>` for `Vec`

This means that `Vec`s can now be returned from `async` functions.

Fixes #3155.

I had to add a new `VectorIntoJsValue` trait, since similarly to
`VectorIntoWasmAbi` the orphan rule won't let us directly implement
`From<Vec<T>>` for `JsValue` outside of `wasm-bindgen`.

* Implement Into<JsValue> for boxed slices of numbers as well

* Add changelog entry

* Fix memory leak

* Add missing if_std!

* Move the changelog entry to the right spot
2024-03-25 11:10:24 +11:00
Lynn
ac462d586a Copy port from headless test server when using WASM_BINDGEN_TEST_ADDRESS (#3873) 2024-03-06 22:04:28 +01:00
daxpedda
2a4a493623 Prepare v0.2.92 release (#3869) 2024-03-04 11:25:46 +01:00
daxpedda
8e992dc906 Don't allow invalid Unicode scalar values in char (#3866) 2024-03-04 08:51:38 +01:00
daxpedda
983ec579a3 Add NonNull<T> as parameter (#3857) 2024-03-02 09:11:13 +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
Aaron Hill
b5a74c8578 Add sorted_iter_mut for NonstandardWitSection.adapters for determinism (#3851) 2024-02-22 17:06:13 +01:00
daxpedda
fe8bc949b5 Prepare v0.2.91 release (#3829) 2024-02-06 12:37:22 +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
Liam Murphy
acef364c7e Shrink JS-allocated strings down to the correct size before passing them to Rust (#3808)
* Shrink JS-allocated strings down to the correct size before passing them to Rust

Fixes #3801.

I opted to solve it this way rather than just pass the capacity to Rust
as well because it means the allocation isn't up to 3x bigger than it
needs to be anymore. I also removed a TODO about fixing that.

* Update reference tests

* Add changelog entry
2024-01-25 20:05:55 +11:00
mx
55a7fb848a Added "type found" msg to expose_assert_* fns #3766 (#3787) 2024-01-16 11:39:06 +01:00
Philip Dubé
8797e93202 wasm-bindgen-cli-support: update base64 0.9 to 0.21 (#3777) 2024-01-14 09:47:25 +01:00
Gabriel Grant
adcf7786d1 Bump versions & update changelog for 0.2.90 release (#3763) 2024-01-12 16:59:28 +01:00
Benjamin Bradshaw
cc9a2195ac Fix JS shim default path detection for the no-modules target (#3748) 2023-12-20 16:54:33 +01:00
GitHub Actions
ab336aad0c bump 2023-11-27 16:20:02 +01:00
Alex Errant
fe88e39a49 __wbindgen_thread_destroy has optional params (#3703) 2023-11-15 23:04:54 +01:00
daxpedda
9fb3bca168 Bump walrus to v0.20 (#3483) 2023-11-10 22:50:43 +01:00
Muhammad Hamza
0b5f0eec2f Bump versions for v0.2.88 (#3676)
* bump

* Bump version in changelog

* Bump date in changelog

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

---------

Co-authored-by: GitHub Actions <actions@github.com>
Co-authored-by: daxpedda <daxpedda@gmail.com>
Co-authored-by: Liam Murphy <liampm32@gmail.com>
2023-11-01 20:35:23 +05:00
Thomas Etter
54f22ee0a5 Return an error if there are two enums with the same name (#3669) 2023-10-26 10:58:22 +02:00
daxpedda
3d2f392ccc Bump MSRV to v1.57 (#3657) 2023-10-16 13:08:13 +02:00
Christian Ivicevic
5b42ffcd9e Add enum types for function parameters and return types (#3647) 2023-10-07 20:49:59 +02:00
daxpedda
277b20f306 Update Clippy (#3645) 2023-10-06 21:58:24 +02:00
daxpedda
9e80eb269c Fix some calls to free() missing alignment (#3639) 2023-10-03 13:15:54 +02:00
Christopher Schramm
2b7ab44676 Lower the ES target for instanceof code (#3618)
Since #3053 the code makes use of the ES2019 optional catch binding syntax, while generated code in general seems to be compatible with ES2015.
2023-09-16 21:01:40 +02:00
Oliver
373b7a57be Replace filter_map().next() with find_map (#3620)
`rust-analyzer` emits a weak warning for this which leads to unnecessary
noise in some editors.

Signed-off-by: Oliver T <geronimooliver00@gmail.com>
2023-09-16 15:56:11 +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
magic-akari
99983c85e1 fix: Optional JSDoc @param (#3577) 2023-08-29 09:59:50 +02: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
Andrea Frigido
74bfc1f85e Update license field following SPDX 2.1 license expression standard (#3529) 2023-07-19 10:57:34 +02:00
daxpedda
f0a8ae3b99 Bump to 0.2.87 (#3475) 2023-06-12 14:25:14 -05: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
85f72c9125 Clippy: Fixes and CI (#3300) 2023-05-17 12:02:02 +02:00
Alex Crichton
2d882c97b0 Bump to 0.2.86 (#3429) 2023-05-15 17:25:39 -05:00
Oliver
4130092077 Improved version mismatch error message (#3428)
Signed-off-by: Oliver T <geronimooliver00@gmail.com>
2023-05-15 20:49:14 +02:00
dAxpeDDa
15ca11bdf5 Fix wrong null check on document.currentScript 2023-05-13 14:41:30 +02:00
Melanie Riise
a459f5abf2 bump version (#3400)
Co-authored-by: Alex Crichton <alex@alexcrichton.com>
2023-05-09 09:15:50 -05:00
daxpedda
b7edbd20bc Expose WebAssembly.Instance (#3296) 2023-05-09 09:11:40 -05:00
Lukas Lihotzki
80b616e3de Fix TextEncoder stub name (#3357) 2023-05-08 10:20:08 -05:00
mx
a5973d6039 Fixed document.currentScript is null #3398 (#3402)
* Fixed document.currentScript is null #3398
* document.currentScript can be null in browser extensions
* its src prop can also be null, but I don't know under what conditions
* replace null with '' and use location.href
* see rustwasm/wasm-bindgen#3398 for details

* Simplified document.currentScript check #3398

* Removed null check for document.currentScript.src #3398

* Replaced document.currentScript check with typeoff #3398
2023-05-08 10:16:47 -05:00
Oliver
dbea2a29e1 add verbose name for internal ptr property (#3408)
* add verbose name for internal `ptr` property

Fixes #3388

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

* fixed failing tests

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

---------

Signed-off-by: Oliver T <geronimooliver00@gmail.com>
2023-05-08 10:16:17 -05:00
Liam Murphy
8481b4f812 Remove experimental interface types support (#3372)
* Change the bare minimum to be able to remove the wit-* deps

* Remove some holdovers

Namely `Generated` being an enum and the `trap` fields of `Instruction::IntToWasm` and `Instruction::WasmToInt`.

* remove tests for interface types

* update reference tests

* fix reference tests (again)

* Allow enabling multi-value

For some reason wasm-bindgen would only allow enabling multi-value if interface types were enabled, throwing an error otherwise. I couldn't see any reason why, so I changed that.

Since that also meant that multi-value + JS bindings wasn't being tested, allowing it exposed some bugs that I fixed.

* update test expectations
2023-04-10 09:34:12 -05:00
Liam Murphy
bea9bdb484 Don't rely on the name section for detecting main (#3364)
Fixes #3362

In some cases, the name section can fail to be parsed because one of the names is too long, which then causes the main function to not be detected because we don't know what any functions' names are.

However, `main` is also exported with the name `main`, so we can look for an export named `main` instead to avoid relying on the name section.
2023-03-24 10:21:10 -05:00
Sunil Pai
76e4cad8bb Fixes #3259: use verbose names for load / init / getImports (#3349)
`init` / `load` / `getImports` are common names for functions, and clash when generated with `--target web`. This adds verbose prefixes to them so there's a smaller chance of clashing with already defined functions.

Fixes #3259.
2023-03-13 21:10:44 +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