Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating Vecs of and implement TryFrom<JsValue> for strings and exported Rust types #3554

Merged

Commits on Aug 26, 2023

  1. Enable passing String vectors and boxed slices across ABI

    This is accomplished via conversion of the Strings to/from JsValues.
    sinking-point authored and Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    9f51d64 View commit details
    Browse the repository at this point in the history
  2. 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.
    sinking-point authored and Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    5d26952 View commit details
    Browse the repository at this point in the history
  3. Remove unneeded require

    sinking-point authored and Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    9752b81 View commit details
    Browse the repository at this point in the history
  4. Move uses out of if_std

    sinking-point authored and Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    734eef4 View commit details
    Browse the repository at this point in the history
  5. Add documentation

    sinking-point authored and Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    905c1c3 View commit details
    Browse the repository at this point in the history
  6. Move incorrect use statements

    sinking-point authored and Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    c8a327f View commit details
    Browse the repository at this point in the history
  7. Fix mistake in comment

    sinking-point authored and Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    cd89028 View commit details
    Browse the repository at this point in the history
  8. 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?
    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    55b7c3e View commit details
    Browse the repository at this point in the history
  9. 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.
    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    dba751d View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    8443320 View commit details
    Browse the repository at this point in the history
  11. Configuration menu
    Copy the full SHA
    0fc2ff9 View commit details
    Browse the repository at this point in the history
  12. test string vecs too

    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    7254342 View commit details
    Browse the repository at this point in the history
  13. 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.
    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    263f635 View commit details
    Browse the repository at this point in the history
  14. 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.
    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    761dc4a View commit details
    Browse the repository at this point in the history
  15. fix clippy

    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    58667b2 View commit details
    Browse the repository at this point in the history
  16. 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`.
    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    8c96579 View commit details
    Browse the repository at this point in the history
  17. reorder some impls

    This is the nitpickiest of nitpicks, but this is my PR goddammit and I can do what I want :)
    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    af06b34 View commit details
    Browse the repository at this point in the history
  18. 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?
    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    a5c9fad View commit details
    Browse the repository at this point in the history
  19. Configuration menu
    Copy the full SHA
    60f74d6 View commit details
    Browse the repository at this point in the history
  20. Configuration menu
    Copy the full SHA
    efc958e View commit details
    Browse the repository at this point in the history
  21. Update the guide

    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    68f2d8f View commit details
    Browse the repository at this point in the history
  22. update reference tests

    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    5f1808a View commit details
    Browse the repository at this point in the history
  23. add WASI check

    Liamolucko committed Aug 26, 2023
    Configuration menu
    Copy the full SHA
    8516726 View commit details
    Browse the repository at this point in the history

Commits on Sep 4, 2023

  1. Extremely nitpicky tweaks

    Liamolucko committed Sep 4, 2023
    Configuration menu
    Copy the full SHA
    e6df778 View commit details
    Browse the repository at this point in the history