code wiki / _hdl_build / nx_js_objproto_gate.nx
nx_js_objproto_gate.nx source
↩ module page · 31 lines · 2469 B
1import "nx_gate_gn.nx"
2// nx_js_objproto_gate.nx -- shared Object.prototype + Object.defineProperty + inherited-method dispatch,
3// V8-pinned, both tiers. Covers the DeltaBlue object-model idiom: defineProperty installs a method on
4// Object.prototype, called on a CONSTRUCTOR function (Ctor.inheritsFrom) with this=Ctor; instances inherit
5// through the resulting chain; new Object() -> {}. expect_exit: 0 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_objproto_gate: Object.prototype + defineProperty + inherited dispatch (both tiers) ===\n" as *u8)
20 var pass: i64 = 0
21 var tot: i64 = 0
22 tot=tot+1; pass = pass + chk2("new-Object-empty" as *u8, "var o=new Object(); o.a=5; o.a" as *u8, 5)
23 tot=tot+1; pass = pass + chk2("defineProp-on-objproto-read" as *u8, "Object.defineProperty(Object.prototype,'gg',{value:7}); var o={}; o.gg" as *u8, 7)
24 tot=tot+1; pass = pass + chk2("objproto-method-on-object" as *u8, "Object.defineProperty(Object.prototype,'m1',{value:function(){return 3;}}); var o={}; o.m1()" as *u8, 3)
25 tot=tot+1; pass = pass + chk2("objproto-method-on-function-this" as *u8, "Object.defineProperty(Object.prototype,'setp',{value:function(v){this.p=v;}}); function F(){} F.setp(9); F.p" as *u8, 9)
26 tot=tot+1; pass = pass + chk2("inheritsFrom-full-chain" as *u8, "Object.defineProperty(Object.prototype,'inh',{value:function(s){function I(){} I.prototype=s.prototype; this.prototype=new I();}}); function A(){} A.prototype.k=function(){return 4;}; function B(){} B.inh(A); var b=new B(); b.k()" as *u8, 4)
27 gw(" --- " as *u8); gn(pass); gw("/" as *u8); gn(tot); gw(" ---\n" as *u8)
28 if pass == tot { gw("=== GREEN: Object.prototype + defineProperty + inherited dispatch match V8, both tiers ===\n" as *u8); return 0 }
29 gw("=== RED ===\n" as *u8)
30 return 1
31}