custom-section.js (1251B)
1 function arraysEqual(a, b) { 2 if (a.length !== b.length) { 3 return false; 4 } 5 for (let i = 0; i < b.length; i++) { 6 if (a[i] !== b[i]) { 7 return false; 8 } 9 } 10 return true; 11 } 12 13 function testCustomSection(moduleText, customSectionName, expectedBytes) { 14 let module = new WebAssembly.Module(wasmTextToBinary(moduleText)); 15 16 let sections = WebAssembly.Module.customSections(module, customSectionName); 17 assertEq(sections.length, 1); 18 19 let section = sections[0]; 20 let sectionView = new Uint8Array(section); 21 22 if (!arraysEqual(sectionView, expectedBytes)) { 23 let got = JSON.stringify(Array.from(sectionView)); 24 let expected = JSON.stringify(Array.from(expectedBytes)); 25 assertEq(true, false, `got: ${got}, expected: ${expected}`) 26 } 27 } 28 29 // Test an unknown custom section 30 testCustomSection( 31 `(module (@custom "unknown" "\\00\\01\\02\\03\\04"))`, 32 "unknown", 33 [0, 1, 2, 3, 4]); 34 35 // Test the name section 36 testCustomSection( 37 `(module (func $test))`, 38 "name", 39 [ 40 /* subsection is functions */ 1, 41 /* subsection is 7 bytes */ 7, 42 /* name map has 1 entry */ 1, 43 /* first entry is index */ 0, 44 /* first entry string 4 bytes */ 4, 45 /* 't' */ 116, 46 /* 'e' */ 101, 47 /* 's' */ 115, 48 /* 't' */ 116]);