code wiki / _hdl_build / nx_multiround_test.nx

nx_multiround_test.nx source

↩ module page · 43 lines · 1895 B

1// nx_multiround_test.nx -- CRUX proof for the network-in-the-loop rung: after js_pending_service settles a 2// fetch's promise and el_drain runs the microtasks, does the .then continuation FIRE (and issue its own next 3// fetch)? If yes, round-1 pending gains the second URL -> the multi-round network sniff is buildable. 4import "nx_syscalls.nx" 5import "nx_js_eval.nx" 6 7func cw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 8func tslen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 9 10func dump_pending(genv: *i64, ub: *u8) -> i64 { 11 let t: i64 = js_pending_total(genv) 12 var i: i64 = 0 13 while i < t { 14 let ul: i64 = js_pending_url(genv, i, ub, 2048) 15 cw(" [" as *u8) 16 if js_pending_serviced(genv, i) == 1 { cw("done" as *u8) } else { cw("open" as *u8) } 17 cw("] " as *u8) 18 if ul > 0 { cw(ub) } else { cw("-" as *u8) } 19 cw("\n" as *u8) 20 i = i + 1 21 } 22 return t 23} 24 25func main() -> i64 { 26 // page JS: fetch an API URL, and in its .then, fetch the (would-be resolved) stream URL. 27 let js: *u8 = "fetch('https://api.test/resolve').then(function(r){fetch('https://cdn.test/real.m3u8')});" as *u8 28 let obx: *i64 = sys_mmap(16) as *i64 29 js_run_source_keep_doc(js, tslen(js), 0 as *u8, 0, obx) 30 let ctx: *i64 = (obx[0]) as *i64 31 let genv: *i64 = (obx[1]) as *i64 32 if (genv as i64) == 0 { cw("NULL genv (parse fail?)\n" as *u8); return 1 } 33 let ub: *u8 = sys_mmap(2048) 34 cw("=== round 0 (after initial run) ===\n" as *u8) 35 dump_pending(genv, ub) 36 // service pending #0 with a canned 200 response, then drain the microtask queue. 37 let body: *u8 = "OK" as *u8 38 js_pending_service(ctx, genv, 0, 200, body, tslen(body)) 39 el_drain(ctx, genv) 40 cw("=== round 1 (after service+drain) ===\n" as *u8) 41 dump_pending(genv, ub) 42 return 0 43}