code wiki / _hdl_build / nx_js_xhr_gate.nx
nx_js_xhr_gate.nx source
↩ module page · 59 lines · 4397 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_js_xhr_gate.nx -- proves R-JS-XHR: XMLHttpRequest (new + no-new) with open/send, a data: URL
4// resolving to status/responseText/readyState, and onload/onreadystatechange firing as MICROtasks
5// (async, after sync). This is the jQuery-$.ajax path older sites use. Uses js_run_source_read.
6// license_tier: ORIGINAL
7import "nx_js_eval.nx"
8
9func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
10" as *u8); return ok }
11func gsl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
12
13func gr_str(label: *u8, src: *u8, rn: *u8, want: *u8) -> i64 {
14 let out: *i64 = sys_mmap(16) as *i64
15 let rc: i64 = js_run_source_read(src, gsl(src), rn, out)
16 gw(" "); gw(label); gw(": " as *u8)
17 if rc != 0 { gw("FAIL (eval rc=1)\n" as *u8); return 0 }
18 if out[0] != VAL_STR { gw("FAIL (not a string, tag="); gn(out[0]); gw(")\n" as *u8); return 0 }
19 let rec: *i64 = (out[1]) as *i64
20 let wl: i64 = gsl(want)
21 if ev_str_len(rec) != wl { gw("FAIL (len; got '" as *u8); sys_write(1, ev_str_bytes(rec), ev_str_len(rec)); gw("')\n" as *u8); return 0 }
22 let rb: *u8 = ev_str_bytes(rec)
23 var i: i64 = 0
24 while i < wl { if (rb[i] & 0xff) != (want[i] & 0xff) { gw("FAIL (got '" as *u8); sys_write(1, rb, wl); gw("')\n" as *u8); return 0 } i = i + 1 }
25 gw("ok ('" as *u8); gw(want); gw("')\n" as *u8); return 1
26}
27func gr_num(label: *u8, src: *u8, rn: *u8, want: i64) -> i64 {
28 let out: *i64 = sys_mmap(16) as *i64
29 let rc: i64 = js_run_source_read(src, gsl(src), rn, out)
30 gw(" "); gw(label); gw(": " as *u8)
31 if rc != 0 { gw("FAIL (eval rc=1)\n" as *u8); return 0 }
32 if out[0] != VAL_NUM { gw("FAIL (not a number, tag="); gn(out[0]); gw(")\n" as *u8); return 0 }
33 if out[1] != want { gw("FAIL (got "); gn(out[1]); gw(" want "); gn(want); gw(")\n" as *u8); return 0 }
34 gw("ok ("); gn(want); gw(")\n" as *u8); return 1
35}
36
37func main(argc: i64, argv: *i64) -> i64 {
38 gw("=== nx_js_xhr_gate: XMLHttpRequest open/send/onload/status ===\n" as *u8)
39 var pass: i64 = 0
40 var tot: i64 = 0
41 // T0 open + send + onload reads responseText.
42 tot = tot + 1; pass = pass + gr_str("T0 xhr onload -> responseText" as *u8, "var xhr=new XMLHttpRequest();xhr.open('GET','data:,hello');var x='';xhr.onload=function(){x=xhr.responseText;};xhr.send();" as *u8, "x" as *u8, "hello" as *u8)
43 // T1 status is 200 after a data: send.
44 tot = tot + 1; pass = pass + gr_num("T1 xhr.status 200" as *u8, "var xhr=new XMLHttpRequest();xhr.open('GET','data:,y');var x=0;xhr.onload=function(){x=xhr.status;};xhr.send();" as *u8, "x" as *u8, 200)
45 // T2 readyState is 4 (DONE) after send.
46 tot = tot + 1; pass = pass + gr_num("T2 xhr.readyState 4" as *u8, "var xhr=new XMLHttpRequest();xhr.open('GET','data:,y');var x=0;xhr.onload=function(){x=xhr.readyState;};xhr.send();" as *u8, "x" as *u8, 4)
47 // T3 onload is ASYNC (microtask): sync after send() runs before onload -> 'SL'.
48 tot = tot + 1; pass = pass + gr_str("T3 send sync before onload = SL" as *u8, "var xhr=new XMLHttpRequest();xhr.open('GET','data:,z');var log='';xhr.onload=function(){log=log+'L';};xhr.send();log=log+'S';" as *u8, "log" as *u8, "SL" as *u8)
49 // T4 onreadystatechange fires and sees readyState 4.
50 tot = tot + 1; pass = pass + gr_str("T4 onreadystatechange@4" as *u8, "var xhr=new XMLHttpRequest();xhr.open('GET','data:,rdy');var x='';xhr.onreadystatechange=function(){if(xhr.readyState==4){x=xhr.responseText;}};xhr.send();" as *u8, "x" as *u8, "rdy" as *u8)
51 // T5 XMLHttpRequest() WITHOUT new also constructs.
52 tot = tot + 1; pass = pass + gr_str("T5 no-new XMLHttpRequest()" as *u8, "var xhr=XMLHttpRequest();xhr.open('GET','data:,nn');var x='';xhr.onload=function(){x=xhr.responseText;};xhr.send();" as *u8, "x" as *u8, "nn" as *u8)
53 // T6 LIAR-KILL: a non-data URL -> status 0 (engine TLS-free; consumer/rung-5 services real URLs).
54 tot = tot + 1; pass = pass + gr_num("T6 liar-kill (http -> status 0)" as *u8, "var xhr=new XMLHttpRequest();xhr.open('GET','http://api/x');var x=999;xhr.onload=function(){x=xhr.status;};xhr.send();" as *u8, "x" as *u8, 0)
55
56 gw("=== "); gn(pass); gw("/"); gn(tot); gw(" "); if pass == tot { gw("GREEN ===\n" as *u8) } else { gw("RED ===\n" as *u8) }
57 if pass == tot { return 0 }
58 return 1
59}