use super::encode::{BinaryDecode, BinaryEncode, EncodeTypeDef, JsRefEncode, TypeDef};
use super::ipc::{DecodeError, DecodedData, EncodedData};
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct JsRef(u64);
impl JsRef {
pub const NULL: Self = Self(129);
pub const UNDEFINED: Self = Self(128);
pub const TRUE: Self = Self(130);
pub const FALSE: Self = Self(131);
#[inline]
pub const fn is_special_value(self) -> bool {
self.0 >= Self::UNDEFINED.0 && self.0 <= Self::FALSE.0
}
#[inline]
pub const fn is_owned_heap_ref(self) -> bool {
self.0 > Self::FALSE.0
}
#[inline]
pub const fn into_abi(self) -> u32 {
self.0 as u32
}
#[inline]
pub const fn from_abi(abi: u32) -> Self {
Self(abi as u64)
}
#[inline]
pub fn next_borrowed_ref() -> Self {
crate::batch::with_runtime(|rt| rt.next_borrowed_ref())
}
#[inline]
pub fn drop_js_object(self) {
crate::batch::drop_js_object(self);
}
#[inline]
pub fn dispose_js_rust_function(self) {
crate::batch::dispose_js_rust_function(self);
}
#[inline]
pub fn invalidate_js_rust_function(self) {
crate::batch::dispose_js_rust_function(self);
}
#[inline]
pub(crate) const fn from_raw(raw: u64) -> Self {
Self(raw)
}
#[inline]
pub(crate) const fn raw(self) -> u64 {
self.0
}
}
impl EncodeTypeDef for JsRef {
fn encode_type_def(type_def: &mut TypeDef) {
type_def.heap_ref();
}
}
impl BinaryEncode for JsRef {
fn encode(self, encoder: &mut EncodedData) {
self.0.encode(encoder);
}
}
impl BinaryDecode for JsRef {
fn decode(_decoder: &mut DecodedData) -> Result<Self, DecodeError> {
Ok(crate::batch::with_runtime(|rt| {
rt.get_next_inbound_js_ref()
}))
}
}
impl JsRefEncode for JsRef {
fn js_ref(&self) -> JsRef {
*self
}
}