//! A package that grows slices of pointer-holding elements in a static heap, executed by hosted
//! compilation and the loader. The loader reads a package from one page, so the program stays small.

/// The allocation error, which the compiler knows.
@language union AllocError: Copy { OutOfMemory }

/// A heap, which holds elements of any type.
@language record Heap {}

/// Allocate the backing storage of a static heap.
@intrinsic constant fn new(size: u32) -> Heap;


/// The allocator view of a heap.
@language record Allocator {}

/// Memory for the slices of this package.
static POOL: Heap = new(256);

/// Numbers that holders point to.
static NUMBERS: [u32; 2] = [20, 22];

/// A record that holds a pointer.
record Holder {
    /// Owned pointer to a number.
    p: *u32,
}

/// Grow slices, and return the checked result.
@default fn main() -> i32 {
    let mut hs: *mut [Holder] = &mut [];
    for i in 0 .. 2 {
        try! hs.append(Holder { p: &NUMBERS[i] }, &POOL);
    }
    let mut big: *mut [Holder] = &mut [];
    try big.reserve(1000, &POOL) catch e {
        assert e == AllocError::OutOfMemory;
        return (*hs[0].p + *hs[1].p) as i32;
    };
    return 1;
}
