summaryrefslogtreecommitdiff
path: root/src/wasm/wasm-binary.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/wasm/wasm-binary.cpp')
-rw-r--r--src/wasm/wasm-binary.cpp74
1 files changed, 61 insertions, 13 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index 466771803..920baf944 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -1189,7 +1189,7 @@ void WasmBinaryWriter::writeSymbolMap() {
}
void WasmBinaryWriter::initializeDebugInfo() {
- lastDebugLocation = {0, /* lineNumber = */ 1, 0};
+ lastDebugLocation = {0, /* lineNumber = */ 1, 0, std::nullopt};
}
void WasmBinaryWriter::writeSourceMapProlog() {
@@ -1225,7 +1225,17 @@ void WasmBinaryWriter::writeSourceMapProlog() {
// TODO respect JSON string encoding, e.g. quotes and control chars.
*sourceMap << "\"" << wasm->debugInfoFileNames[i] << "\"";
}
- *sourceMap << "],\"names\":[],\"mappings\":\"";
+ *sourceMap << "],\"names\":[";
+
+ for (size_t i = 0; i < wasm->debugInfoSymbolNames.size(); i++) {
+ if (i > 0) {
+ *sourceMap << ",";
+ }
+ // TODO respect JSON string encoding, e.g. quotes and control chars.
+ *sourceMap << "\"" << wasm->debugInfoSymbolNames[i] << "\"";
+ }
+
+ *sourceMap << "],\"mappings\":\"";
}
static void writeBase64VLQ(std::ostream& out, int32_t n) {
@@ -1249,7 +1259,10 @@ static void writeBase64VLQ(std::ostream& out, int32_t n) {
void WasmBinaryWriter::writeSourceMapEpilog() {
// write source map entries
size_t lastOffset = 0;
- Function::DebugLocation lastLoc = {0, /* lineNumber = */ 1, 0};
+ BinaryLocation lastFileIndex = 0;
+ BinaryLocation lastLineNumber = 1;
+ BinaryLocation lastColumnNumber = 0;
+ BinaryLocation lastSymbolNameIndex = 0;
for (const auto& [offset, loc] : sourceMapLocations) {
if (lastOffset > 0) {
*sourceMap << ",";
@@ -1257,13 +1270,20 @@ void WasmBinaryWriter::writeSourceMapEpilog() {
writeBase64VLQ(*sourceMap, int32_t(offset - lastOffset));
lastOffset = offset;
if (loc) {
- // There is debug information for this location, so emit the next 3
- // fields and update lastLoc.
- writeBase64VLQ(*sourceMap, int32_t(loc->fileIndex - lastLoc.fileIndex));
- writeBase64VLQ(*sourceMap, int32_t(loc->lineNumber - lastLoc.lineNumber));
- writeBase64VLQ(*sourceMap,
- int32_t(loc->columnNumber - lastLoc.columnNumber));
- lastLoc = *loc;
+ writeBase64VLQ(*sourceMap, int32_t(loc->fileIndex - lastFileIndex));
+ lastFileIndex = loc->fileIndex;
+
+ writeBase64VLQ(*sourceMap, int32_t(loc->lineNumber - lastLineNumber));
+ lastLineNumber = loc->lineNumber;
+
+ writeBase64VLQ(*sourceMap, int32_t(loc->columnNumber - lastColumnNumber));
+ lastColumnNumber = loc->columnNumber;
+
+ if (loc->symbolNameIndex) {
+ writeBase64VLQ(*sourceMap,
+ int32_t(*loc->symbolNameIndex - lastSymbolNameIndex));
+ lastSymbolNameIndex = *loc->symbolNameIndex;
+ }
}
}
*sourceMap << "\"}";
@@ -1716,7 +1736,7 @@ WasmBinaryReader::WasmBinaryReader(Module& wasm,
FeatureSet features,
const std::vector<char>& input)
: wasm(wasm), allocator(wasm.allocator), input(input), sourceMap(nullptr),
- nextDebugPos(0), nextDebugLocation{0, 0, 0},
+ nextDebugPos(0), nextDebugLocation{0, 0, 0, std::nullopt},
nextDebugLocationHasDebugInfo(false), debugLocation() {
wasm.features = features;
}
@@ -2885,6 +2905,21 @@ void WasmBinaryReader::readSourceMapHeader() {
mustReadChar(']');
}
+ if (findField("names")) {
+ skipWhitespace();
+ mustReadChar('[');
+ if (!maybeReadChar(']')) {
+ do {
+ std::string symbol;
+ readString(symbol);
+ Index index = wasm.debugInfoSymbolNames.size();
+ wasm.debugInfoSymbolNames.push_back(symbol);
+ debugInfoSymbolNameIndices[symbol] = index;
+ } while (maybeReadChar(','));
+ mustReadChar(']');
+ }
+ }
+
if (!findField("mappings")) {
throw MapParseException("cannot find the 'mappings' field in map");
}
@@ -2911,7 +2946,12 @@ void WasmBinaryReader::readSourceMapHeader() {
uint32_t lineNumber =
readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number
uint32_t columnNumber = readBase64VLQ(*sourceMap);
- nextDebugLocation = {fileIndex, lineNumber, columnNumber};
+ std::optional<BinaryLocation> symbolNameIndex;
+ peek = sourceMap->peek();
+ if (!(peek == ',' || peek == '\"')) {
+ symbolNameIndex = readBase64VLQ(*sourceMap);
+ }
+ nextDebugLocation = {fileIndex, lineNumber, columnNumber, symbolNameIndex};
nextDebugLocationHasDebugInfo = true;
}
}
@@ -2966,7 +3006,15 @@ void WasmBinaryReader::readNextDebugLocation() {
int32_t columnNumberDelta = readBase64VLQ(*sourceMap);
uint32_t columnNumber = nextDebugLocation.columnNumber + columnNumberDelta;
- nextDebugLocation = {fileIndex, lineNumber, columnNumber};
+ std::optional<BinaryLocation> symbolNameIndex;
+ peek = sourceMap->peek();
+ if (!(peek == ',' || peek == '\"')) {
+ int32_t symbolNameIndexDelta = readBase64VLQ(*sourceMap);
+ symbolNameIndex =
+ nextDebugLocation.symbolNameIndex.value_or(0) + symbolNameIndexDelta;
+ }
+
+ nextDebugLocation = {fileIndex, lineNumber, columnNumber, symbolNameIndex};
nextDebugLocationHasDebugInfo = true;
}
}