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

feat(neon): JsFunction::bind() and Object::prop() #1056

Merged
merged 37 commits into from
Oct 1, 2024
Merged

Conversation

dherman
Copy link
Collaborator

@dherman dherman commented Jul 8, 2024

This PR implements JsFunction::bind(), which creates a builder for calling a function using the Try{From,Into}Js traits.

let n = f.bind(&mut cx)
    .args((1, 2, 3))?
    .arg(4)?
    .arg_with(|cx| Ok(cx.number(5)))?
    .call::<f64>()?;

It also implements Object::prop(), which creates a builder for accessing object properties using the Try{From,Into}Js traits.

let x: f64 = obj
    .prop(&mut cx, "x")
    .get()?;

obj.prop(&mut cx, "y")
    .set(x)?;

let s: String = obj.prop(&mut cx, "toString")
    .bind()?
    .call()?;

Finally, it offers a shorthand convenience method Object::method(), which extracts an object property and binds it as a method:

let s: String = obj.method(&mut cx, "toString").call()?;

@dherman dherman changed the title feat(neon): JsFunction::bind() feat(neon): JsFunction::bind() and Object::prop() Jul 9, 2024
@dherman dherman marked this pull request as ready for review July 9, 2024 17:42
@dherman dherman requested a review from kjvalencik July 10, 2024 22:18
crates/neon/src/object/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/object/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/object/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/types_impl/function/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/types_impl/function/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/types_impl/function/mod.rs Show resolved Hide resolved
crates/neon/src/types_impl/function/mod.rs Show resolved Hide resolved
crates/neon/src/types_impl/function/mod.rs Show resolved Hide resolved
crates/neon/src/types_impl/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/types_impl/mod.rs Show resolved Hide resolved
crates/neon/src/types_impl/mod.rs Show resolved Hide resolved
crates/neon/src/types_impl/function/mod.rs Show resolved Hide resolved
crates/neon/src/types_impl/function/mod.rs Show resolved Hide resolved
crates/neon/src/types_impl/function/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/object/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/object/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/object/mod.rs Show resolved Hide resolved
crates/neon/src/types_impl/extract/with.rs Outdated Show resolved Hide resolved
crates/neon/src/types_impl/mod.rs Outdated Show resolved Hide resolved
dherman and others added 20 commits September 27, 2024 14:09
- tests for both strict and sloppy mode functions to show it works on primitives
…alling .bind(), since it's performed when calling .apply()
Copy link
Member

@kjvalencik kjvalencik left a comment

Choose a reason for hiding this comment

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

The code and API look great! A couple small suggestions for the doc examples, but it's ready to go!

crates/neon/src/object/mod.rs Show resolved Hide resolved
crates/neon/src/event/channel.rs Outdated Show resolved Hide resolved
crates/neon/src/event/mod.rs Outdated Show resolved Hide resolved
crates/neon/src/event/mod.rs Outdated Show resolved Hide resolved
@dherman dherman merged commit d7b27ef into main Oct 1, 2024
9 checks passed
@dherman dherman deleted the prop-options branch October 1, 2024 01:46
@@ -1158,6 +1120,7 @@ impl JsFunction {
/// Calls this function.
///
/// **See also:** [`JsFunction::call_with`].
#[deprecated(since = "TBD", note = "use `JsFunction::bind` instead")]
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this one should be deprecated. It's the low level function that doesn't have the overhead of casting and deserialization. I think we should keep it.

dherman added a commit that referenced this pull request Oct 2, 2024
Implements `JsFunction::bind()`, which creates a builder for calling a function using the `Try{From,Into}Js` traits.

```rust
let n = f.bind(&mut cx)
    .args((1, 2, 3))?
    .arg(4)?
    .arg_with(|cx| Ok(cx.number(5)))?
    .call::<f64>()?;
```

Also implements `Object::prop()`, which creates a builder for accessing object properties using the `Try{From,Into}Js` traits.

```rust
let x: f64 = obj
    .prop(&mut cx, "x")
    .get()?;

obj.prop(&mut cx, "y")
    .set(x)?;

let s: String = obj.prop(&mut cx, "toString")
    .bind()?
    .call()?;
```

Finally, it offers a shorthand convenience method `Object::method()`, which extracts an object property and binds it as a method:

```rust
let s: String = obj.method(&mut cx, "toString").call()?;
```

---------

Co-authored-by: K.J. Valencik <[email protected]>
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