code wiki / (root) / nx_json_emit.nx

nx_json_emit.nx source

↩ module page · 38 lines · 2114 B

1// nx_json_emit.nx -- THE canonical tiny JSON emitter (eats json-emit-dup, tranche 1). 2// MEASURED 2026-07-21 (deduped grep): 1027 runtime files hand-roll a private xx_puts and 473 3// hand-roll xx_num -- the same ~30 lines re-typed a thousand times, each copy free to drift. 4// This is the ONE copy. Organs migrate on touch (D001); nx_dr_semcorpus is the first tranche. 5// ⚠PLACEMENT: lives in runtime/_hdl_build/ ONLY -- the importer set starts there and a second 6// copy in runtime/ would itself be new cross-tree dup-source (the guarded class, seq430); 7// runtime/-tree organs migrate after the staged-merge or on their own touch. 8// je_str/je_key do NOT escape embedded quotes -- byte-identical parity with every existing 9// hand-rolled copy (they emit literal constants); an escaping variant is a follow-on verb. 10// je_num matches the hand-rolled emitters bit-for-bit (incl. their i64-min negation limit). 11// Emit goes to fd 1. Every func <=6 params (seq239). No hardware writes (Rule 26). 12// license_tier: ORIGINAL module: nishi-core.lib.json_emit depends: nx_syscalls.nx 13import "nx_syscalls.nx" 14 15func je_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 16 17func je_q() -> i64 { let b: *u8 = sys_mmap(1); b[0] = 34 as u8; sys_write(1, b, 1); return 0 } 18 19func je_key(name: *u8) -> i64 { je_q(); je_puts(name); je_q(); je_puts(":" as *u8); return 0 } 20 21func je_str(v: *u8) -> i64 { je_q(); je_puts(v); je_q(); return 0 } 22 23func je_num(v: i64) -> i64 { 24 let bb: *u8 = sys_mmap(28); var m: i64 = v; var neg: i64 = 0 25 if m < 0 { neg = 1; m = 0 - m } 26 let t: *u8 = sys_mmap(28); var k: i64 = 0 27 if m == 0 { t[0] = 48 as u8; k = 1 } 28 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 29 var o: i64 = 0 30 if neg == 1 { bb[o] = 45 as u8; o = o + 1 } 31 var i: i64 = 0 32 while i < k { bb[o] = t[k-1-i]; o = o + 1; i = i + 1 } 33 sys_write(1, bb, o); return 0 34} 35 36func je_kv_num(name: *u8, v: i64) -> i64 { je_key(name); je_num(v); return 0 } 37 38func je_kv_str(name: *u8, v: *u8) -> i64 { je_key(name); je_str(v); return 0 }