From e1f61cabc602c97e464383baf472443d15ba2153 Mon Sep 17 00:00:00 2001 From: "K.J. Valencik" Date: Mon, 16 Sep 2024 17:30:36 -0400 Subject: [PATCH] feat(neon): Add TryFromJs implementation for Root --- crates/neon/src/types_impl/extract/private.rs | 5 ++++- .../src/types_impl/extract/try_from_js.rs | 22 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/neon/src/types_impl/extract/private.rs b/crates/neon/src/types_impl/extract/private.rs index 1226ecbee..d78f693b3 100644 --- a/crates/neon/src/types_impl/extract/private.rs +++ b/crates/neon/src/types_impl/extract/private.rs @@ -1,6 +1,7 @@ use crate::{ context::FunctionContext, - handle::Handle, + handle::{Handle, Root}, + object::Object, result::{NeonResult, Throw}, types::{ buffer::Binary, @@ -35,6 +36,8 @@ impl Sealed for &str {} impl<'cx, V: Value> Sealed for Handle<'cx, V> {} +impl Sealed for Root {} + impl Sealed for Option {} impl Sealed for Result {} diff --git a/crates/neon/src/types_impl/extract/try_from_js.rs b/crates/neon/src/types_impl/extract/try_from_js.rs index c34959408..90724794d 100644 --- a/crates/neon/src/types_impl/extract/try_from_js.rs +++ b/crates/neon/src/types_impl/extract/try_from_js.rs @@ -7,7 +7,8 @@ use std::{convert::Infallible, ptr}; use crate::{ context::{internal::ContextInternal, Cx}, - handle::Handle, + handle::{Handle, Root}, + object::Object, result::{NeonResult, ResultExt, Throw}, sys, types::{ @@ -45,6 +46,25 @@ where from_js!(); } +impl<'cx, O> TryFromJs<'cx> for Root +where + O: Object, +{ + type Error = TypeExpected; + + fn try_from_js( + cx: &mut Cx<'cx>, + v: Handle<'cx, JsValue>, + ) -> NeonResult> { + Ok(match v.downcast::(cx) { + Ok(v) => Ok(v.root(cx)), + Err(_) => Err(TypeExpected::new()), + }) + } + + from_js!(); +} + impl<'cx, T> TryFromJs<'cx> for Option where T: TryFromJs<'cx>,