Skip to content

Implement Array.prototype.filter and Array.prototype.find #300

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

Closed
wants to merge 2 commits into from

Conversation

robertDurst
Copy link
Contributor

For both of these, I use JsValue to capture a function as input -- which is possible since functions are values in JS. However, these fail if the user does not input a function. What is the recommended way to safe guard against this? Should I use the #[wasm_bindgen(catch)] with -> Result<JsValue, JsValue>?

@jonathan-s
Copy link
Contributor

jonathan-s commented Jun 23, 2018

Yeah, I would say that is a good idea, since filter will throw an error if it is not a function in normal javascript. You can have a look at #299 for how I handled the case of Result in the tests as an example perhaps.

@robertDurst
Copy link
Contributor Author

@jonathan-s ah I see. Will implement something similar. Thanks!

@robertDurst
Copy link
Contributor Author

Hmm looks like I will need to return Result<Array, JsValue> for Array.prototype.filter which is not yet implemented.

@fitzgen
Copy link
Member

fitzgen commented Jun 23, 2018

For both of these, I use JsValue to capture a function as input -- which is possible since functions are values in JS. However, these fail if the user does not input a function. What is the recommended way to safe guard against this? Should I use the #[wasm_bindgen(catch)] with -> Result<JsValue, JsValue>?

For type mismatch errors that we can statically ensure we won't ever trigger via Rust's type system, there is no need to catch. For example, if we make Array.prototype.filter take a function argument instead of a JsValue argument at the type level, we know that Rust code can never pass a Number to filter and that the type error will never be thrown.

If we are going the other way -- exporting a Rust function that takes a JS function as input (which is also not even implemented yet) -- then we do have to do the dynamic type checks because there is no statically typed compiler checking the JS side of things, like there is on the Rust side in the previous example.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

I think we should make these take functions as input rather than JsValue, see https://rustwasm.github.io/wasm-bindgen/closures.html for details.

@robertDurst
Copy link
Contributor Author

Ah yes @fitzgen thanks that makes a lot more sense.

This makes more sense logically:

pub fn find(this: &Array, function: &Fn(JsValue) -> bool) -> JsValue;

However, I end up getting this error:

the trait `wasm_bindgen::convert::RefFromWasmAbi` is not implemented for `std::ops::Fn(wasm_bindgen::JsValue) -> bool + 'static`

which, if I remember correctly @jonathan-s also received.

@jonathan-s
Copy link
Contributor

jonathan-s commented Jun 24, 2018 via email

@akryvomaz
Copy link
Contributor

I can confirm this error. Also tried with Closure<Fn()>, &Closure<FnMut()> etc.

@robertDurst
Copy link
Contributor Author

What is the solution here? Should we implement
wasm_bindgen::convert::RefFromWasmAbi` for `std::ops::Fn(wasm_bindgen::JsValue) -> bool + 'static?

@fitzgen
Copy link
Member

fitzgen commented Jun 25, 2018

I'm not 100% sure -- I need to dig in a little more to figure out why the existing function support isn't working here.

@robertDurst
Copy link
Contributor Author

Closing for now since Array.prototype.filter was implemented in #314

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.

4 participants