code wiki / (root) / linear_type.nx

linear_type.nx

buildroot/runtime/linear_type.nx

4942 B150 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

linear_type.nx -- runtime tracking for linear/affine resources. Phase A of the full linear-type system (EFFICIENCY_ROADMAP ยง3.2). Today's NishiLang has no compile-time borrow checker, so we approximate the safety benefit at runtime: an owning handle struct that tracks "has this been used yet?" + panics on double-use. Callers that adopt this pattern get the benefit immediately; the compiler-side move/borrow checker can retrofit without breaking the API later. The canonical failure this prevents: double-free / use-after- free / duplicate-consume bugs -- the same class that tonight's "duplicate main in nxc.s" + "alloca clobbered by callee-saved reuse" bugs both lived in. Linear types at compile time would reject both without a test suite. Usage: let h: *Linear = linear_new(ptr as i64, LK_FILE_HANDLE) let taken: i64 = linear_take(h) // consume exactly once linear_take(h) // -> LINEAR_ERR_REUSE Also exposes a RefCount type for when true linearity is too strict and you want shared ownership with explicit counting. Invariants: LT1 `linear_take` on a fresh handle returns its payload + 0. LT2 Subsequent `linear_take` returns LINEAR_ERR_REUSE deterministically. LT3 `refcount_clone` always succeeds + bumps count. LT4 `refcount_drop` decrements; when count hits 0, calls the user-registered free hook exactly once.

dependencies 1 imports · 0 importers

syscalls.nx linear_type.nx

imports: syscalls.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main linear_new linear_alive linear_take refcount_new refcount_count refcount_clone refcount_drop

structs

46struct Linear {
90struct RefCount {

consts

36const LK_GENERIC: i64 = 0
37const LK_FILE_HANDLE: i64 = 1
38const LK_SOCKET: i64 = 2
39const LK_MEMORY_ARENA: i64 = 3
40const LK_MUTEX_GUARD: i64 = 4
41const LK_CAPABILITY: i64 = 5
43const LINEAR_ERR_REUSE: i64 = -1001
44const LINEAR_ERR_NULL: i64 = -1002

functions

53func linear_new(payload: i64, kind: i64) -> *Linear {
called by 1: main
63func linear_take(h: *Linear) -> i64 {
called by 1: main
71func linear_peek(h: *Linear) -> i64 {
77func linear_alive(h: *Linear) -> i64 {
called by 1: main
96func refcount_new(payload: i64, free_hook: i64) -> *RefCount {
called by 1: main
106func refcount_clone(r: *RefCount) -> i64 {
called by 1: main
116func refcount_drop(r: *RefCount) -> i64 {
called by 1: main
122func refcount_count(r: *RefCount) -> i64 {
called by 1: main
127func main() -> i64 {