code wiki / _hdl_build / nx_js_regex_features_gate.nx

nx_js_regex_features_gate.nx source

↩ module page · 67 lines · 6555 B

1// nx_js_regex_features_gate.nx -- regex-engine features added during the Octane RegExp arc (2026-07-12), 2// V8-pinned on BOTH tiers. Locks in: lookahead (?=)/(?!), backreferences \1..\9, \xNN pattern escapes, AND 3// the catastrophic-empty-loop SAFETY fix (`/^(a*)*$/` used to SEGV the native stack -> now bounded, per the 4// engine's never-crash contract) + iterative single-atom loops (RE_LOOPA). Each value is exec-capture-length 5// summed (Exec) or a boolean, checked against node/V8. expect_exit: 0 license_tier: ORIGINAL 6import "nx_js_vm.nx" 7func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 8func 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 } 9static g_want: i64 10func ct(label: *u8, src: *u8) -> i64 { 11 let rt: *i64 = sys_mmap(16) as *i64 12 let r: i64 = js_run_source(src, bsl(src), rt) 13 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) 14 if r == 0 { if rt[1] == g_want { w(" ok\n" as *u8); return 1 } } 15 w(" FAIL\n" as *u8) 16 return 0 17} 18func cv(label: *u8, src: *u8) -> i64 { 19 let rt: *i64 = sys_mmap(16) as *i64 20 let r: i64 = compile_run(src, rt) 21 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) 22 if r == 0 { if rt[1] == g_want { w(" ok\n" as *u8); return 1 } } 23 w(" FAIL\n" as *u8) 24 return 0 25} 26// each src defines Exec + evaluates a numeric answer (V8-pinned in comments). 27func suite_one(mode: i64) -> i64 { 28 var p: i64 = 0 29 // lookahead / negative lookahead 30 g_want=1; if mode==0 { p=p+ct("look-pos " as *u8, "var re=/a(?=b)/; var m=re.exec(\x22ab\x22); m?m[0].length:0" as *u8) } else { p=p+cv("look-pos " as *u8, "var re=/a(?=b)/; var m=re.exec(\x22ab\x22); m?m[0].length:0" as *u8) } 31 g_want=0; if mode==0 { p=p+ct("look-pos-fail " as *u8, "var re=/a(?=b)/; re.test(\x22ac\x22)?1:0" as *u8) } else { p=p+cv("look-pos-fail " as *u8, "var re=/a(?=b)/; re.test(\x22ac\x22)?1:0" as *u8) } 32 g_want=1; if mode==0 { p=p+ct("look-neg " as *u8, "var re=/a(?!b)/; re.test(\x22ac\x22)?1:0" as *u8) } else { p=p+cv("look-neg " as *u8, "var re=/a(?!b)/; re.test(\x22ac\x22)?1:0" as *u8) } 33 g_want=0; if mode==0 { p=p+ct("look-neg-fail " as *u8, "var re=/a(?!b)/; re.test(\x22ab\x22)?1:0" as *u8) } else { p=p+cv("look-neg-fail " as *u8, "var re=/a(?!b)/; re.test(\x22ab\x22)?1:0" as *u8) } 34 // backreference 35 g_want=1; if mode==0 { p=p+ct("backref-hit " as *u8, "/(ab)\\1/.test(\x22abab\x22)?1:0" as *u8) } else { p=p+cv("backref-hit " as *u8, "/(ab)\\1/.test(\x22abab\x22)?1:0" as *u8) } 36 g_want=0; if mode==0 { p=p+ct("backref-miss " as *u8, "/(ab)\\1/.test(\x22abcd\x22)?1:0" as *u8) } else { p=p+cv("backref-miss " as *u8, "/(ab)\\1/.test(\x22abcd\x22)?1:0" as *u8) } 37 // \xNN pattern escape (against a \xNN-built string = byte-consistent) 38 g_want=1; if mode==0 { p=p+ct("xnn-match " as *u8, "/\\xc4/.test(\x22z\\xc4z\x22)?1:0" as *u8) } else { p=p+cv("xnn-match " as *u8, "/\\xc4/.test(\x22z\\xc4z\x22)?1:0" as *u8) } 39 // catastrophic empty loop must NOT crash and must match (safety contract) 40 g_want=1; if mode==0 { p=p+ct("empty-loop-safe" as *u8, "/^(a*)*$/.test(\x22aaa\x22)?1:0" as *u8) } else { p=p+cv("empty-loop-safe" as *u8, "/^(a*)*$/.test(\x22aaa\x22)?1:0" as *u8) } 41 g_want=1; if mode==0 { p=p+ct("nested-plus " as *u8, "/(a+)+b/.test(\x22aaab\x22)?1:0" as *u8) } else { p=p+cv("nested-plus " as *u8, "/(a+)+b/.test(\x22aaab\x22)?1:0" as *u8) } 42 // iterative single-atom loop (RE_LOOPA) greedy + backtrack correctness 43 g_want=5; if mode==0 { p=p+ct("greedy-loopa " as *u8, "var m=/a.*b/.exec(\x22axbxb\x22); m?m[0].length:0" as *u8) } else { p=p+cv("greedy-loopa " as *u8, "var m=/a.*b/.exec(\x22axbxb\x22); m?m[0].length:0" as *u8) } 44 g_want=3; if mode==0 { p=p+ct("lazy-loopa " as *u8, "var m=/a.*?b/.exec(\x22axbxb\x22); m?m[0].length:0" as *u8) } else { p=p+cv("lazy-loopa " as *u8, "var m=/a.*?b/.exec(\x22axbxb\x22); m?m[0].length:0" as *u8) } 45 g_want=3; if mode==0 { p=p+ct("class-plus " as *u8, "var m=/[0-9]+/.exec(\x22ab123c\x22); m?m[0].length:0" as *u8) } else { p=p+cv("class-plus " as *u8, "var m=/[0-9]+/.exec(\x22ab123c\x22); m?m[0].length:0" as *u8) } 46 // NUL byte preserved (String.fromCharCode(0)/concat/substring must NOT truncate -- binary-string correctness) 47 g_want=1; if mode==0 { p=p+ct("fcc-nul-len " as *u8, "String.fromCharCode(0).length" as *u8) } else { p=p+cv("fcc-nul-len " as *u8, "String.fromCharCode(0).length" as *u8) } 48 g_want=3; if mode==0 { p=p+ct("nul-concat " as *u8, "(\x22a\x22+String.fromCharCode(0)+\x22b\x22).length" as *u8) } else { p=p+cv("nul-concat " as *u8, "(\x22a\x22+String.fromCharCode(0)+\x22b\x22).length" as *u8) } 49 // \u in a CHARACTER CLASS: a high-codepoint range matches NO ASCII byte (re68 shape) 50 g_want=1; if mode==0 { p=p+ct("uclass-noascii " as *u8, "/^[\\w\\u0128-\\uffff]+$/.test(\x22abc\x22)?1:0" as *u8) } else { p=p+cv("uclass-noascii " as *u8, "/^[\\w\\u0128-\\uffff]+$/.test(\x22abc\x22)?1:0" as *u8) } 51 g_want=0; if mode==0 { p=p+ct("uclass-punct " as *u8, "/[\\u0128-\\uffff]/.test(\x22=\x22)?1:0" as *u8) } else { p=p+cv("uclass-punct " as *u8, "/[\\u0128-\\uffff]/.test(\x22=\x22)?1:0" as *u8) } 52 // `.` excludes CR as well as LF (V8) 53 g_want=0; if mode==0 { p=p+ct("dot-not-cr " as *u8, "/./.test(String.fromCharCode(13))?1:0" as *u8) } else { p=p+cv("dot-not-cr " as *u8, "/./.test(String.fromCharCode(13))?1:0" as *u8) } 54 return p 55} 56func main(argc: i64, argv: *i64) -> i64 { 57 w("=== nx_js_regex_features_gate: lookahead/backref/\\xNN/empty-loop-safe/iterative-loops, both tiers ===\n" as *u8) 58 var pass: i64 = 0 59 w("--- tree tier ---\n" as *u8) 60 pass = pass + suite_one(0) 61 w("--- VM/JIT tier ---\n" as *u8) 62 pass = pass + suite_one(1) 63 w(" --- " as *u8); nn(pass); w("/34 ---\n" as *u8) 64 if pass == 34 { w("=== GREEN: regex lookahead + backref + \\xNN + \\u-class + NUL-safe strings + .-not-CR + empty-loop crash-safe + iterative loops, V8-pinned both tiers ===\n" as *u8); return 0 } 65 w("=== RED ===\n" as *u8) 66 return 1 67}