mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Implement .push (in theory 😅)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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")
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user