ahk 0.6.2

The ahk language: a lexer, an AST, a parser and an interpreter for AutoHotkey-style scripts
Documentation
// SPDX-License-Identifier: Apache-2.0
//! The machine memory of the buffers of a script.
//!
//! The bytes of a `Buffer` live in the object of the script, and the
//! machine cannot read them there: a `DllCall` that is given `Buffer.Ptr`
//! reads and writes real memory. The runtime keeps a mirror of every
//! buffer whose address was asked for: a block of the heap of Rust, whose
//! address is the `Ptr` of the buffer, and whose bytes are kept current
//! with the object on every write of the script.
//!
//! The machine writes into the block while a native call goes on; when
//! the call comes back, the bytes of the block are read back into the
//! object, so `NumGet` sees what the machine left there. The address is
//! stable while the size is: it changes when the block grows, as v2 says
//! of a buffer that is reallocated.
//!
//! The mirrors belong to one run of one thread: the script runs on its
//! own thread (the callbacks re-enter it there), and when the run is over
//! they are let go, so the addresses of a run are not the addresses of
//! the next one.

use alloc::vec::Vec;
use std::cell::RefCell;

use crate::runtime::{SharedCell, Value};

/// One buffer the machine has the address of: the block of the machine
/// and the object of the script it mirrors.
struct Mirror {
    block: Vec<u8>,
    owner: Value,
}

thread_local! {
    static MIRRORS: RefCell<Vec<Mirror>> = const { RefCell::new(Vec::new()) };
}

/// Whether two values are the same object of the script.
fn the_same_object(first: &Value, second: &Value) -> bool {
    match (first, second) {
        (Value::Object(first), Value::Object(second)) => SharedCell::ptr_eq(first, second),
        _ => false,
    }
}

/// The address of the machine memory of a buffer, and the bytes of the
/// object are kept current in it.
///
/// The mirror is made when the address is asked for the first time; the
/// later asks answer the same address while the size is the same.
pub fn the_address_of_a_buffer(owner: &Value, bytes: &[u8]) -> i64 {
    MIRRORS.with(|mirrors| {
        let mut mirrors = mirrors.borrow_mut();
        if let Some(mirror) = mirrors.iter_mut().find(|m| the_same_object(&m.owner, owner)) {
            mirror.block.resize(bytes.len(), 0);
            mirror.block.copy_from_slice(bytes);
            return mirror.block.as_ptr() as i64;
        }
        let block = bytes.to_vec();
        let address = block.as_ptr() as i64;
        mirrors.push(Mirror {
            block,
            owner: owner.clone(),
        });
        address
    })
}

/// Keep the machine copy of a buffer current after a write of the script.
/// A buffer whose address was never asked for has no mirror, and nothing
/// is made for it here.
pub fn the_mirror_is_updated(owner: &Value, bytes: &[u8]) {
    MIRRORS.with(|mirrors| {
        let mut mirrors = mirrors.borrow_mut();
        if let Some(mirror) = mirrors.iter_mut().find(|m| the_same_object(&m.owner, owner)) {
            mirror.block.resize(bytes.len(), 0);
            mirror.block.copy_from_slice(bytes);
        }
    });
}

/// The bytes the machine left in the mirrors, with the object each one
/// belongs to: the caller reads them back into the objects.
pub fn the_mirrors_are_read() -> Vec<(Value, Vec<u8>)> {
    MIRRORS.with(|mirrors| {
        mirrors
            .borrow()
            .iter()
            .map(|mirror| (mirror.owner.clone(), mirror.block.clone()))
            .collect()
    })
}

/// Let the mirrors of a run go: the addresses of a run do not outlive it.
pub fn forget_all() {
    MIRRORS.with(|mirrors| mirrors.borrow_mut().clear());
}