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
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
5195a1b
Implement JsFunction::bind(), which creates a builder for calling a f…
dherman Jul 8, 2024
2dce48c
prettier
dherman Jul 8, 2024
e23492c
cargo fmt --all
dherman Jul 8, 2024
0f8ff9e
Add PropOptions and `Object::prop()` for accessing object properties …
dherman Jul 9, 2024
a72ea6c
api doc formatting
dherman Jul 9, 2024
1369807
tweak test name for clarity
dherman Jul 9, 2024
9fa6a0d
Optimization: only do the function type check of the callee when call…
dherman Jul 12, 2024
5e26ba1
rebase: {Bind,Prop}Options take Cx instead of generic C: Context
dherman Sep 8, 2024
670837f
remove unnecessary sys::fun::call helper, and add test case for calli…
dherman Sep 9, 2024
1f8e108
prettier
dherman Sep 9, 2024
c2b4d5b
cargo fmt --all
dherman Sep 9, 2024
8b09460
style: move long type parameter list to where-clause
dherman Sep 9, 2024
f1bc138
more style nits (where-clauses)
dherman Sep 9, 2024
6253c56
arg_with's closure doesn't need to require a Result, since TryIntoJs …
dherman Sep 9, 2024
9f13037
remove unnecessary unsafe: use .as_value() instead
dherman Sep 9, 2024
d603630
BindOptions::this() takes a TryIntoJs (and is therefore fallible)
dherman Sep 9, 2024
3305fc2
store object in `PropOptions` as a `JsObject` so it doesn't need a ty…
dherman Sep 10, 2024
b779e37
avoid the double-type-check of eagerly downcasting to function when c…
dherman Sep 10, 2024
a34531e
style: move the InvalidArg tag check to a match guard
dherman Sep 10, 2024
8f3c58e
argc can be usize now that we're not using the c++ backend
dherman Sep 11, 2024
9050dc4
`BindOptions::args_with()` and equivalent `BindOptions::args(With(...))`
dherman Sep 12, 2024
72bc8b8
more readable logic for impl_arguments! and impl_into_arguments! macros
dherman Sep 13, 2024
e37382d
move `call_local` into a method of `ValueInternal`, so there's less o…
dherman Sep 24, 2024
12d0217
co-locate the impl of TryIntoArguments for With into the same module …
dherman Sep 24, 2024
41c1376
safety comment for `.prop()`
dherman Sep 24, 2024
6e44074
PropOptions.set() returns self
dherman Sep 24, 2024
fd643a2
- put back the `O: Object` type parameter to PropOptions for more pre…
dherman Sep 26, 2024
7a4bbe3
add PropOptions::set_with
dherman Sep 26, 2024
3d264bc
eliminate uses of `.get()` and `.set()` in API docs
dherman Sep 27, 2024
7f1d108
- Object::method()
dherman Sep 28, 2024
84ee9ec
experiment: can we use `#[deprecated(since = "TBD")]` for soft deprec…
dherman Sep 28, 2024
2158281
allow fallible args_with
dherman Sep 28, 2024
b5a76b8
add BindOptions::construct()
dherman Sep 28, 2024
39eaada
soft-deprecate JsFunction::{call, construct, call_with, construct_with}
dherman Sep 28, 2024
942fb50
Remove `.call()`, `.construct()`, `.call_with()`, and `.construct_wit…
dherman Sep 28, 2024
46918d2
API docs for `JsFunction::bind()`
dherman Sep 28, 2024
9bc02b6
KJ's suggested doc improvements
dherman Sep 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions crates/neon/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@
//! # use neon::prelude::*;
//! fn log(cx: &mut Cx, msg: &str) -> NeonResult<()> {
//! cx.global::<JsObject>("console")?
//! .call_method_with(cx, "log")?
//! .arg(cx.string(msg))
//! .exec(cx)?;
//!
//! .method(cx, "log")?
//! .arg(msg)?
//! .exec()?;
//! Ok(())
//! }
//!
Expand Down Expand Up @@ -107,22 +106,18 @@
//! # fn iterate(mut cx: FunctionContext) -> JsResult<JsUndefined> {
//! let iterator = cx.argument::<JsObject>(0)?; // iterator object
//! let next: Handle<JsFunction> = // iterator's `next` method
//! iterator.get(&mut cx, "next")?;
//! iterator.prop(&mut cx, "next").get()?;
//! let mut numbers = vec![]; // results vector
//! let mut done = false; // loop controller
//!
//! while !done {
//! done = cx.execute_scoped(|mut cx| { // temporary scope
//! let obj: Handle<JsObject> = next // temporary object
//! .call_with(&cx)
//! .this(iterator)
//! .apply(&mut cx)?;
//! let number: Handle<JsNumber> = // temporary number
//! obj.get(&mut cx, "value")?;
//! numbers.push(number.value(&mut cx));
//! let done: Handle<JsBoolean> = // temporary boolean
//! obj.get(&mut cx, "done")?;
//! Ok(done.value(&mut cx))
//! .bind(&mut cx)
//! .this(iterator)?
//! .call()?;
//! numbers.push(obj.prop(&mut cx, "value").get()?); // temporary number
//! obj.prop(&mut cx, "done").get() // temporary boolean
//! })?;
//! }
//! # Ok(cx.undefined())
Expand Down Expand Up @@ -505,7 +500,7 @@ pub trait Context<'a>: ContextInternal<'a> {
/// # let v: Handle<JsFunction> =
/// {
/// let global = cx.global_object();
/// global.get(cx, name)
/// global.prop(cx, name).get()
/// }
/// # ?;
/// # Ok(v)
Expand Down
11 changes: 5 additions & 6 deletions crates/neon/src/event/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,12 @@ type Callback = Box<dyn FnOnce(sys::Env) + Send + 'static>;
/// // loop. This _will_ block the event loop while executing.
/// channel.send(move |mut cx| {
/// let callback = callback.into_inner(&mut cx);
/// let this = cx.undefined();
/// let args = vec![
/// cx.null().upcast::<JsValue>(),
/// cx.number(result).upcast(),
/// ];
/// let null = cx.null();
dherman marked this conversation as resolved.
Show resolved Hide resolved
///
/// callback.call(&mut cx, this, args)?;
/// callback
/// .bind(&mut cx)
/// .args((null, result))?
/// .exec()?;
///
/// Ok(())
/// });
Expand Down
37 changes: 17 additions & 20 deletions crates/neon/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,28 @@
//! // loop. This _will_ block the event loop while executing.
//! channel.send(move |mut cx| {
//! let callback = callback.into_inner(&mut cx);
//! let this = cx.undefined();
//! let args = match result {
//!
//! match result {
//! Ok(psd) => {
//! // Extract data from the parsed file.
//! let width = cx.number(psd.width());
//! let height = cx.number(psd.height());
//!
//! // Save the data in a result object.
//! let obj = cx.empty_object();
//! obj.set(&mut cx, "width", width)?;
//! obj.set(&mut cx, "height", height)?;
//! vec![
//! cx.null().upcast::<JsValue>(),
//! obj.upcast(),
//! ]
//! let obj = cx.empty_object()
//! .prop(&mut cx, "width").set(psd.width())?
//! .prop("height").set(psd.height())?
//! .this();
//! let null = cx.null();
//!
//! callback
//! .bind(&mut cx)
//! .args((null, obj))?
dherman marked this conversation as resolved.
Show resolved Hide resolved
//! .exec()?;
//! }
//! Err(err) => {
//! let err = cx.string(err.to_string());
//! vec![
//! err.upcast::<JsValue>(),
//! ]
//! callback
//! .bind(&mut cx)
//! .arg(err.to_string())?
dherman marked this conversation as resolved.
Show resolved Hide resolved
//! .exec()?;
//! }
//! };
//!
//! callback.call(&mut cx, this, args)?;
//! }
//!
//! Ok(())
//! });
Expand Down
22 changes: 9 additions & 13 deletions crates/neon/src/handle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,18 @@
//!
//! ## Example
//!
//! This Neon function takes an object as its argument, extracts two properties,
//! `width` and `height`, and multiplies them together as numbers. Each JavaScript
//! value in the calculation is stored locally in a `Handle`.
//! This Neon function takes an object as its argument, extracts an object property,
//! `homeAddress`, and then extracts a string property, `zipCode` from that second
//! object. Each JavaScript value in the calculation is stored locally in a `Handle`.
//!
//! ```
//! # use neon::prelude::*;
//! fn area(mut cx: FunctionContext) -> JsResult<JsNumber> {
//! let rect: Handle<JsObject> = cx.argument(0)?;
//!
//! let width: Handle<JsNumber> = rect.get(&mut cx, "width")?;
//! let w: f64 = width.value(&mut cx);
//!
//! let height: Handle<JsNumber> = rect.get(&mut cx, "height")?;
//! let h: f64 = height.value(&mut cx);
//!
//! Ok(cx.number(w * h))
//! # use neon::export;
//! #[export]
//! fn customer_zip_code<'cx>(cx: &mut FunctionContext<'cx>, customer: Handle<'cx, JsObject>) -> JsResult<'cx, JsString> {
//! let home_address: Handle<JsObject> = customer.prop(cx, "homeAddress").get()?;
//! let zip_code: Handle<JsString> = home_address.prop(cx, "zipCode").get()?;
//! Ok(zip_code)
//! }
//! ```

Expand Down
Loading