mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Array.prototype.includes
This commit is contained in:
@@ -59,27 +59,27 @@ pub fn op_ne(left: Val, right: Val) -> Val {
|
||||
return Val::Bool(left.to_number() != right.to_number());
|
||||
}
|
||||
|
||||
pub fn op_triple_eq(left: Val, right: Val) -> Val {
|
||||
if left.typeof_() != VsType::Number || right.typeof_() != VsType::Number {
|
||||
std::panic!("Not implemented");
|
||||
}
|
||||
|
||||
return Val::Bool(left.to_number() == right.to_number());
|
||||
}
|
||||
|
||||
pub fn op_triple_ne(left: Val, right: Val) -> Val {
|
||||
pub fn op_triple_eq_impl(left: Val, right: Val) -> bool {
|
||||
let type_ = left.typeof_();
|
||||
|
||||
if right.typeof_() != type_ {
|
||||
return Val::Bool(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
return Val::Bool(match type_ {
|
||||
VsType::Undefined => false,
|
||||
VsType::Null => false,
|
||||
VsType::Number => left.to_number() != right.to_number(),
|
||||
return match type_ {
|
||||
VsType::Undefined => true,
|
||||
VsType::Null => true,
|
||||
VsType::Number => left.to_number() == right.to_number(),
|
||||
_ => std::panic!("Not implemented"),
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
pub fn op_triple_eq(left: Val, right: Val) -> Val {
|
||||
return Val::Bool(op_triple_eq_impl(left, right));
|
||||
}
|
||||
|
||||
pub fn op_triple_ne(left: Val, right: Val) -> Val {
|
||||
return Val::Bool(!op_triple_eq_impl(left, right));
|
||||
}
|
||||
|
||||
pub fn op_and(left: Val, right: Val) -> Val {
|
||||
|
||||
@@ -8,6 +8,7 @@ use super::vs_value::{
|
||||
};
|
||||
use super::vs_object::VsObject;
|
||||
use super::native_function::NativeFunction;
|
||||
use super::operations::op_triple_eq_impl;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct VsArray {
|
||||
@@ -53,6 +54,7 @@ impl ValTrait for ArrayPrototype {
|
||||
fn sub(&self, key: Val) -> Val {
|
||||
match key.val_to_string().as_str() {
|
||||
"push" => Val::Static(&PUSH),
|
||||
"includes" => Val::Static(&INCLUDES),
|
||||
_ => Val::Undefined,
|
||||
}
|
||||
}
|
||||
@@ -82,3 +84,22 @@ static PUSH: NativeFunction = NativeFunction {
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
static INCLUDES: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
Val::Array(array_data) => {
|
||||
let search_param = params.get(0).unwrap_or(&Val::Undefined).clone();
|
||||
|
||||
for elem in &array_data.elements {
|
||||
if op_triple_eq_impl(elem.clone(), search_param.clone()) {
|
||||
return Val::Bool(true);
|
||||
}
|
||||
}
|
||||
|
||||
return Val::Bool(false);
|
||||
},
|
||||
_ => std::panic!("Not implemented: exceptions/array indirection")
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user