Skip to content

Commit

Permalink
eliminate uses of .get() and .set() in API docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dherman committed Sep 27, 2024
1 parent 02cebcf commit 83a0fbd
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 79 deletions.
12 changes: 4 additions & 8 deletions crates/neon/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
//! # 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
//!
Expand All @@ -117,12 +117,8 @@
//! .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))
//! 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 +501,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
8 changes: 2 additions & 6 deletions crates/neon/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,9 @@
//! let args = 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)?;
//! obj.prop(&mut cx, "width").set(psd.width())?;
//! obj.prop(&mut cx, "height").set(psd.height())?;
//! vec![
//! cx.null().upcast::<JsValue>(),
//! obj.upcast(),
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
31 changes: 11 additions & 20 deletions crates/neon/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@
//!
//! ```
//! # use neon::prelude::*;
//! fn set_and_check<'a>(
//! cx: &mut impl Context<'a>,
//! obj: Handle<'a, JsObject>
//! ) -> JsResult<'a, JsValue> {
//! let value = cx.string("hello!");
//! fn set_and_check<'cx>(
//! cx: &mut Cx<'cx>,
//! obj: Handle<'cx, JsObject>
//! ) -> JsResult<'cx, JsValue> {
//! // set property "17" with integer shorthand
//! obj.set(cx, 17, value)?;
//! obj.prop(cx, 17).set("hello")?;
//! // get property "17" with string shorthand
//! // returns the same value ("hello!")
//! obj.get(cx, "17")
//! obj.prop(cx, "17").get()
//! }
//! ```
//!
Expand Down Expand Up @@ -210,7 +209,6 @@ where
}

/// Gets the property from the object and attempts to convert it to a Rust value.
/// Equivalent to calling `R::from_js(cx, obj.get(cx)?)`.
///
/// May throw an exception either during accessing the property or converting the
/// result type.
Expand All @@ -220,7 +218,6 @@ where
}

/// Sets the property on the object to a value converted from Rust.
/// Equivalent to calling `obj.set(cx, v.try_into_js(cx)?)`.
///
/// May throw an exception either during converting the value or setting the property.
pub fn set<V: TryIntoJs<'cx>>(&mut self, v: V) -> NeonResult<&mut Self> {
Expand All @@ -230,7 +227,6 @@ where
}

/// Sets the property on the object to a value computed from a closure.
/// Equivalent to calling `obj.set(cx, f(cx).try_into_js(cx)?)`.
///
/// May throw an exception either during converting the value or setting the property.
pub fn set_with<R, F>(&mut self, f: F) -> NeonResult<&mut Self>
Expand Down Expand Up @@ -280,8 +276,7 @@ pub trait Object: Value {
PropOptions { cx, this, key }
}

/// Gets a property from a JavaScript object that may be `undefined` and
/// attempts to downcast the value if it existed.
#[doc(hidden)]
fn get_opt<'a, V: Value, C: Context<'a>, K: PropertyKey>(
&self,
cx: &mut C,
Expand All @@ -296,10 +291,7 @@ pub trait Object: Value {
v.downcast_or_throw(cx).map(Some)
}

/// Gets a property from a JavaScript object as a [`JsValue`].
///
/// If a [`getter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get)
/// is defined on the object, it will be called.
#[doc(hidden)]
fn get_value<'a, C: Context<'a>, K: PropertyKey>(
&self,
cx: &mut C,
Expand All @@ -310,10 +302,7 @@ pub trait Object: Value {
})
}

/// Gets a property from a JavaScript object and attempts to downcast as a specific type.
/// Equivalent to calling `obj.get_value(&mut cx)?.downcast_or_throw(&mut cx)`.
///
/// Throws an exception if the value is a different type.
#[doc(hidden)]
fn get<'a, V: Value, C: Context<'a>, K: PropertyKey>(
&self,
cx: &mut C,
Expand Down Expand Up @@ -358,6 +347,7 @@ pub trait Object: Value {
}
}

#[doc(hidden)]
fn set<'a, C: Context<'a>, K: PropertyKey, W: Value>(
&self,
cx: &mut C,
Expand All @@ -378,6 +368,7 @@ pub trait Object: Value {
Root::new(cx, self)
}

#[doc(hidden)]
fn call_method_with<'a, C, K>(&self, cx: &mut C, method: K) -> NeonResult<CallOptions<'a>>
where
C: Context<'a>,
Expand Down
3 changes: 1 addition & 2 deletions crates/neon/src/result/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
//! # use neon::prelude::*;
//! fn get_message(mut cx: FunctionContext) -> JsResult<JsValue> {
//! let obj: Handle<JsObject> = cx.argument(0)?;
//! let prop: Handle<JsValue> = obj.get(&mut cx, "message")?;
//! Ok(prop)
//! obj.prop(&mut cx, "message").get()
//! }
//! ```
//!
Expand Down
4 changes: 2 additions & 2 deletions crates/neon/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
//! let worker: Handle<JsObject> = require.call_with(cx)
//! .arg(cx.string("node:worker_threads"))
//! .apply(cx)?;
//! let thread_id: Handle<JsNumber> = worker.get(cx, "threadId")?;
//! Ok(thread_id.value(cx) as u32)
//! let thread_id: f64 = worker.prop(cx, "threadId").get()?;
//! Ok(thread_id as u32)
//! }).cloned()
//! }
//! ```
Expand Down
3 changes: 2 additions & 1 deletion crates/neon/src/types_impl/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ impl<T: 'static> Deref for JsBox<T> {
/// fn finalize<'a, C: Context<'a>>(self, cx: &mut C) {
/// let global = cx.global_object();
/// let emit: Handle<JsFunction> = global
/// .get(cx, "emit")
/// .prop(cx.cx_mut(), "emit")
/// .get()
/// .unwrap();
///
/// let args = vec![
Expand Down
7 changes: 2 additions & 5 deletions crates/neon/src/types_impl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ use crate::{
/// let err = cx.type_error("expected a number, found a string")?;
///
/// // Add some custom diagnostic properties to the error:
/// let expected = cx.string("number");
/// err.set(&mut cx, "expected", expected)?;
///
/// let found = cx.string("string");
/// err.set(&mut cx, "found", found)?;
/// err.prop(&mut cx, "expected").set("number")?;
/// err.prop(&mut cx, "found").set("string")?;
///
/// // Throw the error:
/// cx.throw(err)?;
Expand Down
31 changes: 13 additions & 18 deletions crates/neon/src/types_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl ValueInternal for JsNull {
/// # fn test(mut cx: FunctionContext) -> JsResult<JsUndefined> {
/// // Extract the console.log function:
/// let console: Handle<JsObject> = cx.global("console")?;
/// let log: Handle<JsFunction> = console.get(&mut cx, "log")?;
/// let log: Handle<JsFunction> = console.prop(&mut cx, "log").get()?;
///
/// // The two Boolean values:
/// let t = cx.boolean(true);
Expand Down Expand Up @@ -420,7 +420,7 @@ impl ValueInternal for JsBoolean {
/// # fn test(mut cx: FunctionContext) -> JsResult<JsUndefined> {
/// // Extract the console.log function:
/// let console: Handle<JsObject> = cx.global("console")?;
/// let log: Handle<JsFunction> = console.get(&mut cx, "log")?;
/// let log: Handle<JsFunction> = console.prop(&mut cx, "log").get()?;
///
/// // Create a string:
/// let s = cx.string("hello 🥹");
Expand Down Expand Up @@ -697,7 +697,7 @@ impl JsString {
/// # fn test(mut cx: FunctionContext) -> JsResult<JsUndefined> {
/// // Extract the console.log function:
/// let console: Handle<JsObject> = cx.global("console")?;
/// let log: Handle<JsFunction> = console.get(&mut cx, "log")?;
/// let log: Handle<JsFunction> = console.prop(&mut cx, "log").get()?;
///
/// // Create a number:
/// let n = cx.number(17.0);
Expand Down Expand Up @@ -773,16 +773,15 @@ impl ValueInternal for JsNumber {
/// # fn test(mut cx: FunctionContext) -> JsResult<JsUndefined> {
/// // Extract the console.log function:
/// let console: Handle<JsObject> = cx.global("console")?;
/// let log: Handle<JsFunction> = console.get(&mut cx, "log")?;
/// let log: Handle<JsFunction> = console.prop(&mut cx, "log").get()?;
///
/// // Create an object:
/// let obj = cx.empty_object();
///
/// let name = cx.string("Neon");
/// obj.set(&mut cx, "name", name)?;
///
/// let url = cx.string("https://neon-bindings.com");
/// obj.set(&mut cx, "url", url)?;
/// let obj = cx.empty_object()
/// .prop(&mut cx, "name")
/// .set("Neon")?
/// .prop("url")
/// .set("https://neon-bindings.com")?
/// .this();
///
/// // Call console.log(obj):
/// log.call_with(&cx).arg(obj).exec(&mut cx)?;
Expand Down Expand Up @@ -860,13 +859,9 @@ impl JsObject {
/// // Create a new empty array:
/// let a: Handle<JsArray> = cx.empty_array();
///
/// // Create some new values to push onto the array:
/// let n = cx.number(17);
/// let s = cx.string("hello");
///
/// // Push the elements onto the array:
/// a.set(&mut cx, 0, n)?;
/// a.set(&mut cx, 1, s)?;
/// // Push some values onto the array:
/// a.prop(&mut cx, 0).set(17)?;
/// a.prop(&mut cx, 1).set("hello")?;
/// # Ok(a)
/// # }
/// ```
Expand Down
10 changes: 6 additions & 4 deletions crates/neon/src/types_impl/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ const BOUNDARY: FailureBoundary = FailureBoundary {
/// .promise(|mut cx, (indices, kinds)| {
/// let indices = JsUint32Array::from_slice(&mut cx, &indices)?;
/// let kinds = JsUint8Array::from_slice(&mut cx, &kinds)?;
/// let result = cx.empty_object();
/// result.set(&mut cx, "indices", indices)?;
/// result.set(&mut cx, "kinds", kinds)?;
/// Ok(result)
/// Ok(cx.empty_object()
/// .prop(&mut cx, "indices")
/// .set(indices)?
/// .prop("kinds")
/// .set(kinds)?
/// .this())
/// });
///
/// Ok(promise)
Expand Down

0 comments on commit 83a0fbd

Please sign in to comment.