code wiki / _hdl_build / nx_js_instanceof_gate.nx
nx_js_instanceof_gate.nx source
↩ module page · 30 lines · 2108 B
1import "nx_gate_gn.nx"
2// nx_js_instanceof_gate.nx -- `instanceof` on USER constructors (+ inheritance via prototype = new Base()),
3// V8-pinned, both tiers. Native-ctor instanceof (Array/Object) correctly TypeErrors (no native-proto linkage
4// yet) -- that is a documented follow-on, not tested here. expect_exit: 0 license_tier: ORIGINAL
5import "nx_js_vm.nx"
6func gw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
7func chk2(label: *u8, src: *u8, want: i64) -> i64 {
8 let ov: *i64 = sys_mmap(16) as *i64
9 let ot: *i64 = sys_mmap(16) as *i64
10 let rv: i64 = compile_run(src, ov)
11 let rt: i64 = js_run_source(src, bsl(src), ot)
12 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)
13 if rv == 0 { if rt == 0 { if ov[1] == want { if ot[1] == want { gw(" ok\n" as *u8); return 1 } } } }
14 gw(" FAIL\n" as *u8)
15 return 0
16}
17func main(argc: i64, argv: *i64) -> i64 {
18 gw("=== nx_js_instanceof_gate: instanceof on user ctors + inheritance (V8-pinned, both tiers) ===\n" as *u8)
19 var pass: i64 = 0
20 var tot: i64 = 0
21 tot=tot+1; pass = pass + chk2("own-ctor true" as *u8, "function A(){} var a=new A(); a instanceof A ? 1 : 0" as *u8, 1)
22 tot=tot+1; pass = pass + chk2("wrong-ctor false" as *u8, "function A(){} function B(){} var a=new A(); a instanceof B ? 1 : 0" as *u8, 0)
23 tot=tot+1; pass = pass + chk2("inherited true" as *u8, "function A(){} function B(){} B.prototype=new A(); var b=new B(); b instanceof A ? 1 : 0" as *u8, 1)
24 tot=tot+1; pass = pass + chk2("primitive LHS false" as *u8, "function A(){} 5 instanceof A ? 1 : 0" as *u8, 0)
25 tot=tot+1; pass = pass + chk2("in an if-guard" as *u8, "function P(){this.v=9;} var p=new P(); var r=0; if(p instanceof P){ r=p.v; } r" as *u8, 9)
26 gw(" --- " as *u8); gn(pass); gw("/" as *u8); gn(tot); gw(" ---\n" as *u8)
27 if pass == tot { gw("=== GREEN: instanceof matches V8 on user ctors + inheritance, both tiers ===\n" as *u8); return 0 }
28 gw("=== RED ===\n" as *u8)
29 return 1
30}