
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1201 · main · 9 min 57 s
1Étoiles
0Forks
0Issues
| 1 | //! Runtime-kind blocks: strings, byte strings and closures, and the |
| 2 | //! constructor cells runtime functions return (lists, `Result`s). |
| 3 | |
| 4 | use std::ffi::c_char; |
| 5 | |
| 6 | use crate::abi::{ |
| 7 | Block, Ctx, Header, TAG_BYTES, TAG_CLOSURE, TAG_STRING, UNIT, Value, int, is_ptr, |
| 8 | }; |
| 9 | use crate::heap::tl_alloc; |
| 10 | |
| 11 | const WORD: usize = size_of::<Value>(); |
| 12 | |
| 13 | /// Strings have no scanned fields, so the raw area follows the header. |
| 14 | fn string_raw(s: Value) -> *mut u8 { |
| 15 | Block::fields_ptr(s as *mut Block).cast::<u8>() |
| 16 | } |
| 17 | |
| 18 | /// A fresh string of `len` bytes, its length and NUL written, and where its |
| 19 | /// bytes go: the caller writes them (UTF-8) before the string is used. For |
| 20 | /// building a string in place, with one allocation and one copy (issue |
| 21 | /// #129). |
| 22 | pub fn string_uninit(ctx: *mut Ctx, len: usize) -> (Value, *mut u8) { |
| 23 | let s = tl_alloc(ctx, TAG_STRING, 0, WORD + len + 1) as Value; |
| 24 | let raw = string_raw(s); |
| 25 | // SAFETY: the raw area holds an i64 length, `len` bytes, and a NUL. |
| 26 | unsafe { |
| 27 | raw.cast::<i64>().write(len as i64); |
| 28 | raw.add(WORD + len).write(0); |
| 29 | (s, raw.add(WORD)) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /// A fresh `Bytes` of `len` bytes, its length written, and where its bytes |
| 34 | /// go (see [`string_uninit`]). |
| 35 | pub fn bytes_uninit(ctx: *mut Ctx, len: usize) -> (Value, *mut u8) { |
| 36 | let b = tl_alloc(ctx, TAG_BYTES, 0, WORD + len); |
| 37 | let raw = Block::fields_ptr(b).cast::<u8>(); |
| 38 | // SAFETY: the raw area holds an i64 length and `len` bytes. |
| 39 | unsafe { |
| 40 | raw.cast::<i64>().write(len as i64); |
| 41 | (b as Value, raw.add(WORD)) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Shrink the unique string `s`, allocated for `alloced` bytes, to `len` |
| 46 | /// bytes (`len <= alloced`): the block is resized and its length and NUL |
| 47 | /// rewritten. Returns the (possibly moved) string. For writing a string's |
| 48 | /// bytes before its exact length is known (the block's size is derived |
| 49 | /// from the length word when it is freed, so it must not stay over-long). |
| 50 | /// |
| 51 | /// # Safety |
| 52 | /// `s` must be a unique string whose block was allocated for `alloced` |
| 53 | /// bytes (as by [`string_uninit`]), with its first `len` bytes written. |
| 54 | pub unsafe fn string_shrink(s: Value, alloced: usize, len: usize) -> Value { |
| 55 | debug_assert!(len <= alloced); |
| 56 | if len == alloced { |
| 57 | return s; |
| 58 | } |
| 59 | let header = size_of::<crate::abi::Header>(); |
| 60 | // SAFETY: forwarded caller guarantee: `s` is unique and its block was |
| 61 | // allocated with HEADER + WORD + alloced + 1 bytes. |
| 62 | let b = unsafe { |
| 63 | crate::heap::realloc_block( |
| 64 | s as *mut Block, |
| 65 | header + WORD + alloced + 1, |
| 66 | header + WORD + len + 1, |
| 67 | ) |
| 68 | }; |
| 69 | // SAFETY: the raw area holds the length, `len` bytes and a NUL. |
| 70 | unsafe { |
| 71 | let raw = string_raw(b as Value); |
| 72 | raw.cast::<i64>().write(len as i64); |
| 73 | raw.add(WORD + len).write(0); |
| 74 | } |
| 75 | b as Value |
| 76 | } |
| 77 | |
| 78 | /// The total length of `parts`, `sep` between each two: what building |
| 79 | /// their concatenation in one block needs. |
| 80 | pub fn joined_len<'a>(parts: impl Iterator<Item = &'a [u8]>, sep: usize) -> usize { |
| 81 | let (count, len) = parts.fold((0usize, 0usize), |(n, len), p| { |
| 82 | ( |
| 83 | n + 1, |
| 84 | len.checked_add(p.len()).expect("louise: string too large"), |
| 85 | ) |
| 86 | }); |
| 87 | sep.checked_mul(count.saturating_sub(1)) |
| 88 | .and_then(|s| len.checked_add(s)) |
| 89 | .expect("louise: string too large") |
| 90 | } |
| 91 | |
| 92 | /// Write `parts` to `out`, `sep` between each two; `out` must have room for |
| 93 | /// [`joined_len`] bytes. |
| 94 | /// |
| 95 | /// # Safety |
| 96 | /// `out` must be valid for writes of the joined length. |
| 97 | pub unsafe fn write_joined<'a>(out: *mut u8, parts: impl Iterator<Item = &'a [u8]>, sep: &[u8]) { |
| 98 | let mut at = 0; |
| 99 | for (i, p) in parts.enumerate() { |
| 100 | // SAFETY: the caller sized `out` with `joined_len` over the same |
| 101 | // parts and separator. |
| 102 | unsafe { |
| 103 | if i > 0 { |
| 104 | std::ptr::copy_nonoverlapping(sep.as_ptr(), out.add(at), sep.len()); |
| 105 | at += sep.len(); |
| 106 | } |
| 107 | std::ptr::copy_nonoverlapping(p.as_ptr(), out.add(at), p.len()); |
| 108 | } |
| 109 | at += p.len(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Copy `len` bytes (expected UTF-8) into a fresh, NUL-terminated string. |
| 114 | /// |
| 115 | /// # Safety |
| 116 | /// `bytes` must be valid for reads of `len` bytes. |
| 117 | #[unsafe(no_mangle)] |
| 118 | pub unsafe extern "C" fn tl_string_from(ctx: *mut Ctx, bytes: *const c_char, len: usize) -> Value { |
| 119 | let b = tl_alloc(ctx, TAG_STRING, 0, WORD + len + 1); |
| 120 | let s = b as Value; |
| 121 | // SAFETY: the raw area holds an i64 length, `len` bytes, and a NUL. |
| 122 | unsafe { |
| 123 | let raw = string_raw(s); |
| 124 | raw.cast::<i64>().write(len as i64); |
| 125 | std::ptr::copy_nonoverlapping(bytes.cast::<u8>(), raw.add(WORD), len); |
| 126 | raw.add(WORD + len).write(0); |
| 127 | } |
| 128 | s |
| 129 | } |
| 130 | |
| 131 | /// # Safety |
| 132 | /// `s` must be a live string (borrowed). |
| 133 | #[unsafe(no_mangle)] |
| 134 | pub unsafe extern "C" fn tl_string_len(s: Value) -> usize { |
| 135 | // SAFETY: caller guarantees `s` is a live string. |
| 136 | unsafe { string_raw(s).cast::<i64>().read() as usize } |
| 137 | } |
| 138 | |
| 139 | /// # Safety |
| 140 | /// `s` must be a live string (borrowed); the pointer is valid while it lives. |
| 141 | #[unsafe(no_mangle)] |
| 142 | pub unsafe extern "C" fn tl_string_ptr(s: Value) -> *const c_char { |
| 143 | // SAFETY: caller guarantees `s` is a live string. |
| 144 | unsafe { string_raw(s).add(WORD).cast() } |
| 145 | } |
| 146 | |
| 147 | /// # Safety |
| 148 | /// `s` must be a live string (borrowed). |
| 149 | pub unsafe fn string_bytes<'a>(s: Value) -> &'a [u8] { |
| 150 | // SAFETY: caller guarantees `s` is a live string. |
| 151 | unsafe { std::slice::from_raw_parts(tl_string_ptr(s).cast(), tl_string_len(s)) } |
| 152 | } |
| 153 | |
| 154 | /// The text of a string (borrowed). |
| 155 | /// |
| 156 | /// # Safety |
| 157 | /// `s` must be a live string (borrowed). |
| 158 | pub(crate) unsafe fn text<'a>(s: Value) -> &'a str { |
| 159 | // SAFETY: strings hold UTF-8 (they are made from Rust strings, source |
| 160 | // literals or checked bytes). |
| 161 | unsafe { std::str::from_utf8_unchecked(string_bytes(s)) } |
| 162 | } |
| 163 | |
| 164 | /// A fresh string holding `s`. |
| 165 | pub(crate) fn string(ctx: *mut Ctx, s: &str) -> Value { |
| 166 | // SAFETY: `s` is valid for its length. |
| 167 | unsafe { tl_string_from(ctx, s.as_ptr().cast(), s.len()) } |
| 168 | } |
| 169 | |
| 170 | /// An immortal string (rc 0) holding `s`, leaked: never freed and never |
| 171 | /// counted by the leak oracle — it lives outside the counted heap, |
| 172 | /// reachable from a static, like the clock capabilities (`clock.rs`). |
| 173 | /// For the handful of strings a hot path repeats forever (common HTTP |
| 174 | /// header names, `http.rs`). |
| 175 | pub(crate) fn immortal_string(s: &'static str) -> Value { |
| 176 | let words = (WORD + WORD + s.len() + 1).div_ceil(WORD); |
| 177 | let mut block = vec![0 as Value; words].into_boxed_slice(); |
| 178 | let b = block.as_mut_ptr().cast::<Block>(); |
| 179 | // SAFETY: `block` is `Value`-aligned with room for the header and |
| 180 | // the raw area (an i64 length, the bytes, a NUL). |
| 181 | unsafe { |
| 182 | b.cast::<Header>().write(Header { |
| 183 | rc: 0, |
| 184 | tag: TAG_STRING, |
| 185 | scan_fsize: 0, |
| 186 | flags: 0, |
| 187 | }); |
| 188 | let raw = string_raw(b as Value); |
| 189 | raw.cast::<i64>().write(s.len() as i64); |
| 190 | std::ptr::copy_nonoverlapping(s.as_ptr(), raw.add(WORD), s.len()); |
| 191 | } |
| 192 | Box::leak(block); |
| 193 | b as Value |
| 194 | } |
| 195 | |
| 196 | /// Copy `bytes` into fresh `Bytes`. Unlike a string, the raw area has no |
| 197 | /// NUL: an i64 length, then the bytes. |
| 198 | pub fn bytes_from(ctx: *mut Ctx, bytes: &[u8]) -> Value { |
| 199 | let b = tl_alloc(ctx, TAG_BYTES, 0, WORD + bytes.len()); |
| 200 | // SAFETY: the raw area holds an i64 length and `bytes.len()` bytes. |
| 201 | unsafe { |
| 202 | let raw = Block::fields_ptr(b).cast::<u8>(); |
| 203 | raw.cast::<i64>().write(bytes.len() as i64); |
| 204 | std::ptr::copy_nonoverlapping(bytes.as_ptr(), raw.add(WORD), bytes.len()); |
| 205 | } |
| 206 | b as Value |
| 207 | } |
| 208 | |
| 209 | /// The contents of a `String` or `Bytes` (both start their raw area with |
| 210 | /// an i64 length). |
| 211 | /// |
| 212 | /// # Safety |
| 213 | /// `b` must be a live string or byte string (borrowed). |
| 214 | pub unsafe fn bytes_of<'a>(b: Value) -> &'a [u8] { |
| 215 | // SAFETY: forwarded caller guarantee; the layouts agree up to the NUL. |
| 216 | unsafe { string_bytes(b) } |
| 217 | } |
| 218 | |
| 219 | /// A constructor cell with tag `tag` and `fields` (owned). |
| 220 | pub(crate) fn cell(ctx: *mut Ctx, tag: u16, fields: &[Value]) -> Value { |
| 221 | let b = tl_alloc(ctx, tag, fields.len() as u8, 0); |
| 222 | // SAFETY: a fresh block with `fields.len()` scanned fields. |
| 223 | unsafe { |
| 224 | let f = Block::fields_ptr(b); |
| 225 | for (i, v) in fields.iter().enumerate() { |
| 226 | f.add(i).write(*v); |
| 227 | } |
| 228 | } |
| 229 | b as Value |
| 230 | } |
| 231 | |
| 232 | /// `Ok(v)` (`Result` constructor 0). |
| 233 | pub(crate) fn ok(ctx: *mut Ctx, v: Value) -> Value { |
| 234 | cell(ctx, 0, &[v]) |
| 235 | } |
| 236 | |
| 237 | /// `Error(message)` (`Result` constructor 1). |
| 238 | pub(crate) fn error(ctx: *mut Ctx, message: &str) -> Value { |
| 239 | // SAFETY: `message` is valid for its length. |
| 240 | let text = unsafe { tl_string_from(ctx, message.as_ptr().cast(), message.len()) }; |
| 241 | cell(ctx, 1, &[text]) |
| 242 | } |
| 243 | |
| 244 | /// A `List` (`Nil` is the small int 0, `Cons` constructor 1) of `items` |
| 245 | /// (owned). |
| 246 | pub(crate) fn list(ctx: *mut Ctx, items: impl DoubleEndedIterator<Item = Value>) -> Value { |
| 247 | items |
| 248 | .rev() |
| 249 | .fold(int(0), |tail, head| cell(ctx, 1, &[head, tail])) |
| 250 | } |
| 251 | |
| 252 | /// The elements of a `List` (borrowed). |
| 253 | /// |
| 254 | /// # Safety |
| 255 | /// `list` must be a live list. |
| 256 | pub(crate) unsafe fn list_items(mut list: Value) -> impl Iterator<Item = Value> { |
| 257 | std::iter::from_fn(move || { |
| 258 | if !is_ptr(list) { |
| 259 | return None; |
| 260 | } |
| 261 | // SAFETY: a `Cons` cell has the head, then the tail. |
| 262 | unsafe { |
| 263 | let f = Block::fields_ptr(list as *mut Block); |
| 264 | list = f.add(1).read(); |
| 265 | Some(f.read()) |
| 266 | } |
| 267 | }) |
| 268 | } |
| 269 | |
| 270 | /// Print `s` and a newline to stdout, through the console (ADR 0022): |
| 271 | /// the actor parks, rather than its thread blocks, while the console is |
| 272 | /// behind. Borrows `s` (like every function the standard library |
| 273 | /// declares `@extern`). |
| 274 | /// |
| 275 | /// # Safety |
| 276 | /// `ctx` must be the running actor's context (or null); `s` a live string. |
| 277 | #[unsafe(no_mangle)] |
| 278 | pub unsafe extern "C" fn tl_println(ctx: *mut Ctx, s: Value) -> Value { |
| 279 | // SAFETY: caller passes its context and a live string. |
| 280 | unsafe { |
| 281 | crate::console::write(ctx, crate::console::Stream::Out, &[string_bytes(s), b"\n"]); |
| 282 | } |
| 283 | crate::abi::UNIT |
| 284 | } |
| 285 | |
| 286 | pub type ClosureFn = unsafe extern "C" fn(); |
| 287 | |
| 288 | /// Allocate a closure with `env_count` captured values (initialised to |
| 289 | /// unit; the caller stores the environment into fields 0..env_count). |
| 290 | /// `code` is the owned entry, `borrowed` the borrowed-contract one |
| 291 | /// (ADR 0068): called with the closure and the arguments borrowed, it |
| 292 | /// neither drops nor consumes them. |
| 293 | #[unsafe(no_mangle)] |
| 294 | pub extern "C" fn tl_closure_alloc( |
| 295 | ctx: *mut Ctx, |
| 296 | code: ClosureFn, |
| 297 | borrowed: ClosureFn, |
| 298 | env_count: u8, |
| 299 | ) -> Value { |
| 300 | let b = tl_alloc(ctx, TAG_CLOSURE, env_count, 2 * WORD); |
| 301 | // SAFETY: the code pointers live in the raw area after the env fields. |
| 302 | unsafe { |
| 303 | let raw = Block::fields_ptr(b) |
| 304 | .add(env_count as usize) |
| 305 | .cast::<ClosureFn>(); |
| 306 | raw.write(code); |
| 307 | raw.add(1).write(borrowed); |
| 308 | } |
| 309 | b as Value |
| 310 | } |
| 311 | |
| 312 | /// # Safety |
| 313 | /// `clo` must be a live closure (borrowed). |
| 314 | pub unsafe fn closure_code(clo: Value) -> ClosureFn { |
| 315 | // SAFETY: caller guarantees `clo` is a live closure. |
| 316 | unsafe { closure_slot(clo, 0) } |
| 317 | } |
| 318 | |
| 319 | /// The borrowed-contract entry (ADR 0068). |
| 320 | /// |
| 321 | /// # Safety |
| 322 | /// `clo` must be a live closure (borrowed). |
| 323 | pub unsafe fn closure_code_borrowed(clo: Value) -> ClosureFn { |
| 324 | // SAFETY: caller guarantees `clo` is a live closure. |
| 325 | unsafe { closure_slot(clo, 1) } |
| 326 | } |
| 327 | |
| 328 | /// Entry `slot` of `clo` (0 owned, 1 borrowed). |
| 329 | /// |
| 330 | /// # Safety |
| 331 | /// `clo` must be a live closure (borrowed). |
| 332 | unsafe fn closure_slot(clo: Value, slot: usize) -> ClosureFn { |
| 333 | let b = clo as *mut Block; |
| 334 | // SAFETY: caller guarantees `clo` is a live closure; the raw area |
| 335 | // holds two code pointers after the env fields. |
| 336 | unsafe { |
| 337 | let n = (*b).header.scan_fsize as usize; |
| 338 | Block::fields_ptr(b) |
| 339 | .add(n + slot) |
| 340 | .cast::<ClosureFn>() |
| 341 | .read() |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /// The owned-entry signature every closure entry has. |
| 346 | type Entry = unsafe extern "C" fn(*mut Ctx, Value, Value, Value, Value, Value, Value) -> Value; |
| 347 | |
| 348 | /// The borrowed entry (ADR 0068) of a runtime-made closure called with no |
| 349 | /// arguments: the caller keeps its references, so dup `this` for the |
| 350 | /// owned entry, which consumes the duplicate. |
| 351 | /// |
| 352 | /// # Safety |
| 353 | /// `ctx` must be the running actor's context; `this` a live closure whose |
| 354 | /// owned entry takes no arguments. |
| 355 | #[unsafe(no_mangle)] |
| 356 | pub unsafe extern "C" fn tl_borrowed0( |
| 357 | ctx: *mut Ctx, |
| 358 | this: Value, |
| 359 | _: Value, |
| 360 | _: Value, |
| 361 | _: Value, |
| 362 | _: Value, |
| 363 | _: Value, |
| 364 | ) -> Value { |
| 365 | // SAFETY: forwarded caller guarantees; the owned entry consumes the |
| 366 | // duplicates. |
| 367 | unsafe { |
| 368 | crate::heap::dup_value(this); |
| 369 | let code: Entry = std::mem::transmute(closure_code(this)); |
| 370 | code(ctx, this, UNIT, UNIT, UNIT, UNIT, UNIT) |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /// [`tl_borrowed0`] for a closure called with one argument. |
| 375 | /// |
| 376 | /// # Safety |
| 377 | /// `ctx` must be the running actor's context; `this` a live closure whose |
| 378 | /// owned entry takes one argument. |
| 379 | #[unsafe(no_mangle)] |
| 380 | pub unsafe extern "C" fn tl_borrowed1( |
| 381 | ctx: *mut Ctx, |
| 382 | this: Value, |
| 383 | a: Value, |
| 384 | _: Value, |
| 385 | _: Value, |
| 386 | _: Value, |
| 387 | _: Value, |
| 388 | ) -> Value { |
| 389 | // SAFETY: as `tl_borrowed0`. |
| 390 | unsafe { |
| 391 | crate::heap::dup_value(this); |
| 392 | crate::heap::dup_value(a); |
| 393 | let code: Entry = std::mem::transmute(closure_code(this)); |
| 394 | code(ctx, this, a, UNIT, UNIT, UNIT, UNIT) |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /// [`tl_borrowed0`] for a closure called with two arguments. |
| 399 | /// |
| 400 | /// # Safety |
| 401 | /// `ctx` must be the running actor's context; `this` a live closure whose |
| 402 | /// owned entry takes two arguments. |
| 403 | #[unsafe(no_mangle)] |
| 404 | pub unsafe extern "C" fn tl_borrowed2( |
| 405 | ctx: *mut Ctx, |
| 406 | this: Value, |
| 407 | a: Value, |
| 408 | b: Value, |
| 409 | _: Value, |
| 410 | _: Value, |
| 411 | _: Value, |
| 412 | ) -> Value { |
| 413 | // SAFETY: as `tl_borrowed0`. |
| 414 | unsafe { |
| 415 | crate::heap::dup_value(this); |
| 416 | crate::heap::dup_value(a); |
| 417 | crate::heap::dup_value(b); |
| 418 | let code: Entry = std::mem::transmute(closure_code(this)); |
| 419 | code(ctx, this, a, b, UNIT, UNIT, UNIT) |
| 420 | } |
| 421 | } |