1use std::collections::HashMap;
40use std::sync::Arc;
41use std::time::{SystemTime, UNIX_EPOCH};
42
43use crate::bytecode::{Message, Value};
44use crate::output::{OutputSink, StdoutSink};
45use crate::vm::{expect_arg, expect_message, expect_u64, Fault, NativeTable};
46
47pub mod std_native {
50 pub const PRINT: u32 = 0;
51 pub const NOW_MS: u32 = 1;
52 pub const MAKE_MSG: u32 = 2;
53 pub const MSG_SENDER: u32 = 3;
54 pub const MSG_REQUEST_ID: u32 = 4;
55 pub const MSG_TAG: u32 = 5;
56 pub const MSG_PAYLOAD: u32 = 6;
57 pub const MSG_REPLY_CAP: u32 = 7;
58}
59
60pub fn std_native_map() -> HashMap<String, u32> {
66 HashMap::from([
67 ("print".to_owned(), 0),
68 ("now_ms".to_owned(), 1),
69 ("make_msg".to_owned(), 2),
70 ("msg_sender".to_owned(), 3),
71 ("msg_request_id".to_owned(), 4),
72 ("msg_tag".to_owned(), 5),
73 ("msg_payload".to_owned(), 6),
74 ("msg_reply_cap".to_owned(), 7),
75 ])
76}
77
78pub fn std_native_table() -> Arc<NativeTable> {
81 std_native_table_with(Arc::new(StdoutSink))
82}
83
84pub fn std_native_table_with(output: Arc<dyn OutputSink>) -> Arc<NativeTable> {
86 let built = NativeTable::builder()
87 .register("print", {
88 let output = Arc::clone(&output);
89 move |args| {
90 output.write(args);
91 Ok(Value::Unit)
92 }
93 })
94 .and_then(|b| {
95 b.register("now_ms", |_| {
96 let duration = SystemTime::now()
97 .duration_since(UNIX_EPOCH)
98 .map_err(|e| Fault::NativeError(format!("system clock error: {e}")))?;
99 let millis = i64::try_from(duration.as_millis())
100 .map_err(|_| Fault::NativeError("system clock value exceeds i64".into()))?;
101 Ok(Value::Int(millis))
102 })
103 })
104 .and_then(|b| {
105 b.register("make_msg", |args| {
106 let (request_id, tag, payload) = if args.len() >= 4 {
111 (
112 expect_u64(args, 1, "make_msg")?,
113 expect_u64(args, 2, "make_msg")?,
114 expect_arg(args, 3, "make_msg")?.clone(),
115 )
116 } else {
117 (
118 expect_u64(args, 0, "make_msg")?,
119 expect_u64(args, 1, "make_msg")?,
120 expect_arg(args, 2, "make_msg")?.clone(),
121 )
122 };
123 let tag = u16::try_from(tag).map_err(|_| {
124 Fault::NativeError(format!("make_msg: tag {tag} does not fit in u16"))
125 })?;
126 Ok(Value::Message(Message::new(0, request_id, tag, payload)))
127 })
128 })
129 .and_then(|b| {
130 b.register("msg_sender", |args| {
131 Ok(Value::Pid(expect_message(args, 0, "msg_sender")?.sender))
132 })
133 })
134 .and_then(|b| {
135 b.register("msg_request_id", |args| {
136 Ok(Value::Int(
137 expect_message(args, 0, "msg_request_id")?.request_id as i64,
138 ))
139 })
140 })
141 .and_then(|b| {
142 b.register("msg_tag", |args| {
143 Ok(Value::Int(i64::from(
144 expect_message(args, 0, "msg_tag")?.tag,
145 )))
146 })
147 })
148 .and_then(|b| {
149 b.register("msg_payload", |args| {
150 Ok(expect_message(args, 0, "msg_payload")?
151 .payload
152 .as_ref()
153 .clone())
154 })
155 })
156 .and_then(|b| {
157 b.register("msg_reply_cap", |args| {
158 Ok(Value::Cap(
159 expect_message(args, 0, "msg_reply_cap")?.reply_cap,
160 ))
161 })
162 });
163 match built {
164 Ok(b) => b.build(),
165 Err(_) => NativeTable::empty(),
166 }
167}
168
169pub fn std_natives() -> (Arc<NativeTable>, HashMap<String, u32>) {
175 let table = std_native_table();
176 let map: HashMap<String, u32> = table
177 .names()
178 .filter_map(|n| table.index_of(n).map(|i| (n.to_string(), i)))
179 .collect();
180 (table, map)
181}
182
183#[cfg(test)]
184mod tests {
185 use super::*;
186
187 #[test]
188 fn std_native_indices_are_stable() {
189 let map = std_native_map();
190 assert_eq!(map.get("print"), Some(&0));
191 assert_eq!(map.get("now_ms"), Some(&1));
192 assert_eq!(map.get("make_msg"), Some(&2));
193 assert_eq!(map.get("msg_sender"), Some(&3));
194 assert_eq!(map.get("msg_request_id"), Some(&4));
195 assert_eq!(map.get("msg_tag"), Some(&5));
196 assert_eq!(map.get("msg_payload"), Some(&6));
197 assert_eq!(map.get("msg_reply_cap"), Some(&7));
198 }
199
200 #[test]
201 fn std_native_table_matches_map() {
202 let (table, map) = std_natives();
203 assert_eq!(table.len(), 8);
204 for (name, idx) in &map {
205 assert_eq!(table.index_of(name), Some(*idx));
206 }
207 }
208
209 #[test]
210 fn now_ms_returns_non_negative_int() -> Result<(), Box<dyn std::error::Error>> {
211 let table = std_native_table();
212 let now_ms = table.get(1).ok_or("now_ms native")?;
213 let value = now_ms(&[])?;
214 assert!(matches!(value, Value::Int(ms) if ms >= 0));
215 Ok(())
216 }
217
218 use crate::bytecode::CapId;
219
220 #[test]
221 fn make_msg_and_unpack_round_trip() -> Result<(), Box<dyn std::error::Error>> {
222 let table = std_native_table();
223 let make = table.get(2).ok_or("make_msg")?;
224 let msg = make(&[Value::Int(3), Value::Int(7), Value::Int(42)])?;
225 let expected = Message::new(0, 3, 7, 42);
226 assert_eq!(msg.as_message(), Some(&expected));
227 let sender = table.get(3).ok_or("msg_sender")?;
228 let req = table.get(4).ok_or("msg_request_id")?;
229 let tag = table.get(5).ok_or("msg_tag")?;
230 let payload = table.get(6).ok_or("msg_payload")?;
231 let cap = table.get(7).ok_or("msg_reply_cap")?;
232 assert_eq!(sender(std::slice::from_ref(&msg))?, Value::Pid(0));
233 assert_eq!(req(std::slice::from_ref(&msg))?, Value::Int(3));
234 assert_eq!(tag(std::slice::from_ref(&msg))?, Value::Int(7));
235 assert_eq!(payload(std::slice::from_ref(&msg))?, Value::Int(42));
236 assert_eq!(cap(std::slice::from_ref(&msg))?, Value::Cap(CapId::NONE));
237 Ok(())
238 }
239
240 #[test]
241 fn print_accepts_all_current_values() -> Result<(), Box<dyn std::error::Error>> {
242 let table = std_native_table();
243 let print = table.get(0).ok_or("print native")?;
244 let values = [
245 Value::Unit,
246 Value::Bool(true),
247 Value::Int(42),
248 Value::Float(1.5),
249 Value::Pid(7),
250 Value::Message(Message::new(1, 2, 3, 4)),
251 Value::Cap(CapId::from_raw(9)),
252 Value::str("hello"),
253 Value::bytes([1u8, 2, 3]),
254 ];
255 assert_eq!(print(&values)?, Value::Unit);
256 Ok(())
257 }
258
259 #[test]
260 fn print_with_no_args_still_returns_unit() -> Result<(), Box<dyn std::error::Error>> {
261 let table = std_native_table();
262 let print = table.get(0).ok_or("print native")?;
263 assert_eq!(print(&[])?, Value::Unit);
264 Ok(())
265 }
266}