code wiki / _hdl_build / nx_jq_tree.nx

nx_jq_tree.nx source

↩ module page · 111 lines · 7919 B

1// nx_jq_tree.nx -- run real minified jQuery in DOM_TREE mode (production-realistic): wrap surrogate-preamble + 2// jQuery in a <script> inside a minimal HTML doc, render via js_render_page_pending_begin (dt_parse tree + 3// genv[3]=DOM_TREE_SENTINEL). Now createElement builds REAL dt_* nodes + innerHTML PARSES -> jQuery's DOM 4// support-detects (div.innerHTML then lastChild.checked) exercise a real DOM instead of headless stubs. 5import "nx_syscalls.nx" 6import "nx_x509_trust_store.nx" 7import "nx_trust_store_load_from_certdata.nx" 8import "nx_video_sniff.nx" 9import "nx_video_sniff_fetch.nx" 10import "nx_surrogate.nx" 11const K_MAGIC_4194304: i64 = 4194304 12const K_MAGIC_16384: i64 = 16384 13const K_MAGIC_2048: i64 = 2048 14 15func cw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 16func cwnum(n: i64) -> i64 { 17 if n == 0 { cw("0" as *u8); return 0 } 18 var m: i64 = n 19 if m < 0 { cw("-" as *u8); m = 0 - m } 20 let buf: *u8 = sys_mmap(32) 21 var k: i64 = 0 22 while m > 0 { buf[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 23 var i: i64 = k - 1 24 while i >= 0 { sys_write(1, ((buf as i64) + i) as *u8, 1); i = i - 1 } 25 return 0 26} 27func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 28func app(dst: *u8, o: i64, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { dst[o]=s[i]; o=o+1; i=i+1 } return o } 29func findsub(h: *u8, hn: i64, n: *u8, nn: i64) -> i64 { 30 var i: i64 = 0 31 while i <= hn - nn { 32 var j: i64 = 0; var ok: i64 = 1 33 while j < nn { if (h[i+j] & 0xff) != (n[j] & 0xff) { ok = 0; j = nn } else { j = j + 1 } } 34 if ok == 1 { return i } 35 i = i + 1 36 } 37 return 0 - 1 38} 39 40func main() -> i64 { 41 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304) 42 if r <= 0 { cw("certdata load failed\n" as *u8); return 1 } 43 let store: *TrustStore = r as *TrustStore 44 let status: *i64 = sys_mmap(8) as *i64 45 let jq: *u8 = sys_mmap(K_MAGIC_4194304) 46 let tf0: i64 = sys_now_us() 47 let jn: i64 = nx_https_fetch_follow("https://code.jquery.com/jquery-3.7.1.min.js" as *u8, store, jq, K_MAGIC_4194304, 6, status) 48 let tf1: i64 = sys_now_us() 49 if jn <= 0 { cw("(jquery fetch failed)\n" as *u8); return 1 } 50 cw("=== jQuery min.js bytes=" as *u8); cwnum(jn); cw(" fetch_us=" as *u8); cwnum(tf1 - tf0); cw(" ===\n" as *u8) 51 let sgt: *i64 = sg_new(); sg_seed(sgt) 52 let pre: *u8 = sys_mmap(K_MAGIC_16384) 53 let pl: i64 = sg_build_preamble(sgt, pre, K_MAGIC_16384) 54 // build the HTML document: minimal <html><head><body> so dt_parse gives a real documentElement/head/body, 55 // a probe div, then a <script> carrying preamble + jQuery. 56 let html: *u8 = sys_mmap(K_MAGIC_4194304) 57 var o: i64 = 0 58 o = app(html, o, "<html><head><title>t</title></head><body><div id='t'></div><script>" as *u8, slen("<html><head><title>t</title></head><body><div id='t'></div><script>" as *u8)) 59 o = app(html, o, pre, pl) 60 html[o] = 59 as u8; o = o + 1; html[o] = 10 as u8; o = o + 1 // ';' + newline 61 o = app(html, o, jq, jn) 62 // SAME-CTX functional test: appended to jQuery's own <script> so it runs in jQuery's parse ctx (not the 63 // separate js_run_more_in_genv probe ctx, which mis-interprets jQuery's fnodes -> the each-iterates-0 artifact). 64 // DEFINITIVE: real minified jQuery 3.7.1 doing a full render->query->mutate cycle; one probe per capability. 65 // EVENTS + READY: the interactivity primitives real sites use to build/reveal content. 66 // jQuery core + events + ES6 DESTRUCTURING (params/renaming/var) regression. 67 let test: *u8 = ";$('#t').html('<ul><li class=\"i\">a</li><li class=\"i\">b</li></ul>');function P(n,v){fetch('https://p/JQ-'+n+'-'+v+'.x')}try{P('core',$.fn.jquery==='3.7.1'?'OK':'BAD')}catch(e){P('core','THREW')}try{P('descendant',$('#t').find('ul li').length===2?'OK':'BAD')}catch(e){P('descendant','THREW')}try{P('mapget',$('#t').find('li').map(function(i,el){return $(el).text()}).get().join('')==='ab'?'OK':'BAD')}catch(e){P('mapget','THREW')}try{var c=0;$('#b2').on;$('#t').on('click','li',function(){c++});$('#t').find('li').first().trigger('click');P('delegate',c===1?'OK':'BAD')}catch(e){P('delegate','THREW')}try{function dp({a,b:c}){return a+c}P('destparam',dp({a:3,b:4})===7?'OK':'BAD'+dp({a:3,b:4}))}catch(e){P('destparam','THREW')}try{var o={x:5,y:6};var {x,y:yy}=o;P('destvar',(x+yy)===11?'OK':'BAD')}catch(e){P('destvar','THREW')}try{var arr=[7,8];var [p,q]=arr;P('destarr',(p+q)===15?'OK':'BAD')}catch(e){P('destarr','THREW')}try{var m=Object.assign({a:1},{b:2},{a:9});P('assign',(m.a===9&&m.b===2)?'OK':'BAD')}catch(e){P('assign','THREW')}try{var o1={x:1};var o2={y:2};var sp=Object.assign({},o1,o2);P('optchain',(window.CSS?.foo?.bar)===undefined?'OK':'BAD')}catch(e){P('optchain','THREW')}" as *u8 68 o = app(html, o, test, slen(test)) 69 o = app(html, o, "</script></body></html>" as *u8, slen("</script></body></html>" as *u8)) 70 html[o] = 0 as u8 71 // ALSO build a jQuery-ONLY document (empty test) to isolate pure LOAD cost (parse+IIFE) from the DOM test. 72 let htmlL: *u8 = sys_mmap(K_MAGIC_4194304) 73 var oL: i64 = 0 74 oL = app(htmlL, oL, "<html><head><title>t</title></head><body><div id='t'></div><script>" as *u8, slen("<html><head><title>t</title></head><body><div id='t'></div><script>" as *u8)) 75 oL = app(htmlL, oL, pre, pl) 76 htmlL[oL] = 59 as u8; oL = oL + 1; htmlL[oL] = 10 as u8; oL = oL + 1 77 oL = app(htmlL, oL, jq, jn) 78 oL = app(htmlL, oL, "</script></body></html>" as *u8, slen("</script></body></html>" as *u8)) 79 htmlL[oL] = 0 as u8 80 js_set_rt_dbg(0) 81 // best-of-3 timing (perf-claims rule: single run <15% = noise). LOAD-only first, then LOAD+DOM-test. 82 var bi: i64 = 0 83 while bi < 3 { 84 let ob2: *i64 = sys_mmap(16) as *i64 85 let l0: i64 = sys_now_us(); js_render_page_pending_begin(htmlL, oL, 0 as *u8, 0, ob2); let l1: i64 = sys_now_us() 86 cw(" LOAD-only run" as *u8); cwnum(bi); cw(" ms=" as *u8); cwnum((l1 - l0) / 1000); cw(" us=" as *u8); cwnum(l1 - l0); cw("\n" as *u8) 87 bi = bi + 1 88 } 89 let obx: *i64 = sys_mmap(16) as *i64 90 var rr: i64 = 0 91 var bj: i64 = 0 92 while bj < 3 { 93 let obf: *i64 = sys_mmap(16) as *i64 94 let t0: i64 = sys_now_us(); let rc: i64 = js_render_page_pending_begin(html, o, 0 as *u8, 0, obf); let t1: i64 = sys_now_us() 95 cw(" LOAD+DOMtest run" as *u8); cwnum(bj); cw(" ms=" as *u8); cwnum((t1 - t0) / 1000); cw(" us=" as *u8); cwnum(t1 - t0); cw("\n" as *u8) 96 if bj == 2 { obx[1] = obf[1]; rr = rc } // keep the LAST run's genv for the probe readout below 97 bj = bj + 1 98 } 99 cw("=== jQuery DOM_TREE run complete (render rc=" as *u8); if rr==0 { cw("0" as *u8) } else { cw("1-PARSE-FAIL" as *u8) } cw(") ===\n" as *u8) 100 let genv: *i64 = (obx[1]) as *i64 101 if (genv as i64) == 0 { cw("(NULL genv)\n" as *u8); return 1 } 102 js_set_rt_dbg(0) 103 let probe: *u8 = "var outer=function(){var C=function(x){return!!x&&'length' in x&&'number'==typeof x.length};var jq={};jq.each=function(e,t){var n,r=0;if(C(e)){for(n=e.length;r<n;r++)if(!1===t.call(e[r],r,e[r]))break}else for(r in e)if(!1===t.call(e[r],r,e[r]))break;return e};return jq};var J=outer();window.DE=0;J.each([5,6,7],function(i,v){window.DE=window.DE+v});fetch('https://p/deepEach-'+window.DE+'.x');window.DEO=0;J.each({p:1,q:2},function(i,v){window.DEO=window.DEO+v});fetch('https://p/deepEachObj-'+window.DEO+'.x');" as *u8 104 js_run_more_in_genv(genv, probe, slen(probe)) 105 let ub: *u8 = sys_mmap(K_MAGIC_2048) 106 let t: i64 = js_pending_total(genv) 107 var pi: i64 = 0 108 while pi < t { let ul: i64 = js_pending_url(genv, pi, ub, K_MAGIC_2048); if ul > 0 { cw(" PROBE " as *u8); cw(ub); cw("\n" as *u8) } pi = pi + 1 } 109 if t == 0 { cw(" (no probe fetches -- jQuery global read threw)\n" as *u8) } 110 return 0 111}