@@ -1189,7 +1189,7 @@ void WasmBinaryWriter::writeSymbolMap() {
1189
1189
}
1190
1190
1191
1191
void WasmBinaryWriter::initializeDebugInfo() {
1192
- lastDebugLocation = {0, /* lineNumber = */ 1, 0};
1192
+ lastDebugLocation = {0, /* lineNumber = */ 1, 0, std::nullopt };
1193
1193
}
1194
1194
1195
1195
void WasmBinaryWriter::writeSourceMapProlog() {
@@ -1225,7 +1225,17 @@ void WasmBinaryWriter::writeSourceMapProlog() {
1225
1225
// TODO respect JSON string encoding, e.g. quotes and control chars.
1226
1226
*sourceMap << "\"" << wasm->debugInfoFileNames[i] << "\"";
1227
1227
}
1228
- *sourceMap << "],\"names\":[],\"mappings\":\"";
1228
+ *sourceMap << "],\"names\":[";
1229
+
1230
+ for (size_t i = 0; i < wasm->debugInfoSymbolNames.size(); i++) {
1231
+ if (i > 0) {
1232
+ *sourceMap << ",";
1233
+ }
1234
+ // TODO respect JSON string encoding, e.g. quotes and control chars.
1235
+ *sourceMap << "\"" << wasm->debugInfoSymbolNames[i] << "\"";
1236
+ }
1237
+
1238
+ *sourceMap << "],\"mappings\":\"";
1229
1239
}
1230
1240
1231
1241
static void writeBase64VLQ(std::ostream& out, int32_t n) {
@@ -1249,21 +1259,31 @@ static void writeBase64VLQ(std::ostream& out, int32_t n) {
1249
1259
void WasmBinaryWriter::writeSourceMapEpilog() {
1250
1260
// write source map entries
1251
1261
size_t lastOffset = 0;
1252
- Function::DebugLocation lastLoc = {0, /* lineNumber = */ 1, 0};
1262
+ BinaryLocation lastFileIndex = 0;
1263
+ BinaryLocation lastLineNumber = 1;
1264
+ BinaryLocation lastColumnNumber = 0;
1265
+ BinaryLocation lastSymbolNameIndex = 0;
1253
1266
for (const auto& [offset, loc] : sourceMapLocations) {
1254
1267
if (lastOffset > 0) {
1255
1268
*sourceMap << ",";
1256
1269
}
1257
1270
writeBase64VLQ(*sourceMap, int32_t(offset - lastOffset));
1258
1271
lastOffset = offset;
1259
1272
if (loc) {
1260
- // There is debug information for this location, so emit the next 3
1261
- // fields and update lastLoc.
1262
- writeBase64VLQ(*sourceMap, int32_t(loc->fileIndex - lastLoc.fileIndex));
1263
- writeBase64VLQ(*sourceMap, int32_t(loc->lineNumber - lastLoc.lineNumber));
1264
- writeBase64VLQ(*sourceMap,
1265
- int32_t(loc->columnNumber - lastLoc.columnNumber));
1266
- lastLoc = *loc;
1273
+ writeBase64VLQ(*sourceMap, int32_t(loc->fileIndex - lastFileIndex));
1274
+ lastFileIndex = loc->fileIndex;
1275
+
1276
+ writeBase64VLQ(*sourceMap, int32_t(loc->lineNumber - lastLineNumber));
1277
+ lastLineNumber = loc->lineNumber;
1278
+
1279
+ writeBase64VLQ(*sourceMap, int32_t(loc->columnNumber - lastColumnNumber));
1280
+ lastColumnNumber = loc->columnNumber;
1281
+
1282
+ if (loc->symbolNameIndex) {
1283
+ writeBase64VLQ(*sourceMap,
1284
+ int32_t(*loc->symbolNameIndex - lastSymbolNameIndex));
1285
+ lastSymbolNameIndex = *loc->symbolNameIndex;
1286
+ }
1267
1287
}
1268
1288
}
1269
1289
*sourceMap << "\"}";
@@ -1716,7 +1736,7 @@ WasmBinaryReader::WasmBinaryReader(Module& wasm,
1716
1736
FeatureSet features,
1717
1737
const std::vector<char>& input)
1718
1738
: wasm(wasm), allocator(wasm.allocator), input(input), sourceMap(nullptr),
1719
- nextDebugPos(0), nextDebugLocation{0, 0, 0},
1739
+ nextDebugPos(0), nextDebugLocation{0, 0, 0, std::nullopt },
1720
1740
nextDebugLocationHasDebugInfo(false), debugLocation() {
1721
1741
wasm.features = features;
1722
1742
}
@@ -2885,6 +2905,21 @@ void WasmBinaryReader::readSourceMapHeader() {
2885
2905
mustReadChar(']');
2886
2906
}
2887
2907
2908
+ if (findField("names")) {
2909
+ skipWhitespace();
2910
+ mustReadChar('[');
2911
+ if (!maybeReadChar(']')) {
2912
+ do {
2913
+ std::string symbol;
2914
+ readString(symbol);
2915
+ Index index = wasm.debugInfoSymbolNames.size();
2916
+ wasm.debugInfoSymbolNames.push_back(symbol);
2917
+ debugInfoSymbolNameIndices[symbol] = index;
2918
+ } while (maybeReadChar(','));
2919
+ mustReadChar(']');
2920
+ }
2921
+ }
2922
+
2888
2923
if (!findField("mappings")) {
2889
2924
throw MapParseException("cannot find the 'mappings' field in map");
2890
2925
}
@@ -2911,7 +2946,12 @@ void WasmBinaryReader::readSourceMapHeader() {
2911
2946
uint32_t lineNumber =
2912
2947
readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number
2913
2948
uint32_t columnNumber = readBase64VLQ(*sourceMap);
2914
- nextDebugLocation = {fileIndex, lineNumber, columnNumber};
2949
+ std::optional<BinaryLocation> symbolNameIndex;
2950
+ peek = sourceMap->peek();
2951
+ if (!(peek == ',' || peek == '\"')) {
2952
+ symbolNameIndex = readBase64VLQ(*sourceMap);
2953
+ }
2954
+ nextDebugLocation = {fileIndex, lineNumber, columnNumber, symbolNameIndex};
2915
2955
nextDebugLocationHasDebugInfo = true;
2916
2956
}
2917
2957
}
@@ -2966,7 +3006,15 @@ void WasmBinaryReader::readNextDebugLocation() {
2966
3006
int32_t columnNumberDelta = readBase64VLQ(*sourceMap);
2967
3007
uint32_t columnNumber = nextDebugLocation.columnNumber + columnNumberDelta;
2968
3008
2969
- nextDebugLocation = {fileIndex, lineNumber, columnNumber};
3009
+ std::optional<BinaryLocation> symbolNameIndex;
3010
+ peek = sourceMap->peek();
3011
+ if (!(peek == ',' || peek == '\"')) {
3012
+ int32_t symbolNameIndexDelta = readBase64VLQ(*sourceMap);
3013
+ symbolNameIndex =
3014
+ nextDebugLocation.symbolNameIndex.value_or(0) + symbolNameIndexDelta;
3015
+ }
3016
+
3017
+ nextDebugLocation = {fileIndex, lineNumber, columnNumber, symbolNameIndex};
2970
3018
nextDebugLocationHasDebugInfo = true;
2971
3019
}
2972
3020
}
0 commit comments