-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 returning fixed-size tuples and arrays #122
Comments
I think fixed-size-arrays are in theory easy enough (we'd probably pass them through allocated memory), tuples are a bit odder though. You're mostly looking for homogenous tuples, right? And both of these in JS would pop out as arrays? |
Yes. In my case I'd like to pass a list of 2D-coordinates to JS. Using a fixed size array would work fine too of course. |
Would love to see this as well, for both arrays and tuples. The output for either could be a JS array. |
This would be great. In my use case, I wanted to return RGB value as 3 elements tuple or array of #[wasm_bindgen]
pub struct Rgb { pub r: u8, pub g: u8, pub b: u8 } But it is not so efficient since in JavaScript, accessing each field causes interaction between wasm and JS. console.log(color.r, color.g, color.b); For this use case, I could finally use I also think mapping tuples to JS array is fine and reasonable. TypeScript supports tuples and actually it is transpiled into array. https://www.typescriptlang.org/docs/handbook/basic-types.html |
As another use-case, I think tuples support could improve interfaces for js-sys iterators too. In particular, |
Just an update - I imagine this might become easier these days (or, at least, soon) by leveraging multi-value Wasm feature. |
In case it's relevant to the discussion (e.g. RE |
@josephrocca, Tuples refer to Rust tuples, Wasm multi-value tuples return JavaScript arrays, and it would make the most sense to target the Wasm feature so that some cases won't need a wasm-bindgen shim at all. |
Are we waiting for the TC39 tuple proposal? |
is this still the case? I'm not too familiar with the internals of wasm_bindgen (yet), but I'd be interested in taking a stab at this! |
@stegaBOB I'm not entirely sure how easy or hard to implement this really is, but I'm happy to review a PR! |
@daxpedda I think we're pretty close to getting something that's working & ready for a review! As of now we're focused on getting fixed size arrays working only for compiling w/ multi-value. Would proceeding like that and throwing a runtime error on |
Last time I checked multi-value doesn't currently work in Rust: rust-lang/rust#73755. I wouldn't mind merging something that doesn't work for now, but I also don't want to merge something I can't even test. |
@daxpedda I meant multi value using the |
Sounds good then! |
Any examples on where this is implemented? Couldn't get it working from current documentation |
We're currently doing the wasm-bindgen work here: https://github.com/staratlasmeta/wasm-bindgen. It's not in a working state at the moment and there's still work we'll need to do, but its getting there. |
We currently have fixed size arrays for most numeric primitives working when using the |
Thank you for the update! 🚀 |
It appears that returning
(f32, f32, f32, f32)
or[f32; 4]
in a#[wasm_bindgen]
function does not currently work. Would be great if fixed size arrays and tuples (maybe up to length 32) were supported.The text was updated successfully, but these errors were encountered: