From 75370239939df8be9f97c2293cd84c17b51bb2da Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 27 Oct 2026 18:13:38 +0000 Subject: [PATCH] Refactor register_math by extracting callback functions Extract inline math function closures into top-level standalone callbacks to reduce register_math function length and improve readability and maintainability. Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- stdlib/src/math.rs | 192 ++++++++++++++++++++++++++------------------- 1 file changed, 113 insertions(+), 79 deletions(-) diff --git a/stdlib/src/math.rs b/stdlib/src/math.rs index 27bdd3ff..d28de753 100644 --- a/stdlib/src/math.rs +++ b/stdlib/src/math.rs @@ -7,6 +7,106 @@ use techscript_runtime::{ RuntimeContext, }; +fn abs(_ctx: &mut RuntimeContext, args: Vec) -> Result { + match &args[0] { + RuntimeValue::Int(i) => Ok(RuntimeValue::Int(i.abs())), + RuntimeValue::Float(f) => Ok(RuntimeValue::Float(f.abs())), + other => Err(RuntimeError::new( + RuntimeErrorKind::TypeMismatch { + expected: "Int or Float".to_string(), + found: other.runtime_type().to_string(), + }, + None, + None, + )), + } +} + +fn sin(_ctx: &mut RuntimeContext, args: Vec) -> Result { + let f = args[0].try_into_float()?; + Ok(RuntimeValue::Float(f.sin())) +} + +fn cos(_ctx: &mut RuntimeContext, args: Vec) -> Result { + let f = args[0].try_into_float()?; + Ok(RuntimeValue::Float(f.cos())) +} + +fn tan(_ctx: &mut RuntimeContext, args: Vec) -> Result { + let f = args[0].try_into_float()?; + Ok(RuntimeValue::Float(f.tan())) +} + +fn log(_ctx: &mut RuntimeContext, args: Vec) -> Result { + let f = args[0].try_into_float()?; + Ok(RuntimeValue::Float(f.ln())) +} + +fn exp(_ctx: &mut RuntimeContext, args: Vec) -> Result { + let f = args[0].try_into_float()?; + Ok(RuntimeValue::Float(f.exp())) +} + +fn sqrt(_ctx: &mut RuntimeContext, args: Vec) -> Result { + let f = args[0].try_into_float()?; + Ok(RuntimeValue::Float(f.sqrt())) +} + +fn pow(_ctx: &mut RuntimeContext, args: Vec) -> Result { + let base = args[0].try_into_float()?; + let exponent = args[1].try_into_float()?; + Ok(RuntimeValue::Float(base.powf(exponent))) +} + +fn floor(_ctx: &mut RuntimeContext, args: Vec) -> Result { + let f = args[0].try_into_float()?; + Ok(RuntimeValue::Float(f.floor())) +} + +fn ceil(_ctx: &mut RuntimeContext, args: Vec) -> Result { + let f = args[0].try_into_float()?; + Ok(RuntimeValue::Float(f.ceil())) +} + +fn round(_ctx: &mut RuntimeContext, args: Vec) -> Result { + let f = args[0].try_into_float()?; + Ok(RuntimeValue::Float(f.round())) +} + +fn random( + _ctx: &mut RuntimeContext, + _args: Vec, +) -> Result { + use std::cell::Cell; + use std::time::SystemTime; + thread_local! { + static SEED: Cell = Cell::new({ + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap_or_default() + .as_nanos() as u64 + }); + } + let next_seed = SEED.with(|cell| { + let current = cell.get(); + let next = current + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + cell.set(next); + next + }); + let rand_float = (next_seed as f64) / (u64::MAX as f64); + Ok(RuntimeValue::Float(rand_float)) +} + +fn to_float( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { + let f = args[0].try_into_float()?; + Ok(RuntimeValue::Float(f)) +} + impl StdlibRegistry { pub fn register_math(&mut self) { let mut exports: HashMap> = @@ -17,18 +117,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "abs".to_string(), arity: 1, - callback: |_ctx, args| match &args[0] { - RuntimeValue::Int(i) => Ok(RuntimeValue::Int(i.abs())), - RuntimeValue::Float(f) => Ok(RuntimeValue::Float(f.abs())), - other => Err(RuntimeError::new( - RuntimeErrorKind::TypeMismatch { - expected: "Int or Float".to_string(), - found: other.runtime_type().to_string(), - }, - None, - None, - )), - }, + callback: abs, }), ); @@ -37,10 +126,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "sin".to_string(), arity: 1, - callback: |_ctx, args| { - let f = args[0].try_into_float()?; - Ok(RuntimeValue::Float(f.sin())) - }, + callback: sin, }), ); @@ -49,10 +135,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "cos".to_string(), arity: 1, - callback: |_ctx, args| { - let f = args[0].try_into_float()?; - Ok(RuntimeValue::Float(f.cos())) - }, + callback: cos, }), ); @@ -61,10 +144,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "tan".to_string(), arity: 1, - callback: |_ctx, args| { - let f = args[0].try_into_float()?; - Ok(RuntimeValue::Float(f.tan())) - }, + callback: tan, }), ); @@ -73,10 +153,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "log".to_string(), arity: 1, - callback: |_ctx, args| { - let f = args[0].try_into_float()?; - Ok(RuntimeValue::Float(f.ln())) - }, + callback: log, }), ); @@ -85,10 +162,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "exp".to_string(), arity: 1, - callback: |_ctx, args| { - let f = args[0].try_into_float()?; - Ok(RuntimeValue::Float(f.exp())) - }, + callback: exp, }), ); @@ -97,10 +171,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "sqrt".to_string(), arity: 1, - callback: |_ctx, args| { - let f = args[0].try_into_float()?; - Ok(RuntimeValue::Float(f.sqrt())) - }, + callback: sqrt, }), ); @@ -109,11 +180,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "pow".to_string(), arity: 2, - callback: |_ctx, args| { - let base = args[0].try_into_float()?; - let exponent = args[1].try_into_float()?; - Ok(RuntimeValue::Float(base.powf(exponent))) - }, + callback: pow, }), ); @@ -122,10 +189,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "floor".to_string(), arity: 1, - callback: |_ctx, args| { - let f = args[0].try_into_float()?; - Ok(RuntimeValue::Float(f.floor())) - }, + callback: floor, }), ); @@ -134,10 +198,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "ceil".to_string(), arity: 1, - callback: |_ctx, args| { - let f = args[0].try_into_float()?; - Ok(RuntimeValue::Float(f.ceil())) - }, + callback: ceil, }), ); @@ -146,10 +207,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "round".to_string(), arity: 1, - callback: |_ctx, args| { - let f = args[0].try_into_float()?; - Ok(RuntimeValue::Float(f.round())) - }, + callback: round, }), ); @@ -158,28 +216,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "random".to_string(), arity: 0, - callback: |_ctx, _args| { - use std::cell::Cell; - use std::time::SystemTime; - thread_local! { - static SEED: Cell = Cell::new({ - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap_or_default() - .as_nanos() as u64 - }); - } - let next_seed = SEED.with(|cell| { - let current = cell.get(); - let next = current - .wrapping_mul(6364136223846793005) - .wrapping_add(1442695040888963407); - cell.set(next); - next - }); - let rand_float = (next_seed as f64) / (u64::MAX as f64); - Ok(RuntimeValue::Float(rand_float)) - }, + callback: random, }), ); @@ -188,10 +225,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "to_float".to_string(), arity: 1, - callback: |_ctx, args| { - let f = args[0].try_into_float()?; - Ok(RuntimeValue::Float(f)) - }, + callback: to_float, }), );