nx_cell_anatomy_compose_test.nx source
↩ module page · 132 lines · 5715 B
1// nx_cell_anatomy_compose_test.nx -- complete cell anatomy demo.
2//
3// The 6 core biology primitives composed: build a complete cell
4// (cell + brane + vacuole + chromatin + codon stream + methyl
5// mark) and exercise its full lifecycle including vesicle dispatch
6// to a peer cell + termination into lysosome.
7
8import "nx_syscalls.nx"
9import "nx_tier.nx"
10import "nx_budget.nx"
11import "nx_attention_class.nx"
12import "nx_methyl.nx"
13import "nx_chromatin.nx"
14import "nx_ribosome.nx"
15import "nx_cell.nx"
16import "nx_brane.nx"
17import "nx_vacuole.nx"
18import "nx_vesicle.nx"
19import "nx_lysosome.nx"
20import "nx_codon.nx"
21
22func main() -> i64 {
23 let sig: *u8 = (sys_mmap(96)) as *u8
24 sig[0] = 7 as u8
25 let mark_a: *NxMethylMark = nx_methyl_new(10, 0xaaaa, 1000, sig, 96)
26 let mark_b: *NxMethylMark = nx_methyl_new(20, 0xbbbb, 1000, sig, 96)
27
28 // ===== Step 1: build cell A (foreground game cell) ============
29 let b_a: *NxBudget = nx_budget_new(10, 1000000, 100000, 0, 0, 0)
30 let cell_a: *NxCell = nx_cell_new(10,
31 NX_AC_INTERACTIVE_FOREGROUND_GAME, 500,
32 mark_a, b_a, 1000)
33 if (cell_a as i64) == 0 { return 1 }
34 if cell_a.state != NX_CL_NASCENT { return 2 }
35
36 // ===== Step 2: attach brane with grants =======================
37 let brane_a: *NxBrane = nx_brane_new(10, 8)
38 nx_brane_grant(brane_a, NX_CAP_FILE_READ, 0, 0, 1000)
39 nx_brane_grant(brane_a, NX_CAP_PEER_MESSAGE, 0xfee20, 0, 1000)
40 nx_brane_grant(brane_a, NX_CAP_HARDWARE_IO, 0, 0, 1000)
41 nx_cell_attach_brane(cell_a, brane_a as *u8)
42 if nx_brane_token_count(brane_a) != 3 { return 3 }
43
44 // ===== Step 3: attach vacuole and put a sealed payload =========
45 let vac_a: *NxVacuole = nx_vacuole_new(8)
46 let payload: *u8 = (sys_mmap(64)) as *u8
47 payload[0] = 65 as u8
48 nx_vacuole_put(vac_a, 0xfa1, payload, 64, mark_a, 1100)
49 nx_cell_attach_vacuole(cell_a, vac_a as *u8)
50 if nx_vacuole_count(vac_a) != 1 { return 4 }
51
52 // ===== Step 4: attach chromatin (backup snapshot) =============
53 let snap: *u8 = (sys_mmap(64)) as *u8
54 var i: nx_size = 0
55 while i < 32 {
56 snap[i] = (50 + i) as u8
57 i = i + 1
58 }
59 let ch_a: *NxChromatin = nx_chromatin_capture(10, 0xc101, 1200,
60 snap, 32, mark_a, 1)
61 nx_cell_attach_chromatin(cell_a, ch_a as *u8)
62
63 // ===== Step 5: emit codon stream for cell's hot path ==========
64 let codon_a: *NxCodonStream = nx_codon_stream_new(8)
65 nx_codon_emit(codon_a, NX_OP_LOAD, 0, 0, NX_RBT_N_TARGETS)
66 nx_codon_emit(codon_a, NX_OP_ADD, 1, 2, NX_RBT_N_TARGETS)
67 nx_codon_emit(codon_a, NX_OP_MUL, 3, 4, NX_RBT_PTX)
68 nx_codon_emit(codon_a, NX_OP_STORE, 5, 0, NX_RBT_N_TARGETS)
69 nx_codon_emit(codon_a, NX_OP_RET, 0, 0, NX_RBT_N_TARGETS)
70 if nx_codon_stream_length(codon_a) != 5 { return 5 }
71 if nx_codon_count_by_op(codon_a, NX_OP_MUL) != 1 { return 6 }
72
73 // ===== Step 6: transition cell_a NASCENT -> RUNNING ===========
74 if nx_cell_transition(cell_a, NX_CL_RUNNING, 1500) != NX_CELL_OK { return 7 }
75 if nx_cell_can_work(cell_a) != 1 { return 8 }
76
77 // ===== Step 7: build cell B (recipient peer) ==================
78 let b_b: *NxBudget = nx_budget_new(20, 500000, 0, 0, 0, 0)
79 let cell_b: *NxCell = nx_cell_new(20, NX_AC_BACKGROUND_INFERENCE,
80 500, mark_b, b_b, 2000)
81 let brane_b: *NxBrane = nx_brane_new(20, 4)
82 nx_brane_grant(brane_b, NX_CAP_PEER_MESSAGE, 0xfee10, 0, 2000)
83 nx_cell_attach_brane(cell_b, brane_b as *u8)
84 nx_cell_transition(cell_b, NX_CL_RUNNING, 2100)
85
86 // ===== Step 8: cell A sends a vesicle to cell B ==============
87 // Brane check on A: does A have CAP_PEER_MESSAGE for B?
88 if nx_brane_check(brane_a, NX_CAP_PEER_MESSAGE, 0xfee20, 3000) != NX_BR_GRANTED { return 9 }
89
90 let vesicle: *NxVesicle = nx_vesicle_seal(10, 20, NX_VK_BYTES,
91 payload, 64, mark_a, 3000)
92 if (vesicle as i64) == 0 { return 10 }
93
94 // Cell B verifies it before processing
95 if nx_vesicle_verify(vesicle, 3500, 10000, 10) != NX_VES_OK { return 11 }
96 nx_vesicle_ack(vesicle)
97 if nx_vesicle_is_delivered(vesicle) != 1 { return 12 }
98
99 // ===== Step 9: forged vesicle (wrong originator) rejected =====
100 let forged: *NxVesicle = nx_vesicle_seal(99, 20, NX_VK_BYTES,
101 payload, 64, mark_a, 3000) // claims originator A but mark validates
102 if nx_vesicle_verify(forged, 3500, 10000, 999) != NX_VES_ERR_BAD_MARK { return 13 }
103
104 // ===== Step 10: cell A enters abortive then terminates =======
105 nx_cell_transition(cell_a, NX_CL_ABORTIVE, 4000)
106 nx_cell_transition(cell_a, NX_CL_TERMINATED, 4100)
107 if nx_cell_is_terminal(cell_a) != 1 { return 14 }
108
109 // ===== Step 11: lysosome enqueues terminated cell ============
110 let lyso: *NxLysosome = nx_lysosome_new(8)
111 nx_lysosome_enqueue(lyso, 10, 1, 4200)
112 if nx_lysosome_count(lyso) != 1 { return 15 }
113
114 // ===== Step 12: lysosome drains -- caller frees resources ====
115 let drained_id: nx_int = nx_lysosome_drain_one(lyso)
116 if drained_id != 10 { return 16 }
117 // On drain, caller calls nx_brane_revoke_all
118 nx_brane_revoke_all(brane_a)
119 if nx_brane_token_count(brane_a) != 0 { return 17 }
120 // CAP_FILE_READ is now denied
121 if nx_brane_check(brane_a, NX_CAP_FILE_READ, 0, 5000) != NX_BR_DENIED { return 18 }
122
123 // ===== Step 13: cell B still healthy, vacuole intact ========
124 if nx_cell_state(cell_b) != NX_CL_RUNNING { return 19 }
125 if nx_vacuole_contains(vac_a, 0xfa1) != 1 { return 20 }
126
127 // ===== Step 14: codons of dead cell still inspectable for forensics
128 if nx_codon_count_by_op(codon_a, NX_OP_RET) != 1 { return 21 }
129 if nx_codon_resolve_target(codon_a, 2, NX_TIER_WORKSTATION) != NX_RBT_PTX { return 22 }
130
131 return 0
132}