code wiki / _hdl_build / nx_js_proto_probe.nx
nx_js_proto_probe.nx source
↩ module page · 21 lines · 1763 B
1// probe prototype-based OOP on tree-walker vs VM. expect_exit: 0 license_tier: ORIGINAL
2import "nx_js_vm.nx"
3func pw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
4func pn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var q: i64=k-1; var x: i64=0; while q>=0 { o[x]=t[q]; x=x+1; q=q-1 } sys_write(1,o,x); return 0 }
5func probe(src: *u8, name: *u8) -> i64 {
6 let ot: *i64 = sys_mmap(16) as *i64
7 let ov: *i64 = sys_mmap(16) as *i64
8 let rt: i64 = js_run_source(src, bsl(src), ot)
9 let rv: i64 = compile_run(src, ov)
10 pw(" "); pw(name); pw(": TREE rc="); pn(rt); pw(" tag="); pn(ot[0]); pw(" pay="); pn(ot[1])
11 pw(" | VM rc="); pn(rv); pw(" tag="); pn(ov[0]); pw(" pay="); pn(ov[1]); pw("\n" as *u8)
12 return 0
13}
14func main(argc: i64, argv: *i64) -> i64 {
15 pw("=== prototype OOP probe (tree vs VM) ===\n" as *u8)
16 probe("function Counter(){this.n=0;}Counter.prototype.inc=function(){this.n=this.n+1;};Counter.prototype.get=function(){return this.n;};var c=new Counter();c.inc();c.inc();c.inc();c.get()" as *u8, "counter expect 3" as *u8)
17 probe("function P(x){this.x=x;}P.prototype.sq=function(){return this.x*this.x;};var a=new P(3);var b=new P(5);a.sq()+b.sq()" as *u8, "two-inst sq expect 34" as *u8)
18 probe("function Q(){this.v=10;}Q.prototype.k=7;var q=new Q();q.v+q.k" as *u8, "inherited data prop expect 17" as *u8)
19 probe("function A(){}A.prototype.hi=function(){return 'hi';};var x=new A();x.hi()=='hi'?1:0" as *u8, "proto string method expect 1" as *u8)
20 return 0
21}