-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys.rs
More file actions
148 lines (140 loc) · 5.34 KB
/
Copy pathsys.rs
File metadata and controls
148 lines (140 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use indexmap::IndexMap;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{
context::Capability,
error::{RuntimeError, RuntimeErrorKind},
value::RuntimeValue,
};
impl StdlibRegistry {
pub fn register_sys(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
exports.insert(
"read_file".to_string(),
Rc::new(StdFunction {
name: "read_file".to_string(),
arity: 1,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: FileSystem capability is denied"
.to_string(),
),
None,
None,
));
}
let path = args[0].try_into_string()?;
let content = std::fs::read_to_string(path).map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!("IO error: {}", e)),
None,
None,
)
})?;
Ok(RuntimeValue::Str(content))
},
}),
);
exports.insert(
"write_file".to_string(),
Rc::new(StdFunction {
name: "write_file".to_string(),
arity: 2,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: FileSystem capability is denied"
.to_string(),
),
None,
None,
));
}
let path = args[0].try_into_string()?;
let content = args[1].try_into_string()?;
std::fs::write(path, content).map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!("IO error: {}", e)),
None,
None,
)
})?;
Ok(RuntimeValue::Null)
},
}),
);
exports.insert(
"exists".to_string(),
Rc::new(StdFunction {
name: "exists".to_string(),
arity: 1,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::FileSystem) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: FileSystem capability is denied"
.to_string(),
),
None,
None,
));
}
let path = args[0].try_into_string()?;
Ok(RuntimeValue::Bool(std::path::Path::new(&path).exists()))
},
}),
);
self.register_module(
"std.fs",
StdlibModule {
name: "std.fs".to_string(),
version: "1.0.0".to_string(),
exports: exports.clone(),
required_capabilities: vec![Capability::FileSystem],
},
);
let mut time_exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
time_exports.insert(
"now".to_string(),
Rc::new(StdFunction {
name: "now".to_string(),
arity: 0,
callback: |_ctx, _args| {
let start = std::time::SystemTime::now();
let since_the_epoch = start
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default();
Ok(RuntimeValue::Float(since_the_epoch.as_secs_f64()))
},
}),
);
time_exports.insert(
"sleep".to_string(),
Rc::new(StdFunction {
name: "sleep".to_string(),
arity: 1,
callback: |_ctx, args| {
let ms = args[0].try_into_int()?;
std::thread::sleep(std::time::Duration::from_millis(ms as u64));
Ok(RuntimeValue::Null)
},
}),
);
self.register_module(
"std.time",
StdlibModule {
name: "std.time".to_string(),
version: "1.0.0".to_string(),
exports: time_exports,
required_capabilities: Vec::new(),
},
);
}
}