summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Lively <7121787+tlively@users.noreply.github.com>2022-01-20 16:39:27 -0800
committerGitHub <noreply@github.com>2022-01-20 16:39:27 -0800
commit060442225165d0423d06ea33ab865e850b54f61b (patch)
treeb18e210cef9b1106caf8cbdaea186ce232cb0721 /src
parent93f69e5805a5476a60d2b593c24e80c5fce779e7 (diff)
downloadbinaryen-060442225165d0423d06ea33ab865e850b54f61b.tar.gz
binaryen-060442225165d0423d06ea33ab865e850b54f61b.tar.bz2
binaryen-060442225165d0423d06ea33ab865e850b54f61b.zip
Reset global type state between tests (#4468)
Add a `destroyAllTypes` function to clear the global state of the type system and use it in a custom gtest test fixture to ensure that each test starts and ends with a fresh state.
Diffstat (limited to 'src')
-rw-r--r--src/wasm-type.h5
-rw-r--r--src/wasm/wasm-type.cpp13
2 files changed, 18 insertions, 0 deletions
diff --git a/src/wasm-type.h b/src/wasm-type.h
index 7d6306ef7..48b6f2217 100644
--- a/src/wasm-type.h
+++ b/src/wasm-type.h
@@ -45,6 +45,11 @@ void setTypeSystem(TypeSystem system);
TypeSystem getTypeSystem();
+// Dangerous! Frees all types and heap types that have ever been created and
+// resets the type system's internal state. This is only really meant to be used
+// for tests.
+void destroyAllTypesForTestingPurposesOnly();
+
// The types defined in this file. All of them are small and typically passed by
// value except for `Tuple` and `Struct`, which may own an unbounded amount of
// data.
diff --git a/src/wasm/wasm-type.cpp b/src/wasm/wasm-type.cpp
index fc42fe230..af382da23 100644
--- a/src/wasm/wasm-type.cpp
+++ b/src/wasm/wasm-type.cpp
@@ -653,6 +653,11 @@ template<typename Info> struct Store {
}
bool hasCanonical(const Info& info, typename Info::type_t& canonical);
+ void clear() {
+ typeIDs.clear();
+ constructedTypes.clear();
+ }
+
private:
template<typename Ref> typename Info::type_t doInsert(Ref& infoRef) {
const Info& info = [&]() {
@@ -754,12 +759,20 @@ struct SignatureTypeCache {
std::lock_guard<std::mutex> lock(mutex);
cache.insert({type.getSignature(), type});
}
+
+ void clear() { cache.clear(); }
};
static SignatureTypeCache nominalSignatureCache;
} // anonymous namespace
+void destroyAllTypesForTestingPurposesOnly() {
+ globalTypeStore.clear();
+ globalHeapTypeStore.clear();
+ nominalSignatureCache.clear();
+}
+
Type::Type(std::initializer_list<Type> types) : Type(Tuple(types)) {}
Type::Type(const Tuple& tuple) {