| @@ -13,8 +13,8 @@ use tilrt_prims::{ |
| 13 | 13 | hash_bytes, hash_combine, html_escape, mix, random_below, random_bytes, split, url_decode, |
| 14 | 14 | }; |
| 15 | 15 | |
| 16 | | −use crate::abi::{Block, Ctx, TAG_FLOAT, TAG_REF, TAG_REGION, UNIT, Value, int, is_ptr}; |
| 17 | | −use crate::heap::{drop_value, tl_alloc}; |
| 16 | +use crate::abi::{Block, Ctx, TAG_FLOAT, TAG_REF, TAG_REGION, UNIT, Value, int}; |
| 17 | +use crate::heap::{drop_value, dup_value, tl_alloc}; |
| 18 | 18 | use crate::int::saturating; |
| 19 | 19 | use crate::values::{cell, list, string, string_bytes, text, tl_string_from}; |
| 20 | 20 | |
| @@ -23,7 +23,10 @@ const WORD: usize = size_of::<Value>(); |
| 23 | 23 | // ---- panics ----------------------------------------------------------------- |
| 24 | 24 | |
| 25 | 25 | /// Start unwinding the actor with `message` (see `tl_panic_str`). |
| 26 | | −pub(crate) fn panic_with(ctx: *mut Ctx, message: &str) -> Value { |
| 26 | +/// |
| 27 | +/// # Safety |
| 28 | +/// `ctx` must be the running actor's context. |
| 29 | +pub(crate) unsafe fn panic_with(ctx: *mut Ctx, message: &str) -> Value { |
| 27 | 30 | let c = std::ffi::CString::new(message).unwrap_or_default(); |
| 28 | 31 | // SAFETY: forwarded caller guarantee on `ctx`; `c` is NUL-terminated. |
| 29 | 32 | unsafe { crate::effects::tl_panic_str(ctx, c.as_ptr()) } |
| @@ -37,7 +40,8 @@ pub(crate) fn panic_with(ctx: *mut Ctx, message: &str) -> Value { |
| 37 | 40 | pub unsafe extern "C" fn tl_panic(ctx: *mut Ctx, message: Value) -> Value { |
| 38 | 41 | // SAFETY: caller guarantees a live string. |
| 39 | 42 | let text = unsafe { text(message) }.to_owned(); |
| 40 | | − panic_with(ctx, &text) |
| 43 | + // SAFETY: caller guarantees the running actor's context. |
| 44 | + unsafe { panic_with(ctx, &text) } |
| 41 | 45 | } |
| 42 | 46 | |
| 43 | 47 | /// With the `TILRT_LEAKS` environment variable set, print the number of |
| @@ -67,22 +71,6 @@ pub(crate) fn order(o: Order) -> Value { |
| 67 | 71 | }) |
| 68 | 72 | } |
| 69 | 73 | |
| 70 | | −pub(crate) fn dup(v: Value) -> Value { |
| 71 | | − if is_ptr(v) { |
| 72 | | − let b = v as *mut Block; |
| 73 | | − // SAFETY: a value held by a live block is live. |
| 74 | | − unsafe { |
| 75 | | − let rc = (*b).header.rc; |
| 76 | | − if rc > 0 { |
| 77 | | − (*b).header.rc = rc + 1; |
| 78 | | − } else { |
| 79 | | − crate::heap::tl_dup_shared(b); |
| 80 | | − } |
| 81 | | − } |
| 82 | | − } |
| 83 | | − v |
| 84 | | −} |
| 85 | | − |
| 86 | 74 | /// Call closure `clo` (consumed) with `args` (consumed), following the |
| 87 | 75 | /// generated-code convention: the closure first, five more values in |
| 88 | 76 | /// registers, the rest in `ctx->xargs`. |
| @@ -428,8 +416,8 @@ pub unsafe extern "C" fn tl_ref_new(ctx: *mut Ctx, region: Value, value: Value) |
| 428 | 416 | /// `r` must be a live reference (borrowed). |
| 429 | 417 | #[unsafe(no_mangle)] |
| 430 | 418 | pub unsafe extern "C" fn tl_ref_get(_ctx: *mut Ctx, r: Value) -> Value { |
| 431 | | − // SAFETY: caller guarantees a live reference. |
| 432 | | − dup(unsafe { Block::fields_ptr(r as *mut Block).read() }) |
| 419 | + // SAFETY: caller guarantees a live reference, which holds its value. |
| 420 | + unsafe { dup_value(Block::fields_ptr(r as *mut Block).read()) } |
| 433 | 421 | } |
| 434 | 422 | |
| 435 | 423 | /// # Safety |
| @@ -480,7 +468,7 @@ pub unsafe extern "C" fn tl_region_exit(ctx: *mut Ctx, region: Value) { |
| 480 | 468 | let data = region_data(region); |
| 481 | 469 | if (*ctx).yielding == crate::abi::YIELD_CLONE { |
| 482 | 470 | data.copies += 1; |
| 483 | | − dup(region); |
| 471 | + dup_value(region); |
| 484 | 472 | return; |
| 485 | 473 | } |
| 486 | 474 | if data.copies > 0 { |
| @@ -579,7 +567,8 @@ pub unsafe extern "C" fn tl_random_int(ctx: *mut Ctx, below: Value) -> Value { |
| 579 | 567 | // SAFETY: caller guarantees a live Int. |
| 580 | 568 | let below = unsafe { saturating(below) }; |
| 581 | 569 | if below <= 0 { |
| 582 | | − return panic_with(ctx, "random.int: the bound must be positive"); |
| 570 | + // SAFETY: caller guarantees the running actor's context. |
| 571 | + return unsafe { panic_with(ctx, "random.int: the bound must be positive") }; |
| 583 | 572 | } |
| 584 | 573 | crate::int::from_i128(ctx, i128::from(random_below(below as u64))) |
| 585 | 574 | } |