mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Add stubs for math functions
This commit is contained in:
@@ -3,6 +3,7 @@ use std::rc::Rc;
|
||||
use super::vs_value::{Val, VsType, ValTrait, LoadFunctionResult};
|
||||
use super::vs_array::VsArray;
|
||||
use super::vs_object::VsObject;
|
||||
use super::native_function::NativeFunction;
|
||||
|
||||
pub struct Math {}
|
||||
|
||||
@@ -37,6 +38,45 @@ impl ValTrait for Math {
|
||||
"PI" => Val::Number(std::f64::consts::PI),
|
||||
"SQRT1_2" => Val::Number(std::f64::consts::FRAC_1_SQRT_2),
|
||||
"SQRT2" => Val::Number(std::f64::consts::SQRT_2),
|
||||
"abs" => Val::Static(&ABS),
|
||||
|
||||
"acos" => Val::Static(&ACOS),
|
||||
"acosh" => Val::Static(&ACOSH),
|
||||
"asin" => Val::Static(&ASIN),
|
||||
"asinh" => Val::Static(&ASINH),
|
||||
"atan" => Val::Static(&ATAN),
|
||||
"atan2" => Val::Static(&ATAN2),
|
||||
"atanh" => Val::Static(&ATANH),
|
||||
"cbrt" => Val::Static(&CBRT),
|
||||
"ceil" => Val::Static(&CEIL),
|
||||
"clz32" => Val::Static(&CLZ32),
|
||||
"cos" => Val::Static(&COS),
|
||||
"cosh" => Val::Static(&COSH),
|
||||
"exp" => Val::Static(&EXP),
|
||||
"expm1" => Val::Static(&EXPM1),
|
||||
"floor" => Val::Static(&FLOOR),
|
||||
"fround" => Val::Static(&FROUND),
|
||||
"hypot" => Val::Static(&HYPOT),
|
||||
"imul" => Val::Static(&IMUL),
|
||||
"log" => Val::Static(&LOG),
|
||||
"log10" => Val::Static(&LOG10),
|
||||
"log1p" => Val::Static(&LOG1P),
|
||||
"log2" => Val::Static(&LOG2),
|
||||
"max" => Val::Static(&MAX),
|
||||
"min" => Val::Static(&MIN),
|
||||
"pow" => Val::Static(&POW),
|
||||
|
||||
// random: Not included because it cannot work as expected in ValueScript
|
||||
|
||||
"round" => Val::Static(&ROUND),
|
||||
"sign" => Val::Static(&SIGN),
|
||||
"sin" => Val::Static(&SIN),
|
||||
"sinh" => Val::Static(&SINH),
|
||||
"sqrt" => Val::Static(&SQRT),
|
||||
"tan" => Val::Static(&TAN),
|
||||
"tanh" => Val::Static(&TANH),
|
||||
"trunc" => Val::Static(&TRUNC),
|
||||
|
||||
_ => Val::Undefined,
|
||||
}
|
||||
}
|
||||
@@ -49,3 +89,217 @@ impl ValTrait for Math {
|
||||
write!(f, "\x1b[36m[Math]\x1b[39m")
|
||||
}
|
||||
}
|
||||
|
||||
fn param_to_number(param: Option<&Val>) -> f64 {
|
||||
match param {
|
||||
None => f64::NAN,
|
||||
Some(p) => p.to_number(),
|
||||
}
|
||||
}
|
||||
|
||||
static ABS: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, params: Vec<Val>| -> Val {
|
||||
let x = param_to_number(params.get(0));
|
||||
|
||||
return Val::Number(x.abs());
|
||||
}
|
||||
};
|
||||
|
||||
static ACOS: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: ACOS");
|
||||
}
|
||||
};
|
||||
|
||||
static ACOSH: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: ACOSH");
|
||||
}
|
||||
};
|
||||
|
||||
static ASIN: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: ASIN");
|
||||
}
|
||||
};
|
||||
|
||||
static ASINH: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: ASINH");
|
||||
}
|
||||
};
|
||||
|
||||
static ATAN: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: ATAN");
|
||||
}
|
||||
};
|
||||
|
||||
static ATAN2: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: ATAN2");
|
||||
}
|
||||
};
|
||||
|
||||
static ATANH: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: ATANH");
|
||||
}
|
||||
};
|
||||
|
||||
static CBRT: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: CBRT");
|
||||
}
|
||||
};
|
||||
|
||||
static CEIL: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: CEIL");
|
||||
}
|
||||
};
|
||||
|
||||
static CLZ32: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: CLZ32");
|
||||
}
|
||||
};
|
||||
|
||||
static COS: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: COS");
|
||||
}
|
||||
};
|
||||
|
||||
static COSH: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: COSH");
|
||||
}
|
||||
};
|
||||
|
||||
static EXP: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: EXP");
|
||||
}
|
||||
};
|
||||
|
||||
static EXPM1: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: EXPM1");
|
||||
}
|
||||
};
|
||||
|
||||
static FLOOR: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: FLOOR");
|
||||
}
|
||||
};
|
||||
|
||||
static FROUND: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: FROUND");
|
||||
}
|
||||
};
|
||||
|
||||
static HYPOT: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: HYPOT");
|
||||
}
|
||||
};
|
||||
|
||||
static IMUL: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: IMUL");
|
||||
}
|
||||
};
|
||||
|
||||
static LOG: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: LOG");
|
||||
}
|
||||
};
|
||||
|
||||
static LOG10: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: LOG10");
|
||||
}
|
||||
};
|
||||
|
||||
static LOG1P: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: LOG1P");
|
||||
}
|
||||
};
|
||||
|
||||
static LOG2: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: LOG2");
|
||||
}
|
||||
};
|
||||
|
||||
static MAX: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: MAX");
|
||||
}
|
||||
};
|
||||
|
||||
static MIN: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: MIN");
|
||||
}
|
||||
};
|
||||
|
||||
static POW: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: POW");
|
||||
}
|
||||
};
|
||||
|
||||
static ROUND: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: ROUND");
|
||||
}
|
||||
};
|
||||
|
||||
static SIGN: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: SIGN");
|
||||
}
|
||||
};
|
||||
|
||||
static SIN: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: SIN");
|
||||
}
|
||||
};
|
||||
|
||||
static SINH: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: SINH");
|
||||
}
|
||||
};
|
||||
|
||||
static SQRT: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: SQRT");
|
||||
}
|
||||
};
|
||||
|
||||
static TAN: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: TAN");
|
||||
}
|
||||
};
|
||||
|
||||
static TANH: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: TANH");
|
||||
}
|
||||
};
|
||||
|
||||
static TRUNC: NativeFunction = NativeFunction {
|
||||
fn_: |_this: &mut Val, _params: Vec<Val>| -> Val {
|
||||
std::panic!("Not implemented: TRUNC");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user