code wiki / _hdl_build / nx_js_callapply_gate.nx

nx_js_callapply_gate.nx source

↩ module page · 32 lines · 2220 B

1import "nx_gate_gn.nx" 2// nx_js_callapply_gate.nx -- Function.prototype.call / .apply, V8-pinned, BOTH tiers (interpreter + JIT via 3// jit_callm_prep). Covers explicit-this dispatch, arg forwarding, apply-from-array, and the classic 4// superConstructor.call(this,...) inheritance idiom (DeltaBlue's object model). expect_exit: 0 5// license_tier: ORIGINAL 6import "nx_js_vm.nx" 7func gw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 8func chk2(label: *u8, src: *u8, want: i64) -> i64 { 9 let ov: *i64 = sys_mmap(16) as *i64 10 let ot: *i64 = sys_mmap(16) as *i64 11 let rv: i64 = compile_run(src, ov) 12 let rt: i64 = js_run_source(src, bsl(src), ot) 13 gw(" " as *u8); gw(label); gw(": vm=" as *u8); gn(ov[1]); gw(" tree=" as *u8); gn(ot[1]); gw(" want=" as *u8); gn(want) 14 if rv == 0 { if rt == 0 { if ov[1] == want { if ot[1] == want { gw(" ok\n" as *u8); return 1 } } } } 15 gw(" FAIL\n" as *u8) 16 return 0 17} 18func main(argc: i64, argv: *i64) -> i64 { 19 gw("=== nx_js_callapply_gate: Function.prototype.call/.apply (V8-pinned, both tiers) ===\n" as *u8) 20 var pass: i64 = 0 21 var tot: i64 = 0 22 tot=tot+1; pass = pass + chk2("call-args" as *u8, "function g(a,b,c){return a*100+b*10+c;} g.call(null,1,2,3)" as *u8, 123) 23 tot=tot+1; pass = pass + chk2("apply-array" as *u8, "function g(a,b,c){return a*100+b*10+c;} g.apply(null,[4,5,6])" as *u8, 456) 24 tot=tot+1; pass = pass + chk2("call-explicit-this" as *u8, "var o={v:9}; function gv(){return this.v;} gv.call(o)" as *u8, 9) 25 tot=tot+1; pass = pass + chk2("apply-explicit-this" as *u8, "var o={v:7}; function gv(k){return this.v+k;} gv.apply(o,[5])" as *u8, 12) 26 tot=tot+1; pass = pass + chk2("superctor-call-inherit" as *u8, "function Base(x){this.x=x;} function Der(x,y){Base.call(this,x); this.y=y;} var d=new Der(10,5); d.x+d.y" as *u8, 15) 27 tot=tot+1; pass = pass + chk2("call-zero-args" as *u8, "function f(){return 42;} f.call(null)" as *u8, 42) 28 gw(" --- " as *u8); gn(pass); gw("/" as *u8); gn(tot); gw(" ---\n" as *u8) 29 if pass == tot { gw("=== GREEN: call/apply match V8 on both tiers ===\n" as *u8); return 0 } 30 gw("=== RED ===\n" as *u8) 31 return 1 32}