From 147f421f9b2c7cbed2510096b8c42b56ff250bfe 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:12:22 +0000 Subject: [PATCH 1/3] Refactor register_sync in stdlib/src/sync.rs Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- stdlib/src/sync.rs | 97 ++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 41 deletions(-) diff --git a/stdlib/src/sync.rs b/stdlib/src/sync.rs index 36ec88f4..58c0aaad 100644 --- a/stdlib/src/sync.rs +++ b/stdlib/src/sync.rs @@ -4,7 +4,7 @@ use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; use std::sync::{Condvar, Mutex}; -use techscript_runtime::{error::RuntimeError, value::RuntimeValue}; +use techscript_runtime::{error::RuntimeError, value::RuntimeValue, RuntimeContext}; pub struct ScriptMutex { locked: Mutex, @@ -34,6 +34,58 @@ impl ScriptMutex { } } +fn make_mutex( + ctx: &mut RuntimeContext, + _args: Vec, +) -> Result { + let mutex = ScriptMutex::new(); + let handle_id = ctx.resources.borrow_mut().insert(mutex); + let mut map = IndexMap::new(); + map.insert("_handle".to_string(), RuntimeValue::Int(handle_id as i64)); + Ok(RuntimeValue::Map { + entries: Rc::new(RefCell::new(map)), + is_const: false, + }) +} + +fn mutex_lock( + ctx: &mut RuntimeContext, + args: Vec, +) -> Result { + if let RuntimeValue::Map { entries, .. } = &args[0] { + let handle_id = entries + .borrow() + .get("_handle") + .cloned() + .unwrap_or(RuntimeValue::Null) + .try_into_int()? as u32; + let resources = ctx.resources.borrow(); + if let Some(mutex) = resources.get::(handle_id) { + mutex.lock(); + } + } + Ok(RuntimeValue::Null) +} + +fn mutex_unlock( + ctx: &mut RuntimeContext, + args: Vec, +) -> Result { + if let RuntimeValue::Map { entries, .. } = &args[0] { + let handle_id = entries + .borrow() + .get("_handle") + .cloned() + .unwrap_or(RuntimeValue::Null) + .try_into_int()? as u32; + let resources = ctx.resources.borrow(); + if let Some(mutex) = resources.get::(handle_id) { + mutex.unlock(); + } + } + Ok(RuntimeValue::Null) +} + impl StdlibRegistry { pub fn register_sync(&mut self) { let mut exports: HashMap> = @@ -44,16 +96,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "make_mutex".to_string(), arity: 0, - callback: |ctx, _args| { - let mutex = ScriptMutex::new(); - let handle_id = ctx.resources.borrow_mut().insert(mutex); - let mut map = IndexMap::new(); - map.insert("_handle".to_string(), RuntimeValue::Int(handle_id as i64)); - Ok(RuntimeValue::Map { - entries: Rc::new(RefCell::new(map)), - is_const: false, - }) - }, + callback: make_mutex, }), ); @@ -62,21 +105,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "mutex_lock".to_string(), arity: 1, - callback: |ctx, args| { - if let RuntimeValue::Map { entries, .. } = &args[0] { - let handle_id = entries - .borrow() - .get("_handle") - .cloned() - .unwrap_or(RuntimeValue::Null) - .try_into_int()? as u32; - let resources = ctx.resources.borrow(); - if let Some(mutex) = resources.get::(handle_id) { - mutex.lock(); - } - } - Ok(RuntimeValue::Null) - }, + callback: mutex_lock, }), ); @@ -85,21 +114,7 @@ impl StdlibRegistry { Rc::new(StdFunction { name: "mutex_unlock".to_string(), arity: 1, - callback: |ctx, args| { - if let RuntimeValue::Map { entries, .. } = &args[0] { - let handle_id = entries - .borrow() - .get("_handle") - .cloned() - .unwrap_or(RuntimeValue::Null) - .try_into_int()? as u32; - let resources = ctx.resources.borrow(); - if let Some(mutex) = resources.get::(handle_id) { - mutex.unlock(); - } - } - Ok(RuntimeValue::Null) - }, + callback: mutex_unlock, }), ); From 61ca2982019735a9e027febc34f62ef517c17c67 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 19:16:29 +0000 Subject: [PATCH 2/3] Refactor register_sync in stdlib/src/sync.rs Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> From 502aea3d6a0d49d0f4ffe2e970b50d41a88720b0 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 19:26:22 +0000 Subject: [PATCH 3/3] Refactor register_sync in stdlib/src/sync.rs Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>