Boolean builtin

This commit is contained in:
Andrew Morris
2023-03-20 09:56:50 +11:00
parent 15fc9fa12b
commit 07a299c465
5 changed files with 106 additions and 3 deletions

View File

@@ -0,0 +1,80 @@
use std::rc::Rc;
use crate::{
vs_array::VsArray,
vs_class::VsClass,
vs_object::VsObject,
vs_value::{LoadFunctionResult, Val, VsType},
ValTrait,
};
pub struct BooleanBuiltin {}
pub static BOOLEAN_BUILTIN: BooleanBuiltin = BooleanBuiltin {};
impl ValTrait for BooleanBuiltin {
fn typeof_(&self) -> VsType {
VsType::Object
}
fn val_to_string(&self) -> String {
"function Boolean() { [native code] }".to_string()
}
fn to_number(&self) -> f64 {
core::f64::NAN
}
fn to_index(&self) -> Option<usize> {
None
}
fn is_primitive(&self) -> bool {
false
}
fn to_primitive(&self) -> Val {
Val::String(Rc::new("function Boolean() { [native code] }".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::NativeFunction(to_boolean)
}
fn sub(&self, _key: Val) -> Val {
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[Boolean]\x1b[39m")
}
fn codify(&self) -> String {
"Boolean".into()
}
}
fn to_boolean(_: &mut Val, params: Vec<Val>) -> Val {
if let Some(value) = params.get(0) {
Val::Bool(value.is_truthy())
} else {
Val::Bool(false)
}
}

View File

@@ -1,3 +1,4 @@
mod boolean_builtin;
mod debug_builtin;
mod math_builtin;
mod number_builtin;
@@ -12,4 +13,5 @@ pub static BUILTIN_VALS: [&'static (dyn ValTrait + Sync); BUILTIN_COUNT] = [
&math_builtin::MATH_BUILTIN,
&string_builtin::STRING_BUILTIN,
&number_builtin::NUMBER_BUILTIN,
&boolean_builtin::BOOLEAN_BUILTIN,
];

View File

@@ -209,7 +209,7 @@ impl ValTrait for Val {
Undefined => false,
Null => false,
Bool(b) => *b,
Number(x) => *x != 0_f64,
Number(x) => *x != 0_f64 && !x.is_nan(),
String(s) => s.len() > 0,
Array(_) => true,
Object(_) => true,