Projekt

Obecné

Profil

Stáhnout (3.22 KB) Statistiky
| Větev: | Revize:
1
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2

    
3
import { encodeNode } from "@webassemblyjs/wasm-gen";
4
import { overrideBytesInBuffer } from "@webassemblyjs/helper-buffer";
5
import constants from "@webassemblyjs/helper-wasm-bytecode";
6
import * as t from "@webassemblyjs/ast";
7

    
8
function findLastSection(ast, forSection) {
9
  var targetSectionId = constants.sections[forSection]; // $FlowIgnore: metadata can not be empty
10

    
11
  var moduleSections = ast.body[0].metadata.sections;
12
  var lastSection;
13
  var lastId = 0;
14

    
15
  for (var i = 0, len = moduleSections.length; i < len; i++) {
16
    var section = moduleSections[i]; // Ignore custom section since they can actually occur everywhere
17

    
18
    if (section.section === "custom") {
19
      continue;
20
    }
21

    
22
    var sectionId = constants.sections[section.section];
23

    
24
    if (targetSectionId > lastId && targetSectionId < sectionId) {
25
      return lastSection;
26
    }
27

    
28
    lastId = sectionId;
29
    lastSection = section;
30
  }
31

    
32
  return lastSection;
33
}
34

    
35
export function createEmptySection(ast, uint8Buffer, section) {
36
  // previous section after which we are going to insert our section
37
  var lastSection = findLastSection(ast, section);
38
  var start, end;
39
  /**
40
   * It's the first section
41
   */
42

    
43
  if (lastSection == null || lastSection.section === "custom") {
44
    start = 8
45
    /* wasm header size */
46
    ;
47
    end = start;
48
  } else {
49
    start = lastSection.startOffset + lastSection.size.value + 1;
50
    end = start;
51
  } // section id
52

    
53

    
54
  start += 1;
55
  var sizeStartLoc = {
56
    line: -1,
57
    column: start
58
  };
59
  var sizeEndLoc = {
60
    line: -1,
61
    column: start + 1
62
  }; // 1 byte for the empty vector
63

    
64
  var size = t.withLoc(t.numberLiteralFromRaw(1), sizeEndLoc, sizeStartLoc);
65
  var vectorOfSizeStartLoc = {
66
    line: -1,
67
    column: sizeEndLoc.column
68
  };
69
  var vectorOfSizeEndLoc = {
70
    line: -1,
71
    column: sizeEndLoc.column + 1
72
  };
73
  var vectorOfSize = t.withLoc(t.numberLiteralFromRaw(0), vectorOfSizeEndLoc, vectorOfSizeStartLoc);
74
  var sectionMetadata = t.sectionMetadata(section, start, size, vectorOfSize);
75
  var sectionBytes = encodeNode(sectionMetadata);
76
  uint8Buffer = overrideBytesInBuffer(uint8Buffer, start - 1, end, sectionBytes); // Add section into the AST for later lookups
77

    
78
  if (_typeof(ast.body[0].metadata) === "object") {
79
    // $FlowIgnore: metadata can not be empty
80
    ast.body[0].metadata.sections.push(sectionMetadata);
81
    t.sortSectionMetadata(ast.body[0]);
82
  }
83
  /**
84
   * Update AST
85
   */
86
  // Once we hit our section every that is after needs to be shifted by the delta
87

    
88

    
89
  var deltaBytes = +sectionBytes.length;
90
  var encounteredSection = false;
91
  t.traverse(ast, {
92
    SectionMetadata: function SectionMetadata(path) {
93
      if (path.node.section === section) {
94
        encounteredSection = true;
95
        return;
96
      }
97

    
98
      if (encounteredSection === true) {
99
        t.shiftSection(ast, path.node, deltaBytes);
100
      }
101
    }
102
  });
103
  return {
104
    uint8Buffer: uint8Buffer,
105
    sectionMetadata: sectionMetadata
106
  };
107
}
(1-1/4)