MCPcopy Create free account
hub / github.com/ad-si/LuaCAD / execute_lua

Function execute_lua

crates/luacad/src/lua_engine.rs:201–1434  ·  view source on GitHub ↗

Execute LuaCAD code and return the resulting geometries. Returns `Ok(geometries)` on success, or `Err(message)` on failure.

(code: &str)

Source from the content-addressed store, hash-verified

199 }
200
201 // cube(size) — uniform
202 Ok((s, s, s, false))
203}
204
205// ---------------------------------------------------------------------------
206// sphere() argument parsing
207// ---------------------------------------------------------------------------
208
209/// Returns (radius, segments).
210fn parse_sphere_args(args: &mlua::MultiValue) -> mlua::Result<(f32, u32)> {
211 if args.is_empty() {
212 return Err(mlua::Error::RuntimeError(
213 "sphere() requires at least 1 argument".to_string(),
214 ));
215 }
216
217 let first = &args[0];
218
219 if let LuaValue::Table(t) = first {
220 check_table_keys(t, "sphere", &["r", "d", "segments", "fn"])?;
221
222 let radius = if let Some(r) = table_get_f32(t, "r") {
223 r
224 } else if let Some(d) = table_get_f32(t, "d") {
225 d / 2.0
226 } else {
227 t.get::<f32>(1).unwrap_or(1.0)
228 };
229 let segments = table_segments(t, DEFAULT_SEGMENTS);
230 return Ok((radius, segments));
231 }
232
233 let radius = lua_val_to_f32(first).ok_or_else(|| {
234 mlua::Error::RuntimeError(
235 "sphere() argument must be a number or {r=..} table".to_string(),
236 )
237 })?;
238 let segments = args
239 .get(1)
240 .and_then(lua_val_to_f32)
241 .map(|v| v as u32)
242 .unwrap_or(DEFAULT_SEGMENTS);
243 Ok((radius, segments))
244}
245
246// ---------------------------------------------------------------------------
247// cylinder() argument parsing
248// ---------------------------------------------------------------------------
249
250/// Returns (r1, r2, height, segments, center).
251fn parse_cylinder_args(
252 args: &mlua::MultiValue,
253) -> mlua::Result<(f32, f32, f32, u32, bool)> {
254 if args.is_empty() {
255 return Err(mlua::Error::RuntimeError(
256 "cylinder() requires at least 1 argument".to_string(),
257 ));
258 }

Callers 15

execute_lua_codeMethod · 0.85
do_convertFunction · 0.85
cmd_infoFunction · 0.85
cmd_runFunction · 0.85
cmd_renderFunction · 0.85
run_lua_scadFunction · 0.85
cube_requires_argumentFunction · 0.85
sphere_requires_argumentFunction · 0.85
sign_positiveFunction · 0.85
sign_zeroFunction · 0.85
sign_negativeFunction · 0.85

Calls 12

parse_cube_argsFunction · 0.85
parse_sphere_argsFunction · 0.85
parse_cylinder_argsFunction · 0.85
lua_val_to_f32Function · 0.85
table_segmentsFunction · 0.85
register_vector_typeFunction · 0.85
register_boslFunction · 0.85
is_emptyMethod · 0.80
table_get_f32Function · 0.70
table_get_u32Function · 0.70
table_get_boolFunction · 0.70
execMethod · 0.45

Tested by 15

cube_requires_argumentFunction · 0.68
sphere_requires_argumentFunction · 0.68
sign_positiveFunction · 0.68
sign_zeroFunction · 0.68
sign_negativeFunction · 0.68
sign_in_cad_modelFunction · 0.68
math_globals_trigFunction · 0.68
math_globals_piFunction · 0.68
round_functionFunction · 0.68
type_checking_functionsFunction · 0.68
vector_cross_productFunction · 0.68