use super::*;
#[derive(Clone, Debug, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct Table {
inner: Any,
}
impl FromVal for Table {
fn from_val(v: &Any) -> Self {
Table {
inner: Any::from_val(v),
}
}
fn take_ownership(v: AnyHandle) -> Self {
Self::from_val(&Any::take_ownership(v))
}
fn as_handle(&self) -> AnyHandle {
self.inner.as_handle()
}
}
impl core::ops::Deref for Table {
type Target = Any;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl core::ops::DerefMut for Table {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl AsRef<Any> for Table {
fn as_ref(&self) -> &Any {
&self.inner
}
}
impl AsMut<Any> for Table {
fn as_mut(&mut self) -> &mut Any {
&mut self.inner
}
}
impl From<Table> for Any {
fn from(s: Table) -> Any {
let handle = s.inner.as_handle();
core::mem::forget(s);
Any::take_ownership(handle)
}
}
impl From<&Table> for Any {
fn from(s: &Table) -> Any {
s.inner.clone().into()
}
}
jsbind::utils::impl_dyn_cast!(Table);
impl Table {
pub fn length(&self) -> u32 {
self.inner.get("length").as_::<u32>()
}
}
impl Table {
pub fn new(descriptor: &TableDescriptor) -> Table {
Self {
inner: Any::global("Table").new(&[descriptor.into()]).as_::<Any>(),
}
}
}
impl Table {
pub fn new_with_value(descriptor: &TableDescriptor, value: &Any) -> Table {
Self {
inner: Any::global("Table")
.new(&[descriptor.into(), value.into()])
.as_::<Any>(),
}
}
}
impl Table {
pub fn grow(&self, delta: u32) -> u32 {
self.inner.call("grow", &[delta.into()]).as_::<u32>()
}
}
impl Table {
pub fn grow_with_value(&self, delta: u32, value: &Any) -> u32 {
self.inner
.call("grow", &[delta.into(), value.into()])
.as_::<u32>()
}
}
impl Table {
pub fn get(&self, index: u32) -> Any {
self.inner.call("get", &[index.into()]).as_::<Any>()
}
}
impl Table {
pub fn set(&self, index: u32) -> Undefined {
self.inner.call("set", &[index.into()]).as_::<Undefined>()
}
}
impl Table {
pub fn set_with_value(&self, index: u32, value: &Any) -> Undefined {
self.inner
.call("set", &[index.into(), value.into()])
.as_::<Undefined>()
}
}