Skip to content

Update wasm_offset_converter. NFC #24588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 71 additions & 72 deletions src/source_map_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,96 +4,95 @@
* SPDX-License-Identifier: MIT
*/

/**
* @constructor
*/
function WasmSourceMap(sourceMap) {
this.version = sourceMap.version;
this.sources = sourceMap.sources;
this.names = sourceMap.names;
class WasmSourceMap {
mapping = {};
offsets = [];

this.mapping = {};
this.offsets = [];
constructor(sourceMap) {
this.version = sourceMap.version;
this.sources = sourceMap.sources;
this.names = sourceMap.names;

var vlqMap = {};
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split('').forEach((c, i) => vlqMap[c] = i);
var vlqMap = {};
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split('').forEach((c, i) => vlqMap[c] = i);

// based on https://github.com/Rich-Harris/vlq/blob/master/src/vlq.ts
function decodeVLQ(string) {
var result = [];
var shift = 0;
var value = 0;
// based on https://github.com/Rich-Harris/vlq/blob/master/src/vlq.ts
function decodeVLQ(string) {
var result = [];
var shift = 0;
var value = 0;

for (var i = 0; i < string.length; ++i) {
var integer = vlqMap[string[i]];
if (integer === undefined) {
throw new Error('Invalid character (' + string[i] + ')');
}
for (var ch of string) {
var integer = vlqMap[ch];
if (integer === undefined) {
throw new Error(`Invalid character (${ch})`);
}

value += (integer & 31) << shift;
value += (integer & 31) << shift;

if (integer & 32) {
shift += 5;
} else {
var negate = value & 1;
value >>= 1;
result.push(negate ? -value : value);
value = shift = 0;
if (integer & 32) {
shift += 5;
} else {
var negate = value & 1;
value >>= 1;
result.push(negate ? -value : value);
value = shift = 0;
}
}
return result;
}
return result;
}

var offset = 0, src = 0, line = 1, col = 1, name = 0;
sourceMap.mappings.split(',').forEach(function (segment, index) {
if (!segment) return;
var data = decodeVLQ(segment);
var info = {};
var offset = 0, src = 0, line = 1, col = 1, name = 0;
sourceMap.mappings.split(',').forEach(function (segment, index) {
if (!segment) return;
var data = decodeVLQ(segment);
var info = {};

offset += data[0];
if (data.length >= 2) info.source = src += data[1];
if (data.length >= 3) info.line = line += data[2];
if (data.length >= 4) info.column = col += data[3];
if (data.length >= 5) info.name = name += data[4];
this.mapping[offset] = info;
this.offsets.push(offset);
}, this);
this.offsets.sort((a, b) => a - b);
}
offset += data[0];
if (data.length >= 2) info.source = src += data[1];
if (data.length >= 3) info.line = line += data[2];
if (data.length >= 4) info.column = col += data[3];
if (data.length >= 5) info.name = name += data[4];
this.mapping[offset] = info;
this.offsets.push(offset);
}, this);
this.offsets.sort((a, b) => a - b);
}

WasmSourceMap.prototype.lookup = function (offset) {
var normalized = this.normalizeOffset(offset);
lookup(offset) {
var normalized = this.normalizeOffset(offset);
#if USE_OFFSET_CONVERTER
if (!wasmOffsetConverter.isSameFunc(offset, normalized)) {
return null;
}
if (!wasmOffsetConverter.isSameFunc(offset, normalized)) {
return null;
}
#endif
var info = this.mapping[normalized];
if (!info) {
return null;
var info = this.mapping[normalized];
if (!info) {
return null;
}
return {
file: this.sources[info.source],
line: info.line,
column: info.column,
name: this.names[info.name],
};
}
return {
file: this.sources[info.source],
line: info.line,
column: info.column,
name: this.names[info.name],
};
}

WasmSourceMap.prototype.normalizeOffset = function (offset) {
var lo = 0;
var hi = this.offsets.length;
var mid;
normalizeOffset(offset) {
var lo = 0;
var hi = this.offsets.length;
var mid;

while (lo < hi) {
mid = Math.floor((lo + hi) / 2);
if (this.offsets[mid] > offset) {
hi = mid;
} else {
lo = mid + 1;
while (lo < hi) {
mid = Math.floor((lo + hi) / 2);
if (this.offsets[mid] > offset) {
hi = mid;
} else {
lo = mid + 1;
}
}
return this.offsets[lo - 1];
}
return this.offsets[lo - 1];
}

var wasmSourceMapFile = '{{{ WASM_BINARY_FILE }}}.map';
Expand Down
Loading