From cc6f7f4eb685af70f09385cf046aa0b6de189663 Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Sat, 21 May 2022 18:09:53 +1000 Subject: [PATCH] Alphabetize array methods --- src/vstc/virtual_machine/vs_array.rs | 114 +++++++++++++-------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/src/vstc/virtual_machine/vs_array.rs b/src/vstc/virtual_machine/vs_array.rs index 4ff5f12..cd512bb 100644 --- a/src/vstc/virtual_machine/vs_array.rs +++ b/src/vstc/virtual_machine/vs_array.rs @@ -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 { + 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 { + 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 { match this { @@ -313,6 +349,23 @@ static PUSH: NativeFunction = NativeFunction { } }; +static SHIFT: NativeFunction = NativeFunction { + fn_: |this: &mut Val, _params: Vec| -> 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 { match this { @@ -332,56 +385,3 @@ static UNSHIFT: NativeFunction = NativeFunction { }; } }; - -static POP: NativeFunction = NativeFunction { - fn_: |this: &mut Val, _params: Vec| -> 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 { - 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 { - 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") - }; - } -};