Skip to content

Commit

Permalink
refactor: 🎨 Refactored global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
BHznJNs committed Aug 17, 2023
1 parent 7d74ef2 commit 3e96da8
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 85 deletions.
23 changes: 12 additions & 11 deletions src/public/run_time/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::f64::consts::E as STD_E;
use std::f64::consts::PI as STD_PI;

use crate::public::value::unique::EMPTY_GLOBAL_UNIQUE;
use crate::public::value::{number::Number, unique::GlobalUnique, value::Value};

pub const PI: Value = Value::Number(Number::Float(STD_PI));
Expand All @@ -10,17 +11,17 @@ pub const TRUE: Value = Value::Boolean(true);
pub const FALSE: Value = Value::Boolean(false);

pub static mut IS_INITED: bool = false;
pub static mut VOID_T: GlobalUnique = GlobalUnique { value: None };
pub static mut BOOL_T: GlobalUnique = GlobalUnique { value: None };
pub static mut NUMBER_T: GlobalUnique = GlobalUnique { value: None };
pub static mut UNIQUE_T: GlobalUnique = GlobalUnique { value: None };
pub static mut STRING_T: GlobalUnique = GlobalUnique { value: None };
pub static mut ARRAY_T: GlobalUnique = GlobalUnique { value: None };
pub static mut MAP_T: GlobalUnique = GlobalUnique { value: None };
pub static mut LAZYEXPR_T: GlobalUnique = GlobalUnique { value: None };
pub static mut FUNCTION_T: GlobalUnique = GlobalUnique { value: None };
pub static mut CLASS_T: GlobalUnique = GlobalUnique { value: None };
pub static mut OBJECT_T: GlobalUnique = GlobalUnique { value: None };
pub static mut VOID_T: GlobalUnique = EMPTY_GLOBAL_UNIQUE;
pub static mut BOOL_T: GlobalUnique = EMPTY_GLOBAL_UNIQUE;
pub static mut NUMBER_T: GlobalUnique = EMPTY_GLOBAL_UNIQUE;
pub static mut UNIQUE_T: GlobalUnique = EMPTY_GLOBAL_UNIQUE;
pub static mut STRING_T: GlobalUnique = EMPTY_GLOBAL_UNIQUE;
pub static mut ARRAY_T: GlobalUnique = EMPTY_GLOBAL_UNIQUE;
pub static mut MAP_T: GlobalUnique = EMPTY_GLOBAL_UNIQUE;
pub static mut LAZYEXPR_T: GlobalUnique = EMPTY_GLOBAL_UNIQUE;
pub static mut FUNCTION_T: GlobalUnique = EMPTY_GLOBAL_UNIQUE;
pub static mut CLASS_T: GlobalUnique = EMPTY_GLOBAL_UNIQUE;
pub static mut OBJECT_T: GlobalUnique = EMPTY_GLOBAL_UNIQUE;

unsafe fn static_init() {
IS_INITED = true;
Expand Down
22 changes: 22 additions & 0 deletions src/public/std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,25 @@ impl StdModules {
}
}
}

// --- --- --- --- --- ---

pub struct ModuleClass(Option<Rc<Class>>);
pub const EMPTY_MODULE_CLASS: ModuleClass = ModuleClass(None);

impl ModuleClass {
pub fn is_some_or_init(&mut self, class_cb: fn() -> Class) {
if self.0.is_none() {
let class = class_cb();
self.0 = Some(class.into());
}
}

pub fn unwrap(&self) -> Rc<Class> {
let value_ref = self.0.as_ref();
let Some(wraped) = value_ref else {
unreachable!()
};
return wraped.clone();
}
}
46 changes: 21 additions & 25 deletions src/public/std/modules/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::rc::Rc;

use crate::public::run_time::build_in::BuildInFnIdenti;
use crate::public::run_time::scope::Scope;
use crate::public::std::{ModuleClass, EMPTY_MODULE_CLASS};
use crate::public::std::utils::get_self_prop::get_self_prop;
use crate::public::value::function::{BuildInFnParam, BuildInFunction, Function};
use crate::public::value::oop::class::{Class, Property};
Expand All @@ -23,9 +24,9 @@ pub enum ArrayModule {
JOIN,
}

static mut MODULE_CLASS: Option<Rc<Class>> = None;
pub static mut MODULE_CLASS: ModuleClass = EMPTY_MODULE_CLASS;
impl ClassModule for ArrayModule {
fn __static_class_init() {
fn __static_class__() -> Class {
let push = BuildInFunction {
params: vec![
BuildInFnParam(ValueType::Object, "self"),
Expand Down Expand Up @@ -82,32 +83,27 @@ impl ClassModule for ArrayModule {

// --- --- --- --- --- ---

unsafe {
MODULE_CLASS = Some(
Class::new(
vec![Property(ValueType::Array, String::from("v"))],
vec![
(String::from("push"), Function::from(push)),
(String::from("pop"), Function::from(pop)),
(String::from("shift"), Function::from(shift)),
(String::from("unshift"), Function::from(unshift)),
(String::from("insert"), Function::from(insert)),
(String::from("remove"), Function::from(remove)),
(String::from("contains"), Function::from(contains)),
(String::from("slice"), Function::from(slice)),
(String::from("join"), Function::from(join)),
],
)
.into(),
)
}
return Class::new(
vec![Property(ValueType::Array, String::from("v"))],
vec![
(String::from("push"), Function::from(push)),
(String::from("pop"), Function::from(pop)),
(String::from("shift"), Function::from(shift)),
(String::from("unshift"), Function::from(unshift)),
(String::from("insert"), Function::from(insert)),
(String::from("remove"), Function::from(remove)),
(String::from("contains"), Function::from(contains)),
(String::from("slice"), Function::from(slice)),
(String::from("join"), Function::from(join)),
],
);
}

fn module_class() -> Rc<Class> {
if unsafe { MODULE_CLASS.is_none() } {
Self::__static_class_init();
}
let class = unsafe { MODULE_CLASS.as_ref().unwrap().clone() };
let class = unsafe {
MODULE_CLASS.is_some_or_init(Self::__static_class__);
MODULE_CLASS.unwrap()
};
return class;
}
}
Expand Down
37 changes: 16 additions & 21 deletions src/public/std/modules/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::rc::Rc;

use crate::public::{
run_time::{build_in::BuildInFnIdenti, scope::Scope},
std::utils::{get_self_prop::get_self_prop, get_val::get_val},
std::{utils::{get_self_prop::get_self_prop, get_val::get_val}, ModuleClass, EMPTY_MODULE_CLASS},
value::{
array::ArrayLiteral,
function::{BuildInFnParam, BuildInFunction, Function},
Expand All @@ -21,9 +21,9 @@ pub enum MapModule {
HASKEY,
}

static mut MODULE_CLASS: Option<Rc<Class>> = None;
static mut MODULE_CLASS: ModuleClass = EMPTY_MODULE_CLASS;
impl ClassModule for MapModule {
fn __static_class_init() {
fn __static_class__() -> Class {
let clear = BuildInFunction {
params: vec![BuildInFnParam(ValueType::Object, "self")],
identi: BuildInFnIdenti::Map(Self::CLEAR),
Expand All @@ -42,26 +42,21 @@ impl ClassModule for MapModule {
identi: BuildInFnIdenti::Map(Self::HASKEY),
};

unsafe {
MODULE_CLASS = Some(
Class::new(
vec![Property(ValueType::Map, String::from("v"))],
vec![
(String::from("clear"), Function::from(clear)),
(String::from("keys"), Function::from(keys)),
(String::from("values"), Function::from(values)),
(String::from("has_key"), Function::from(has_key)),
],
)
.into(),
)
}
return Class::new(
vec![Property(ValueType::Map, String::from("v"))],
vec![
(String::from("clear"), Function::from(clear)),
(String::from("keys"), Function::from(keys)),
(String::from("values"), Function::from(values)),
(String::from("has_key"), Function::from(has_key)),
],
);
}
fn module_class() -> Rc<Class> {
if unsafe { MODULE_CLASS.is_none() } {
Self::__static_class_init();
}
let class = unsafe { MODULE_CLASS.as_ref().unwrap().clone() };
let class = unsafe {
MODULE_CLASS.is_some_or_init(Self::__static_class__);
MODULE_CLASS.unwrap()
};
return class;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/public/std/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait FunctionModule: BuildInFnCall {
}

pub trait ClassModule: BuildInFnCall {
fn __static_class_init();
fn __static_class__() -> Class;
fn module_class() -> Rc<Class>;
}
pub trait ObjectModule: BuildInFnCall {
Expand Down
39 changes: 17 additions & 22 deletions src/public/std/modules/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::rc::Rc;

use crate::public::run_time::build_in::BuildInFnIdenti;
use crate::public::run_time::scope::Scope;
use crate::public::std::{ModuleClass, EMPTY_MODULE_CLASS};
use crate::public::std::utils::get_self_prop::get_self_prop;
use crate::public::value::function::{BuildInFnParam, BuildInFunction, Function};
use crate::public::value::oop::class::{Class, Property};
Expand All @@ -20,9 +21,9 @@ pub enum StringModule {
ENDWITH,
}

static mut MODULE_CLASS: Option<Rc<Class>> = None;
static mut MODULE_CLASS: ModuleClass = EMPTY_MODULE_CLASS;
impl ClassModule for StringModule {
fn __static_class_init() {
fn __static_class__() -> Class{
let split = BuildInFunction {
params: vec![
BuildInFnParam(ValueType::Object, "self"),
Expand Down Expand Up @@ -59,28 +60,22 @@ impl ClassModule for StringModule {
],
identi: BuildInFnIdenti::String(Self::ENDWITH),
};

unsafe {
MODULE_CLASS = Some(
Class::new(
vec![Property(ValueType::String, String::from("v"))],
vec![
(String::from("split"), Function::from(split)),
(String::from("replace"), Function::from(replace)),
(String::from("repeat"), Function::from(repeat)),
(String::from("start_with"), Function::from(start_with)),
(String::from("end_with"), Function::from(end_with)),
],
)
.into(),
)
}
return Class::new(
vec![Property(ValueType::String, String::from("v"))],
vec![
(String::from("split"), Function::from(split)),
(String::from("replace"), Function::from(replace)),
(String::from("repeat"), Function::from(repeat)),
(String::from("start_with"), Function::from(start_with)),
(String::from("end_with"), Function::from(end_with)),
],
);
}
fn module_class() -> Rc<Class> {
if unsafe { MODULE_CLASS.is_none() } {
Self::__static_class_init();
}
let class = unsafe { MODULE_CLASS.as_ref().unwrap().clone() };
let class = unsafe {
MODULE_CLASS.is_some_or_init(Self::__static_class__);
MODULE_CLASS.unwrap()
};
return class;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/public/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub(super) trait GetAddr {
fn get_addr(&self) -> Addr;
}

// --- --- --- --- --- ---

pub trait ComplexStructure {
fn display(
f: &mut fmt::Formatter<'_>,
Expand Down
10 changes: 5 additions & 5 deletions src/public/value/unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ impl GetAddr for Unique {

// --- --- --- --- --- ---

pub struct GlobalUnique {
pub value: Option<Unique>,
}
pub struct GlobalUnique(Option<Unique>);
pub const EMPTY_GLOBAL_UNIQUE: GlobalUnique = GlobalUnique(None);

impl GlobalUnique {
pub fn init(&mut self, identi: &str) {
self.value = Some(Unique::from(identi));
self.0 = Some(Unique::from(identi));
}

pub fn unwrap(&self) -> Unique {
let value_ref = self.value.as_ref();
let value_ref = self.0.as_ref();
let Some(wraped) = value_ref else {
unreachable!()
};
Expand Down

0 comments on commit 3e96da8

Please sign in to comment.