diff --git a/crates/neon/src/context/mod.rs b/crates/neon/src/context/mod.rs index 97282e979..a3c9b8e92 100644 --- a/crates/neon/src/context/mod.rs +++ b/crates/neon/src/context/mod.rs @@ -359,8 +359,30 @@ pub trait Context<'a>: ContextInternal<'a> { JsDate::new(self, value) } + /// Convenience method for looking up a global property by name. + /// + /// Equivalent to: + /// + /// ``` + /// # use neon::prelude::*; + /// # fn get_array_global<'cx, C: Context<'cx>>(cx: &mut C) -> JsResult<'cx, JsFunction> { + /// # let name = "Array"; + /// # let v: Handle = + /// { + /// let global = cx.global_object(); + /// global.get(cx, name) + /// } + /// # ?; + /// # Ok(v) + /// # } + /// ``` + fn global(&mut self, name: &str) -> JsResult<'a, T> { + let global = self.global_object(); + global.get(self, name) + } + /// Produces a handle to the JavaScript global object. - fn global(&mut self) -> Handle<'a, JsObject> { + fn global_object(&mut self) -> Handle<'a, JsObject> { JsObject::build(|out| unsafe { sys::scope::get_global(self.env().to_raw(), out); }) diff --git a/crates/neon/src/thread/mod.rs b/crates/neon/src/thread/mod.rs index 34a21fc05..136ecad55 100644 --- a/crates/neon/src/thread/mod.rs +++ b/crates/neon/src/thread/mod.rs @@ -15,8 +15,7 @@ //! //! pub fn thread_id<'cx, C: Context<'cx>>(cx: &mut C) -> NeonResult { //! THREAD_ID.get_or_try_init(cx, |cx| { -//! let global = cx.global(); -//! let require: Handle = global.get(cx, "require")?; +//! let require: Handle = cx.global("require")?; //! let worker: Handle = require.call_with(cx) //! .arg(cx.string("node:worker_threads")) //! .apply(cx)?; diff --git a/crates/neon/src/types_impl/boxed.rs b/crates/neon/src/types_impl/boxed.rs index 8b2fa0579..4306bbed5 100644 --- a/crates/neon/src/types_impl/boxed.rs +++ b/crates/neon/src/types_impl/boxed.rs @@ -296,7 +296,7 @@ impl Deref for JsBox { /// /// impl Finalize for Point { /// fn finalize<'a, C: Context<'a>>(self, cx: &mut C) { -/// let global = cx.global(); +/// let global = cx.global_object(); /// let emit: Handle = global /// .get(cx, "emit") /// .unwrap(); diff --git a/crates/neon/src/types_impl/function/mod.rs b/crates/neon/src/types_impl/function/mod.rs index 743f0bdf2..d5c52523e 100644 --- a/crates/neon/src/types_impl/function/mod.rs +++ b/crates/neon/src/types_impl/function/mod.rs @@ -18,8 +18,7 @@ pub(crate) mod private; /// ``` /// # use neon::prelude::*; /// # fn foo(mut cx: FunctionContext) -> JsResult { -/// # let global = cx.global(); -/// # let parse_int: Handle = global.get(&mut cx, "parseInt")?; +/// # let parse_int: Handle = cx.global("parseInt")?; /// let x: Handle = parse_int /// .call_with(&cx) /// .arg(cx.string("42")) @@ -77,8 +76,7 @@ impl<'a> CallOptions<'a> { /// ``` /// # use neon::prelude::*; /// # fn foo(mut cx: FunctionContext) -> JsResult { -/// # let global = cx.global(); -/// # let url: Handle = global.get(&mut cx, "URL")?; +/// # let url: Handle = cx.global("URL")?; /// let obj = url /// .construct_with(&cx) /// .arg(cx.string("https://neon-bindings.com")) diff --git a/crates/neon/src/types_impl/mod.rs b/crates/neon/src/types_impl/mod.rs index 90799c273..0927965a5 100644 --- a/crates/neon/src/types_impl/mod.rs +++ b/crates/neon/src/types_impl/mod.rs @@ -201,7 +201,7 @@ impl JsValue { /// # use neon::prelude::*; /// # fn test(mut cx: FunctionContext) -> JsResult { /// // Extract the console object: -/// let console: Handle = cx.global().get(&mut cx, "console")?; +/// let console: Handle = cx.global("console")?; /// /// // The undefined value: /// let undefined = cx.undefined(); @@ -272,8 +272,7 @@ impl ValueInternal for JsUndefined { /// ``` /// # use neon::prelude::*; /// # fn test(mut cx: FunctionContext) -> JsResult { -/// cx.global() -/// .get::(&mut cx, "console")? +/// cx.global::("console")? /// .call_method_with(&mut cx, "log")? /// .arg(cx.null()) /// .exec(&mut cx)?; @@ -342,7 +341,7 @@ impl ValueInternal for JsNull { /// # use neon::prelude::*; /// # fn test(mut cx: FunctionContext) -> JsResult { /// // Extract the console.log function: -/// let console: Handle = cx.global().get(&mut cx, "console")?; +/// let console: Handle = cx.global("console")?; /// let log: Handle = console.get(&mut cx, "log")?; /// /// // The two Boolean values: @@ -419,7 +418,7 @@ impl ValueInternal for JsBoolean { /// # use neon::prelude::*; /// # fn test(mut cx: FunctionContext) -> JsResult { /// // Extract the console.log function: -/// let console: Handle = cx.global().get(&mut cx, "console")?; +/// let console: Handle = cx.global("console")?; /// let log: Handle = console.get(&mut cx, "log")?; /// /// // Create a string: @@ -696,7 +695,7 @@ impl JsString { /// # use neon::prelude::*; /// # fn test(mut cx: FunctionContext) -> JsResult { /// // Extract the console.log function: -/// let console: Handle = cx.global().get(&mut cx, "console")?; +/// let console: Handle = cx.global("console")?; /// let log: Handle = console.get(&mut cx, "log")?; /// /// // Create a number: @@ -772,7 +771,7 @@ impl ValueInternal for JsNumber { /// # use neon::prelude::*; /// # fn test(mut cx: FunctionContext) -> JsResult { /// // Extract the console.log function: -/// let console: Handle = cx.global().get(&mut cx, "console")?; +/// let console: Handle = cx.global("console")?; /// let log: Handle = console.get(&mut cx, "log")?; /// /// // Create an object: @@ -985,9 +984,8 @@ impl Object for JsArray {} /// ``` /// # use neon::prelude::*; /// # fn foo(mut cx: FunctionContext) -> JsResult { -/// # let global = cx.global(); /// // Extract the parseInt function from the global object -/// let parse_int: Handle = global.get(&mut cx, "parseInt")?; +/// let parse_int: Handle = cx.global("parseInt")?; /// /// // Call parseInt("42") /// let x: Handle = parse_int @@ -1006,9 +1004,8 @@ impl Object for JsArray {} /// ``` /// # use neon::prelude::*; /// # fn foo(mut cx: FunctionContext) -> JsResult { -/// # let global = cx.global(); /// // Extract the URL constructor from the global object -/// let url: Handle = global.get(&mut cx, "URL")?; +/// let url: Handle = cx.global("URL")?; /// /// // Call new URL("https://neon-bindings.com") /// let obj = url diff --git a/test/napi/src/js/functions.rs b/test/napi/src/js/functions.rs index 0a099774a..3baea1174 100644 --- a/test/napi/src/js/functions.rs +++ b/test/napi/src/js/functions.rs @@ -27,7 +27,7 @@ pub fn call_js_function_idiomatically(mut cx: FunctionContext) -> JsResult(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsFunction> { - let math: Handle = cx.global().get(cx, "Math")?; + let math: Handle = cx.global("Math")?; let max: Handle = math.get(cx, "max")?; Ok(max) } @@ -96,8 +96,7 @@ pub fn exec_js_function_with_implicit_this(mut cx: FunctionContext) -> JsResult< } pub fn call_js_function_with_heterogeneous_tuple(mut cx: FunctionContext) -> JsResult { - cx.global() - .get::(&mut cx, "Array")? + cx.global::("Array")? .call_with(&cx) .args((cx.number(1.0), cx.string("hello"), cx.boolean(true))) .apply(&mut cx) @@ -129,8 +128,7 @@ pub fn construct_js_function_idiomatically(mut cx: FunctionContext) -> JsResult< } pub fn construct_js_function_with_overloaded_result(mut cx: FunctionContext) -> JsResult { - let global = cx.global(); - let f: Handle = global.get(&mut cx, "Array")?; + let f: Handle = cx.global("Array")?; f.construct_with(&cx) .arg(cx.number(1)) .arg(cx.number(2)) @@ -225,7 +223,7 @@ pub fn throw_and_catch(mut cx: FunctionContext) -> JsResult { pub fn call_and_catch(mut cx: FunctionContext) -> JsResult { let f: Handle = cx.argument(0)?; Ok(cx - .try_catch(|cx| f.call_with(cx).this(cx.global()).apply(cx)) + .try_catch(|cx| f.call_with(cx).this(cx.global_object()).apply(cx)) .unwrap_or_else(|err| err)) } diff --git a/test/napi/src/js/objects.rs b/test/napi/src/js/objects.rs index 4c4f7f582..5e9013b6e 100644 --- a/test/napi/src/js/objects.rs +++ b/test/napi/src/js/objects.rs @@ -3,7 +3,7 @@ use std::borrow::Cow; use neon::{prelude::*, types::buffer::TypedArray}; pub fn return_js_global_object(mut cx: FunctionContext) -> JsResult { - Ok(cx.global()) + Ok(cx.global_object()) } pub fn return_js_object(mut cx: FunctionContext) -> JsResult { diff --git a/test/napi/src/js/workers.rs b/test/napi/src/js/workers.rs index bfd17f74c..bc6c64bfe 100644 --- a/test/napi/src/js/workers.rs +++ b/test/napi/src/js/workers.rs @@ -77,7 +77,7 @@ static GLOBAL_OBJECT: LocalKey> = LocalKey::new(); pub fn stash_global_object(mut cx: FunctionContext) -> JsResult { GLOBAL_OBJECT.get_or_try_init(&mut cx, |cx| { - let global = cx.global(); + let global = cx.global_object(); let global: Root = Root::new(cx, &global); Ok(global) })?;