| @@ -1,12 +1,14 @@ |
| 1 | 1 | //! The interpreter's built-in functions: the runtime's externs |
| 2 | 2 | //! (`tilc_core::externs`), with the native runtime's semantics (`tilrt`), |
| 3 | | −//! and the data they build. |
| 3 | +//! and the data they build. Their pure kernels are the runtime's own |
| 4 | +//! (`tilrt_prims`). |
| 4 | 5 | |
| 5 | 6 | use std::cell::{Cell, RefCell}; |
| 6 | 7 | use std::io::Write; |
| 7 | 8 | use std::rc::Rc; |
| 8 | 9 | |
| 9 | 10 | use tilc_core::ir::Program; |
| 11 | +use tilrt_prims::{hash_bytes, mix}; |
| 10 | 12 | |
| 11 | 13 | use crate::{Interp, R, ROOT, RtError, State, Val, panic}; |
| 12 | 14 | |
| @@ -91,11 +93,7 @@ impl Interp { |
| 91 | 93 | "tl_string_trim" => Val::Str(string(&arg(a))?.trim().into()), |
| 92 | 94 | "tl_string_split" => { |
| 93 | 95 | let (s, sep) = (string(&arg(a))?, string(&arg(b))?); |
| 94 | | − let parts: Vec<&str> = if sep.is_empty() { |
| 95 | | − vec![&*s] |
| 96 | | − } else { |
| 97 | | − s.split(&*sep).collect() |
| 98 | | − }; |
| 96 | + let parts = tilrt_prims::split(&s, &sep); |
| 99 | 97 | list_val(parts.iter().map(|p| Val::Str((*p).into()))) |
| 100 | 98 | } |
| 101 | 99 | "tl_string_join" => { |
| @@ -127,33 +125,17 @@ impl Interp { |
| 127 | 125 | Val::Str(found.unwrap_or_default().into()), |
| 128 | 126 | ])) |
| 129 | 127 | } |
| 130 | | − "tl_url_decode" => Val::Str(url_decode(&string(&arg(a))?).into()), |
| 128 | + "tl_url_decode" => Val::Str(tilrt_prims::url_decode(&string(&arg(a))?).into()), |
| 131 | 129 | "tl_string_length" => Val::Int(string(&arg(a))?.chars().count() as i64), |
| 132 | 130 | "tl_string_debug" => Val::Str(format!("{:?}", &*string(&arg(a))?).into()), |
| 133 | 131 | "tl_hash_float" => Val::Int(mix(float(&arg(a))?.to_bits())), |
| 134 | 132 | "tl_hash_string" => Val::Int(hash_bytes(string(&arg(a))?.as_bytes())), |
| 135 | | − "tl_hash_combine" => { |
| 136 | | − let (x, y) = (int(&arg(a))? as u64, int(&arg(b))? as u64); |
| 137 | | − Val::Int(mix(x.wrapping_mul(31).wrapping_add(y) ^ (x >> 7))) |
| 138 | | − } |
| 133 | + "tl_hash_combine" => Val::Int(tilrt_prims::hash_combine(int(&arg(a))?, int(&arg(b))?)), |
| 139 | 134 | "tl_pid_eq" => match (arg(a), arg(b)) { |
| 140 | 135 | (Val::Pid(x), Val::Pid(y)) => bool_val(x == y), |
| 141 | 136 | _ => return panic("tl_pid_eq: expected pids"), |
| 142 | 137 | }, |
| 143 | | − "tl_html_escape" => { |
| 144 | | − let mut out = String::new(); |
| 145 | | − for c in string(&arg(a))?.chars() { |
| 146 | | − match c { |
| 147 | | − '&' => out.push_str("&"), |
| 148 | | − '<' => out.push_str("<"), |
| 149 | | − '>' => out.push_str(">"), |
| 150 | | − '"' => out.push_str("""), |
| 151 | | − '\'' => out.push_str("'"), |
| 152 | | − c => out.push(c), |
| 153 | | − } |
| 154 | | − } |
| 155 | | − Val::Str(out.into()) |
| 156 | | − } |
| 138 | + "tl_html_escape" => Val::Str(tilrt_prims::html_escape(&string(&arg(a))?).into()), |
| 157 | 139 | "tl_println" => { |
| 158 | 140 | self.write(&string(&arg(a))?)?; |
| 159 | 141 | self.write("\n")?; |
| @@ -173,13 +155,12 @@ impl Interp { |
| 173 | 155 | if below <= 0 { |
| 174 | 156 | return panic("random.int: the bound must be positive"); |
| 175 | 157 | } |
| 176 | | − let x = u64::from_le_bytes(random_bytes(8).try_into().expect("8 bytes")); |
| 177 | | − crate::int::from_i128(i128::from(x % below as u64)) |
| 158 | + crate::int::from_i128(i128::from(tilrt_prims::random_below(below as u64))) |
| 178 | 159 | } |
| 179 | 160 | "tl_random_token" => { |
| 180 | 161 | let n = int(&arg(a))?.clamp(0, 1 << 20) as usize; |
| 181 | 162 | Val::Str( |
| 182 | | − random_bytes(n) |
| 163 | + tilrt_prims::random_bytes(n) |
| 183 | 164 | .iter() |
| 184 | 165 | .map(|b| format!("{b:02x}")) |
| 185 | 166 | .collect::<String>() |
| @@ -387,57 +368,6 @@ fn list_items(list: &Val) -> R<Vec<Val>> { |
| 387 | 368 | } |
| 388 | 369 | } |
| 389 | 370 | |
| 390 | | −/// FNV-1a, then mixed (strings and `Bytes`). |
| 391 | | −fn hash_bytes(bytes: &[u8]) -> i64 { |
| 392 | | − let h = bytes.iter().fold(0xcbf2_9ce4_8422_2325u64, |h, b| { |
| 393 | | − (h ^ u64::from(*b)).wrapping_mul(0x100_0000_01b3) |
| 394 | | − }); |
| 395 | | − mix(h) |
| 396 | | −} |
| 397 | | − |
| 398 | | −/// Decode `%XX` and `+` (URL form encoding); invalid escapes stay. Same |
| 399 | | −/// as the runtime's (`tilrt::prims::url_decode`). |
| 400 | | −fn url_decode(s: &str) -> String { |
| 401 | | − let bytes = s.as_bytes(); |
| 402 | | − let mut out = Vec::with_capacity(bytes.len()); |
| 403 | | − let mut i = 0; |
| 404 | | − while i < bytes.len() { |
| 405 | | − match bytes[i] { |
| 406 | | − b'+' => out.push(b' '), |
| 407 | | − b'%' if i + 2 < bytes.len() => { |
| 408 | | − let hex = |b: u8| (b as char).to_digit(16); |
| 409 | | − match (hex(bytes[i + 1]), hex(bytes[i + 2])) { |
| 410 | | − (Some(h), Some(l)) => { |
| 411 | | − out.push((h * 16 + l) as u8); |
| 412 | | − i += 2; |
| 413 | | − } |
| 414 | | − _ => out.push(b'%'), |
| 415 | | − } |
| 416 | | − } |
| 417 | | − b => out.push(b), |
| 418 | | − } |
| 419 | | − i += 1; |
| 420 | | − } |
| 421 | | − String::from_utf8_lossy(&out).into_owned() |
| 422 | | −} |
| 423 | | − |
| 424 | | −fn random_bytes(n: usize) -> Vec<u8> { |
| 425 | | − use std::io::Read; |
| 426 | | − let mut buf = vec![0u8; n]; |
| 427 | | − std::fs::File::open("/dev/urandom") |
| 428 | | − .and_then(|mut f| f.read_exact(&mut buf)) |
| 429 | | − .expect("cannot read /dev/urandom"); |
| 430 | | − buf |
| 431 | | −} |
| 432 | | − |
| 433 | | −/// A 64-bit mixer (splitmix64's finalizer), reduced to a non-negative Int |
| 434 | | −/// (62 bits), so hashes are valid 63-bit Ints. |
| 435 | | −pub(crate) fn mix(mut z: u64) -> i64 { |
| 436 | | − z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9); |
| 437 | | − z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb); |
| 438 | | − ((z ^ (z >> 31)) >> 2) as i64 |
| 439 | | −} |
| 440 | | − |
| 441 | 371 | pub(crate) fn bool_val(b: bool) -> Val { |
| 442 | 372 | Val::Data { |
| 443 | 373 | tag: u32::from(b), |