code wiki / _hdl_build / nx_js_run_demo.nx
nx_js_run_demo.nx source
↩ module page · 19 lines · 1770 B
1// nx_js_run_demo.nx -- END-TO-END COMPOSITION proof for the sovereign JS engine
2// (WB-JS-001 rungs LEX..CALLBACK). The 60 gate KATs test features in isolation; this
3// runs ONE real, non-trivial program that exercises closures + objects + arrays +
4// map/filter/reduce + a for-loop + member access + .join + Math.max + console.log
5// TOGETHER, through js_run_source, printing real output. No gcc/V8/node -- sovereign.
6import "nx_js_eval.nx"
7
8func main() -> i64 {
9 let src: *u8 = "function makeCounter(){ var c = 0; return function(){ c = c + 1; return c; }; }\nvar hit = makeCounter();\nvar nums = [1,2,3,4,5,6,7,8,9,10];\nvar evens = nums.filter(function(n){ return n % 2 == 0; });\nvar doubled = evens.map(function(n){ return n * 2; });\nvar total = doubled.reduce(function(a,b){ return a + b; }, 0);\nvar people = [{name:'Ada',age:36},{name:'Alan',age:41},{name:'Grace',age:45}];\nvar names = people.map(function(p){ return p.name; }).join(', ');\nvar ageSum = 0;\nfor (var i = 0; i < people.length; i = i + 1){ ageSum = ageSum + people[i].age; }\nconsole.log('clicks:', hit(), hit(), hit());\nconsole.log('evens:', evens.join(','));\nconsole.log('doubled-sum:', total);\nconsole.log('names:', names);\nconsole.log('age-sum:', ageSum);\nconsole.log('oldest:', Math.max(people[0].age, people[1].age, people[2].age));\n\x00"
10 var n: i64 = 0
11 while src[n] != (0 as u8) { n = n + 1 }
12 sys_write(1, "=== sovereign JS engine: running a real composed program ===\n\x00" as *u8, 60)
13 let out: *i64 = sys_mmap(16) as *i64
14 let rc: i64 = js_run_source(src, n, out)
15 sys_write(1, "=== program exit rc=\x00" as *u8, 21)
16 if rc == 0 { sys_write(1, "0 (OK)\n\x00" as *u8, 7) }
17 if rc != 0 { sys_write(1, "1 (ERROR)\n\x00" as *u8, 10) }
18 return rc
19}