Support undefined + null literals in vm

This commit is contained in:
Andrew Morris
2022-05-03 13:00:48 +10:00
parent c09ca141f1
commit fa64b907e2
3 changed files with 45 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
use std::rc::Rc;
use super::vs_value::Val;
use super::vs_undefined::VsUndefined;
use super::vs_null::VsNull;
use super::vs_number::VsNumber;
use super::vs_string::VsString;
use super::vs_pointer::VsPointer;
@@ -81,8 +83,8 @@ impl BytecodeDecoder {
return match self.decode_type() {
End => std::panic!("Cannot decode end"),
Undefined => std::panic!("Not implemented"),
Null => std::panic!("Not implemented"),
Undefined => VsUndefined::new(),
Null => VsNull::new(),
False => std::panic!("Not implemented"),
True => std::panic!("Not implemented"),
SignedByte => VsNumber::from_f64(

View File

@@ -1,5 +1,6 @@
mod vs_value;
mod vs_undefined;
mod vs_null;
mod vs_bool;
mod vs_number;
mod vs_string;

View File

@@ -0,0 +1,40 @@
use std::rc::Rc;
use super::vs_value::Val;
use super::vs_value::VsType;
use super::vs_value::VsValue;
use super::virtual_machine::StackFrame;
pub struct VsNull {}
impl VsNull {
pub fn new() -> Val {
return Rc::new(VsNull {});
}
}
impl VsValue for VsNull {
fn typeof_(&self) -> VsType {
return VsType::Null;
}
fn to_string(&self) -> String {
return "null".to_string();
}
fn to_number(&self) -> f64 {
return 0_f64;
}
fn is_primitive(&self) -> bool {
return true;
}
fn make_frame(&self) -> Option<StackFrame> {
return None;
}
fn is_truthy(&self) -> bool {
return false;
}
}