Add Debug.log

This commit is contained in:
Andrew Morris
2022-05-30 11:16:07 +10:00
parent 8ad6802289
commit fe823a8644
5 changed files with 65 additions and 1 deletions

View File

@@ -579,8 +579,9 @@ impl<'a> Assembler<'a> {
}
fn assemble_builtin(&mut self) {
match self.parse_one_of(&["Math"]).as_str() {
match self.parse_one_of(&["Math", "Debug"]).as_str() {
"Math" => self.write_varsize_uint(0),
"Debug" => self.write_varsize_uint(1),
_ => std::panic!("Shouldn't happen"),
}
}

View File

@@ -7,6 +7,7 @@ use super::function_compiler::QueuedFunction;
#[derive(Clone, Debug)]
pub enum Builtin {
Math,
Debug,
}
impl std::fmt::Display for Builtin {
@@ -84,6 +85,7 @@ pub fn init_std_scope() -> Scope {
return Rc::new(RefCell::new(ScopeData {
name_map: HashMap::from([
("Math".to_string(), MappedName::Builtin(Builtin::Math)),
("Debug".to_string(), MappedName::Builtin(Builtin::Debug)),
]),
parent: None,
})).nest();

View File

@@ -1,11 +1,13 @@
use super::vs_value::ValTrait;
use super::math::MATH;
use super::debug::DEBUG;
// TODO: Investigate whether a static array can be used for this and why rust
// seems to not like it when I try.
pub fn get_builtin(index: usize) -> &'static dyn ValTrait {
return match index {
0 => &MATH,
1 => &DEBUG,
_ => std::panic!(""),
}
}

View File

@@ -0,0 +1,58 @@
use std::rc::Rc;
use super::vs_value::{Val, VsType, ValTrait, LoadFunctionResult};
use super::vs_array::VsArray;
use super::vs_object::VsObject;
use super::vs_class::VsClass;
use super::native_function::NativeFunction;
pub struct Debug {}
pub static DEBUG: Debug = Debug {};
impl ValTrait for Debug {
fn typeof_(&self) -> VsType { VsType::Object }
fn val_to_string(&self) -> String { "[object Debug]".to_string() }
fn to_number(&self) -> f64 { f64::NAN }
fn to_index(&self) -> Option<usize> { None }
fn is_primitive(&self) -> bool { false }
fn to_primitive(&self) -> Val { Val::String(Rc::new(self.val_to_string())) }
fn is_truthy(&self) -> bool { true }
fn is_nullish(&self) -> bool { false }
fn bind(&self, _params: Vec<Val>) -> Option<Val> { None }
fn as_array_data(&self) -> Option<Rc<VsArray>> { None }
fn as_object_data(&self) -> Option<Rc<VsObject>> { None }
fn as_class_data(&self) -> Option<Rc<VsClass>> { None }
fn load_function(&self) -> LoadFunctionResult {
LoadFunctionResult::NotAFunction
}
fn sub(&self, key: Val) -> Val {
match key.val_to_string().as_str() {
"log" => Val::Static(&LOG),
_ => Val::Undefined,
}
}
fn submov(&mut self, _key: Val, _value: Val) {
std::panic!("Not implemented: exceptions");
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[Debug]\x1b[39m")
}
}
static LOG: NativeFunction = NativeFunction {
fn_: |_this: &mut Val, params: Vec<Val>| -> Val {
for p in params {
println!("Debug.log: {}", p);
}
return Val::Undefined;
}
};

View File

@@ -16,5 +16,6 @@ mod stack_frame;
mod first_stack_frame;
mod array_higher_functions;
mod native_frame_function;
mod debug;
pub use virtual_machine::VirtualMachine;