| @@ -1,5 +1,7 @@ |
| 1 | 1 | //! Functions compiled tilcayo programs call: the standard library's |
| 2 | | −//! `@extern` declarations, boxed floats, and region references. |
| 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`. |
| 3 | 5 | //! |
| 4 | 6 | //! Like every `@extern` function, these **borrow** their arguments and |
| 5 | 7 | //! return an owned result (see `tilc_perceus`). They must behave exactly |
| @@ -13,8 +15,8 @@ use tilrt_prims::{ |
| 13 | 15 | hash_bytes, hash_combine, html_escape, mix, random_below, random_bytes, split, url_decode, |
| 14 | 16 | }; |
| 15 | 17 | |
| 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 | +use crate::abi::{Block, Ctx, TAG_FLOAT, UNIT, Value, int}; |
| 19 | +use crate::heap::tl_alloc; |
| 18 | 20 | use crate::int::saturating; |
| 19 | 21 | use crate::values::{cell, list, string, string_bytes, text, tl_string_from}; |
| 20 | 22 | |
| @@ -349,212 +351,6 @@ pub unsafe extern "C" fn tl_eprintln(_ctx: *mut Ctx, a: Value) -> Value { |
| 349 | 351 | UNIT |
| 350 | 352 | } |
| 351 | 353 | |
| 352 | | −// ---- regions -------------------------------------------------------------------- |
| 353 | | −// |
| 354 | | −// A region block's raw area holds a `Box<RegionData>`: the references made |
| 355 | | −// in the region, each holding one count. Ending the region clears them |
| 356 | | −// (breaking any cycle) and releases them (ADR 0002, Revision 1). |
| 357 | | −// |
| 358 | | −// Copies of a `multi ctl` continuation share the regions it is inside |
| 359 | | −// (ADR 0017): each copy leaves them, and a region ends when the last one |
| 360 | | −// does. |
| 361 | | − |
| 362 | | −struct RegionData { |
| 363 | | − refs: Vec<Value>, |
| 364 | | − /// Copies of the frame leaving the region that are still to leave it. |
| 365 | | − copies: u32, |
| 366 | | −} |
| 367 | | − |
| 368 | | −/// # Safety |
| 369 | | −/// `region` must be a live region block. |
| 370 | | −unsafe fn region_data<'a>(region: Value) -> &'a mut RegionData { |
| 371 | | − // SAFETY: caller guarantees a region; its raw area holds the box. |
| 372 | | − unsafe { |
| 373 | | − &mut *Block::fields_ptr(region as *mut Block) |
| 374 | | − .cast::<*mut RegionData>() |
| 375 | | − .read() |
| 376 | | − } |
| 377 | | −} |
| 378 | | − |
| 379 | | −/// # Safety |
| 380 | | −/// `region` must be a live region block. |
| 381 | | −unsafe fn refs<'a>(region: Value) -> &'a mut Vec<Value> { |
| 382 | | − // SAFETY: forwarded caller guarantee. |
| 383 | | − unsafe { &mut region_data(region).refs } |
| 384 | | −} |
| 385 | | − |
| 386 | | −#[unsafe(no_mangle)] |
| 387 | | −pub extern "C" fn tl_region_new(ctx: *mut Ctx) -> Value { |
| 388 | | − let b = tl_alloc(ctx, TAG_REGION, 0, WORD); |
| 389 | | − let data = Box::into_raw(Box::new(RegionData { |
| 390 | | − refs: Vec::new(), |
| 391 | | − copies: 0, |
| 392 | | − })); |
| 393 | | − // SAFETY: the raw area holds one pointer. |
| 394 | | − unsafe { Block::fields_ptr(b).cast::<*mut RegionData>().write(data) }; |
| 395 | | − b as Value |
| 396 | | −} |
| 397 | | − |
| 398 | | −/// A new reference in `region` (borrowed) holding `value` (consumed). |
| 399 | | −/// |
| 400 | | −/// # Safety |
| 401 | | −/// `region` must be a live region; `value` owned. |
| 402 | | −#[unsafe(no_mangle)] |
| 403 | | −pub unsafe extern "C" fn tl_ref_new(ctx: *mut Ctx, region: Value, value: Value) -> Value { |
| 404 | | − let r = tl_alloc(ctx, TAG_REF, 1, 0); |
| 405 | | − // SAFETY: a fresh block with one scanned field; one count for the |
| 406 | | − // result and one for the region. |
| 407 | | − unsafe { |
| 408 | | − Block::fields_ptr(r).write(value); |
| 409 | | − (*r).header.rc = 2; |
| 410 | | − refs(region).push(r as Value); |
| 411 | | − } |
| 412 | | − r as Value |
| 413 | | −} |
| 414 | | − |
| 415 | | −/// # Safety |
| 416 | | −/// `r` must be a live reference (borrowed). |
| 417 | | −#[unsafe(no_mangle)] |
| 418 | | −pub unsafe extern "C" fn tl_ref_get(_ctx: *mut Ctx, r: Value) -> Value { |
| 419 | | − // SAFETY: caller guarantees a live reference, which holds its value. |
| 420 | | − unsafe { dup_value(Block::fields_ptr(r as *mut Block).read()) } |
| 421 | | −} |
| 422 | | − |
| 423 | | −/// # Safety |
| 424 | | −/// `r` must be a live reference (borrowed); `value` owned. |
| 425 | | −#[unsafe(no_mangle)] |
| 426 | | −pub unsafe extern "C" fn tl_ref_set(ctx: *mut Ctx, r: Value, value: Value) -> Value { |
| 427 | | − // SAFETY: caller guarantees a live reference; the old value was owned |
| 428 | | − // by it. |
| 429 | | − unsafe { |
| 430 | | − let field = Block::fields_ptr(r as *mut Block); |
| 431 | | − let old = field.read(); |
| 432 | | − field.write(value); |
| 433 | | − drop_value(ctx, old); |
| 434 | | − } |
| 435 | | − UNIT |
| 436 | | −} |
| 437 | | − |
| 438 | | −/// End `region` (borrowed): clear and release every reference made in it. |
| 439 | | −/// |
| 440 | | −/// # Safety |
| 441 | | −/// `region` must be a live region. |
| 442 | | −#[unsafe(no_mangle)] |
| 443 | | −pub unsafe extern "C" fn tl_region_end(ctx: *mut Ctx, region: Value) -> Value { |
| 444 | | − // SAFETY: caller guarantees a live region; its references are live |
| 445 | | − // (it holds a count on each). |
| 446 | | − unsafe { |
| 447 | | − for r in std::mem::take(refs(region)) { |
| 448 | | − let field = Block::fields_ptr(r as *mut Block); |
| 449 | | − let old = field.read(); |
| 450 | | − field.write(UNIT); |
| 451 | | − drop_value(ctx, old); |
| 452 | | − drop_value(ctx, r); |
| 453 | | − } |
| 454 | | − } |
| 455 | | − UNIT |
| 456 | | −} |
| 457 | | − |
| 458 | | −/// The frame of `region` (consumed) leaves it, normally or by unwinding. |
| 459 | | −/// In a copying pass over a `multi ctl` continuation (ADR 0017), the copy |
| 460 | | −/// will leave it too: count it, and take a reference for it. |
| 461 | | −/// |
| 462 | | −/// # Safety |
| 463 | | −/// `region` must be a live region, owned. |
| 464 | | −#[unsafe(no_mangle)] |
| 465 | | −pub unsafe extern "C" fn tl_region_exit(ctx: *mut Ctx, region: Value) { |
| 466 | | − // SAFETY: caller guarantees a live, owned region. |
| 467 | | − unsafe { |
| 468 | | − let data = region_data(region); |
| 469 | | − if (*ctx).yielding == crate::abi::YIELD_CLONE { |
| 470 | | − data.copies += 1; |
| 471 | | − dup_value(region); |
| 472 | | − return; |
| 473 | | − } |
| 474 | | − if data.copies > 0 { |
| 475 | | − data.copies -= 1; |
| 476 | | − } else { |
| 477 | | − tl_region_end(ctx, region); |
| 478 | | − } |
| 479 | | − drop_value(ctx, region); |
| 480 | | − } |
| 481 | | −} |
| 482 | | − |
| 483 | | −/// Called when a region block is freed (normally after `tl_region_end`): |
| 484 | | −/// releases references it still holds into the drop worklist. |
| 485 | | −/// |
| 486 | | −/// # Safety |
| 487 | | −/// `b` must be a region block being freed. |
| 488 | | −pub(crate) unsafe fn free_region(b: *mut Block, pending: &mut Vec<*mut Block>) { |
| 489 | | − // SAFETY: caller guarantees a region; the box was made by |
| 490 | | − // `tl_region_new` and is freed exactly once, here. |
| 491 | | − unsafe { |
| 492 | | − let data = Box::from_raw(Block::fields_ptr(b).cast::<*mut RegionData>().read()); |
| 493 | | − for r in data.refs { |
| 494 | | − let field = Block::fields_ptr(r as *mut Block); |
| 495 | | − crate::heap::release_into(field.read(), pending); |
| 496 | | − field.write(UNIT); |
| 497 | | − crate::heap::release_into(r, pending); |
| 498 | | − } |
| 499 | | − } |
| 500 | | −} |
| 501 | | − |
| 502 | | −// ---- resources (ADR 0005) ------------------------------------------------------ |
| 503 | | −// |
| 504 | | −// A `Resource<t>` is a block holding a foreign pointer and its finalizer, |
| 505 | | −// called when the last reference is dropped. Resources that are not |
| 506 | | −// thread-safe are pinned: they cannot be sent to another thread's actor. |
| 507 | | − |
| 508 | | −/// A finalizer for a foreign pointer. It must not touch tilcayo values. |
| 509 | | −pub type Finalizer = unsafe extern "C" fn(*mut std::ffi::c_void); |
| 510 | | − |
| 511 | | −/// Wrap `ptr` with `finalize` (may be null) as a resource value (owned). |
| 512 | | −#[unsafe(no_mangle)] |
| 513 | | −pub extern "C" fn tl_resource_new( |
| 514 | | − ctx: *mut Ctx, |
| 515 | | − ptr: *mut std::ffi::c_void, |
| 516 | | − finalize: Option<Finalizer>, |
| 517 | | − thread_safe: bool, |
| 518 | | −) -> Value { |
| 519 | | − let b = tl_alloc(ctx, crate::abi::TAG_RESOURCE, 0, 2 * WORD); |
| 520 | | − // SAFETY: the raw area holds the pointer, then the finalizer. |
| 521 | | − unsafe { |
| 522 | | − let raw = Block::fields_ptr(b).cast::<usize>(); |
| 523 | | − raw.write(ptr as usize); |
| 524 | | − raw.add(1).write(finalize.map_or(0, |f| f as usize)); |
| 525 | | − if !thread_safe { |
| 526 | | − (*b).header.flags |= crate::abi::FLAG_PINNED; |
| 527 | | − } |
| 528 | | − } |
| 529 | | − b as Value |
| 530 | | −} |
| 531 | | − |
| 532 | | −/// The foreign pointer of a resource (borrowed). |
| 533 | | −/// |
| 534 | | −/// # Safety |
| 535 | | −/// `r` must be a live resource. |
| 536 | | −#[unsafe(no_mangle)] |
| 537 | | −pub unsafe extern "C" fn tl_resource_get(r: Value) -> *mut std::ffi::c_void { |
| 538 | | − // SAFETY: caller guarantees a resource. |
| 539 | | − unsafe { Block::fields_ptr(r as *mut Block).cast::<usize>().read() as *mut std::ffi::c_void } |
| 540 | | −} |
| 541 | | − |
| 542 | | −/// Called when a resource block is freed: run its finalizer. |
| 543 | | −/// |
| 544 | | −/// # Safety |
| 545 | | −/// `b` must be a resource block being freed. |
| 546 | | −pub(crate) unsafe fn finalize_resource(b: *mut Block) { |
| 547 | | − // SAFETY: caller guarantees a resource block. |
| 548 | | − unsafe { |
| 549 | | − let raw = Block::fields_ptr(b).cast::<usize>(); |
| 550 | | − let (ptr, f) = (raw.read(), raw.add(1).read()); |
| 551 | | − if f != 0 { |
| 552 | | − let f: Finalizer = std::mem::transmute(f); |
| 553 | | − f(ptr as *mut std::ffi::c_void); |
| 554 | | − } |
| 555 | | − } |
| 556 | | −} |
| 557 | | − |
| 558 | 354 | // ---- randomness (std/random) ------------------------------------------------------ |
| 559 | 355 | |
| 560 | 356 | /// A uniformly random Int in `0..below` (`below` > 0; a bound beyond 64 |
| @@ -611,6 +407,7 @@ pub unsafe extern "C" fn tl_url_decode(ctx: *mut Ctx, s: Value) -> Value { |
| 611 | 407 | mod tests { |
| 612 | 408 | use super::*; |
| 613 | 409 | use crate::abi::int_get; |
| 410 | + use crate::heap::drop_value; |
| 614 | 411 | use crate::values::list_items; |
| 615 | 412 | |
| 616 | 413 | /// The prelude values these externs build: `List` (`Nil` 0, `Cons` 1) |