Skip to content

Commit

Permalink
finish refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Dec 4, 2023
1 parent 3fd795b commit 24d87c1
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 165 deletions.
3 changes: 2 additions & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use netsblox_vm::runtime::*;
use netsblox_vm::project::*;
use netsblox_vm::gc::*;
use netsblox_vm::ast;
use netsblox_vm::compact_str::CompactString;

use std::io::Read;
use std::time::Duration;
Expand Down Expand Up @@ -123,7 +124,7 @@ fn main() {
};

// initialize our system with all the info we've put together
let system = Rc::new(StdSystem::new_sync(BASE_URL.to_owned(), None, config, clock.clone()));
let system = Rc::new(StdSystem::new_sync(CompactString::new(BASE_URL), None, config, clock.clone()));
let mut env = get_running_project(&xml, system);

// begin running the code - these are some helpers to make things more efficient in terms of memory and cpu resources
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ use serde::{Serialize, Deserialize};
use monostate::MustBeU128;
use num_traits::FromPrimitive;
use bin_pool::BinPool;
use compact_str::CompactString;

use crate::*;
use crate::meta::*;
use crate::runtime::{Color, Number, NumberError, Event, KeyCode, Property, PrintStyle, Type, CustomTypes, System};
use crate::util::LosslessJoin;
use crate::vecmap::VecMap;
use crate::compact_str::CompactString;

/// Number of bytes to display on each line of a hex dump
#[cfg(feature = "std")]
Expand Down
3 changes: 1 addition & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ use crossterm::event::{self, Event, KeyCode as RawKeyCode, KeyModifiers as RawKe
use crossterm::terminal::{self, ClearType};
use crossterm::style::{ResetColor, SetForegroundColor, Color, Print};

use compact_str::{CompactString, format_compact};

use crate::*;
use crate::gc::*;
use crate::json::*;
Expand All @@ -48,6 +46,7 @@ use crate::runtime::*;
use crate::process::*;
use crate::project::*;
use crate::template::*;
use crate::compact_str::*;

const DEFAULT_BASE_URL: &str = "https://cloud.netsblox.org";
const DEFAULT_EDITOR_URL: &str = "https://editor.netsblox.org";
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ pub mod real_time {
pub use time::{self, OffsetDateTime, UtcOffset};
}

/// The re-exported version of the `compact-str` crate.
pub mod compact_str {
pub use ::compact_str::{self, CompactString, ToCompactString, CompactStringExt, format_compact};
}

/// The re-exported version of the `netsblox-ast` crate.
pub use netsblox_ast as ast;

/// The re-exported version of the `compact-str` crate.
pub use compact_str;

pub mod bytecode;
pub mod slotmap;
pub mod vecmap;
Expand Down
13 changes: 6 additions & 7 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use alloc::collections::{BTreeMap, BTreeSet, VecDeque, vec_deque::Iter as VecDeq
use core::iter::{self, Cycle};
use core::cmp::Ordering;

use compact_str::{CompactString, format_compact, ToCompactString};

use unicase::UniCase;

#[cfg(feature = "serde")]
Expand All @@ -27,6 +25,7 @@ use crate::json::*;
use crate::runtime::*;
use crate::bytecode::*;
use crate::util::*;
use crate::compact_str::*;

fn empty_string() -> Rc<CompactString> {
#[cfg(feature = "std")]
Expand Down Expand Up @@ -1872,17 +1871,17 @@ mod ops {
(Value::Bool(_), _) | (_, Value::Bool(_)) => None,

(Value::Number(a), Value::Number(b)) => a.cmp(b).into(),
(Value::String(a), Value::String(b)) => match Value::<C, S>::parse_number(a).and_then(|a| Value::<C, S>::parse_number(b).map(|b| (a, b))) {
(Value::String(a), Value::String(b)) => match SimpleValue::parse_number(a).and_then(|a| SimpleValue::parse_number(b).map(|b| (a, b))) {
Some((a, b)) => a.cmp(&b).into(),
None => UniCase::new(a.as_str()).cmp(&UniCase::new(b.as_str())).into(),
}
(Value::Number(a), Value::String(b)) => match Value::<C, S>::parse_number(b) {
(Value::Number(a), Value::String(b)) => match SimpleValue::parse_number(b) {
Some(b) => a.cmp(&b).into(),
None => UniCase::new(Value::<C, S>::stringify_number(*a).as_str()).cmp(&UniCase::new(b.as_str())).into(),
None => UniCase::new(SimpleValue::stringify_number(*a).as_str()).cmp(&UniCase::new(b.as_str())).into(),
}
(Value::String(a), Value::Number(b)) => match Value::<C, S>::parse_number(a) {
(Value::String(a), Value::Number(b)) => match SimpleValue::parse_number(a) {
Some(a) => a.cmp(b).into(),
None => UniCase::new(a.as_str()).cmp(&UniCase::new(&Value::<C, S>::stringify_number(*b).as_str())).into(),
None => UniCase::new(a.as_str()).cmp(&UniCase::new(&SimpleValue::stringify_number(*b).as_str())).into(),
}
(Value::Number(_), _) | (_, Value::Number(_)) => None,
(Value::String(_), _) | (_, Value::String(_)) => None,
Expand Down
5 changes: 2 additions & 3 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use crate::slotmap::*;
use crate::runtime::*;
use crate::bytecode::*;
use crate::process::*;
use crate::vecmap::VecMap;

use compact_str::CompactString;
use crate::compact_str::*;
use crate::vecmap::*;

new_key! {
struct ProcessKey;
Expand Down
45 changes: 24 additions & 21 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use core::cell::Ref;

use rand::distributions::uniform::{SampleUniform, SampleRange};
use checked_float::{FloatChecker, CheckedFloat};
use compact_str::{CompactString, format_compact};

#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
Expand All @@ -23,7 +22,8 @@ use crate::json::*;
use crate::real_time::*;
use crate::bytecode::*;
use crate::process::*;
use crate::vecmap::VecMap;
use crate::compact_str::*;
use crate::vecmap::*;

/// Error type used by [`NumberChecker`].
#[derive(Debug)]
Expand Down Expand Up @@ -803,6 +803,25 @@ impl SimpleValue {
}
})
}

/// Parses a string into a number just as the runtime would do natively.
pub fn parse_number(s: &str) -> Option<Number> {
let s = s.trim();
let parsed = match s.get(..2) {
Some("0x" | "0X") => i64::from_str_radix(&s[2..], 16).ok().map(|x| x as f64),
Some("0o" | "0O") => i64::from_str_radix(&s[2..], 8).ok().map(|x| x as f64),
Some("0b" | "0B") => i64::from_str_radix(&s[2..], 2).ok().map(|x| x as f64),
_ => s.parse::<f64>().ok(),
};
parsed.and_then(|x| Number::new(x).ok())
}
/// Stringifies a number just as the runtime would do natively.
pub fn stringify_number(v: Number) -> CompactString {
debug_assert!(v.get().is_finite());
let mut buf = ryu::Buffer::new();
let res = buf.format_finite(v.get());
CompactString::new(res.strip_suffix(".0").unwrap_or(res))
}
}

#[test]
Expand Down Expand Up @@ -873,6 +892,7 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> GetType for Value<'gc, C, S> {
}
}

#[derive(Clone)]
pub enum CompactCow<'a> {
Borrowed(&'a str),
Owned(CompactString),
Expand Down Expand Up @@ -1013,23 +1033,6 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> Value<'gc, C, S> {
}
}

pub(crate) fn parse_number(s: &str) -> Option<Number> {
let s = s.trim();
let parsed = match s.get(..2) {
Some("0x" | "0X") => i64::from_str_radix(&s[2..], 16).ok().map(|x| x as f64),
Some("0o" | "0O") => i64::from_str_radix(&s[2..], 8).ok().map(|x| x as f64),
Some("0b" | "0B") => i64::from_str_radix(&s[2..], 2).ok().map(|x| x as f64),
_ => s.parse::<f64>().ok(),
};
parsed.and_then(|x| Number::new(x).ok())
}
pub(crate) fn stringify_number(v: Number) -> CompactString {
debug_assert!(v.get().is_finite());
let mut buf = ryu::Buffer::new();
let res = buf.format_finite(v.get());
CompactString::new(res.strip_suffix(".0").unwrap_or(res))
}

/// Attempts to interpret this value as a bool.
pub fn as_bool(&self) -> Result<bool, ConversionError<C, S>> {
Ok(match self {
Expand All @@ -1041,15 +1044,15 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> Value<'gc, C, S> {
pub fn as_number(&self) -> Result<Number, ConversionError<C, S>> {
match self {
Value::Number(x) => Ok(*x),
Value::String(x) => Self::parse_number(x).ok_or(ConversionError { got: Type::String, expected: Type::Number }),
Value::String(x) => SimpleValue::parse_number(x).ok_or(ConversionError { got: Type::String, expected: Type::Number }),
x => Err(ConversionError { got: x.get_type(), expected: Type::Number }),
}
}
/// Attempts to interpret this value as a string.
pub fn as_string(&self) -> Result<CompactCow, ConversionError<C, S>> {
Ok(match self {
Value::String(x) => CompactCow::Borrowed(&**x),
Value::Number(x) => CompactCow::Owned(Self::stringify_number(*x)),
Value::Number(x) => CompactCow::Owned(SimpleValue::stringify_number(*x)),
x => return Err(ConversionError { got: x.get_type(), expected: Type::String }),
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/std_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ use rand_chacha::ChaChaRng;
use rand::{Rng, SeedableRng};
use tokio_tungstenite::tungstenite::Message;
use futures::{StreamExt, SinkExt};
use compact_str::{CompactString, format_compact};
use uuid::Uuid;

use crate::runtime::*;
use crate::process::*;
use crate::json::*;
use crate::gc::*;
use crate::std_util::*;
use crate::vecmap::VecMap;
use crate::vecmap::*;
use crate::compact_str::*;
use crate::*;

const MESSAGE_REPLY_TIMEOUT: Duration = Duration::from_millis(1500);
Expand Down
5 changes: 2 additions & 3 deletions src/std_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use std::sync::{Arc, Mutex};
use crate::real_time::*;
use crate::runtime::*;
use crate::json::*;
use crate::vecmap::VecMap;
use crate::vecmap::*;
use crate::compact_str::*;
use crate::*;

use compact_str::CompactString;

pub struct NetsBloxContext {
pub base_url: CompactString,
pub client_id: CompactString,
Expand Down
Loading

0 comments on commit 24d87c1

Please sign in to comment.