mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Boolean builtin
This commit is contained in:
18
inputs/passing/boolean.ts
Normal file
18
inputs/passing/boolean.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
// test_output! [true,false,false,true,true,true,true,false,true,false,false,false]
|
||||
|
||||
export default function () {
|
||||
return [
|
||||
true,
|
||||
false,
|
||||
"",
|
||||
"0",
|
||||
"1",
|
||||
{},
|
||||
[],
|
||||
0,
|
||||
1,
|
||||
null,
|
||||
undefined,
|
||||
0 / 0,
|
||||
].map(Boolean);
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
use strum::EnumCount;
|
||||
use strum_macros::{EnumCount, EnumString};
|
||||
|
||||
#[derive(strum_macros::EnumString, strum_macros::EnumCount, Clone, Copy)]
|
||||
#[derive(EnumString, EnumCount, Clone, Copy)]
|
||||
pub enum BuiltinName {
|
||||
Debug,
|
||||
Math,
|
||||
String,
|
||||
Number,
|
||||
Boolean,
|
||||
}
|
||||
|
||||
pub const BUILTIN_NAMES: [&str; BuiltinName::COUNT] = ["Debug", "Math", "String", "Number"];
|
||||
pub const BUILTIN_NAMES: [&str; BuiltinName::COUNT] =
|
||||
["Debug", "Math", "String", "Number", "Boolean"];
|
||||
|
||||
pub const BUILTIN_COUNT: usize = BuiltinName::COUNT;
|
||||
|
||||
|
||||
80
valuescript_vm/src/builtins/boolean_builtin.rs
Normal file
80
valuescript_vm/src/builtins/boolean_builtin.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
];
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user