
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 | //! Functions compiled Louise programs call: the standard library's |
| 2 | //! `@extern` declarations over floats, strings, randomness and the |
| 3 | //! environment, panics, and helpers the other modules share. Regions are |
| 4 | //! in `region`, resources in `resource`. |
| 5 | //! |
| 6 | //! Like every `@extern` function, these **borrow** their arguments and |
| 7 | //! return an owned result (see `louise_perceus`). They must behave exactly |
| 8 | //! like the interpreter's (`louise_interp`): tests compare the output |
| 9 | //! of both. Their pure kernels are shared with it (`louise_rt_prims`). |
| 10 | |
| 11 | use std::cmp::Ordering as Order; |
| 12 | |
| 13 | use louise_rt_prims::{ |
| 14 | chars, hash_bytes, hash_combine, html_escaped, html_escaped_len, mix, split, |
| 15 | }; |
| 16 | |
| 17 | use crate::abi::{Block, Ctx, TAG_FLOAT, UNIT, Value, int}; |
| 18 | use crate::console; |
| 19 | use crate::heap::{drop_value, tl_alloc}; |
| 20 | use crate::int::saturating; |
| 21 | use crate::values::{bytes_from, cell, error, list, ok, string, string_bytes, text}; |
| 22 | |
| 23 | const WORD: usize = size_of::<Value>(); |
| 24 | |
| 25 | // ---- panics ----------------------------------------------------------------- |
| 26 | |
| 27 | /// Start unwinding the actor with `message` (see `tl_panic_str`). |
| 28 | /// |
| 29 | /// # Safety |
| 30 | /// `ctx` must be the running actor's context. |
| 31 | pub(crate) unsafe fn panic_with(ctx: *mut Ctx, message: &str) -> Value { |
| 32 | let c = std::ffi::CString::new(message).unwrap_or_default(); |
| 33 | // SAFETY: forwarded caller guarantee on `ctx`; `c` is NUL-terminated. |
| 34 | unsafe { crate::effects::tl_panic_str(ctx, c.as_ptr()) } |
| 35 | } |
| 36 | |
| 37 | /// `panic(message)` from the prelude. |
| 38 | /// |
| 39 | /// # Safety |
| 40 | /// `ctx` must be the running actor's context; `message` a live string. |
| 41 | #[unsafe(no_mangle)] |
| 42 | pub unsafe extern "C" fn tl_panic(ctx: *mut Ctx, message: Value) -> Value { |
| 43 | // SAFETY: caller guarantees a live string. |
| 44 | let text = unsafe { text(message) }.to_owned(); |
| 45 | // SAFETY: caller guarantees the running actor's context. |
| 46 | unsafe { panic_with(ctx, &text) } |
| 47 | } |
| 48 | |
| 49 | /// With the `LOUISE_LEAKS` environment variable set, print the number of |
| 50 | /// live heap blocks (the Perceus leak oracle; tests require 0). With |
| 51 | /// `LOUISE_STATS` set, print the number of blocks ever allocated (the |
| 52 | /// allocation measurement; per-request costs are this divided by the |
| 53 | /// requests a run served). |
| 54 | #[unsafe(no_mangle)] |
| 55 | pub extern "C" fn tl_report_leaks() { |
| 56 | if std::env::var_os("LOUISE_LEAKS").is_some() { |
| 57 | match crate::heap::stats::tl_stats_live_blocks() { |
| 58 | // The `fast` runtime has no leak oracle (ADR 0031). |
| 59 | -1 => eprintln!( |
| 60 | "louise: live blocks: unknown (this runtime has no leak oracle; link the checked one: LOUISE_RUNTIME=checked)" |
| 61 | ), |
| 62 | live => eprintln!("louise: live blocks: {live}"), |
| 63 | } |
| 64 | #[cfg(feature = "lock-stats")] |
| 65 | { |
| 66 | let totals = crate::sched::lock_stats::totals(); |
| 67 | for (name, (taken, contended)) in crate::sched::lock_stats::NAMES.iter().zip(totals) { |
| 68 | eprintln!("louise: {name}: {taken} taken, {contended} found taken"); |
| 69 | } |
| 70 | } |
| 71 | #[cfg(feature = "sched-stats")] |
| 72 | for (name, n) in crate::sched::sched_stats::NAMES |
| 73 | .iter() |
| 74 | .zip(crate::sched::sched_stats::totals()) |
| 75 | { |
| 76 | eprintln!("louise: {name}: {n}"); |
| 77 | } |
| 78 | } |
| 79 | if std::env::var_os("LOUISE_STATS").is_some() { |
| 80 | match crate::heap::stats::tl_stats_allocations() { |
| 81 | // The `fast` runtime has no allocation counter either. |
| 82 | -1 => eprintln!( |
| 83 | "louise: allocs: unknown (this runtime has no allocation counter; link the checked one: LOUISE_RUNTIME=checked)" |
| 84 | ), |
| 85 | allocs => eprintln!("louise: allocs: {allocs}"), |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // ---- helpers ------------------------------------------------------------------ |
| 91 | |
| 92 | pub(crate) fn boolean(b: bool) -> Value { |
| 93 | int(i64::from(b)) |
| 94 | } |
| 95 | |
| 96 | /// `Ordering` (`Lt`, `Eq`, `Gt` are constructors 0, 1, 2). |
| 97 | pub(crate) fn order(o: Order) -> Value { |
| 98 | int(match o { |
| 99 | Order::Less => 0, |
| 100 | Order::Equal => 1, |
| 101 | Order::Greater => 2, |
| 102 | }) |
| 103 | } |
| 104 | |
| 105 | /// Call closure `clo` (consumed) with `args` (consumed), following the |
| 106 | /// generated-code convention: the closure first, five more values in |
| 107 | /// registers, the rest in `ctx->xargs`. |
| 108 | /// |
| 109 | /// # Safety |
| 110 | /// `ctx` must be the running actor's context; `clo` a live closure. |
| 111 | pub(crate) unsafe fn call_closure(ctx: *mut Ctx, clo: Value, args: &[Value]) -> Value { |
| 112 | type Code = unsafe extern "C" fn(*mut Ctx, Value, Value, Value, Value, Value, Value) -> Value; |
| 113 | let mut regs = [UNIT; 5]; |
| 114 | for (i, a) in args.iter().enumerate() { |
| 115 | if i < 5 { |
| 116 | regs[i] = *a; |
| 117 | } else { |
| 118 | // SAFETY: caller guarantees a valid context. |
| 119 | unsafe { *(*ctx).xargs.add(i - 5) = *a }; |
| 120 | } |
| 121 | } |
| 122 | // SAFETY: closure code has the generated-code signature. |
| 123 | unsafe { |
| 124 | let code: Code = std::mem::transmute(crate::values::closure_code(clo)); |
| 125 | code(ctx, clo, regs[0], regs[1], regs[2], regs[3], regs[4]) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // ---- floats ------------------------------------------------------------------- |
| 130 | |
| 131 | /// Box a Float. |
| 132 | #[unsafe(no_mangle)] |
| 133 | pub extern "C" fn tl_float_from(ctx: *mut Ctx, x: f64) -> Value { |
| 134 | let b = tl_alloc(ctx, TAG_FLOAT, 0, WORD); |
| 135 | // SAFETY: the raw area holds one f64. |
| 136 | unsafe { Block::fields_ptr(b).cast::<f64>().write(x) }; |
| 137 | b as Value |
| 138 | } |
| 139 | |
| 140 | /// # Safety |
| 141 | /// `v` must be a live Float. |
| 142 | #[unsafe(no_mangle)] |
| 143 | pub unsafe extern "C" fn tl_float_get(v: Value) -> f64 { |
| 144 | // SAFETY: caller guarantees a live Float. |
| 145 | unsafe { Block::fields_ptr(v as *mut Block).cast::<f64>().read() } |
| 146 | } |
| 147 | |
| 148 | macro_rules! float_op { |
| 149 | ($name:ident, $op:tt) => { |
| 150 | /// # Safety |
| 151 | /// Both arguments must be live Floats. |
| 152 | #[unsafe(no_mangle)] |
| 153 | pub unsafe extern "C" fn $name(ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 154 | // SAFETY: caller guarantees live Floats. |
| 155 | let (x, y) = unsafe { (tl_float_get(a), tl_float_get(b)) }; |
| 156 | tl_float_from(ctx, x $op y) |
| 157 | } |
| 158 | }; |
| 159 | } |
| 160 | float_op!(tl_float_add, +); |
| 161 | float_op!(tl_float_sub, -); |
| 162 | float_op!(tl_float_mul, *); |
| 163 | float_op!(tl_float_div, /); |
| 164 | float_op!(tl_float_rem, %); |
| 165 | |
| 166 | /// # Safety |
| 167 | /// Both arguments must be live Floats. |
| 168 | #[unsafe(no_mangle)] |
| 169 | pub unsafe extern "C" fn tl_float_eq(_ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 170 | // SAFETY: caller guarantees live Floats. |
| 171 | boolean(unsafe { tl_float_get(a) == tl_float_get(b) }) |
| 172 | } |
| 173 | |
| 174 | /// # Safety |
| 175 | /// Both arguments must be live Floats. |
| 176 | #[unsafe(no_mangle)] |
| 177 | pub unsafe extern "C" fn tl_float_compare(_ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 178 | // SAFETY: caller guarantees live Floats. |
| 179 | let (x, y) = unsafe { (tl_float_get(a), tl_float_get(b)) }; |
| 180 | order(x.partial_cmp(&y).unwrap_or(Order::Equal)) |
| 181 | } |
| 182 | |
| 183 | /// # Safety |
| 184 | /// `a` must be a live Float. |
| 185 | #[unsafe(no_mangle)] |
| 186 | pub unsafe extern "C" fn tl_float_show(ctx: *mut Ctx, a: Value) -> Value { |
| 187 | // SAFETY: caller guarantees a live Float. |
| 188 | string(ctx, &format!("{:?}", unsafe { tl_float_get(a) })) |
| 189 | } |
| 190 | |
| 191 | /// # Safety |
| 192 | /// `a` must be a live Float. |
| 193 | #[unsafe(no_mangle)] |
| 194 | pub unsafe extern "C" fn tl_hash_float(_ctx: *mut Ctx, a: Value) -> Value { |
| 195 | // SAFETY: caller guarantees a live Float. |
| 196 | int(mix(unsafe { tl_float_get(a) }.to_bits())) |
| 197 | } |
| 198 | |
| 199 | /// `float.to_bits(x)`: the IEEE 754 bits of a Float, in `[0, 2^64)` (a big |
| 200 | /// integer above 2^62). NaN payloads are kept. |
| 201 | /// |
| 202 | /// # Safety |
| 203 | /// `a` must be a live Float. |
| 204 | #[unsafe(no_mangle)] |
| 205 | pub unsafe extern "C" fn tl_float_to_bits(ctx: *mut Ctx, a: Value) -> Value { |
| 206 | // SAFETY: caller guarantees a live Float. |
| 207 | let bits = unsafe { tl_float_get(a) }.to_bits(); |
| 208 | crate::int::from_i128(ctx, i128::from(bits)) |
| 209 | } |
| 210 | |
| 211 | /// `float.from_bits(n)`: the Float whose bits are the low 64 bits of `n`. |
| 212 | /// |
| 213 | /// # Safety |
| 214 | /// `n` must be a live `Int`. |
| 215 | #[unsafe(no_mangle)] |
| 216 | pub unsafe extern "C" fn tl_float_from_bits(ctx: *mut Ctx, n: Value) -> Value { |
| 217 | // SAFETY: caller guarantees a live Int. |
| 218 | tl_float_from(ctx, f64::from_bits(unsafe { crate::int::low_u64(n) })) |
| 219 | } |
| 220 | |
| 221 | /// `float.to_bits32(x)`: `x` rounded to a single, its 32 bits. |
| 222 | /// |
| 223 | /// # Safety |
| 224 | /// `a` must be a live Float. |
| 225 | #[unsafe(no_mangle)] |
| 226 | pub unsafe extern "C" fn tl_float_to_bits32(_ctx: *mut Ctx, a: Value) -> Value { |
| 227 | // SAFETY: caller guarantees a live Float. |
| 228 | int(i64::from(louise_rt_prims::f64_to_f32_bits(unsafe { |
| 229 | tl_float_get(a) |
| 230 | }))) |
| 231 | } |
| 232 | |
| 233 | /// `float.from_bits32(n)`: the single whose bits are the low 32 bits of |
| 234 | /// `n`, as a Float. |
| 235 | /// |
| 236 | /// # Safety |
| 237 | /// `n` must be a live `Int`. |
| 238 | #[unsafe(no_mangle)] |
| 239 | pub unsafe extern "C" fn tl_float_from_bits32(ctx: *mut Ctx, n: Value) -> Value { |
| 240 | // SAFETY: caller guarantees a live Int. |
| 241 | let low = unsafe { crate::int::low_u64(n) } as u32; |
| 242 | tl_float_from(ctx, louise_rt_prims::f32_bits_to_f64(low)) |
| 243 | } |
| 244 | |
| 245 | // ---- ints (see `int.rs` for arithmetic) ------------------------------------------ |
| 246 | |
| 247 | /// Mix two hashes (`hash_combine`). |
| 248 | /// |
| 249 | /// # Safety |
| 250 | /// `a` and `b` must be live `Int`s. |
| 251 | #[unsafe(no_mangle)] |
| 252 | pub unsafe extern "C" fn tl_hash_combine(_ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 253 | // SAFETY: caller guarantees live Ints. |
| 254 | int(unsafe { hash_combine(saturating(a), saturating(b)) }) |
| 255 | } |
| 256 | |
| 257 | // ---- strings -------------------------------------------------------------------- |
| 258 | |
| 259 | /// # Safety |
| 260 | /// Both arguments must be live strings. |
| 261 | #[unsafe(no_mangle)] |
| 262 | pub unsafe extern "C" fn tl_string_eq(_ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 263 | // SAFETY: caller guarantees live strings. |
| 264 | boolean(unsafe { string_bytes(a) == string_bytes(b) }) |
| 265 | } |
| 266 | |
| 267 | /// ASCII case-insensitive equality (what `std/http`'s header lookup |
| 268 | /// wants, without lowercasing either side). |
| 269 | /// |
| 270 | /// # Safety |
| 271 | /// Both arguments must be live strings. |
| 272 | #[unsafe(no_mangle)] |
| 273 | pub unsafe extern "C" fn tl_string_eq_ignore_ascii_case( |
| 274 | _ctx: *mut Ctx, |
| 275 | a: Value, |
| 276 | b: Value, |
| 277 | ) -> Value { |
| 278 | // SAFETY: caller guarantees live strings. |
| 279 | boolean(unsafe { string_bytes(a).eq_ignore_ascii_case(string_bytes(b)) }) |
| 280 | } |
| 281 | |
| 282 | /// # Safety |
| 283 | /// Both arguments must be live strings. |
| 284 | #[unsafe(no_mangle)] |
| 285 | pub unsafe extern "C" fn tl_string_compare(_ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 286 | // SAFETY: caller guarantees live strings. |
| 287 | order(unsafe { text(a).cmp(text(b)) }) |
| 288 | } |
| 289 | |
| 290 | /// # Safety |
| 291 | /// Both arguments must be live strings. |
| 292 | #[unsafe(no_mangle)] |
| 293 | pub unsafe extern "C" fn tl_string_concat(ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 294 | // SAFETY: caller guarantees live strings. |
| 295 | let (x, y) = unsafe { (string_bytes(a), string_bytes(b)) }; |
| 296 | let parts = || [x, y].into_iter(); |
| 297 | let (s, out) = crate::values::string_uninit(ctx, crate::values::joined_len(parts(), 0)); |
| 298 | // SAFETY: `out` has room for both, by the length just computed. |
| 299 | unsafe { crate::values::write_joined(out, parts(), &[]) }; |
| 300 | s |
| 301 | } |
| 302 | |
| 303 | macro_rules! string_test { |
| 304 | ($name:ident, $method:ident) => { |
| 305 | /// # Safety |
| 306 | /// Both arguments must be live strings. |
| 307 | #[unsafe(no_mangle)] |
| 308 | pub unsafe extern "C" fn $name(_ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 309 | // SAFETY: caller guarantees live strings. |
| 310 | boolean(unsafe { text(a).$method(text(b)) }) |
| 311 | } |
| 312 | }; |
| 313 | } |
| 314 | string_test!(tl_string_starts_with, starts_with); |
| 315 | string_test!(tl_string_ends_with, ends_with); |
| 316 | |
| 317 | /// Whether `needle` occurs in `a` (the shared kernel, |
| 318 | /// `louise_rt_prims::find`). |
| 319 | /// |
| 320 | /// # Safety |
| 321 | /// Both arguments must be live strings. |
| 322 | #[unsafe(no_mangle)] |
| 323 | pub unsafe extern "C" fn tl_string_contains(_ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 324 | // SAFETY: caller guarantees live strings. |
| 325 | boolean(louise_rt_prims::find(unsafe { string_bytes(a) }, unsafe { string_bytes(b) }).is_some()) |
| 326 | } |
| 327 | |
| 328 | /// `Some(i)` with `i` the byte index of `b`'s first occurrence in `a`, |
| 329 | /// or `None` (the shared kernel, `louise_rt_prims::find`). |
| 330 | /// |
| 331 | /// # Safety |
| 332 | /// Both arguments must be live strings. |
| 333 | #[unsafe(no_mangle)] |
| 334 | pub unsafe extern "C" fn tl_string_find(ctx: *mut Ctx, a: Value, b: Value) -> Value { |
| 335 | // SAFETY: caller guarantees live strings. |
| 336 | match louise_rt_prims::find(unsafe { string_bytes(a) }, unsafe { string_bytes(b) }) { |
| 337 | Some(i) => cell(ctx, 1, &[int(i as i64)]), |
| 338 | None => int(0), |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /// # Safety |
| 343 | /// `a` must be a live string. |
| 344 | #[unsafe(no_mangle)] |
| 345 | pub unsafe extern "C" fn tl_string_byte_length(_ctx: *mut Ctx, a: Value) -> Value { |
| 346 | // SAFETY: caller guarantees a live string. |
| 347 | int(unsafe { string_bytes(a) }.len() as i64) |
| 348 | } |
| 349 | |
| 350 | /// # Safety |
| 351 | /// `a` must be a live string. |
| 352 | #[unsafe(no_mangle)] |
| 353 | pub unsafe extern "C" fn tl_string_to_lower(ctx: *mut Ctx, a: Value) -> Value { |
| 354 | // SAFETY: caller guarantees a live string. |
| 355 | let x = unsafe { string_bytes(a) }; |
| 356 | let (s, out) = crate::values::string_uninit(ctx, x.len()); |
| 357 | // SAFETY: `out` has room for `x`, which ASCII lowercasing keeps the |
| 358 | // same length (and valid UTF-8). |
| 359 | unsafe { |
| 360 | std::ptr::copy_nonoverlapping(x.as_ptr(), out, x.len()); |
| 361 | std::slice::from_raw_parts_mut(out, x.len()).make_ascii_lowercase(); |
| 362 | } |
| 363 | s |
| 364 | } |
| 365 | |
| 366 | /// # Safety |
| 367 | /// `a` must be a live string. |
| 368 | #[unsafe(no_mangle)] |
| 369 | pub unsafe extern "C" fn tl_string_trim(ctx: *mut Ctx, a: Value) -> Value { |
| 370 | // SAFETY: caller guarantees a live string. |
| 371 | string(ctx, unsafe { text(a) }.trim()) |
| 372 | } |
| 373 | |
| 374 | /// The parts of `a` between occurrences of `sep`, as a `List<String>`. |
| 375 | /// |
| 376 | /// # Safety |
| 377 | /// Both arguments must be live strings. |
| 378 | #[unsafe(no_mangle)] |
| 379 | pub unsafe extern "C" fn tl_string_split(ctx: *mut Ctx, a: Value, sep: Value) -> Value { |
| 380 | // SAFETY: caller guarantees live strings. |
| 381 | let parts = unsafe { split(text(a), text(sep)) }; |
| 382 | list(ctx, parts.iter().map(|p| string(ctx, p))) |
| 383 | } |
| 384 | |
| 385 | /// The characters of `a`, as a `List<String>` of single-character strings. |
| 386 | /// |
| 387 | /// # Safety |
| 388 | /// `a` must be a live string. |
| 389 | #[unsafe(no_mangle)] |
| 390 | pub unsafe extern "C" fn tl_string_chars(ctx: *mut Ctx, a: Value) -> Value { |
| 391 | // SAFETY: caller guarantees a live string. |
| 392 | let parts = chars(unsafe { text(a) }); |
| 393 | list(ctx, parts.iter().map(|p| string(ctx, p))) |
| 394 | } |
| 395 | |
| 396 | /// The characters of `s` from `start` up to `end` (excluded), both |
| 397 | /// clamped to `0..=length(s)`; empty when `end <= start` (`string.slice`). |
| 398 | /// |
| 399 | /// # Safety |
| 400 | /// `s` must be a live string; `start` and `end` live Ints. |
| 401 | #[unsafe(no_mangle)] |
| 402 | pub unsafe extern "C" fn tl_string_slice( |
| 403 | ctx: *mut Ctx, |
| 404 | s: Value, |
| 405 | start: Value, |
| 406 | end: Value, |
| 407 | ) -> Value { |
| 408 | // SAFETY: caller guarantees a live string and live Ints. |
| 409 | let (text, start, end) = unsafe { (text(s), saturating(start), saturating(end)) }; |
| 410 | string(ctx, louise_rt_prims::char_slice(text, start, end)) |
| 411 | } |
| 412 | |
| 413 | /// `a` (Bytes) in base64 (RFC 4648). |
| 414 | /// |
| 415 | /// # Safety |
| 416 | /// `a` must be live `Bytes`. |
| 417 | #[unsafe(no_mangle)] |
| 418 | pub unsafe extern "C" fn tl_base64_encode(ctx: *mut Ctx, a: Value) -> Value { |
| 419 | // SAFETY: caller guarantees live Bytes. |
| 420 | let encoded = louise_rt_prims::base64_encode(unsafe { string_bytes(a) }); |
| 421 | string(ctx, &encoded) |
| 422 | } |
| 423 | |
| 424 | /// The bytes of a base64 text, or `Error` with why it is not one. |
| 425 | /// |
| 426 | /// # Safety |
| 427 | /// `a` must be a live string. |
| 428 | #[unsafe(no_mangle)] |
| 429 | pub unsafe extern "C" fn tl_base64_decode(ctx: *mut Ctx, a: Value) -> Value { |
| 430 | // SAFETY: caller guarantees a live string. |
| 431 | match louise_rt_prims::base64_decode(unsafe { text(a) }) { |
| 432 | Ok(bytes) => { |
| 433 | let b = bytes_from(ctx, &bytes); |
| 434 | ok(ctx, b) |
| 435 | } |
| 436 | Err(message) => error(ctx, message), |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /// `(True, f)` if `a` is a float in text, else `(False, 0.0)`. |
| 441 | /// |
| 442 | /// # Safety |
| 443 | /// `a` must be a live string. |
| 444 | #[unsafe(no_mangle)] |
| 445 | pub unsafe extern "C" fn tl_float_parse(ctx: *mut Ctx, a: Value) -> Value { |
| 446 | // SAFETY: caller guarantees a live string. |
| 447 | match louise_rt_prims::parse_float(unsafe { text(a) }) { |
| 448 | Ok(f) => cell(ctx, 0, &[boolean(true), tl_float_from(ctx, f)]), |
| 449 | Err(_) => cell(ctx, 0, &[boolean(false), tl_float_from(ctx, 0.0)]), |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /// `(True, n)` if `a` is a decimal Int (optional sign), else `(False, 0)`. |
| 454 | /// |
| 455 | /// # Safety |
| 456 | /// `a` must be a live string. |
| 457 | #[unsafe(no_mangle)] |
| 458 | pub unsafe extern "C" fn tl_string_parse_int(ctx: *mut Ctx, a: Value) -> Value { |
| 459 | // SAFETY: caller guarantees a live string. |
| 460 | let parsed = crate::int::parse(unsafe { text(a) }); |
| 461 | let n = parsed |
| 462 | .as_ref() |
| 463 | .map_or(int(0), |n| crate::int::from_big(ctx, n)); |
| 464 | cell(ctx, 0, &[boolean(parsed.is_some()), n]) |
| 465 | } |
| 466 | |
| 467 | /// The number of characters (Unicode scalar values). |
| 468 | /// |
| 469 | /// # Safety |
| 470 | /// `a` must be a live string. |
| 471 | #[unsafe(no_mangle)] |
| 472 | pub unsafe extern "C" fn tl_string_length(_ctx: *mut Ctx, a: Value) -> Value { |
| 473 | // SAFETY: caller guarantees a live string. |
| 474 | int(unsafe { text(a) }.chars().count() as i64) |
| 475 | } |
| 476 | |
| 477 | /// The string as a quoted, escaped literal (Rust's `{:?}`). |
| 478 | /// |
| 479 | /// # Safety |
| 480 | /// `a` must be a live string. |
| 481 | #[unsafe(no_mangle)] |
| 482 | pub unsafe extern "C" fn tl_string_debug(ctx: *mut Ctx, a: Value) -> Value { |
| 483 | // SAFETY: caller guarantees a live string. |
| 484 | string(ctx, &format!("{:?}", unsafe { text(a) })) |
| 485 | } |
| 486 | |
| 487 | /// # Safety |
| 488 | /// `a` must be a live string. |
| 489 | #[unsafe(no_mangle)] |
| 490 | pub unsafe extern "C" fn tl_hash_string(_ctx: *mut Ctx, a: Value) -> Value { |
| 491 | // SAFETY: caller guarantees a live string. |
| 492 | int(hash_bytes(unsafe { string_bytes(a) })) |
| 493 | } |
| 494 | |
| 495 | /// The strings of `parts` (a `List<String>`) with `sep` between them, in |
| 496 | /// one allocation. |
| 497 | /// |
| 498 | /// # Safety |
| 499 | /// `parts` must be a live list of strings; `sep` a live string. |
| 500 | #[unsafe(no_mangle)] |
| 501 | pub unsafe extern "C" fn tl_string_join(ctx: *mut Ctx, parts: Value, sep: Value) -> Value { |
| 502 | // Two passes over the list: the length, then the bytes, straight into |
| 503 | // the string (issue #129). |
| 504 | // SAFETY: caller guarantees a list of strings and a string, live while |
| 505 | // the new string is built. |
| 506 | let (items, sep) = unsafe { |
| 507 | ( |
| 508 | || crate::values::list_items(parts).map(|p| string_bytes(p)), |
| 509 | string_bytes(sep), |
| 510 | ) |
| 511 | }; |
| 512 | let (s, out) = crate::values::string_uninit(ctx, crate::values::joined_len(items(), sep.len())); |
| 513 | // SAFETY: `out` has room for the joined length of the same parts. |
| 514 | unsafe { crate::values::write_joined(out, items(), sep) }; |
| 515 | s |
| 516 | } |
| 517 | |
| 518 | /// `a`, escaped for HTML (`html.escape`). Consumes `a` (an `owned` |
| 519 | /// extern): a string without metacharacters — the common case — is |
| 520 | /// returned as is; otherwise the escaped form is written straight into |
| 521 | /// one fresh string, without an intermediate `String`. |
| 522 | /// |
| 523 | /// # Safety |
| 524 | /// `a` must be a live string (owned). |
| 525 | #[unsafe(no_mangle)] |
| 526 | pub unsafe extern "C" fn tl_html_escape(ctx: *mut Ctx, a: Value) -> Value { |
| 527 | // SAFETY: caller guarantees a live string. |
| 528 | let s = unsafe { text(a) }; |
| 529 | let Some(len) = html_escaped_len(s) else { |
| 530 | return a; |
| 531 | }; |
| 532 | let (out, at) = crate::values::string_uninit(ctx, len); |
| 533 | // SAFETY: `at` has room for the escaped length just computed, over the |
| 534 | // same chunks. |
| 535 | unsafe { crate::values::write_joined(at, html_escaped(s), &[]) }; |
| 536 | // SAFETY: `a` is owned and not used again. |
| 537 | unsafe { drop_value(ctx, a) }; |
| 538 | out |
| 539 | } |
| 540 | |
| 541 | /// Print to stdout, through the console (ADR 0022, as `tl_println`). |
| 542 | /// |
| 543 | /// # Safety |
| 544 | /// `ctx` must be the running actor's context (or null); `a` a live string. |
| 545 | #[unsafe(no_mangle)] |
| 546 | pub unsafe extern "C" fn tl_print(ctx: *mut Ctx, a: Value) -> Value { |
| 547 | // SAFETY: caller guarantees its context and a live string. |
| 548 | unsafe { console::write(ctx, console::Stream::Out, &[string_bytes(a)]) }; |
| 549 | UNIT |
| 550 | } |
| 551 | |
| 552 | /// Print a line to stderr, through the console (ADR 0022, as |
| 553 | /// `tl_println`). |
| 554 | /// |
| 555 | /// # Safety |
| 556 | /// `ctx` must be the running actor's context (or null); `a` a live string. |
| 557 | #[unsafe(no_mangle)] |
| 558 | pub unsafe extern "C" fn tl_eprintln(ctx: *mut Ctx, a: Value) -> Value { |
| 559 | // SAFETY: caller guarantees its context and a live string. |
| 560 | unsafe { console::write(ctx, console::Stream::Err, &[string_bytes(a), b"\n"]) }; |
| 561 | UNIT |
| 562 | } |
| 563 | |
| 564 | // ---- environment and URLs --------------------------------------------------------- |
| 565 | |
| 566 | /// `(True, value)` of an environment variable, or `(False, "")`. |
| 567 | /// |
| 568 | /// # Safety |
| 569 | /// `name` must be a live string. |
| 570 | #[unsafe(no_mangle)] |
| 571 | pub unsafe extern "C" fn tl_os_env(ctx: *mut Ctx, name: Value) -> Value { |
| 572 | // SAFETY: caller guarantees a live string. |
| 573 | let found = std::env::var(unsafe { text(name) }).ok(); |
| 574 | let value = string(ctx, found.as_deref().unwrap_or("")); |
| 575 | cell(ctx, 0, &[boolean(found.is_some()), value]) |
| 576 | } |
| 577 | |
| 578 | /// The process's command-line arguments (argv), the program's name first. |
| 579 | /// An argument that is not valid UTF-8 is lossy-decoded. |
| 580 | /// |
| 581 | /// # Safety |
| 582 | /// `ctx` must be the calling actor's context. |
| 583 | #[unsafe(no_mangle)] |
| 584 | pub unsafe extern "C" fn tl_os_args(ctx: *mut Ctx) -> Value { |
| 585 | let args: Vec<Value> = std::env::args_os() |
| 586 | .map(|a| string(ctx, &a.to_string_lossy())) |
| 587 | .collect(); |
| 588 | list(ctx, args.into_iter()) |
| 589 | } |
| 590 | |
| 591 | /// Exit the process with `code`, once everything printed so far is |
| 592 | /// written (as when `main` ends; C's `exit` also runs the console's |
| 593 | /// `atexit` flush). Never returns. |
| 594 | /// |
| 595 | /// # Safety |
| 596 | /// `code` must be a live `Int`. |
| 597 | #[unsafe(no_mangle)] |
| 598 | pub unsafe extern "C" fn tl_os_exit(_ctx: *mut Ctx, code: Value) -> Value { |
| 599 | console::flush(); |
| 600 | // SAFETY: caller guarantees a live Int; a big integer saturates. |
| 601 | std::process::exit(unsafe { crate::int::saturating(code) } as i32) |
| 602 | } |
| 603 | |
| 604 | /// `s`, URL-decoded (`http.url_decode`). Consumes `s` (an `owned` |
| 605 | /// extern): a string without `%` or `+` is returned as is; otherwise the |
| 606 | /// decoded form is written straight into one fresh string (it is never |
| 607 | /// longer than `s`), without the intermediate buffers of the kernel. |
| 608 | /// |
| 609 | /// # Safety |
| 610 | /// `s` must be a live string (owned). |
| 611 | #[unsafe(no_mangle)] |
| 612 | pub unsafe extern "C" fn tl_url_decode(ctx: *mut Ctx, s: Value) -> Value { |
| 613 | // SAFETY: caller guarantees a live string. |
| 614 | let text = unsafe { text(s) }; |
| 615 | if !louise_rt_prims::url_needs_decode(text) { |
| 616 | return s; |
| 617 | } |
| 618 | let (out, at) = crate::values::string_uninit(ctx, text.len()); |
| 619 | // SAFETY: `at` has room for `text.len()` bytes. |
| 620 | let n = unsafe { |
| 621 | louise_rt_prims::url_decode_into(text, std::slice::from_raw_parts_mut(at, text.len())) |
| 622 | }; |
| 623 | // SAFETY: the first `n` bytes at `at` were just written. |
| 624 | let decoded = unsafe { std::slice::from_raw_parts(at, n) }; |
| 625 | if std::str::from_utf8(decoded).is_ok() { |
| 626 | // SAFETY: `out` is a fresh unique string allocated for |
| 627 | // `text.len()` bytes, its first `n` written. |
| 628 | let out = unsafe { crate::values::string_shrink(out, text.len(), n) }; |
| 629 | // SAFETY: `s` is owned and not used again. |
| 630 | unsafe { drop_value(ctx, s) }; |
| 631 | return out; |
| 632 | } |
| 633 | // Decoded bytes that are not valid UTF-8 (rare): the lossy form can |
| 634 | // be longer than they are, so it is built in a second string. |
| 635 | let fixed = string(ctx, &String::from_utf8_lossy(decoded)); |
| 636 | // SAFETY: both are owned and not used again. |
| 637 | unsafe { |
| 638 | drop_value(ctx, out); |
| 639 | drop_value(ctx, s); |
| 640 | } |
| 641 | fixed |
| 642 | } |
| 643 | |
| 644 | /// `s`, percent-encoded (`http.url_encode`). Consumes `s` (an `owned` |
| 645 | /// extern): a string without a byte to encode is returned as is; |
| 646 | /// otherwise the encoded form is written straight into one fresh string |
| 647 | /// (at most three times `s`'s length), without the kernel's intermediate |
| 648 | /// buffer. |
| 649 | /// |
| 650 | /// # Safety |
| 651 | /// `s` must be a live string (owned). |
| 652 | #[unsafe(no_mangle)] |
| 653 | pub unsafe extern "C" fn tl_url_encode(ctx: *mut Ctx, s: Value) -> Value { |
| 654 | // SAFETY: caller guarantees a live string. |
| 655 | let text = unsafe { text(s) }; |
| 656 | if !louise_rt_prims::url_needs_encode(text) { |
| 657 | return s; |
| 658 | } |
| 659 | let alloced = 3 * text.len(); |
| 660 | let (out, at) = crate::values::string_uninit(ctx, alloced); |
| 661 | // SAFETY: `at` has room for `alloced` bytes. |
| 662 | let n = unsafe { |
| 663 | louise_rt_prims::url_encode_into(text, std::slice::from_raw_parts_mut(at, alloced)) |
| 664 | }; |
| 665 | // SAFETY: `out` is a fresh unique string allocated for `alloced` |
| 666 | // bytes, its first `n` written. |
| 667 | let out = unsafe { crate::values::string_shrink(out, alloced, n) }; |
| 668 | // SAFETY: `s` is owned and not used again. |
| 669 | unsafe { drop_value(ctx, s) }; |
| 670 | out |
| 671 | } |
| 672 | |
| 673 | #[cfg(test)] |
| 674 | mod tests { |
| 675 | use super::*; |
| 676 | use crate::abi::int_get; |
| 677 | use crate::heap::drop_value; |
| 678 | use crate::values::list_items; |
| 679 | |
| 680 | /// The prelude values these externs build: `List` (`Nil` 0, `Cons` 1) |
| 681 | /// and tuples (tag 0). |
| 682 | unsafe extern "C" fn builds_prelude_values(ctx: *mut Ctx) -> Value { |
| 683 | // SAFETY: every value is owned by this test. |
| 684 | unsafe { |
| 685 | let (s, sep, empty) = (string(ctx, "a,b"), string(ctx, ","), string(ctx, "")); |
| 686 | let parts = tl_string_split(ctx, s, sep); |
| 687 | assert_eq!((*(parts as *mut Block)).header.tag, 1); |
| 688 | let texts: Vec<&str> = list_items(parts).map(|p| text(p)).collect(); |
| 689 | assert_eq!(texts, ["a", "b"]); |
| 690 | let none = tl_string_split(ctx, empty, sep); |
| 691 | assert_eq!(list_items(none).map(|p| text(p)).collect::<Vec<_>>(), [""]); |
| 692 | let n = string(ctx, "-42"); |
| 693 | let parsed = tl_string_parse_int(ctx, n); |
| 694 | let fields = Block::fields_ptr(parsed as *mut Block); |
| 695 | assert_eq!((*(parsed as *mut Block)).header.tag, 0); |
| 696 | assert_eq!((*fields, int_get(*fields.add(1))), (boolean(true), -42)); |
| 697 | let bad = tl_string_parse_int(ctx, sep); |
| 698 | let fields = Block::fields_ptr(bad as *mut Block); |
| 699 | assert_eq!((*fields, *fields.add(1)), (boolean(false), int(0))); |
| 700 | for v in [s, sep, empty, parts, none, n, parsed, bad] { |
| 701 | drop_value(ctx, v); |
| 702 | } |
| 703 | } |
| 704 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 705 | UNIT |
| 706 | } |
| 707 | |
| 708 | /// Strings joined and concatenated in place (issue #129): lengths, NUL |
| 709 | /// terminators and contents, and nothing leaked. |
| 710 | unsafe extern "C" fn builds_strings_in_place(ctx: *mut Ctx) -> Value { |
| 711 | // SAFETY: every value is owned by this test. |
| 712 | unsafe { |
| 713 | let check = |s: Value, want: &str| { |
| 714 | assert_eq!(text(s), want); |
| 715 | assert_eq!(*string_bytes(s).as_ptr().add(want.len()), 0, "NUL"); |
| 716 | drop_value(ctx, s); |
| 717 | }; |
| 718 | let big = "é".repeat(500_000); |
| 719 | for (parts, sep, want) in [ |
| 720 | (vec![], ", ", String::new()), |
| 721 | (vec![""], ", ", String::new()), |
| 722 | (vec!["a"], ", ", "a".into()), |
| 723 | (vec!["a", "b", "c"], ", ", "a, b, c".into()), |
| 724 | (vec!["a", "", "c"], "", "ac".into()), |
| 725 | (vec!["日本", "語"], "·", "日本·語".into()), |
| 726 | (vec![big.as_str(), "!"], "", format!("{big}!")), |
| 727 | ] { |
| 728 | let list = list(ctx, parts.iter().map(|p| string(ctx, p))); |
| 729 | let sep = string(ctx, sep); |
| 730 | check(tl_string_join(ctx, list, sep), &want); |
| 731 | drop_value(ctx, list); |
| 732 | drop_value(ctx, sep); |
| 733 | } |
| 734 | let (x, y) = (string(ctx, "ab"), string(ctx, "çd")); |
| 735 | check(tl_string_concat(ctx, x, y), "abçd"); |
| 736 | check(tl_string_to_lower(ctx, y), "çd"); |
| 737 | let shout = string(ctx, "HeLLo, Wörld"); |
| 738 | check(tl_string_to_lower(ctx, shout), "hello, wörld"); |
| 739 | for v in [x, y, shout] { |
| 740 | drop_value(ctx, v); |
| 741 | } |
| 742 | } |
| 743 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 744 | UNIT |
| 745 | } |
| 746 | |
| 747 | /// HTML escaping and URL decoding: a clean input is returned as is |
| 748 | /// (the same block, no allocation); a dirty one is built in place, |
| 749 | /// with its NUL, and the input released (the leak oracle checks). |
| 750 | unsafe extern "C" fn escapes_and_decodes(ctx: *mut Ctx) -> Value { |
| 751 | // SAFETY: every value is owned by this test. |
| 752 | unsafe { |
| 753 | for (input, want) in [ |
| 754 | // Clean: the five metacharacters absent (unicode incl.). |
| 755 | ("", ""), |
| 756 | ("plain", "plain"), |
| 757 | ("héllo 🦀", "héllo 🦀"), |
| 758 | // Each metacharacter, alone and combined. |
| 759 | ("&", "&"), |
| 760 | ("<>&\"'", "<>&"'"), |
| 761 | ( |
| 762 | r#"<a href="x">Tom & 'Jerry'</a>"#, |
| 763 | "<a href="x">Tom & 'Jerry'</a>", |
| 764 | ), |
| 765 | // Escaped text is escaped again. |
| 766 | ("&", "&amp;"), |
| 767 | // Multibyte characters around a metacharacter. |
| 768 | ("日本<語", "日本<語"), |
| 769 | ] { |
| 770 | let s = string(ctx, input); |
| 771 | let out = tl_html_escape(ctx, s); |
| 772 | assert_eq!(text(out), want, "escape {input:?}"); |
| 773 | assert_eq!(*string_bytes(out).as_ptr().add(want.len()), 0, "NUL"); |
| 774 | let clean = html_escaped_len(input).is_none(); |
| 775 | assert_eq!(out == s, clean, "clean returned as is: {input:?}"); |
| 776 | drop_value(ctx, out); |
| 777 | } |
| 778 | for (input, want) in [ |
| 779 | // Clean: no `%`, no `+` (a space is not a `+`). |
| 780 | ("", ""), |
| 781 | ("plain text", "plain text"), |
| 782 | ("é", "é"), |
| 783 | // `+` and escapes, at the edges too. |
| 784 | ("a+b%20c", "a b c"), |
| 785 | ("+a+", " a "), |
| 786 | ("%41%42%43", "ABC"), |
| 787 | ("%00", "\0"), |
| 788 | ("%C3%A9", "é"), |
| 789 | ("%c3%a9", "é"), |
| 790 | // Invalid escapes stay; invalid UTF-8 becomes U+FFFD. |
| 791 | ("%", "%"), |
| 792 | ("%2", "%2"), |
| 793 | ("%2G", "%2G"), |
| 794 | ("%zz", "%zz"), |
| 795 | ("%%41", "%A"), |
| 796 | ("%ff", "\u{fffd}"), |
| 797 | ("%C3", "\u{fffd}"), |
| 798 | ("%A9%C3", "\u{fffd}\u{fffd}"), |
| 799 | ] { |
| 800 | let s = string(ctx, input); |
| 801 | let out = tl_url_decode(ctx, s); |
| 802 | assert_eq!(text(out), want, "decode {input:?}"); |
| 803 | assert_eq!(*string_bytes(out).as_ptr().add(want.len()), 0, "NUL"); |
| 804 | let clean = !louise_rt_prims::url_needs_decode(input); |
| 805 | assert_eq!(out == s, clean, "clean returned as is: {input:?}"); |
| 806 | drop_value(ctx, out); |
| 807 | } |
| 808 | } |
| 809 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 810 | UNIT |
| 811 | } |
| 812 | |
| 813 | #[test] |
| 814 | fn escaping_and_decoding_are_in_place() { |
| 815 | // SAFETY: the test follows the generated-code convention. |
| 816 | let code = unsafe { crate::sched::run_test_actor(escapes_and_decodes) }; |
| 817 | assert_eq!(code, 0); |
| 818 | } |
| 819 | |
| 820 | /// URL encoding and string slicing: a clean encode input is returned |
| 821 | /// as is (the same block, no allocation); a dirty one is built in |
| 822 | /// place, with its NUL, and the input released. Slices borrow their |
| 823 | /// input and copy the range. The leak oracle checks both. |
| 824 | unsafe extern "C" fn encodes_and_slices(ctx: *mut Ctx) -> Value { |
| 825 | // SAFETY: every value is owned by this test. |
| 826 | unsafe { |
| 827 | for (input, want) in [ |
| 828 | // Clean: only unreserved bytes (unicode excluded). |
| 829 | ("", ""), |
| 830 | ("plain-text_1.0~x", "plain-text_1.0~x"), |
| 831 | // Each kind of escape: space, reserved, `%`, unicode. |
| 832 | ("a b", "a%20b"), |
| 833 | ("=&?/#%+", "%3D%26%3F%2F%23%25%2B"), |
| 834 | ("%41", "%2541"), |
| 835 | ("é", "%C3%A9"), |
| 836 | ("héllo 🦀", "h%C3%A9llo%20%F0%9F%A6%80"), |
| 837 | ] { |
| 838 | let s = string(ctx, input); |
| 839 | let out = tl_url_encode(ctx, s); |
| 840 | assert_eq!(text(out), want, "encode {input:?}"); |
| 841 | assert_eq!(*string_bytes(out).as_ptr().add(want.len()), 0, "NUL"); |
| 842 | let clean = !louise_rt_prims::url_needs_encode(input); |
| 843 | assert_eq!(out == s, clean, "clean returned as is: {input:?}"); |
| 844 | drop_value(ctx, out); |
| 845 | } |
| 846 | for (input, start, end, want) in [ |
| 847 | ("hello", 0, 5, "hello"), |
| 848 | ("hello", 1, 4, "ell"), |
| 849 | // Multibyte characters are indexed, not their bytes. |
| 850 | ("héllo", 1, 3, "él"), |
| 851 | ("日本語", 1, 2, "本"), |
| 852 | // Clamping and the empty cases. |
| 853 | ("héllo", -3, 2, "hé"), |
| 854 | ("héllo", 3, 100, "lo"), |
| 855 | ("héllo", 4, 2, ""), |
| 856 | ("héllo", 7, 9, ""), |
| 857 | ] { |
| 858 | let s = string(ctx, input); |
| 859 | let out = tl_string_slice(ctx, s, int(start), int(end)); |
| 860 | assert_eq!(text(out), want, "slice {input:?} {start}..{end}"); |
| 861 | assert_eq!(*string_bytes(out).as_ptr().add(want.len()), 0, "NUL"); |
| 862 | // The input is borrowed: it outlives the call. |
| 863 | assert_eq!(text(s), input); |
| 864 | drop_value(ctx, s); |
| 865 | drop_value(ctx, out); |
| 866 | } |
| 867 | } |
| 868 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 869 | UNIT |
| 870 | } |
| 871 | |
| 872 | #[test] |
| 873 | fn encoding_and_slicing_are_in_place() { |
| 874 | // SAFETY: the test follows the generated-code convention. |
| 875 | let code = unsafe { crate::sched::run_test_actor(encodes_and_slices) }; |
| 876 | assert_eq!(code, 0); |
| 877 | } |
| 878 | |
| 879 | #[test] |
| 880 | fn strings_are_built_in_place() { |
| 881 | // SAFETY: the test follows the generated-code convention. |
| 882 | let code = unsafe { crate::sched::run_test_actor(builds_strings_in_place) }; |
| 883 | assert_eq!(code, 0); |
| 884 | } |
| 885 | |
| 886 | /// `tl_string_find` and `tl_string_contains`: `Some(i)`/`None` built |
| 887 | /// as the prelude's `Option`, nothing leaked. |
| 888 | unsafe extern "C" fn finds_substrings(ctx: *mut Ctx) -> Value { |
| 889 | // SAFETY: every value is owned by this test. |
| 890 | unsafe { |
| 891 | for (haystack, needle, want) in [ |
| 892 | ("louise", "uis", Some(2i64)), |
| 893 | ("louise", "e", Some(5)), |
| 894 | ("louise", "x", None), |
| 895 | ("louise", "", Some(0)), |
| 896 | ("héllo", "llo", Some(3)), // a byte index |
| 897 | ("aaaa", "aa", Some(0)), |
| 898 | ("ab", "abc", None), |
| 899 | ("", "", Some(0)), |
| 900 | ] { |
| 901 | let (s, n) = (string(ctx, haystack), string(ctx, needle)); |
| 902 | let found = tl_string_find(ctx, s, n); |
| 903 | match want { |
| 904 | Some(i) => { |
| 905 | assert_eq!((*(found as *mut Block)).header.tag, 1, "Some"); |
| 906 | assert_eq!(int_get(*Block::fields_ptr(found as *mut Block)), i); |
| 907 | } |
| 908 | None => assert_eq!(found, int(0), "None"), |
| 909 | } |
| 910 | assert_eq!(tl_string_contains(ctx, s, n), boolean(want.is_some())); |
| 911 | drop_value(ctx, found); |
| 912 | drop_value(ctx, s); |
| 913 | drop_value(ctx, n); |
| 914 | } |
| 915 | } |
| 916 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 917 | UNIT |
| 918 | } |
| 919 | |
| 920 | #[test] |
| 921 | fn substrings_are_found() { |
| 922 | // SAFETY: the test follows the generated-code convention. |
| 923 | let code = unsafe { crate::sched::run_test_actor(finds_substrings) }; |
| 924 | assert_eq!(code, 0); |
| 925 | } |
| 926 | |
| 927 | #[test] |
| 928 | fn externs_build_prelude_values() { |
| 929 | // SAFETY: the test follows the generated-code convention. |
| 930 | let code = unsafe { crate::sched::run_test_actor(builds_prelude_values) }; |
| 931 | assert_eq!(code, 0); |
| 932 | } |
| 933 | } |