codegen-test-common.js (1378B)
1 // Set to true to emit ' +' instead of the unreadable '\s+'. 2 var SPACEDEBUG = false; 3 4 // Any hex string 5 var HEX = '[0-9a-fA-F]' 6 var HEXES = `${HEX}+`; 7 8 function wrap(options, funcs) { 9 if ('memory' in options) 10 return `(module (memory ${options.memory}) ${funcs})`; 11 return `(module ${funcs})`; 12 } 13 14 function fixlines(s) { 15 return s.split(/\n+/) 16 .map(strip) 17 .filter(x => x.length > 0) 18 .map(spaces) 19 .join('\n'); 20 } 21 22 function stripencoding(s, insEncoding) { 23 var encoding = RegExp(`^(?:0x)?${HEX}+\\s+${insEncoding}\\s+(.*)$`); 24 return s.split('\n') 25 .map(x => x.match(encoding)?.[1] ?? x) 26 .join('\n'); 27 } 28 29 function strip(s) { 30 var start = 0, end = s.length; 31 while (start < s.length && isspace(s.charAt(start))) 32 start++; 33 while (end > start && isspace(s.charAt(end - 1))) 34 end--; 35 return s.substring(start, end); 36 } 37 38 function striplines(s) { 39 return s.split('\n').map(strip).join('\n'); 40 } 41 42 function spaces(s) { 43 let t = ''; 44 let i = 0; 45 while (i < s.length) { 46 if (isspace(s.charAt(i))) { 47 t += SPACEDEBUG ? ' +' : '\\s+'; 48 i++; 49 while (i < s.length && isspace(s.charAt(i))) 50 i++; 51 } else { 52 t += s.charAt(i++); 53 } 54 } 55 return t; 56 } 57 58 function isspace(c) { 59 return c == ' ' || c == '\t'; 60 }