From 732da70c6e46c381d7cb875983d8b0de6780c47d Mon Sep 17 00:00:00 2001 From: Alistair Date: Fri, 24 Nov 2023 11:22:04 +0000 Subject: [PATCH] Add `Object::set_native_object_data` --- boa_engine/src/object/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/boa_engine/src/object/mod.rs b/boa_engine/src/object/mod.rs index c0e246027d3..c02afa77667 100644 --- a/boa_engine/src/object/mod.rs +++ b/boa_engine/src/object/mod.rs @@ -78,6 +78,7 @@ use boa_gc::{custom_trace, Finalize, Trace, WeakGc}; use std::{ any::{Any, TypeId}, fmt::{self, Debug}, + mem, ops::{Deref, DerefMut}, }; @@ -1990,6 +1991,19 @@ impl Object { } } + /// Sets the native object data returning the previous data, if the + /// object is a 'NativeObject'. + #[inline] + pub fn set_native_object_data( + &mut self, + data: T, + ) -> Option> { + match self.kind { + ObjectKind::NativeObject(ref mut object) => Some(mem::replace(object, Box::new(data))), + _ => None, + } + } + /// Checks if it is a `Promise` object. #[inline] #[must_use]