Skip to content

Commit b3aec8b

Browse files
refactor(stdlib): extract inline closures in sys registry exports
Extracted inline closures in `stdlib/src/sys.rs` to separate named functions `sys_read_file`, `sys_write_file`, `sys_exists`, `sys_now`, and `sys_sleep`. This reduces boilerplate inside the module registration and separates the function setup from the capability validation and implementation logic. Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
1 parent 7318bfa commit b3aec8b

1 file changed

Lines changed: 92 additions & 70 deletions

File tree

‎stdlib/src/sys.rs‎

Lines changed: 92 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,97 @@ use std::cell::RefCell;
44
use std::collections::HashMap;
55
use std::rc::Rc;
66
use techscript_runtime::{
7-
context::Capability,
7+
context::{Capability, RuntimeContext},
88
error::{RuntimeError, RuntimeErrorKind},
99
value::RuntimeValue,
1010
};
1111

12+
fn sys_read_file(
13+
ctx: &mut RuntimeContext,
14+
args: Vec<RuntimeValue>,
15+
) -> Result<RuntimeValue, RuntimeError> {
16+
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
17+
return Err(RuntimeError::new(
18+
RuntimeErrorKind::InvalidOperation(
19+
"Security policy violation: FileSystem capability is denied".to_string(),
20+
),
21+
None,
22+
None,
23+
));
24+
}
25+
let path = args[0].try_into_string()?;
26+
let content = std::fs::read_to_string(path).map_err(|e| {
27+
RuntimeError::new(
28+
RuntimeErrorKind::InvalidOperation(format!("IO error: {}", e)),
29+
None,
30+
None,
31+
)
32+
})?;
33+
Ok(RuntimeValue::Str(content))
34+
}
35+
36+
fn sys_write_file(
37+
ctx: &mut RuntimeContext,
38+
args: Vec<RuntimeValue>,
39+
) -> Result<RuntimeValue, RuntimeError> {
40+
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
41+
return Err(RuntimeError::new(
42+
RuntimeErrorKind::InvalidOperation(
43+
"Security policy violation: FileSystem capability is denied".to_string(),
44+
),
45+
None,
46+
None,
47+
));
48+
}
49+
let path = args[0].try_into_string()?;
50+
let content = args[1].try_into_string()?;
51+
std::fs::write(path, content).map_err(|e| {
52+
RuntimeError::new(
53+
RuntimeErrorKind::InvalidOperation(format!("IO error: {}", e)),
54+
None,
55+
None,
56+
)
57+
})?;
58+
Ok(RuntimeValue::Null)
59+
}
60+
61+
fn sys_exists(
62+
ctx: &mut RuntimeContext,
63+
args: Vec<RuntimeValue>,
64+
) -> Result<RuntimeValue, RuntimeError> {
65+
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
66+
return Err(RuntimeError::new(
67+
RuntimeErrorKind::InvalidOperation(
68+
"Security policy violation: FileSystem capability is denied".to_string(),
69+
),
70+
None,
71+
None,
72+
));
73+
}
74+
let path = args[0].try_into_string()?;
75+
Ok(RuntimeValue::Bool(std::path::Path::new(&path).exists()))
76+
}
77+
78+
fn sys_now(
79+
_ctx: &mut RuntimeContext,
80+
_args: Vec<RuntimeValue>,
81+
) -> Result<RuntimeValue, RuntimeError> {
82+
let start = std::time::SystemTime::now();
83+
let since_the_epoch = start
84+
.duration_since(std::time::UNIX_EPOCH)
85+
.unwrap_or_default();
86+
Ok(RuntimeValue::Float(since_the_epoch.as_secs_f64()))
87+
}
88+
89+
fn sys_sleep(
90+
_ctx: &mut RuntimeContext,
91+
args: Vec<RuntimeValue>,
92+
) -> Result<RuntimeValue, RuntimeError> {
93+
let ms = args[0].try_into_int()?;
94+
std::thread::sleep(std::time::Duration::from_millis(ms as u64));
95+
Ok(RuntimeValue::Null)
96+
}
97+
1298
impl StdlibRegistry {
1399
pub fn register_sys(&mut self) {
14100
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
@@ -19,27 +105,7 @@ impl StdlibRegistry {
19105
Rc::new(StdFunction {
20106
name: "read_file".to_string(),
21107
arity: 1,
22-
callback: |ctx, args| {
23-
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
24-
return Err(RuntimeError::new(
25-
RuntimeErrorKind::InvalidOperation(
26-
"Security policy violation: FileSystem capability is denied"
27-
.to_string(),
28-
),
29-
None,
30-
None,
31-
));
32-
}
33-
let path = args[0].try_into_string()?;
34-
let content = std::fs::read_to_string(path).map_err(|e| {
35-
RuntimeError::new(
36-
RuntimeErrorKind::InvalidOperation(format!("IO error: {}", e)),
37-
None,
38-
None,
39-
)
40-
})?;
41-
Ok(RuntimeValue::Str(content))
42-
},
108+
callback: sys_read_file,
43109
}),
44110
);
45111

@@ -48,28 +114,7 @@ impl StdlibRegistry {
48114
Rc::new(StdFunction {
49115
name: "write_file".to_string(),
50116
arity: 2,
51-
callback: |ctx, args| {
52-
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
53-
return Err(RuntimeError::new(
54-
RuntimeErrorKind::InvalidOperation(
55-
"Security policy violation: FileSystem capability is denied"
56-
.to_string(),
57-
),
58-
None,
59-
None,
60-
));
61-
}
62-
let path = args[0].try_into_string()?;
63-
let content = args[1].try_into_string()?;
64-
std::fs::write(path, content).map_err(|e| {
65-
RuntimeError::new(
66-
RuntimeErrorKind::InvalidOperation(format!("IO error: {}", e)),
67-
None,
68-
None,
69-
)
70-
})?;
71-
Ok(RuntimeValue::Null)
72-
},
117+
callback: sys_write_file,
73118
}),
74119
);
75120

@@ -78,20 +123,7 @@ impl StdlibRegistry {
78123
Rc::new(StdFunction {
79124
name: "exists".to_string(),
80125
arity: 1,
81-
callback: |ctx, args| {
82-
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
83-
return Err(RuntimeError::new(
84-
RuntimeErrorKind::InvalidOperation(
85-
"Security policy violation: FileSystem capability is denied"
86-
.to_string(),
87-
),
88-
None,
89-
None,
90-
));
91-
}
92-
let path = args[0].try_into_string()?;
93-
Ok(RuntimeValue::Bool(std::path::Path::new(&path).exists()))
94-
},
126+
callback: sys_exists,
95127
}),
96128
);
97129

@@ -112,13 +144,7 @@ impl StdlibRegistry {
112144
Rc::new(StdFunction {
113145
name: "now".to_string(),
114146
arity: 0,
115-
callback: |_ctx, _args| {
116-
let start = std::time::SystemTime::now();
117-
let since_the_epoch = start
118-
.duration_since(std::time::UNIX_EPOCH)
119-
.unwrap_or_default();
120-
Ok(RuntimeValue::Float(since_the_epoch.as_secs_f64()))
121-
},
147+
callback: sys_now,
122148
}),
123149
);
124150

@@ -127,11 +153,7 @@ impl StdlibRegistry {
127153
Rc::new(StdFunction {
128154
name: "sleep".to_string(),
129155
arity: 1,
130-
callback: |_ctx, args| {
131-
let ms = args[0].try_into_int()?;
132-
std::thread::sleep(std::time::Duration::from_millis(ms as u64));
133-
Ok(RuntimeValue::Null)
134-
},
156+
callback: sys_sleep,
135157
}),
136158
);
137159

0 commit comments

Comments
 (0)