use std::sync::Arc; use oxide_kernel::bind_methods; use oxide_kernel::kernel::{KernelCore, KernelSession}; use oxide_types::object::{JsObject, PropAttributes}; use oxide_types::value::JsValue; /// 把 Math 单例对象及其方法(abs/floor/max/random 等)绑定到 global。 pub fn bind_math(core: &Arc, session: &KernelSession, global: &mut JsObject) { let math_ptr = session.builtin_world().math_object.as_ptr() as *mut JsObject; let math = unsafe { &mut *math_ptr }; let bw = session.builtin_world(); let sf = core.perm_interner().as_ref(); let sh = core.shape_forge().as_ref(); bind_methods!( bw, math, sf, sh, ("abs", oxide_builtins::math::math_abs::, 1), ("acos", oxide_builtins::math::math_acos::, 1), ("asin", oxide_builtins::math::math_asin::, 1), ("atan", oxide_builtins::math::math_atan::, 1), ("atan2", oxide_builtins::math::math_atan2::, 2), ("cbrt", oxide_builtins::math::math_cbrt::, 1), ("ceil", oxide_builtins::math::math_ceil::, 1), ("cos", oxide_builtins::math::math_cos::, 1), ("cosh", oxide_builtins::math::math_cosh::, 1), ("exp", oxide_builtins::math::math_exp::, 1), ("floor", oxide_builtins::math::math_floor::, 1), ("hypot", oxide_builtins::math::math_hypot::, 2), ("imul", oxide_builtins::math::math_imul::, 2), ("log", oxide_builtins::math::math_log::, 1), ("log10", oxide_builtins::math::math_log10::, 1), ("log2", oxide_builtins::math::math_log2::, 1), ("max", oxide_builtins::math::math_max::, 2), ("min", oxide_builtins::math::math_min::, 2), ("pow", oxide_builtins::math::math_pow::, 2), ("random", oxide_builtins::math::math_random::, 0), ("round", oxide_builtins::math::math_round::, 1), ("sign", oxide_builtins::math::math_sign::, 1), ("sin", oxide_builtins::math::math_sin::, 1), ("sinh", oxide_builtins::math::math_sinh::, 1), ("sqrt", oxide_builtins::math::math_sqrt::, 1), ("tan", oxide_builtins::math::math_tan::, 1), ("tanh", oxide_builtins::math::math_tanh::, 1), ("trunc", oxide_builtins::math::math_trunc::, 1), ("acosh", oxide_builtins::math::math_acosh::, 1), ("asinh", oxide_builtins::math::math_asinh::, 1), ("atanh", oxide_builtins::math::math_atanh::, 1), ("clz32", oxide_builtins::math::math_clz32::, 1), ("expm1", oxide_builtins::math::math_expm1::, 1), ("fround", oxide_builtins::math::math_fround::, 1), ("log1p", oxide_builtins::math::math_log1p::, 1), ); for (name, val) in [ ("PI", std::f64::consts::PI), ("E", std::f64::consts::E), ("LN10", std::f64::consts::LN_10), ("LN2", std::f64::consts::LN_2), ("LOG10E", std::f64::consts::LOG10_E), ("LOG2E", std::f64::consts::LOG2_E), ("SQRT1_2", std::f64::consts::FRAC_1_SQRT_2), ("SQRT2", std::f64::consts::SQRT_2), ] { let si = core.perm_interner().as_ref().intern(name).0; let sh_c = core.shape_forge().as_ref().make_shape(math.shape_id(), si); math.set_shape_id(sh_c); math.ensure_hash_props().push(JsValue::float(val)); // Math 常量描述符:{ writable:false, enumerable:false, configurable:false }, // 保证 Object.keys(Math) 为空(与 Number 常量同款收口)。 let pos = math.prop_vec_len().saturating_sub(1) as u32; math.set_data_meta(pos, PropAttributes::new(false, false, false)); } let si_m = core.perm_interner().intern("Math").0; let m_shape = core.shape_forge().make_shape(global.shape_id(), si_m); let m_val = JsValue::from_js_object(session.builtin_world().math_object.as_ptr() as *mut JsObject); global.set_shape_id(m_shape); global.ensure_hash_props().push(m_val); // 全局 Math 槽位非枚举(规范全局命名空间对象属性均 enumerable:false)。 let pos = global.prop_vec_len().saturating_sub(1) as u32; global.set_data_meta(pos, PropAttributes::new(true, false, true)); global.bump_generation(); }