Add clone and ToJsString trait to everything

This commit is contained in:
mStar aka a person 2024-01-25 16:42:04 +01:00
parent fd9f5436e5
commit 7ad59ab87e
8 changed files with 318 additions and 250 deletions

View file

@ -1,6 +1,22 @@
use super::VariableContent;
use widestring::Utf16String;
#[derive(Debug)]
use super::{string::JsString, ToJsString, ValueType};
#[derive(Debug, Clone)]
pub struct JsArray {
pub values: Vec<VariableContent>
pub values: Vec<ValueType>
}
impl ToJsString for JsArray {
fn to_string(&self) -> JsString {
let mut s = Utf16String::from("[");
for i in self.values.iter() {
let i = i as &dyn ToJsString;
s.push_utfstr(&i.to_string().value);
}
s += "]";
JsString {
value: s
}
}
}

View file

@ -1,4 +1,8 @@
#[derive(Debug)]
use widestring::Utf16String;
use super::{string::JsString, ToJsString};
#[derive(Debug, Clone)]
pub struct JsBool {
pub value: bool
}
@ -10,3 +14,11 @@ pub const TRUE: JsBool = JsBool{
pub const FALSE: JsBool = JsBool{
value: false,
};
impl ToJsString for JsBool {
fn to_string(&self) -> JsString {
JsString {
value: if self.value {Utf16String::from("true")} else {Utf16String::from("false")}
}
}
}

View file

@ -9,7 +9,7 @@ pub mod big_int;
pub mod object;
pub mod array;
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub enum VariableContent {
#[default]
Undefined,
@ -17,7 +17,7 @@ pub enum VariableContent {
Value(ValueType),
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum ValueType {
Bool(JsBool),
String(JsString),
@ -26,3 +26,20 @@ pub enum ValueType {
BigInt(JsBigInt),
Object(JsObject),
}
pub trait ToJsString {
fn to_string(&self) -> JsString;
}
impl ToJsString for ValueType {
fn to_string(&self) -> JsString {
match &self {
ValueType::Bool(x) => x.to_string(),
ValueType::String(x) => x.clone(),
ValueType::Symbol(x) => todo!(),
ValueType::Number(x) => x.to_string(),
ValueType::BigInt(x) => x.to_string(),
ValueType::Object(x) => x.to_string(),
}
}
}

View file

@ -1,4 +1,4 @@
use super::{bool::JsBool, string::JsString};
use super::{bool::JsBool, string::JsString, ToJsString};
use widestring::{utf16str, Utf16String};
#[derive(Debug, Clone, Copy)]
@ -114,7 +114,10 @@ impl JsNumber {
jsnum_math_op(&self, &x, |x, y| ((x.floor() as i64) | (y.floor() as i64)) as f64)
}
pub fn to_string(&self) -> JsString {
}
impl ToJsString for JsNumber {
fn to_string(&self) -> JsString {
if self.value.is_infinite() {
JsString{
value: utf16str!("Infinity").to_owned()

View file

@ -1,6 +1,18 @@
use std::collections::HashMap;
#[derive(Debug)]
use widestring::Utf16String;
use super::{string::JsString, ToJsString};
#[derive(Debug, Clone)]
pub struct JsObject {
pub value: HashMap<String, super::VariableContent>
}
impl ToJsString for JsObject {
fn to_string(&self) -> JsString {
JsString{
value: Utf16String::from_str("[object Object]")
}
}
}

View file

@ -1,6 +1,14 @@
use widestring::Utf16String;
#[derive(Debug)]
use super::ToJsString;
#[derive(Debug, Clone)]
pub struct JsString {
pub value: Utf16String,
}
impl ToJsString for JsString {
fn to_string(&self) -> JsString {
self.clone()
}
}

View file

@ -1,4 +1,4 @@
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct JsSymbol {
pub value: f64, // TODO: Understand what symbol is and turn this into a representation for those
}