Skip to content

Commit b4b5469

Browse files
authored
Update wasm_offset_converter. NFC (emscripten-core#24588)
- Convert to ES class - Use string templates
1 parent 4a818e6 commit b4b5469

File tree

2 files changed

+200
-200
lines changed

2 files changed

+200
-200
lines changed

src/source_map_support.js

Lines changed: 71 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,96 +4,95 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7-
/**
8-
* @constructor
9-
*/
10-
function WasmSourceMap(sourceMap) {
11-
this.version = sourceMap.version;
12-
this.sources = sourceMap.sources;
13-
this.names = sourceMap.names;
7+
class WasmSourceMap {
8+
mapping = {};
9+
offsets = [];
1410

15-
this.mapping = {};
16-
this.offsets = [];
11+
constructor(sourceMap) {
12+
this.version = sourceMap.version;
13+
this.sources = sourceMap.sources;
14+
this.names = sourceMap.names;
1715

18-
var vlqMap = {};
19-
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split('').forEach((c, i) => vlqMap[c] = i);
16+
var vlqMap = {};
17+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split('').forEach((c, i) => vlqMap[c] = i);
2018

21-
// based on https://github.com/Rich-Harris/vlq/blob/master/src/vlq.ts
22-
function decodeVLQ(string) {
23-
var result = [];
24-
var shift = 0;
25-
var value = 0;
19+
// based on https://github.com/Rich-Harris/vlq/blob/master/src/vlq.ts
20+
function decodeVLQ(string) {
21+
var result = [];
22+
var shift = 0;
23+
var value = 0;
2624

27-
for (var i = 0; i < string.length; ++i) {
28-
var integer = vlqMap[string[i]];
29-
if (integer === undefined) {
30-
throw new Error('Invalid character (' + string[i] + ')');
31-
}
25+
for (var ch of string) {
26+
var integer = vlqMap[ch];
27+
if (integer === undefined) {
28+
throw new Error(`Invalid character (${ch})`);
29+
}
3230

33-
value += (integer & 31) << shift;
31+
value += (integer & 31) << shift;
3432

35-
if (integer & 32) {
36-
shift += 5;
37-
} else {
38-
var negate = value & 1;
39-
value >>= 1;
40-
result.push(negate ? -value : value);
41-
value = shift = 0;
33+
if (integer & 32) {
34+
shift += 5;
35+
} else {
36+
var negate = value & 1;
37+
value >>= 1;
38+
result.push(negate ? -value : value);
39+
value = shift = 0;
40+
}
4241
}
42+
return result;
4343
}
44-
return result;
45-
}
4644

47-
var offset = 0, src = 0, line = 1, col = 1, name = 0;
48-
sourceMap.mappings.split(',').forEach(function (segment, index) {
49-
if (!segment) return;
50-
var data = decodeVLQ(segment);
51-
var info = {};
45+
var offset = 0, src = 0, line = 1, col = 1, name = 0;
46+
sourceMap.mappings.split(',').forEach(function (segment, index) {
47+
if (!segment) return;
48+
var data = decodeVLQ(segment);
49+
var info = {};
5250

53-
offset += data[0];
54-
if (data.length >= 2) info.source = src += data[1];
55-
if (data.length >= 3) info.line = line += data[2];
56-
if (data.length >= 4) info.column = col += data[3];
57-
if (data.length >= 5) info.name = name += data[4];
58-
this.mapping[offset] = info;
59-
this.offsets.push(offset);
60-
}, this);
61-
this.offsets.sort((a, b) => a - b);
62-
}
51+
offset += data[0];
52+
if (data.length >= 2) info.source = src += data[1];
53+
if (data.length >= 3) info.line = line += data[2];
54+
if (data.length >= 4) info.column = col += data[3];
55+
if (data.length >= 5) info.name = name += data[4];
56+
this.mapping[offset] = info;
57+
this.offsets.push(offset);
58+
}, this);
59+
this.offsets.sort((a, b) => a - b);
60+
}
6361

64-
WasmSourceMap.prototype.lookup = function (offset) {
65-
var normalized = this.normalizeOffset(offset);
62+
lookup(offset) {
63+
var normalized = this.normalizeOffset(offset);
6664
#if USE_OFFSET_CONVERTER
67-
if (!wasmOffsetConverter.isSameFunc(offset, normalized)) {
68-
return null;
69-
}
65+
if (!wasmOffsetConverter.isSameFunc(offset, normalized)) {
66+
return null;
67+
}
7068
#endif
71-
var info = this.mapping[normalized];
72-
if (!info) {
73-
return null;
69+
var info = this.mapping[normalized];
70+
if (!info) {
71+
return null;
72+
}
73+
return {
74+
file: this.sources[info.source],
75+
line: info.line,
76+
column: info.column,
77+
name: this.names[info.name],
78+
};
7479
}
75-
return {
76-
file: this.sources[info.source],
77-
line: info.line,
78-
column: info.column,
79-
name: this.names[info.name],
80-
};
81-
}
8280

83-
WasmSourceMap.prototype.normalizeOffset = function (offset) {
84-
var lo = 0;
85-
var hi = this.offsets.length;
86-
var mid;
81+
normalizeOffset(offset) {
82+
var lo = 0;
83+
var hi = this.offsets.length;
84+
var mid;
8785

88-
while (lo < hi) {
89-
mid = Math.floor((lo + hi) / 2);
90-
if (this.offsets[mid] > offset) {
91-
hi = mid;
92-
} else {
93-
lo = mid + 1;
86+
while (lo < hi) {
87+
mid = Math.floor((lo + hi) / 2);
88+
if (this.offsets[mid] > offset) {
89+
hi = mid;
90+
} else {
91+
lo = mid + 1;
92+
}
9493
}
94+
return this.offsets[lo - 1];
9595
}
96-
return this.offsets[lo - 1];
9796
}
9897

9998
var wasmSourceMapFile = '{{{ WASM_BINARY_FILE }}}.map';

0 commit comments

Comments
 (0)