summaryrefslogtreecommitdiff
path: root/src/wasm/wasm-binary.cpp
diff options
context:
space:
mode:
authorSam Clegg <sbc@chromium.org>2018-06-13 16:01:14 -0300
committerGitHub <noreply@github.com>2018-06-13 16:01:14 -0300
commitaab16136b9752eb0586b3a5b32b1d0e7a5803835 (patch)
tree2349ff2f5496f0f9399739c86f9dc921de21b762 /src/wasm/wasm-binary.cpp
parent7bed8a09423dbe94200fd73bf1b9ede5eb646e59 (diff)
downloadbinaryen-aab16136b9752eb0586b3a5b32b1d0e7a5803835.tar.gz
binaryen-aab16136b9752eb0586b3a5b32b1d0e7a5803835.tar.bz2
binaryen-aab16136b9752eb0586b3a5b32b1d0e7a5803835.zip
Improve source map parsing to handle whitespace (#1598)
Diffstat (limited to 'src/wasm/wasm-binary.cpp')
-rw-r--r--src/wasm/wasm-binary.cpp48
1 files changed, 34 insertions, 14 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index c36496bfe..3602a70e7 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -1832,39 +1832,55 @@ static int32_t readBase64VLQ(std::istream& in) {
void WasmBinaryBuilder::readSourceMapHeader() {
if (!sourceMap) return;
+ auto skipWhitespace = [&]() {
+ while (sourceMap->peek() == ' ' || sourceMap->peek() == '\n')
+ sourceMap->get();
+ };
+
auto maybeReadChar = [&](char expected) {
if (sourceMap->peek() != expected) return false;
sourceMap->get();
return true;
};
+
auto mustReadChar = [&](char expected) {
- if (sourceMap->get() != expected) {
- throw MapParseException("Unexpected char");
+ char c = sourceMap->get();
+ if (c != expected) {
+ throw MapParseException(std::string("Unexpected char: expected '") +
+ expected + "' got '" + c + "'");
}
};
- auto findField = [&](const char* name, size_t len) {
+
+ auto findField = [&](const char* name) {
bool matching = false;
+ size_t len = strlen(name);
size_t pos;
while (1) {
int ch = sourceMap->get();
if (ch == EOF) return false;
if (ch == '\"') {
- matching = true;
- pos = 0;
+ if (matching) {
+ // we matched a terminating quote.
+ if (pos == len)
+ break;
+ matching = false;
+ } else {
+ matching = true;
+ pos = 0;
+ }
} else if (matching && name[pos] == ch) {
++pos;
- if (pos == len) {
- if (maybeReadChar('\"')) break; // found field
- }
- } else {
- matching = false;
}
}
+ skipWhitespace();
mustReadChar(':');
+ skipWhitespace();
return true;
};
+
auto readString = [&](std::string& str) {
std::vector<char> vec;
+ skipWhitespace();
mustReadChar('\"');
if (!maybeReadChar('\"')) {
while (1) {
@@ -1876,12 +1892,15 @@ void WasmBinaryBuilder::readSourceMapHeader() {
vec.push_back(ch);
}
}
+ skipWhitespace();
str = std::string(vec.begin(), vec.end());
};
- if (!findField("sources", strlen("sources"))) {
- throw MapParseException("cannot find the sources field in map");
+ if (!findField("sources")) {
+ throw MapParseException("cannot find the 'sources' field in map");
}
+
+ skipWhitespace();
mustReadChar('[');
if (!maybeReadChar(']')) {
do {
@@ -1894,9 +1913,10 @@ void WasmBinaryBuilder::readSourceMapHeader() {
mustReadChar(']');
}
- if (!findField("mappings", strlen("mappings"))) {
- throw MapParseException("cannot find the mappings field in map");
+ if (!findField("mappings")) {
+ throw MapParseException("cannot find the 'mappings' field in map");
}
+
mustReadChar('\"');
if (maybeReadChar('\"')) { // empty mappings
nextDebugLocation.first = 0;