use std::os::raw::{c_double, c_char, c_ulong};
use std::ffi::CStr;
#[no_mangle]
pub extern "C" fn js_math_sin(x: c_double) -> c_double {
x.sin()
}
#[no_mangle]
pub extern "C" fn js_math_cos(x: c_double) -> c_double {
x.cos()
}
#[no_mangle]
pub extern "C" fn js_math_tan(x: c_double) -> c_double {
x.tan()
}
#[no_mangle]
pub extern "C" fn js_math_sqrt(x: c_double) -> c_double {
if x < 0.0 { 0.0 } else { x.sqrt() }
}
#[no_mangle]
pub extern "C" fn js_math_pow(base: c_double, exp: c_double) -> c_double {
base.powf(exp)
}
#[no_mangle]
pub extern "C" fn js_math_abs(x: c_double) -> c_double {
x.abs()
}
#[no_mangle]
pub extern "C" fn js_math_floor(x: c_double) -> c_double {
x.floor()
}
#[no_mangle]
pub extern "C" fn js_math_ceil(x: c_double) -> c_double {
x.ceil()
}
#[no_mangle]
pub extern "C" fn js_math_round(x: c_double) -> c_double {
x.round()
}
#[no_mangle]
pub extern "C" fn js_math_min(a: c_double, b: c_double) -> c_double {
a.min(b)
}
#[no_mangle]
pub extern "C" fn js_math_max(a: c_double, b: c_double) -> c_double {
a.max(b)
}
#[no_mangle]
pub extern "C" fn js_parse_int(str_ptr: *const c_char) -> c_double {
if str_ptr.is_null() {
return 0.0;
}
let c_str = unsafe { CStr::from_ptr(str_ptr) };
if let Ok(s) = c_str.to_str() {
s.trim().parse::<i64>().unwrap_or(0) as c_double
} else {
0.0
}
}
#[no_mangle]
pub extern "C" fn js_parse_float(str_ptr: *const c_char) -> c_double {
if str_ptr.is_null() {
return 0.0;
}
let c_str = unsafe { CStr::from_ptr(str_ptr) };
if let Ok(s) = c_str.to_str() {
s.trim().parse::<f64>().unwrap_or(0.0)
} else {
0.0
}
}
#[no_mangle]
pub extern "C" fn js_console_log(value: c_ulong) {
println!("[log] {:?}", value);
}
#[no_mangle]
pub extern "C" fn js_console_error(value: c_ulong) {
eprintln!("[error] {:?}", value);
}