Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
chen ruixiang committed Feb 23, 2023
1 parent ccccb90 commit e951f70
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,12 @@ class WasmBinaryBuilder {
MixedArena& allocator;
const std::vector<char>& input;
std::istream* sourceMap;
std::tuple<uint32_t, uint32_t, Function::DebugLocation> nextDebugLocation; // available pos, previous pos, next debug location
struct NextDebugLocation {
uint32_t availablePos;
uint32_t previousPos;
Function::DebugLocation next;
};
NextDebugLocation nextDebugLocation;
bool debugInfo = true;
bool DWARF = false;
bool skipFunctionBodies = false;
Expand Down
22 changes: 11 additions & 11 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,7 @@ WasmBinaryBuilder::WasmBinaryBuilder(Module& wasm,
FeatureSet features,
const std::vector<char>& input)
: wasm(wasm), allocator(wasm.allocator), input(input), sourceMap(nullptr),
nextDebugLocation(0, 0, {0, 0, 0}), debugLocation() {
nextDebugLocation{0, 0, {0, 0, 0}}, debugLocation() {
wasm.features = features;
}

Expand Down Expand Up @@ -2741,7 +2741,7 @@ void WasmBinaryBuilder::readSourceMapHeader() {

mustReadChar('\"');
if (maybeReadChar('\"')) { // empty mappings
std::get<0>(nextDebugLocation) = 0;
nextDebugLocation.availablePos = 0;
return;
}
// read first debug location
Expand All @@ -2758,41 +2758,41 @@ void WasmBinaryBuilder::readNextDebugLocation() {
return;
}

if (std::get<0>(nextDebugLocation) == 0 && std::get<1>(nextDebugLocation) <= pos) {
if (nextDebugLocation.availablePos == 0 && nextDebugLocation.previousPos <= pos) {
// if source map file had already reached the end and cache position also cannot cover the pos
// clear the debug location
debugLocation.clear();
return;
}

while (std::get<0>(nextDebugLocation) && std::get<0>(nextDebugLocation) <= pos) {
while (nextDebugLocation.availablePos && nextDebugLocation.availablePos <= pos) {
debugLocation.clear();
// use debugLocation only for function expressions
if (currFunction) {
debugLocation.insert(std::get<2>(nextDebugLocation));
debugLocation.insert(nextDebugLocation.next);
}

char ch;
*sourceMap >> ch;
if (ch == '\"') { // end of records
std::get<0>(nextDebugLocation) = 0;
nextDebugLocation.availablePos = 0;
break;
}
if (ch != ',') {
throw MapParseException("Unexpected delimiter");
}

int32_t positionDelta = readBase64VLQ(*sourceMap);
uint32_t position = std::get<0>(nextDebugLocation) + positionDelta;
uint32_t position = nextDebugLocation.availablePos + positionDelta;
int32_t fileIndexDelta = readBase64VLQ(*sourceMap);
uint32_t fileIndex = std::get<2>(nextDebugLocation).fileIndex + fileIndexDelta;
uint32_t fileIndex = nextDebugLocation.next.fileIndex + fileIndexDelta;
int32_t lineNumberDelta = readBase64VLQ(*sourceMap);
uint32_t lineNumber = std::get<2>(nextDebugLocation).lineNumber + lineNumberDelta;
uint32_t lineNumber = nextDebugLocation.next.lineNumber + lineNumberDelta;
int32_t columnNumberDelta = readBase64VLQ(*sourceMap);
uint32_t columnNumber =
std::get<2>(nextDebugLocation).columnNumber + columnNumberDelta;
nextDebugLocation.next.columnNumber + columnNumberDelta;

nextDebugLocation = {position, std::get<0>(nextDebugLocation), {fileIndex, lineNumber, columnNumber}};
nextDebugLocation = {position, nextDebugLocation.availablePos, {fileIndex, lineNumber, columnNumber}};
}
}

Expand Down

0 comments on commit e951f70

Please sign in to comment.