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

Wasm arrays as arguments and return types #4

Merged
merged 4 commits into from
Dec 25, 2022

Conversation

ross-weir
Copy link
Collaborator

@ross-weir ross-weir commented Dec 23, 2022

Changes

This PR adds the following:

  • Allows downcasting a JsValue into a rust struct that is wasm binded (required for the below 2 array ops)
  • Allow rust accepting array of wasm structs from JS
  • Allow returning array of structs from rust to JS

Related wasm-bindgen issues:

The problem with using JSON / serde for handling passing/receiving arrays is that deser/ser converts to JS objects instead of classes so library consumers can't use class methods without converting to or from JSON first. With the derives added in this PR this is no longer needed

Simple example not using arrays but to describe the problem with JSON:

Rust

#[wasm_bindgen]
#[derive(Serialize, Deserialize)]
pub struct MyType(usize);

#[wasm_bindgen]
impl MyType {
    pub fn new(&self, n: usize) -> MyType {
        MyType(n)
    }

    pub fn inner_num(&self) -> usize {
        self.0
    }

    pub fn to_json(&self) -> Result<JsValue, serde_wasm_bindgen::Error> {
        serde_wasm_bindgen::to_value(&self.0)
    }
}

JS

const myType = MyType.new(5);
const myTypeJson = myType.to_json();

myType.inner_num(); // 5
myTypeJson.inner_num(); // doesn't work, json object doesn't have the func

const jsonBackToMyType = MyType.from_json(myTypeJson);
jsonBackToMyType.inner_num(); // 5

The hope is one day wasm_bindgen supports vecs of structs so we can remove this 😄

@changeset-bot
Copy link

changeset-bot bot commented Dec 23, 2022

⚠️ No Changeset found

Latest commit: 9dbc6de

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@ross-weir ross-weir changed the title [DRAFT] Wasm arrays as arguments and return types Wasm arrays as arguments and return types Dec 23, 2022
@ross-weir ross-weir marked this pull request as ready for review December 23, 2022 22:12
@ross-weir ross-weir requested a review from greenhat December 23, 2022 22:12
type ReturnType = #name;

fn try_as_vec(&self) -> Result<Vec<Self::ReturnType>, ::wasm_bindgen::JsValue> {
let js_array: &::js_sys::Array = self.dyn_ref().unwrap();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: remove the unwraps in this func

Copy link
Member

@greenhat greenhat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks awesome! It improves the DX enormously and removes so much boilerplate. Great job!

@ross-weir ross-weir merged commit 765cf13 into main Dec 25, 2022
@ross-weir ross-weir deleted the downcast-jsval-for-vector-support branch December 25, 2022 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants