diff options
-rw-r--r-- | src/wasm-type.h | 5 | ||||
-rw-r--r-- | src/wasm/wasm-type.cpp | 13 | ||||
-rw-r--r-- | test/gtest/type-builder.cpp | 21 |
3 files changed, 37 insertions, 2 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) { diff --git a/test/gtest/type-builder.cpp b/test/gtest/type-builder.cpp index 37e47c148..49221f667 100644 --- a/test/gtest/type-builder.cpp +++ b/test/gtest/type-builder.cpp @@ -3,6 +3,24 @@ using namespace wasm; +// Helper test fixture for managing the global type system state. +template<TypeSystem system> class TypeSystemTest : public ::testing::Test { + TypeSystem originalSystem; + +protected: + void SetUp() override { + originalSystem = getTypeSystem(); + setTypeSystem(system); + } + void TearDown() override { + destroyAllTypesForTestingPurposesOnly(); + setTypeSystem(originalSystem); + } +}; +using EquirecursiveTest = TypeSystemTest<TypeSystem::Equirecursive>; +using NominalTest = TypeSystemTest<TypeSystem::Nominal>; +using IsorecursiveTest = TypeSystemTest<TypeSystem::Isorecursive>; + TEST(TypeBuilder, Growth) { TypeBuilder builder; EXPECT_EQ(builder.size(), size_t{0}); @@ -10,11 +28,10 @@ TEST(TypeBuilder, Growth) { EXPECT_EQ(builder.size(), size_t{3}); } -TEST(TypeBuilder, Basics) { +TEST_F(EquirecursiveTest, Basics) { // (type $sig (func (param (ref $struct)) (result (ref $array) i32))) // (type $struct (struct (field (ref null $array) (mut rtt 0 $array)))) // (type $array (array (mut externref))) - TypeBuilder builder(3); ASSERT_EQ(builder.size(), size_t{3}); |