code wiki / _hdl_build / nx_js_proto_vm_gate.nx

nx_js_proto_vm_gate.nx source

↩ module page · 48 lines · 3622 B

1// nx_js_proto_vm_gate.nx -- built-in prototype EXTENSION on the FAST tiers (VM interpreter BC_CALLM + 2// baseline JIT jit_callm_prep). Completes the tree-only rung proved by nx_js_proto_gate.nx: user code adds 3// methods to String.prototype / Array.prototype and calls them on string/array primitives (this=receiver) 4// while the program runs through compile_run (bytecode VM, and AOT baseline JIT when enabled). Each case is 5// run TWICE -- js_jit_disable(1) forces the VM interpreter path, js_jit_disable(0) forces the AOT JIT path -- 6// so both mirrors are exercised deterministically (jit_compile is AOT, so JIT-on compiles the callee up front 7// and jit_callm_prep resolves its native entry). expect_exit: 0 license_tier: ORIGINAL 8import "nx_js_vm.nx" 9func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 10func nn(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 } 11static g_want: i64 12func vt(label: *u8, src: *u8) -> i64 { // VM/JIT-tier check vs g_want (tier chosen by js_jit_disable in main) 13 let rt: *i64 = sys_mmap(16) as *i64 14 let r: i64 = compile_run(src, rt) 15 w(" " as *u8); w(label); w(": rc=" as *u8); nn(r); w(" got=" as *u8); nn(rt[1]); w(" want=" as *u8); nn(g_want) 16 if r == 0 { if rt[1] == g_want { w(" ok\n" as *u8); return 1 } } 17 w(" FAIL\n" as *u8) 18 return 0 19} 20// The 8 cases are identical to the tree gate so tree/VM/JIT parity is provable by inspection. 21func suite() -> i64 { 22 var pass: i64 = 0 23 g_want=1; pass=pass+vt("proto-assign-in " as *u8, "String.prototype.z=9; (\x22z\x22 in String.prototype)?1:0" as *u8) 24 g_want=6; pass=pass+vt("str-method-this " as *u8, "String.prototype.twice=function(){return this.length*2;}; \x22abc\x22.twice()" as *u8) 25 g_want=7; pass=pass+vt("str-method-arg " as *u8, "String.prototype.plus=function(n){return this.length+n;}; \x22abc\x22.plus(4)" as *u8) 26 g_want=20; pass=pass+vt("arr-method-this " as *u8, "Array.prototype.snd=function(){return this[1];}; var a=[10,20,30]; a.snd()" as *u8) 27 g_want=60; pass=pass+vt("arr-method-loop " as *u8, "Array.prototype.total=function(){var s=0;var i=0;while(i<this.length){s=s+this[i];i=i+1;}return s;}; var a=[10,20,30]; a.total()" as *u8) 28 g_want=5; pass=pass+vt("str-read-method " as *u8, "String.prototype.five=function(){return 5;}; var f=\x22x\x22.five; f()" as *u8) 29 g_want=3; pass=pass+vt("builtin-still-ok" as *u8, "\x22abc\x22.length" as *u8) 30 g_want=1; pass=pass+vt("builtin-charat " as *u8, "(\x22abc\x22.charAt(0)==\x22a\x22)?1:0" as *u8) 31 return pass 32} 33func main(argc: i64, argv: *i64) -> i64 { 34 w("=== nx_js_proto_vm_gate: user String/Array.prototype methods on the FAST tiers ===\n" as *u8) 35 var pass: i64 = 0 36 let per: i64 = 8 37 w("--- phase 1: VM interpreter (jit_disabled=1, exercises BC_CALLM mirror) ---\n" as *u8) 38 js_jit_disable(1) 39 pass = pass + suite() 40 w("--- phase 2: baseline JIT (jit_disabled=0, exercises jit_callm_prep mirror) ---\n" as *u8) 41 js_jit_disable(0) 42 pass = pass + suite() 43 let tot: i64 = per * 2 44 w(" --- " as *u8); nn(pass); w("/" as *u8); nn(tot); w(" ---\n" as *u8) 45 if pass == tot { w("=== GREEN: user String/Array.prototype methods callable on primitives (VM + JIT) ===\n" as *u8); return 0 } 46 w("=== RED ===\n" as *u8) 47 return 1 48}