← Back to Demos & Examples

03_oro_buffers_and_errors.vk

Raw .vk
// Example 03: ORO mut buffer loans, secret slabs, defer LIFO, /or error recovery, and recur
// Track A (ORO VM) : veko run examples/03_oro_buffers_and_errors.vk
// Track A (.vko)   : veko compile --vko examples/03_oro_buffers_and_errors.vk -o dist/03.vko ; veko run dist/03.vko
// Track B (Wasm 3) : veko run --wasm examples/03_oro_buffers_and_errors.vk
// Track C (Native) : veko compile --native examples/03_oro_buffers_and_errors.vk -o dist/03.exe ; ./dist/03.exe
use [bytes, ascii, str]

// In-place mutation without reallocating per step
mut pkt = bytes/with-cap 4kb
bytes/push-all mut pkt (bytes/from-str '  ping trustnode  ')
ascii/trim-right! mut pkt
ascii/upper! mut pkt
bytes/push mut pkt 33
println (str/from-bytes pkt)

// Secret buffer zeroized deterministically when scope ends
use-secret = fn [label] [
	mut secret = bytes/with-secret 64b
	bytes/push-all mut secret (bytes/from-str 'ephemeral-session-key')
	println (str/append label ':secret-ready')
	secret.len
]
println (use-secret 'auth')

// Inline /or error handling + LIFO defer
safe-find = fn [hay needle] [
	defer [println 'cleanup-done']
	idx = str/index-of/or hay needle [-1]
	idx
]
println (safe-find 'veko-oro-vm' 'oro')
println (safe-find 'veko-oro-vm' 'missing')

// Constant-stack tail recursion with recur
gcd = fn [a b] [
	if zero? b [return a]
	recur b (a % b)
]
println (gcd 84 36)