summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/wasm/wasm-type.cpp30
-rw-r--r--test/gtest/type-builder.cpp38
2 files changed, 68 insertions, 0 deletions
diff --git a/src/wasm/wasm-type.cpp b/src/wasm/wasm-type.cpp
index c77099a1d..d24e42acb 100644
--- a/src/wasm/wasm-type.cpp
+++ b/src/wasm/wasm-type.cpp
@@ -1392,6 +1392,36 @@ size_t HeapType::getDepth() const {
for (auto curr = *this; (super = curr.getSuperType()); curr = *super) {
++depth;
}
+ // In addition to the explicit supertypes we just traversed over, there is
+ // implicit supertyping wrt basic types. A signature type always has one more
+ // super, HeapType::func, etc.
+ if (!isBasic()) {
+ if (isFunction()) {
+ depth++;
+ } else if (isData()) {
+ // specific struct types <: data <: eq <: any
+ depth += 3;
+ }
+ } else {
+ // Some basic types have supers.
+ switch (getBasic()) {
+ case HeapType::ext:
+ case HeapType::func:
+ case HeapType::any:
+ break;
+ case HeapType::eq:
+ depth++;
+ break;
+ case HeapType::i31:
+ case HeapType::data:
+ case HeapType::string:
+ case HeapType::stringview_wtf8:
+ case HeapType::stringview_wtf16:
+ case HeapType::stringview_iter:
+ depth += 2;
+ break;
+ }
+ }
return depth;
}
diff --git a/test/gtest/type-builder.cpp b/test/gtest/type-builder.cpp
index 525a45c31..bc7b72e5d 100644
--- a/test/gtest/type-builder.cpp
+++ b/test/gtest/type-builder.cpp
@@ -619,3 +619,41 @@ TEST_F(IsorecursiveTest, TestExistingSuperType) {
EXPECT_EQ(A1.getHeapType(), A2.getHeapType());
EXPECT_EQ(B1.getHeapType(), B2.getHeapType());
}
+
+// Test .depth() helper.
+TEST_F(NominalTest, TestDepth) {
+ HeapType A, B, C;
+ {
+ TypeBuilder builder(3);
+ builder[0] = Struct();
+ builder[1] = Struct();
+ builder[2] = Array(Field(Type::i32, Immutable));
+ builder.setSubType(1, builder.getTempHeapType(0));
+ auto result = builder.build();
+ ASSERT_TRUE(result);
+ auto built = *result;
+ A = built[0];
+ B = built[1];
+ C = built[2];
+ }
+
+ // any < eq < data < specific struct and array types
+ EXPECT_EQ(HeapType(HeapType::any).getDepth(), 0U);
+ EXPECT_EQ(HeapType(HeapType::eq).getDepth(), 1U);
+ EXPECT_EQ(HeapType(HeapType::data).getDepth(), 2U);
+ EXPECT_EQ(A.getDepth(), 3U);
+ EXPECT_EQ(B.getDepth(), 4U);
+ EXPECT_EQ(C.getDepth(), 3U);
+
+ // Signature types are subtypes of func.
+ EXPECT_EQ(HeapType(HeapType::func).getDepth(), 0U);
+ EXPECT_EQ(HeapType(Signature(Type::none, Type::none)).getDepth(), 1U);
+
+ EXPECT_EQ(HeapType(HeapType::ext).getDepth(), 0U);
+
+ EXPECT_EQ(HeapType(HeapType::i31).getDepth(), 2U);
+ EXPECT_EQ(HeapType(HeapType::string).getDepth(), 2U);
+ EXPECT_EQ(HeapType(HeapType::stringview_wtf8).getDepth(), 2U);
+ EXPECT_EQ(HeapType(HeapType::stringview_wtf16).getDepth(), 2U);
+ EXPECT_EQ(HeapType(HeapType::stringview_iter).getDepth(), 2U);
+}