code wiki / _hdl_build / nx_parse_probe.nx

nx_parse_probe.nx source

↩ module page · 50 lines · 2643 B

1// nx_parse_probe.nx -- pinpoint the PARSER gaps that block minified jQuery. Tries jp_parse_source on each 2// ES5-minifier idiom jQuery uses; prints OK/FAIL per construct. The FAILs are the exact parser-hardening TODO. 3import "nx_syscalls.nx" 4import "nx_js_eval.nx" 5 6func cw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 7func tslen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 8 9func try_parse(src: *u8) -> i64 { 10 let ctxbox: *i64 = sys_mmap(16) as *i64 11 let prog: i64 = jp_parse_source(src, tslen(src), ctxbox) 12 let ctx: *i64 = (ctxbox[0]) as *i64 13 if (ctx as i64) == 0 { return 0 } 14 let pst: *i64 = jp_pst(ctx) 15 if pst[PST_ERR] == 1 { return 0 } 16 if jp_nkind(ctx, prog) != ND_PROGRAM { return 0 } 17 return 1 18} 19func probe(label: *u8, src: *u8) -> i64 { 20 if try_parse(src) == 1 { cw(" OK " as *u8) } else { cw(" FAIL " as *u8) } 21 cw(label); cw("\n" as *u8) 22 return 0 23} 24 25func main() -> i64 { 26 cw("=== parser probe (minified-jQuery idioms) ===\n" as *u8) 27 probe("regex literal /re/flags" as *u8, "var r=/[a-z]+/gi;r.test('x');" as *u8) 28 probe("regex after return" as *u8, "function f(){return /ab/.test('a');}" as *u8) 29 probe("regex after (" as *u8, "var x=(/a/).source;" as *u8) 30 probe("comma sequence" as *u8, "var a=1,b=2,c=3;a=(b,c);" as *u8) 31 probe("void 0" as *u8, "var u=void 0;" as *u8) 32 probe("ternary chain" as *u8, "var x=a?b:c?d:e;" as *u8) 33 probe("IIFE !function" as *u8, "!function(e,t){return e}(1,2);" as *u8) 34 probe("IIFE (function)()" as *u8, "(function(){return 1})();" as *u8) 35 probe("labeled break" as *u8, "a:for(var i=0;i<3;i++){break a;}" as *u8) 36 probe("labeled continue" as *u8, "o:for(;;){continue o;}" as *u8) 37 probe("for-in" as *u8, "for(var k in obj){x=k;}" as *u8) 38 probe("do-while" as *u8, "do{x++;}while(x<3);" as *u8) 39 probe("try optional-catch" as *u8, "try{f();}catch(e){}" as *u8) 40 probe("switch fallthrough" as *u8, "switch(x){case 1:case 2:y=1;break;default:y=0;}" as *u8) 41 probe("chained new+call" as *u8, "var d=new Date().getTime();" as *u8) 42 probe("bitwise+shift" as *u8, "var n=(a|0)&(b>>2)^~c;" as *u8) 43 probe("typeof compare" as *u8, "if(typeof x===\"object\"){y=1;}" as *u8) 44 probe("computed member" as *u8, "var v=o['a-b'][c];" as *u8) 45 probe("in operator" as *u8, "if('k' in o){y=1;}" as *u8) 46 probe("instanceof" as *u8, "if(x instanceof Array){y=1;}" as *u8) 47 probe("logical assign chain" as *u8, "var x=a&&b||c;" as *u8) 48 probe("nested fn expr" as *u8, "var f=function g(n){return n<2?1:g(n-1);};" as *u8) 49 return 0 50}