use std::any::Any;
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use crate::userdata::{Ud, UserData};
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Value {
Nil,
Bool(bool),
Number(f64),
String(Rc<str>),
Function(Function),
Table(Table),
UserData(AnyUserData),
Thread(Thread),
Buffer(Buffer),
Vector([f32; 3]),
}
#[derive(Clone, Debug)]
pub struct Buffer {
pub(crate) r: rhue_vm::BufRef,
pub(crate) pin: Rc<rhue_vm::Pin>,
}
impl PartialEq for Buffer {
fn eq(&self, other: &Self) -> bool {
self.r == other.r && self.pin.vm_id() == other.pin.vm_id()
}
}
#[derive(Clone, Debug)]
pub struct Function {
pub(crate) r: rhue_vm::FnRef,
pub(crate) pin: Rc<rhue_vm::Pin>,
}
impl PartialEq for Function {
fn eq(&self, other: &Self) -> bool {
self.r == other.r && self.pin.vm_id() == other.pin.vm_id()
}
}
#[derive(Clone, Debug)]
pub struct Table {
pub(crate) r: rhue_vm::TableRef,
pub(crate) pin: Rc<rhue_vm::Pin>,
}
impl PartialEq for Table {
fn eq(&self, other: &Self) -> bool {
self.r == other.r && self.pin.vm_id() == other.pin.vm_id()
}
}
#[derive(Clone, Debug)]
pub struct Thread {
pub(crate) r: rhue_vm::ThreadRef,
pub(crate) pin: Rc<rhue_vm::Pin>,
}
impl PartialEq for Thread {
fn eq(&self, other: &Self) -> bool {
self.r == other.r && self.pin.vm_id() == other.pin.vm_id()
}
}
#[derive(Clone)]
pub struct AnyUserData {
pub(crate) r: rhue_vm::UdRef,
pub(crate) data: Rc<dyn Any>,
pub(crate) type_name: Rc<str>,
pub(crate) pin: Rc<rhue_vm::Pin>,
}
impl AnyUserData {
pub fn type_name(&self) -> &str {
&self.type_name
}
pub fn is<T: UserData>(&self) -> bool {
self.data.is::<RefCell<T>>()
}
pub fn downcast<T: UserData>(&self) -> Option<Ud<T>> {
let cell = self.data.clone().downcast::<RefCell<T>>().ok()?;
Some(Ud {
any: self.clone(),
cell,
})
}
}
impl PartialEq for AnyUserData {
fn eq(&self, other: &Self) -> bool {
self.r == other.r && self.pin.vm_id() == other.pin.vm_id()
}
}
impl fmt::Debug for AnyUserData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "AnyUserData({}: 0x{:08x})", self.type_name, self.r.id())
}
}
impl Value {
pub fn type_name(&self) -> &'static str {
match self {
Value::Nil => "nil",
Value::Bool(_) => "boolean",
Value::Number(_) => "number",
Value::String(_) => "string",
Value::Function(_) => "function",
Value::Table(_) => "table",
Value::UserData(_) => "userdata",
Value::Thread(_) => "thread",
Value::Buffer(_) => "buffer",
Value::Vector(_) => "vector",
}
}
pub fn type_of(&self) -> &str {
match self {
Value::UserData(u) => u.type_name(),
other => other.type_name(),
}
}
pub fn as_number(&self) -> Option<f64> {
match self {
Value::Number(n) => Some(*n),
_ => None,
}
}
pub fn as_str(&self) -> Option<&str> {
match self {
Value::String(s) => Some(s),
_ => None,
}
}
pub fn is_nil(&self) -> bool {
matches!(self, Value::Nil)
}
pub fn is_truthy(&self) -> bool {
!matches!(self, Value::Nil | Value::Bool(false))
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Value::Nil => f.write_str("nil"),
Value::Bool(b) => write!(f, "{b}"),
Value::Number(n) => f.write_str(&rhue_bytecode::format_number(*n)),
Value::String(s) => f.write_str(s),
Value::Function(func) => write!(f, "function: 0x{:08x}", func.r.id()),
Value::Table(t) => write!(f, "table: 0x{:08x}", t.r.id()),
Value::UserData(u) => write!(f, "{}: 0x{:08x}", u.type_name, u.r.id()),
Value::Thread(t) => write!(f, "thread: 0x{:08x}", t.r.id()),
Value::Buffer(b) => write!(f, "buffer: 0x{:08x}", b.r.id()),
Value::Vector(v) => f.write_str(&rhue_vm::vector_text(*v)),
}
}
}
impl From<f64> for Value {
fn from(n: f64) -> Self {
Value::Number(n)
}
}
impl From<i32> for Value {
fn from(n: i32) -> Self {
Value::Number(n as f64)
}
}
impl From<bool> for Value {
fn from(b: bool) -> Self {
Value::Bool(b)
}
}
impl From<&str> for Value {
fn from(s: &str) -> Self {
Value::String(Rc::from(s))
}
}
impl From<String> for Value {
fn from(s: String) -> Self {
Value::String(Rc::from(s))
}
}
impl From<()> for Value {
fn from(_: ()) -> Self {
Value::Nil
}
}
impl From<Function> for Value {
fn from(f: Function) -> Self {
Value::Function(f)
}
}
impl From<Table> for Value {
fn from(t: Table) -> Self {
Value::Table(t)
}
}