code wiki / _hdl_build / nx_js_dom_demo.nx

nx_js_dom_demo.nx source

↩ module page · 63 lines · 4718 B

1// nx_js_dom_demo.nx -- INDEPENDENT proof of the JS<->DOM binding (R-JS-DOM1/2/3): seed a realistic 2// HTML page, run REAL JS programs through the sovereign engine that read the DOM via getElementById / 3// querySelector / getAttribute, and print the extracted values. No gcc / no V8 / no node. 4import "nx_js_eval.nx" 5 6func dputs(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 7 8// run one JS program against the seeded page; print "JS: <src>\n -> <result>". 9func show(html: *u8, js: *u8) -> i64 { 10 let out: *i64 = sys_mmap(16) as *i64 11 dputs(" JS: " as *u8); dputs(js); dputs("\n -> " as *u8) 12 let rc: i64 = js_run_source_doc(js, je_strlen(js), html, je_strlen(html), out) 13 if rc != 0 { dputs("[error]\n" as *u8); return 0 } 14 if out[0] == VAL_STR { 15 let rec: *i64 = (out[1]) as *i64 16 sys_write(1, ev_str_bytes(rec), ev_str_len(rec)); dputs("\n" as *u8); return 0 17 } 18 if out[0] == VAL_NULL { dputs("null\n" as *u8); return 0 } 19 if out[0] == VAL_NUM { je_fdn(1, out[1]); dputs("\n" as *u8); return 0 } 20 dputs("[non-string value]\n" as *u8); return 0 21} 22 23func main() -> i64 { 24 let page: *u8 = "<html><head><title>Nishi</title></head><body><h1 id='hdr'>Welcome to Nishi</h1><a id='lnk' href='https://nishi.example/x' class='ext nav'>open</a><img id='logo' src='/logo.png' alt='Nishi'><span id='msg'>It works.</span></body></html>\x00" as *u8 25 dputs("=== R-JS-DOM1/2/3 independent demo: sovereign JS reading the parsed DOM ===\n\n" as *u8) 26 dputs("-- getElementById --\n" as *u8) 27 show(page, "document.getElementById('hdr').textContent\x00" as *u8) 28 show(page, "document.getElementById('hdr').tagName\x00" as *u8) 29 show(page, "document.getElementById('absent')\x00" as *u8) 30 dputs("-- querySelector (#id / .class / tag) --\n" as *u8) 31 show(page, "document.querySelector('#msg').textContent\x00" as *u8) 32 show(page, "document.querySelector('.ext').textContent\x00" as *u8) 33 show(page, "document.querySelector('a').tagName\x00" as *u8) 34 show(page, "document.querySelector('.missing')\x00" as *u8) 35 dputs("-- getAttribute (incl. the void <img> element) --\n" as *u8) 36 show(page, "document.getElementById('lnk').getAttribute('href')\x00" as *u8) 37 show(page, "document.querySelector('a').getAttribute('class')\x00" as *u8) 38 show(page, "document.getElementById('logo').getAttribute('src')\x00" as *u8) 39 show(page, "document.querySelector('img').getAttribute('alt')\x00" as *u8) 40 show(page, "document.getElementById('logo').getAttribute('href')\x00" as *u8) 41 dputs("-- querySelectorAll (a real Array, composes with .map/.join) --\n" as *u8) 42 let ldoc: *u8 = "<ul><li class='item'>Apple</li><li class='item'>Banana</li><li>Other</li><li class='item'>Cherry</li></ul>\x00" as *u8 43 show(ldoc, "document.querySelectorAll('.item').length\x00" as *u8) 44 show(ldoc, "document.querySelectorAll('li').length\x00" as *u8) 45 show(ldoc, "document.querySelectorAll('.item').map(function(e){ return e.textContent; }).join(' | ')\x00" as *u8) 46 show(ldoc, "document.querySelectorAll('.none').length\x00" as *u8) 47 dputs("-- textContent flattens nested descendant markup --\n" as *u8) 48 show("<p id='p'>See <a href='/x'>here</a> for <b>more</b>.</p>\x00" as *u8, "document.getElementById('p').textContent\x00" as *u8) 49 dputs("-- extraction pattern: all image src URLs (the image-search path) --\n" as *u8) 50 show("<div><img src='/a.png'><img src='/b.jpg'><img src='/c.gif'></div>\x00" as *u8, "document.querySelectorAll('img').map(function(e){ return e.getAttribute('src'); }).join(', ')\x00" as *u8) 51 dputs("-- nish integration: the browser EXECUTES the page's own inline <script> --\n" as *u8) 52 let sdoc: *u8 = "<html><body><h1 id='hdr'>Nishi News</h1><ul><li class='hl'>Alpha</li><li class='hl'>Beta</li></ul><script>console.log('[page script] title = ' + document.getElementById('hdr').textContent);console.log('[page script] headlines = ' + document.querySelectorAll('.hl').map(function(e){ return e.textContent; }).join(', '));</script></body></html>\x00" as *u8 53 let nscripts: i64 = js_run_page_scripts(sdoc, je_strlen(sdoc)) 54 dputs(" (" as *u8); je_fdn(1, nscripts); dputs(" inline script block(s) executed against the page DOM)\n" as *u8) 55 dputs("-- f64 numbers (real float arithmetic, sovereign soft-IEEE) --\n" as *u8) 56 let e: *u8 = "\x00" as *u8 57 show(e, "'' + (0.5 + 0.25)\x00" as *u8) 58 show(e, "'' + (10.0 / 4)\x00" as *u8) 59 show(e, "'' + (3 + 0.5)\x00" as *u8) 60 show(e, "'' + (1.5 * 2)\x00" as *u8) 61 dputs("\n(all values above were extracted by the sovereign engine from the HTML, no gcc/V8/node)\n" as *u8) 62 return 0 63}