* 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.
* 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
* 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
* 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
* 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>
* 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>
* 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
* 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
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.
`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.