| 117 | self.offsets = [] |
| 118 | |
| 119 | def parse(self, filename): |
| 120 | source_map_json = json.loads(utils.read_file(filename)) |
| 121 | if shared.DEBUG: |
| 122 | print(source_map_json) |
| 123 | |
| 124 | self.version = source_map_json['version'] |
| 125 | self.sources = source_map_json['sources'] |
| 126 | self.funcs = source_map_json['names'] |
| 127 | |
| 128 | chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' |
| 129 | vlq_map = {c: i for i, c in enumerate(chars)} |
| 130 | |
| 131 | def decodeVLQ(string): |
| 132 | result = [] |
| 133 | shift = 0 |
| 134 | value = 0 |
| 135 | for c in string: |
| 136 | try: |
| 137 | integer = vlq_map[c] |
| 138 | except ValueError as e: |
| 139 | raise Error(f'Invalid character ({c}) in VLQ') from e |
| 140 | value += (integer & 31) << shift |
| 141 | if integer & 32: |
| 142 | shift += 5 |
| 143 | else: |
| 144 | negate = value & 1 |
| 145 | value >>= 1 |
| 146 | result.append(-value if negate else value) |
| 147 | value = shift = 0 |
| 148 | return result |
| 149 | |
| 150 | offset = 0 |
| 151 | src = 0 |
| 152 | line = 1 |
| 153 | col = 1 |
| 154 | func = 0 |
| 155 | for segment in source_map_json['mappings'].split(','): |
| 156 | data = decodeVLQ(segment) |
| 157 | info = [] |
| 158 | |
| 159 | offset += data[0] |
| 160 | if len(data) >= 2: |
| 161 | src += data[1] |
| 162 | info.append(src) |
| 163 | if len(data) >= 3: |
| 164 | line += data[2] |
| 165 | info.append(line) |
| 166 | if len(data) >= 4: |
| 167 | col += data[3] |
| 168 | info.append(col) |
| 169 | if len(data) == 5: |
| 170 | func += data[4] |
| 171 | info.append(func) |
| 172 | |
| 173 | self.mappings[offset] = WasmSourceMap.Location(*info) |
| 174 | self.offsets.append(offset) |
| 175 | self.offsets.sort() |
| 176 | |