Implement .push (in theory 😅)

This commit is contained in:
Andrew Morris
2022-05-20 11:54:43 +10:00
parent 0a0c3e98d3
commit b756605d5d
3 changed files with 22 additions and 2 deletions

View File

@@ -10,7 +10,7 @@ use super::vs_object::VsObject;
use super::vs_array::VsArray;
pub struct NativeFunction {
fn_: fn(this: &mut Val, params: Vec<Val>) -> Val,
pub fn_: fn(this: &mut Val, params: Vec<Val>) -> Val,
}
impl ValTrait for NativeFunction {

View File

@@ -7,6 +7,7 @@ use super::vs_value::{
LoadFunctionResult,
};
use super::vs_object::VsObject;
use super::native_function::NativeFunction;
#[derive(Clone)]
pub struct VsArray {
@@ -51,11 +52,29 @@ impl ValTrait for ArrayPrototype {
fn sub(&self, key: Val) -> Val {
match key.val_to_string().as_str() {
"push" => Val::Static(&PUSH),
_ => Val::Undefined,
}
}
fn submov(&mut self, key: Val, value: Val) {
fn submov(&mut self, _key: Val, _value: Val) {
std::panic!("Not implemented: exceptions");
}
}
static PUSH: NativeFunction = NativeFunction {
fn_: |this: &mut Val, params: Vec<Val>| -> Val {
match this {
Val::Array(array_data) => {
let array_data_mut = Rc::make_mut(array_data);
for p in params {
array_data_mut.elements.push(p);
}
return Val::Number(array_data_mut.elements.len() as f64);
},
_ => std::panic!("Not implemented: exceptions/array indirection")
};
}
};

View File

@@ -257,6 +257,7 @@ impl ValTrait for Val {
return match self {
Function(f) => LoadFunctionResult::StackFrame(f.make_frame()),
Static(s) => s.load_function(),
Custom(val) => val.load_function(),
_ => LoadFunctionResult::NotAFunction,