summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/custom-section.js
blob: e0a9946242d2fd95aa6dddb8d5bb47b79e1a5435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function arraysEqual(a, b) {
  if (a.length !== b.length) {
    return false;
  }
  for (let i = 0; i < b.length; i++) {
    if (a[i] !== b[i]) {
      return false;
    }
  }
  return true;
}

function testCustomSection(moduleText, customSectionName, expectedBytes) {
  let module = new WebAssembly.Module(wasmTextToBinary(moduleText));

  let sections = WebAssembly.Module.customSections(module, customSectionName);
  assertEq(sections.length, 1);

  let section = sections[0];
  let sectionView = new Uint8Array(section);

  if (!arraysEqual(sectionView, expectedBytes)) {
    let got = JSON.stringify(Array.from(sectionView));
    let expected = JSON.stringify(Array.from(expectedBytes));
    assertEq(true, false, `got: ${got}, expected: ${expected}`)
  }
}

// Test an unknown custom section
testCustomSection(
  `(module (@custom "unknown" "\\00\\01\\02\\03\\04"))`,
  "unknown",
  [0, 1, 2, 3, 4]);

// Test the name section
testCustomSection(
  `(module (func $test))`,
  "name",
  [
    /* subsection is functions */ 1,
    /* subsection is 7 bytes */ 7,
    /* name map has 1 entry */ 1,
    /* first entry is index */ 0,
    /* first entry string 4 bytes */ 4,
    /* 't' */ 116,
    /* 'e' */ 101,
    /* 's' */ 115,
    /* 't' */ 116]);