code wiki / _hdl_build / nx_js_proto_gate.nx
nx_js_proto_gate.nx source
↩ module page · 33 lines · 2920 B
1// nx_js_proto_gate.nx -- built-in prototype EXTENSION on the tree tier: user code adds methods to
2// String.prototype / Array.prototype and calls them on string/array primitives (this=receiver). This was the
3// EarleyBoyer runtime blocker (String.prototype was an error). Tree-only for now (VM BC_CALLM mirror = follow-on),
4// so tests use js_run_source directly. expect_exit: 0 license_tier: ORIGINAL
5import "nx_js_vm.nx"
6func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
7func 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 }
8static g_want: i64
9func tt(label: *u8, src: *u8) -> i64 { // TREE-tier check vs g_want (String.prototype is tree-only this rung)
10 let rt: *i64 = sys_mmap(16) as *i64
11 let r: i64 = js_run_source(src, bsl(src), rt)
12 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)
13 if r == 0 { if rt[1] == g_want { w(" ok\n" as *u8); return 1 } }
14 w(" FAIL\n" as *u8)
15 return 0
16}
17func main(argc: i64, argv: *i64) -> i64 {
18 w("=== nx_js_proto_gate: built-in prototype extension (String/Array.prototype), tree tier ===\n" as *u8)
19 var pass: i64 = 0
20 var tot: i64 = 0
21 g_want=1; tot=tot+1; pass=pass+tt("proto-assign-in " as *u8, "String.prototype.z=9; (\x22z\x22 in String.prototype)?1:0" as *u8)
22 g_want=6; tot=tot+1; pass=pass+tt("str-method-this " as *u8, "String.prototype.twice=function(){return this.length*2;}; \x22abc\x22.twice()" as *u8)
23 g_want=7; tot=tot+1; pass=pass+tt("str-method-arg " as *u8, "String.prototype.plus=function(n){return this.length+n;}; \x22abc\x22.plus(4)" as *u8)
24 g_want=20; tot=tot+1; pass=pass+tt("arr-method-this " as *u8, "Array.prototype.snd=function(){return this[1];}; var a=[10,20,30]; a.snd()" as *u8)
25 g_want=60; tot=tot+1; pass=pass+tt("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)
26 g_want=5; tot=tot+1; pass=pass+tt("str-read-method " as *u8, "String.prototype.five=function(){return 5;}; var f=\x22x\x22.five; f()" as *u8)
27 g_want=3; tot=tot+1; pass=pass+tt("builtin-still-ok" as *u8, "\x22abc\x22.length" as *u8)
28 g_want=1; tot=tot+1; pass=pass+tt("builtin-charat " as *u8, "(\x22abc\x22.charAt(0)==\x22a\x22)?1:0" as *u8)
29 w(" --- " as *u8); nn(pass); w("/" as *u8); nn(tot); w(" ---\n" as *u8)
30 if pass == tot { w("=== GREEN: user String/Array.prototype methods callable on primitives (tree) ===\n" as *u8); return 0 }
31 w("=== RED ===\n" as *u8)
32 return 1
33}