From caaece0d9a62994ef07cf4597d7641762ba8f1ec Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sun, 19 Jun 2022 08:39:11 +0530 Subject: [PATCH] feat(serde_v8): add serde_v8::Global (#14761) --- serde_v8/lib.rs | 1 + serde_v8/magic/global.rs | 41 ++++++++++++++++++++++++++++++++++++++++ serde_v8/magic/mod.rs | 2 ++ 3 files changed, 44 insertions(+) create mode 100644 serde_v8/magic/global.rs diff --git a/serde_v8/lib.rs b/serde_v8/lib.rs index 49f6691518..73259a0616 100644 --- a/serde_v8/lib.rs +++ b/serde_v8/lib.rs @@ -16,6 +16,7 @@ pub use magic::bytestring::ByteString; pub use magic::detached_buffer::DetachedBuffer; pub use magic::string_or_buffer::StringOrBuffer; pub use magic::u16string::U16String; +pub use magic::Global; pub use magic::Value; pub use ser::{to_v8, Serializer}; pub use serializable::{Serializable, SerializablePkg}; diff --git a/serde_v8/magic/global.rs b/serde_v8/magic/global.rs new file mode 100644 index 0000000000..9423940678 --- /dev/null +++ b/serde_v8/magic/global.rs @@ -0,0 +1,41 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + +use crate::magic::transl8::impl_magic; +use crate::magic::transl8::FromV8; +use crate::magic::transl8::ToV8; + +pub struct Global { + pub v8_value: v8::Global, +} +impl_magic!(Global); + +impl From> for Global { + fn from(v8_value: v8::Global) -> Self { + Self { v8_value } + } +} + +impl From for v8::Global { + fn from(v: Global) -> Self { + v.v8_value + } +} + +impl ToV8 for Global { + fn to_v8<'a>( + &self, + scope: &mut v8::HandleScope<'a>, + ) -> Result, crate::Error> { + Ok(v8::Local::new(scope, self.v8_value.clone())) + } +} + +impl FromV8 for Global { + fn from_v8( + scope: &mut v8::HandleScope, + value: v8::Local, + ) -> Result { + let global = v8::Global::new(scope, value); + Ok(global.into()) + } +} diff --git a/serde_v8/magic/mod.rs b/serde_v8/magic/mod.rs index c9bfce5732..0cac688316 100644 --- a/serde_v8/magic/mod.rs +++ b/serde_v8/magic/mod.rs @@ -2,10 +2,12 @@ pub mod buffer; pub mod bytestring; pub mod detached_buffer; +mod global; pub(super) mod rawbytes; pub mod string_or_buffer; pub mod transl8; pub mod u16string; pub mod v8slice; mod value; +pub use global::Global; pub use value::Value;