diff options
author | Alon Zakai <alonzakai@gmail.com> | 2017-06-01 17:23:01 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2017-06-01 17:23:01 -0700 |
commit | 0db29f6e05f03800b05b4778c118818149a193f9 (patch) | |
tree | 42aca780387f324b2b7361957dfaa2079aacd1a6 /src | |
parent | f47d054dcc34dd709f36716c9fab07087db829e1 (diff) | |
download | binaryen-0db29f6e05f03800b05b4778c118818149a193f9.tar.gz binaryen-0db29f6e05f03800b05b4778c118818149a193f9.tar.bz2 binaryen-0db29f6e05f03800b05b4778c118818149a193f9.zip |
refactor s-expr parsing code to remove duplication and unnecessary things
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm-s-parser.h | 12 | ||||
-rw-r--r-- | src/wasm/wasm-s-parser.cpp | 50 |
2 files changed, 23 insertions, 39 deletions
diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index b02b6523b..53594e7aa 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -53,10 +53,10 @@ class Element { public: Element(MixedArena& allocator) : isList_(true), list_(allocator), line(-1), col(-1), loc(nullptr) {} - bool isList() { return isList_; } - bool isStr() { return !isList_; } - bool dollared() { return isStr() && dollared_; } - bool quoted() { return isStr() && quoted_; } + bool isList() const { return isList_; } + bool isStr() const { return !isList_; } + bool dollared() const { return isStr() && dollared_; } + bool quoted() const { return isStr() && quoted_; } size_t line, col; SourceLocation* loc; @@ -69,8 +69,8 @@ public: } // string methods - cashew::IString str(); - const char* c_str(); + cashew::IString str() const; + const char* c_str() const; Element* setString(cashew::IString str__, bool dollared__, bool quoted__); Element* setMetadata(size_t line_, size_t col_, SourceLocation* loc_); diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index d3f411263..a9efae7a7 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -42,6 +42,15 @@ int unhex(char c) { } namespace wasm { + +static Address getCheckedAddress(const Element* s, const char* errorText) { + uint64_t num = atoi(s->c_str()); + if (num > std::numeric_limits<Address::address_t>::max()) { + throw ParseException(errorText, s->line, s->col); + } + return num; +} + Element::List& Element::list() { if (!isList()) throw ParseException("expected list", line, col); return list_; @@ -53,12 +62,12 @@ Element* Element::operator[](unsigned i) { return list()[i]; } -IString Element::str() { +IString Element::str() const { if (!isStr()) throw ParseException("expected string", line, col); return str_; } -const char* Element::c_str() { +const char* Element::c_str() const { if (!isStr()) throw ParseException("expected string", line, col); return str_.str; } @@ -580,7 +589,6 @@ void SExpressionWasmBuilder::parseFunction(Element& s, bool preParseImport) { if (wasm.getImportOrNull(im->name)) throw ParseException("duplicate import", s.line, s.col); wasm.addImport(im.release()); if (currFunction) throw ParseException("import module inside function dec"); - assert(!currFunction); currLocalTypes.clear(); nameMapper.clear(); return; @@ -1460,11 +1468,7 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) { return; } } - uint64_t num = atoi(s[i++]->c_str()); - if (num > std::numeric_limits<Address::address_t>::max()) { - throw ParseException("excessive memory init", s.line, s.col); - } - wasm.memory.initial = num; + wasm.memory.initial = getCheckedAddress(s[i++], "excessive memory init"); if (i == s.size()) return; if (s[i]->isStr()) { uint64_t max = atoll(s[i]->c_str()); @@ -1479,11 +1483,7 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) { if (curr[0]->str() == DATA) { offsetValue = 0; } else { - uint64_t num = atoi(curr[j++]->c_str()); - if (num > std::numeric_limits<Address::address_t>::max()) { - throw ParseException("excessive memory offset", s.line, s.col); - } - offsetValue = num; + offsetValue = getCheckedAddress(curr[j++], "excessive memory offset"); } const char *input = curr[j]->c_str(); auto* offset = allocator.alloc<Const>(); @@ -1664,36 +1664,20 @@ void SExpressionWasmBuilder::parseImport(Element& s) { } } else if (im->kind == ExternalKind::Table) { if (j < inner.size() - 1) { - uint64_t num = atoi(inner[j++]->c_str()); - if (num > std::numeric_limits<Address::address_t>::max()) { - throw ParseException("excessive table size", s.line, s.col); - } - wasm.table.initial = num; + wasm.table.initial = getCheckedAddress(inner[j++], "excessive table init size"); } if (j < inner.size() - 1) { - uint64_t num = atoi(inner[j++]->c_str()); - if (num > std::numeric_limits<Address::address_t>::max()) { - throw ParseException("excessive table size", s.line, s.col); - } - wasm.table.max = num; + wasm.table.max = getCheckedAddress(inner[j++], "excessive table max size"); } else { wasm.table.max = Table::kMaxSize; } // ends with the table element type } else if (im->kind == ExternalKind::Memory) { if (j < inner.size()) { - uint64_t num = atoi(inner[j++]->c_str()); - if (num > std::numeric_limits<Address::address_t>::max()) { - throw ParseException("excessive table size", s.line, s.col); - } - wasm.memory.initial = num; + wasm.memory.initial = getCheckedAddress(inner[j++], "excessive memory init size"); } if (j < inner.size()) { - uint64_t num = atoi(inner[j++]->c_str()); - if (num > std::numeric_limits<Address::address_t>::max()) { - throw ParseException("excessive table size", s.line, s.col); - } - wasm.memory.max = num; + wasm.memory.max = getCheckedAddress(inner[j++], "excessive memory max size"); } } if (wasm.getImportOrNull(im->name)) throw ParseException("duplicate import", s.line, s.col); |