Skip to content

Commit 2e188a2

Browse files
🧹 Refactor register_sync in stdlib/src/sync.rs into modular helper functions
Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
1 parent dddc025 commit 2e188a2

1 file changed

Lines changed: 64 additions & 63 deletions

File tree

‎stdlib/src/sync.rs‎

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -34,74 +34,75 @@ impl ScriptMutex {
3434
}
3535
}
3636

37+
fn make_mutex_fn() -> Rc<dyn techscript_runtime::function::Callable> {
38+
Rc::new(StdFunction {
39+
name: "make_mutex".to_string(),
40+
arity: 0,
41+
callback: |ctx, _args| {
42+
let mutex = ScriptMutex::new();
43+
let handle_id = ctx.resources.borrow_mut().insert(mutex);
44+
let mut map = IndexMap::new();
45+
map.insert("_handle".to_string(), RuntimeValue::Int(handle_id as i64));
46+
Ok(RuntimeValue::Map {
47+
entries: Rc::new(RefCell::new(map)),
48+
is_const: false,
49+
})
50+
},
51+
})
52+
}
53+
54+
fn mutex_lock_fn() -> Rc<dyn techscript_runtime::function::Callable> {
55+
Rc::new(StdFunction {
56+
name: "mutex_lock".to_string(),
57+
arity: 1,
58+
callback: |ctx, args| {
59+
if let RuntimeValue::Map { entries, .. } = &args[0] {
60+
let handle_id = entries
61+
.borrow()
62+
.get("_handle")
63+
.cloned()
64+
.unwrap_or(RuntimeValue::Null)
65+
.try_into_int()? as u32;
66+
let resources = ctx.resources.borrow();
67+
if let Some(mutex) = resources.get::<ScriptMutex>(handle_id) {
68+
mutex.lock();
69+
}
70+
}
71+
Ok(RuntimeValue::Null)
72+
},
73+
})
74+
}
75+
76+
fn mutex_unlock_fn() -> Rc<dyn techscript_runtime::function::Callable> {
77+
Rc::new(StdFunction {
78+
name: "mutex_unlock".to_string(),
79+
arity: 1,
80+
callback: |ctx, args| {
81+
if let RuntimeValue::Map { entries, .. } = &args[0] {
82+
let handle_id = entries
83+
.borrow()
84+
.get("_handle")
85+
.cloned()
86+
.unwrap_or(RuntimeValue::Null)
87+
.try_into_int()? as u32;
88+
let resources = ctx.resources.borrow();
89+
if let Some(mutex) = resources.get::<ScriptMutex>(handle_id) {
90+
mutex.unlock();
91+
}
92+
}
93+
Ok(RuntimeValue::Null)
94+
},
95+
})
96+
}
97+
3798
impl StdlibRegistry {
3899
pub fn register_sync(&mut self) {
39100
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
40101
HashMap::new();
41102

42-
exports.insert(
43-
"make_mutex".to_string(),
44-
Rc::new(StdFunction {
45-
name: "make_mutex".to_string(),
46-
arity: 0,
47-
callback: |ctx, _args| {
48-
let mutex = ScriptMutex::new();
49-
let handle_id = ctx.resources.borrow_mut().insert(mutex);
50-
let mut map = IndexMap::new();
51-
map.insert("_handle".to_string(), RuntimeValue::Int(handle_id as i64));
52-
Ok(RuntimeValue::Map {
53-
entries: Rc::new(RefCell::new(map)),
54-
is_const: false,
55-
})
56-
},
57-
}),
58-
);
59-
60-
exports.insert(
61-
"mutex_lock".to_string(),
62-
Rc::new(StdFunction {
63-
name: "mutex_lock".to_string(),
64-
arity: 1,
65-
callback: |ctx, args| {
66-
if let RuntimeValue::Map { entries, .. } = &args[0] {
67-
let handle_id = entries
68-
.borrow()
69-
.get("_handle")
70-
.cloned()
71-
.unwrap_or(RuntimeValue::Null)
72-
.try_into_int()? as u32;
73-
let resources = ctx.resources.borrow();
74-
if let Some(mutex) = resources.get::<ScriptMutex>(handle_id) {
75-
mutex.lock();
76-
}
77-
}
78-
Ok(RuntimeValue::Null)
79-
},
80-
}),
81-
);
82-
83-
exports.insert(
84-
"mutex_unlock".to_string(),
85-
Rc::new(StdFunction {
86-
name: "mutex_unlock".to_string(),
87-
arity: 1,
88-
callback: |ctx, args| {
89-
if let RuntimeValue::Map { entries, .. } = &args[0] {
90-
let handle_id = entries
91-
.borrow()
92-
.get("_handle")
93-
.cloned()
94-
.unwrap_or(RuntimeValue::Null)
95-
.try_into_int()? as u32;
96-
let resources = ctx.resources.borrow();
97-
if let Some(mutex) = resources.get::<ScriptMutex>(handle_id) {
98-
mutex.unlock();
99-
}
100-
}
101-
Ok(RuntimeValue::Null)
102-
},
103-
}),
104-
);
103+
exports.insert("make_mutex".to_string(), make_mutex_fn());
104+
exports.insert("mutex_lock".to_string(), mutex_lock_fn());
105+
exports.insert("mutex_unlock".to_string(), mutex_unlock_fn());
105106

106107
self.register_module(
107108
"std.sync",

0 commit comments

Comments
 (0)