| @@ -273,6 +273,42 @@ pub unsafe extern "C" fn tl_bytes_to_list(ctx: *mut Ctx, b: Value) -> Value { |
| 273 | 273 | list(ctx, bytes.iter().map(|&b| int(i64::from(b)))) |
| 274 | 274 | } |
| 275 | 275 | |
| 276 | +/// The length of `b`, for C bindings (`tl_bytes_ptr` gives its bytes). |
| 277 | +/// |
| 278 | +/// # Safety |
| 279 | +/// `b` must be live `Bytes` (borrowed). |
| 280 | +#[unsafe(no_mangle)] |
| 281 | +pub unsafe extern "C" fn tl_bytes_len(b: Value) -> usize { |
| 282 | + // SAFETY: caller guarantees live `Bytes`. |
| 283 | + unsafe { bytes_of(b) }.len() |
| 284 | +} |
| 285 | + |
| 286 | +/// The first byte of `b`, for C bindings: `tl_bytes_len(b)` bytes, valid |
| 287 | +/// while `b` lives, not NUL-terminated (`Bytes` may hold zeros). |
| 288 | +/// |
| 289 | +/// # Safety |
| 290 | +/// `b` must be live `Bytes` (borrowed). |
| 291 | +#[unsafe(no_mangle)] |
| 292 | +pub unsafe extern "C" fn tl_bytes_ptr(b: Value) -> *const u8 { |
| 293 | + // SAFETY: caller guarantees live `Bytes`. |
| 294 | + unsafe { bytes_of(b) }.as_ptr() |
| 295 | +} |
| 296 | + |
| 297 | +/// New `Bytes` (owned) holding a copy of the `len` bytes at `bytes`, for C |
| 298 | +/// bindings. `bytes` may be null when `len` is 0. |
| 299 | +/// |
| 300 | +/// # Safety |
| 301 | +/// `ctx` must be the running actor's context; `bytes` valid for reads of |
| 302 | +/// `len` bytes. |
| 303 | +#[unsafe(no_mangle)] |
| 304 | +pub unsafe extern "C" fn tl_bytes_from(ctx: *mut Ctx, bytes: *const u8, len: usize) -> Value { |
| 305 | + if len == 0 { |
| 306 | + return bytes_from(ctx, &[]); |
| 307 | + } |
| 308 | + // SAFETY: caller guarantees `len` readable bytes (non-null when len > 0). |
| 309 | + bytes_from(ctx, unsafe { std::slice::from_raw_parts(bytes, len) }) |
| 310 | +} |
| 311 | + |
| 276 | 312 | #[cfg(test)] |
| 277 | 313 | mod tests { |
| 278 | 314 | use super::*; |
| @@ -364,6 +400,34 @@ mod tests { |
| 364 | 400 | UNIT |
| 365 | 401 | } |
| 366 | 402 | |
| 403 | + /// The C bindings' accessors: a copy in, the same bytes out, zeros |
| 404 | + /// included; an empty copy from a null pointer. |
| 405 | + unsafe extern "C" fn binding_accessors(ctx: *mut Ctx) -> Value { |
| 406 | + // SAFETY: every value is owned by this test. |
| 407 | + unsafe { |
| 408 | + let data = [0u8, 1, 0, 255, 7]; |
| 409 | + let b = tl_bytes_from(ctx, data.as_ptr(), data.len()); |
| 410 | + assert_eq!(tl_bytes_len(b), 5); |
| 411 | + assert_eq!(std::slice::from_raw_parts(tl_bytes_ptr(b), 5), data); |
| 412 | + assert_eq!(int_get(tl_bytes_length(ctx, b)), 5); |
| 413 | + let empty = tl_bytes_from(ctx, std::ptr::null(), 0); |
| 414 | + assert_eq!(tl_bytes_len(empty), 0); |
| 415 | + drop_value(ctx, b); |
| 416 | + drop_value(ctx, empty); |
| 417 | + } |
| 418 | + assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 419 | + UNIT |
| 420 | + } |
| 421 | + |
| 422 | + #[test] |
| 423 | + fn binding_accessors_round_trip() { |
| 424 | + // SAFETY: `binding_accessors` follows the generated-code convention. |
| 425 | + assert_eq!( |
| 426 | + unsafe { crate::sched::run_test_actor(binding_accessors) }, |
| 427 | + 0 |
| 428 | + ); |
| 429 | + } |
| 430 | + |
| 367 | 431 | #[test] |
| 368 | 432 | fn cursor_reads_and_string_ranges() { |
| 369 | 433 | // SAFETY: `scenarios` follows the generated-code convention. |