examples: complete source ports and run the corpus in CI
This commit is contained in:
1 parent
b1629a59fd
commit
184cb7b321
11 files changed
+491
-91
No files matched your search
@@ -0,0 +1,18 @@
|
||||
# Executable Aji examples
|
||||
|
||||
These examples use the implemented `.aji` syntax. Build with `zig build`, then run from the repository root. Each command supplies a deterministic fuel budget and the capabilities its program requests.
|
||||
|
||||
| Example | Demonstrates | Command |
|
||||
|---|---|---|
|
||||
| [hello.aji](hello.aji) | Explicit stdout authority | `zig-out/bin/aji run --fuel 100000 --grant stdout=io.write examples/hello.aji` |
|
||||
| [file_copy.aji](file_copy.aji) | Borrowed files and explicit close | `zig-out/bin/aji run --fuel 100000 --grant fs_read=fs.read:/in --grant fs_write=fs.write:/out --vfs '/in/data.txt=some text' examples/file_copy.aji` |
|
||||
| [parallel_map.aji](parallel_map.aji) | Disjoint mutable loans to two scoped tasks; returns 204 | `zig-out/bin/aji run --fuel 100000 examples/parallel_map.aji` |
|
||||
| [channel_transfer.aji](channel_transfer.aji) | Move an Island through a bounded channel; returns 42 | `zig-out/bin/aji run --fuel 100000 --grant heap=mem.alloc:256 examples/channel_transfer.aji` |
|
||||
| [atomic_publish.aji](atomic_publish.aji) | Explicit release/acquire publication, including a read before join; returns 42 | `zig-out/bin/aji run --fuel 100000 examples/atomic_publish.aji` |
|
||||
| [append_log.aji](append_log.aji) | Bounded records, checked offsets and recoverable I/O; returns 19 | `zig-out/bin/aji run --fuel 100000 --grant fs_write=fs.write:/out examples/append_log.aji` |
|
||||
|
||||
The journal example creates a new file, replacing previous contents. Its wire format is a 16-byte little-endian header (u32 payload length, u32 payload-only CRC32C, u64 byte offset), followed by the payload. A failed write poisons the Log and blocks further appends; closing the file remains explicit. Header integrity, atomic durable records and recovery are outside this demonstration. The 16 MiB limit is an explicit guard; source `requires`/`ensures` support remains P6-6.
|
||||
|
||||
[modules/](modules/aji.toml) demonstrates explicit module inputs: `zig-out/bin/aji run --fuel 100000 --manifest examples/modules/aji.toml`. [packages/hello/](packages/hello/aji.toml) runs a source entry with manifest grants: `zig-out/bin/aji run --fuel 100000 --manifest examples/packages/hello/aji.toml`. Its retained `main.air` is an execution counterpart, rather than the package entry.
|
||||
|
||||
[std/](std/) contains eighteen examples for the compiler-pinned standard modules. `python3 tests/hosted/examples.py --stress 200` checks and runs every standalone example and both packages, verifies journal bytes independently, injects a partial host write, and compares concurrent source/AIR runs over schedule seeds. The five larger [reference programs](../tests/conformance/source/programs/) run with their AIR counterparts through `zig build conformance -- --filter source/programs --stress 200`.
|
||||
+100
-36
@@ -1,52 +1,116 @@
|
||||
module examples.append_log;
|
||||
|
||||
const HEADER_SIZE: usize = 16;
|
||||
// Run: aji run --fuel 100000 --grant fs_write=fs.write:/out examples/append_log.aji
|
||||
module examples.append_log
|
||||
import std.bytes;
|
||||
import std.hash;
|
||||
import std.math;
|
||||
|
||||
struct Header {
|
||||
len: u32,
|
||||
length: u32,
|
||||
crc: u32,
|
||||
sequence: u64,
|
||||
offset: u64
|
||||
}
|
||||
|
||||
struct Log {
|
||||
file: File,
|
||||
file: file<write>,
|
||||
offset: u64,
|
||||
failed: bool
|
||||
}
|
||||
|
||||
fn encode_header(header: ref<Header>) -> [u8; HEADER_SIZE]
|
||||
effects []
|
||||
{
|
||||
var out = [0u8; HEADER_SIZE];
|
||||
bytes.put_u32_le(mut out, 0, header.len);
|
||||
bytes.put_u32_le(mut out, 4, header.crc);
|
||||
bytes.put_u64_le(mut out, 8, header.sequence);
|
||||
return out;
|
||||
enum AppendResult {
|
||||
ok(u64),
|
||||
too_large,
|
||||
offset_overflow,
|
||||
poisoned,
|
||||
io_failed(io_error)
|
||||
}
|
||||
|
||||
fn append(
|
||||
log: mut<Log>,
|
||||
data: slice<u8>
|
||||
) -> Result<u64, AppendError>
|
||||
requires {
|
||||
len(data) <= 16 MiB
|
||||
fn encoded(result: std.bytes.WriteResult) -> void effects [] {
|
||||
match result {
|
||||
std.bytes.WriteResult::ok => {}
|
||||
std.bytes.WriteResult::short_output => {
|
||||
trap;
|
||||
}
|
||||
}
|
||||
}
|
||||
effects [fs.write]
|
||||
{
|
||||
let crc = crc32c(data);
|
||||
let len32 = checked_cast<u32>(len(data))?;
|
||||
|
||||
let header = Header {
|
||||
len: len32,
|
||||
crc: crc,
|
||||
sequence: log.offset,
|
||||
};
|
||||
let encoded = encode_header(ref header);
|
||||
|
||||
fs.write_all(ref log.file, slice.of(encoded))?;
|
||||
fs.write_all(ref log.file, data)?;
|
||||
fn encode_header(header: ref<Header>) -> [u8; 16] effects [] {
|
||||
var out: [u8; 16] = zero;
|
||||
encoded(std.bytes.write_u32_le(mut out[0usize..4usize], header.length));
|
||||
encoded(std.bytes.write_u32_le(mut out[4usize..8usize], header.crc));
|
||||
encoded(std.bytes.write_u64_le(mut out[8usize..16usize], header.offset));
|
||||
out
|
||||
}
|
||||
|
||||
fn append(log: mut<Log>, data: slice<u8>) -> AppendResult effects [fs.write] {
|
||||
// Explicit guards until source contracts (P6-6) are implemented.
|
||||
if log.failed {
|
||||
return AppendResult::poisoned;
|
||||
}
|
||||
if data.len > 16777216usize {
|
||||
return AppendResult::too_large;
|
||||
}
|
||||
let old = log.offset;
|
||||
log.offset = checked_add(log.offset, HEADER_SIZE + len(data))?;
|
||||
|
||||
return Ok(old);
|
||||
let next = match std.math.checked_add_u64(old, 16u64 + data.len as! u64) {
|
||||
std.math.U64Result::ok(value) => {
|
||||
value
|
||||
},
|
||||
std.math.U64Result::overflow => {
|
||||
return AppendResult::offset_overflow;
|
||||
}
|
||||
};
|
||||
// Wire format: little-endian length, payload-only CRC32C, byte offset.
|
||||
// This demonstration has no header checksum or crash-recovery protocol.
|
||||
let header = Header { length: data.len as! u32, crc: std.hash.crc32c(data), offset: old };
|
||||
let bytes = encode_header(ref header);
|
||||
{
|
||||
mut output = log.file;
|
||||
match fs.write_all(output, ref bytes) {
|
||||
ok => {}
|
||||
err(error) => {
|
||||
log.failed = true;
|
||||
return AppendResult::io_failed(error);
|
||||
}
|
||||
}
|
||||
match fs.write_all(output, data) {
|
||||
ok => {}
|
||||
err(error) => {
|
||||
log.failed = true;
|
||||
return AppendResult::io_failed(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
// I/O failure may leave a partial record. Further appends are blocked;
|
||||
// the caller must close and recover the file outside this example.
|
||||
log.offset = next;
|
||||
AppendResult::ok(old)
|
||||
}
|
||||
|
||||
fn main(fs_write: cap<fs.write>) -> u64 effects [fs.write] {
|
||||
// Start a new log, replacing any existing file rather than guessing its size.
|
||||
let file = fs.create(fs_write, b"/out/log.bin") or trap;
|
||||
let log = Log { file: move file, offset: 0u64, failed: false };
|
||||
let result = append(mut log, b"aji");
|
||||
let offset = match result {
|
||||
AppendResult::ok(value) => {
|
||||
value
|
||||
},
|
||||
AppendResult::too_large => {
|
||||
trap;
|
||||
}
|
||||
AppendResult::offset_overflow => {
|
||||
trap;
|
||||
}
|
||||
AppendResult::io_failed(error) => {
|
||||
trap;
|
||||
}
|
||||
AppendResult::poisoned => {
|
||||
trap;
|
||||
}
|
||||
};
|
||||
let (file, end, failed) = struct.unpack(move log);
|
||||
fs.close(move file) or trap;
|
||||
assert(offset == 0u64);
|
||||
assert(end == 19u64);
|
||||
assert(not failed);
|
||||
end
|
||||
}
|
||||
+58
-17
@@ -1,25 +1,66 @@
|
||||
module examples.atomic_publish;
|
||||
// Run: aji run --fuel 100000 examples/atomic_publish.aji
|
||||
module examples.atomic_publish
|
||||
|
||||
struct Shared {
|
||||
ready: AtomicBool,
|
||||
value: AtomicU64,
|
||||
ready: atomic<bool>,
|
||||
value: atomic<u64>
|
||||
}
|
||||
|
||||
fn publish(shared: ref<Shared>, value: u64) -> void
|
||||
effects [atomic]
|
||||
{
|
||||
atomic.store(ref shared.value, value, relaxed);
|
||||
atomic.store(ref shared.ready, true, release);
|
||||
enum ReadResult {
|
||||
some(u64),
|
||||
none
|
||||
}
|
||||
|
||||
fn try_read(shared: ref<Shared>) -> Option<u64>
|
||||
effects [atomic]
|
||||
{
|
||||
if atomic.load(ref shared.ready, acquire) {
|
||||
// The acquire observes the release on `ready`; the earlier relaxed
|
||||
// store to `value` is ordered before publication. `value` remains
|
||||
// atomic storage, so no ordinary-memory alias exception is needed.
|
||||
return Some(atomic.load(ref shared.value, relaxed));
|
||||
fn publish(shared: ref<Shared>, value: u64) -> void effects [atomic, atomic.weak] {
|
||||
ref data = shared.value;
|
||||
ref ready = shared.ready;
|
||||
atomic.store(data, value, relaxed);
|
||||
atomic.store(ready, true, release);
|
||||
}
|
||||
|
||||
fn try_read(shared: ref<Shared>) -> ReadResult effects [atomic, atomic.weak] {
|
||||
ref ready = shared.ready;
|
||||
if atomic.load(ready, acquire) {
|
||||
// The acquire observes publication through ready. Value remains
|
||||
// atomic storage; no ordinary-memory alias exception is needed.
|
||||
ref data = shared.value;
|
||||
return ReadResult::some(atomic.load(data, relaxed));
|
||||
}
|
||||
return None;
|
||||
ReadResult::none
|
||||
}
|
||||
|
||||
fn main() -> u64 effects [atomic, atomic.weak, task.spawn, task.wait] {
|
||||
let shared = Shared { ready: atomic.new(false), value: atomic.new(0u64) };
|
||||
var result: u64 = 0;
|
||||
{
|
||||
ref view = shared;
|
||||
match try_read(view) {
|
||||
ReadResult::some(value) => {
|
||||
trap;
|
||||
}
|
||||
ReadResult::none => {}
|
||||
}
|
||||
scope s {
|
||||
let t = task.spawn(s, publish, view, 42u64);
|
||||
match try_read(view) {
|
||||
ReadResult::some(value) => {
|
||||
assert(value == 42u64);
|
||||
}
|
||||
ReadResult::none => {}
|
||||
}
|
||||
task.join(move t);
|
||||
}
|
||||
result = match try_read(view) {
|
||||
ReadResult::some(value) => {
|
||||
value
|
||||
},
|
||||
ReadResult::none => {
|
||||
trap;
|
||||
}
|
||||
};
|
||||
}
|
||||
let (ready, value) = struct.unpack(move shared);
|
||||
assert(atomic.take(move ready));
|
||||
assert(atomic.take(move value) == result);
|
||||
result
|
||||
}
|
||||
@@ -1,23 +1,51 @@
|
||||
module examples.channel_transfer;
|
||||
// Run: aji run --fuel 100000 --grant heap=mem.alloc:256 examples/channel_transfer.aji
|
||||
module examples.channel_transfer
|
||||
|
||||
struct Message {
|
||||
id: u64,
|
||||
id: u64
|
||||
}
|
||||
|
||||
fn producer(
|
||||
tx: ref<Sender<Island<Message>>>,
|
||||
alloc: cap<mem.alloc>
|
||||
) -> Result<void, ChannelError>
|
||||
effects [mem.alloc]
|
||||
{
|
||||
var message = island.create(alloc, Message { id: 0 })?;
|
||||
|
||||
borrow message as mut root {
|
||||
root.id = 42;
|
||||
fn producer(tx: sender<island<Message>>, heap: cap<mem.alloc>) -> void effects [mem.alloc, task.wait] {
|
||||
let message = island.create(Message { id: 0u64 }, heap) or trap;
|
||||
{
|
||||
mut graph = message;
|
||||
let address = island.root(graph);
|
||||
mut root = island.at(graph, address);
|
||||
root.id = 42u64;
|
||||
}
|
||||
match channel.send(tx, move message) {
|
||||
ok => {}
|
||||
closed(back) => {
|
||||
island.destroy(move back);
|
||||
} cancelled(back) => {
|
||||
island.destroy(move back);
|
||||
}
|
||||
}
|
||||
// The producer cannot use message after its ownership transfer.
|
||||
channel.close_sender(move tx);
|
||||
}
|
||||
|
||||
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc, task.spawn, task.wait] {
|
||||
match channel.make<island<Message>>(1usize, heap) {
|
||||
ok(tx, rx) => {
|
||||
var result: u64 = 0;
|
||||
scope s {
|
||||
let t = task.spawn(s, producer, move tx, heap);
|
||||
let message = channel.recv(rx) or trap;
|
||||
{
|
||||
ref graph = message;
|
||||
let address = island.root(graph);
|
||||
ref root = island.at(graph, address);
|
||||
result = root.id;
|
||||
}
|
||||
island.destroy(move message);
|
||||
task.join(move t);
|
||||
}
|
||||
channel.close_receiver(move rx) or trap;
|
||||
result
|
||||
}
|
||||
err(error) => {
|
||||
trap;
|
||||
}
|
||||
}
|
||||
|
||||
channel.send(tx, move message)?;
|
||||
|
||||
// message is MOVED and cannot be used here.
|
||||
return Ok();
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
[package]
|
||||
name = "hello"
|
||||
version = "0.1.0"
|
||||
entry = "main.air"
|
||||
entry = "main.aji"
|
||||
|
||||
[capabilities]
|
||||
out = "io.write"
|
||||
@@ -0,0 +1,5 @@
|
||||
module examples.hello
|
||||
|
||||
fn main(out: cap<io.write>) -> void effects [io.write] {
|
||||
io.stdout_write(out, b"hello, aji\n") or trap;
|
||||
}
|
||||
+24
-19
@@ -1,26 +1,31 @@
|
||||
module examples.parallel_map;
|
||||
// Run: aji run --fuel 100000 examples/parallel_map.aji
|
||||
module examples.parallel_map
|
||||
|
||||
fn square_half(values: slice_mut<u64>) -> void
|
||||
effects []
|
||||
{
|
||||
for i in 0..len(values) {
|
||||
fn square_half(values: slice_mut<u64>) -> void effects [] {
|
||||
for i in 0usize..values.len {
|
||||
values[i] = values[i] * values[i];
|
||||
}
|
||||
}
|
||||
|
||||
fn square_all(values: slice_mut<u64>) -> Result<void, TaskError>
|
||||
effects [task.spawn]
|
||||
{
|
||||
let mid = len(values) / 2;
|
||||
let (left, right) = slice.split_mut(values, mid);
|
||||
|
||||
task.scope s {
|
||||
let a = task.spawn_borrow_mut(s, square_half, left)?;
|
||||
let b = task.spawn_borrow_mut(s, square_half, right)?;
|
||||
|
||||
task.join(move a)?;
|
||||
task.join(move b)?;
|
||||
fn square_all(values: slice_mut<u64>) -> void effects [task.spawn, task.wait] {
|
||||
let (left, right) = values.split_at(values.len / 2usize);
|
||||
scope s {
|
||||
let a = task.spawn(s, square_half, left);
|
||||
let b = task.spawn(s, square_half, right);
|
||||
task.join(move a);
|
||||
task.join(move b);
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
fn main() -> u64 effects [task.spawn, task.wait] {
|
||||
var values: [u64; 8] = zero;
|
||||
for i in 0usize..values.len {
|
||||
values[i] = i as! u64 + 1u64;
|
||||
}
|
||||
square_all(mut values);
|
||||
var total: u64 = 0;
|
||||
for i in 0usize..values.len {
|
||||
total = total + values[i];
|
||||
}
|
||||
total
|
||||
}
|
||||
Reference in new issue
Block a user