mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-01-12 23:18:03 -05:00
Alphabetize array methods
This commit is contained in:
@@ -58,11 +58,11 @@ impl ValTrait for ArrayPrototype {
|
||||
"copyWithin" => Val::Static(©_WITHIN),
|
||||
"fill" => Val::Static(&FILL),
|
||||
"flat" => Val::Static(&FLAT),
|
||||
"push" => Val::Static(&PUSH),
|
||||
"unshift" => Val::Static(&UNSHIFT),
|
||||
"pop" => Val::Static(&POP),
|
||||
"shift" => Val::Static(&SHIFT),
|
||||
"includes" => Val::Static(&INCLUDES),
|
||||
"pop" => Val::Static(&POP),
|
||||
"push" => Val::Static(&PUSH),
|
||||
"shift" => Val::Static(&SHIFT),
|
||||
"unshift" => Val::Static(&UNSHIFT),
|
||||
_ => Val::Undefined,
|
||||
}
|
||||
}
|
||||
@@ -296,6 +296,42 @@ static FLAT: 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")
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
static POP: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
Val::Array(array_data) => {
|
||||
if array_data.elements.len() == 0 {
|
||||
return Val::Undefined;
|
||||
}
|
||||
|
||||
let array_data_mut = Rc::make_mut(array_data);
|
||||
|
||||
return array_data_mut.elements.remove(array_data_mut.elements.len() - 1);
|
||||
},
|
||||
_ => std::panic!("Not implemented: exceptions/array indirection")
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
static PUSH: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
@@ -313,6 +349,23 @@ static PUSH: NativeFunction = NativeFunction {
|
||||
}
|
||||
};
|
||||
|
||||
static SHIFT: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
Val::Array(array_data) => {
|
||||
if array_data.elements.len() == 0 {
|
||||
return Val::Undefined;
|
||||
}
|
||||
|
||||
let array_data_mut = Rc::make_mut(array_data);
|
||||
|
||||
return array_data_mut.elements.remove(0);
|
||||
},
|
||||
_ => std::panic!("Not implemented: exceptions/array indirection")
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
static UNSHIFT: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
@@ -332,56 +385,3 @@ static UNSHIFT: NativeFunction = NativeFunction {
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
static POP: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
Val::Array(array_data) => {
|
||||
if array_data.elements.len() == 0 {
|
||||
return Val::Undefined;
|
||||
}
|
||||
|
||||
let array_data_mut = Rc::make_mut(array_data);
|
||||
|
||||
return array_data_mut.elements.remove(array_data_mut.elements.len() - 1);
|
||||
},
|
||||
_ => std::panic!("Not implemented: exceptions/array indirection")
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
static SHIFT: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
Val::Array(array_data) => {
|
||||
if array_data.elements.len() == 0 {
|
||||
return Val::Undefined;
|
||||
}
|
||||
|
||||
let array_data_mut = Rc::make_mut(array_data);
|
||||
|
||||
return array_data_mut.elements.remove(0);
|
||||
},
|
||||
_ => std::panic!("Not implemented: exceptions/array indirection")
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
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