Skip to content

Commit 1eeb379

Browse files
committed
[NFC] Add HeapType::isMaybeShared(BasicHeapType) utility
This abbreviates a common pattern where we first had to check whether a heap type was basic, then if it was, get its unshared version and compare it to some expected BasicHeapType. Suggested in #6771 (comment).
1 parent 848a289 commit 1eeb379

File tree

8 files changed

+11
-11
lines changed

8 files changed

+11
-11
lines changed

src/literal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class Literal {
281281
return i32;
282282
}
283283
int32_t geti31(bool signed_ = true) const {
284-
assert(type.getHeapType().getBasic(Unshared) == HeapType::i31);
284+
assert(type.getHeapType().isMaybeShared(HeapType::i31));
285285
// Cast to unsigned for the left shift to avoid undefined behavior.
286286
return signed_ ? int32_t((uint32_t(i32) << 1)) >> 1 : (i32 & 0x7fffffff);
287287
}

src/tools/fuzzing/heap-types.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,7 @@ void Inhabitator::markExternRefsNullable() {
759759
auto children = type.getTypeChildren();
760760
for (size_t i = 0; i < children.size(); ++i) {
761761
auto child = children[i];
762-
if (child.isRef() && child.getHeapType().isBasic() &&
763-
child.getHeapType().getBasic(Unshared) == HeapType::ext &&
762+
if (child.isRef() && child.getHeapType().isMaybeShared(HeapType::ext) &&
764763
child.isNonNullable()) {
765764
markNullable({type, i});
766765
}

src/wasm-builder.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,7 @@ class Builder {
12171217
if (type.isFunction()) {
12181218
return makeRefFunc(value.getFunc(), type.getHeapType());
12191219
}
1220-
if (type.isRef() && type.getHeapType().isBasic() &&
1221-
type.getHeapType().getBasic(Unshared) == HeapType::i31) {
1220+
if (type.isRef() && type.getHeapType().isMaybeShared(HeapType::i31)) {
12221221
return makeRefI31(makeConst(value.geti31()),
12231222
type.getHeapType().getShared());
12241223
}

src/wasm-type.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ class HeapType {
385385

386386
Shareability getShared() const;
387387

388+
bool isMaybeShared(BasicHeapType type) {
389+
return isBasic() && getBasic(Unshared) == type;
390+
}
391+
388392
Signature getSignature() const;
389393
Continuation getContinuation() const;
390394

src/wasm/literal.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ Literal::Literal(Type type) : type(type) {
5757
return;
5858
}
5959

60-
if (type.isRef() && type.getHeapType().isBasic() &&
61-
type.getHeapType().getBasic(Unshared) == HeapType::i31) {
60+
if (type.isRef() && type.getHeapType().isMaybeShared(HeapType::i31)) {
6261
assert(type.isNonNullable());
6362
i32 = 0;
6463
return;

src/wasm/wasm-binary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ void WasmBinaryWriter::writeType(Type type) {
15211521
// those more refined types.
15221522
if (!wasm->features.hasGC()) {
15231523
auto ht = type.getHeapType();
1524-
if (ht.isBasic() && ht.getBasic(Unshared) == HeapType::string) {
1524+
if (ht.isMaybeShared(HeapType::string)) {
15251525
// Do not overgeneralize stringref to anyref. We have tests that when a
15261526
// stringref is expected, we actually get a stringref. If we see a
15271527
// string, the stringref feature must be enabled.

src/wasm/wasm-validator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2910,7 +2910,7 @@ void FunctionValidator::visitStructSet(StructSet* curr) {
29102910
return;
29112911
}
29122912
auto type = curr->ref->type.getHeapType();
2913-
if (type.isBasic() && type.getBasic(Unshared) == HeapType::none) {
2913+
if (type.isMaybeShared(HeapType::none)) {
29142914
return;
29152915
}
29162916
if (!shouldBeTrue(

src/wasm/wasm.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,8 +964,7 @@ void RefI31::finalize() {
964964
if (value->type == Type::unreachable) {
965965
type = Type::unreachable;
966966
} else {
967-
assert(type.isRef() && type.getHeapType().isBasic() &&
968-
type.getHeapType().getBasic(Unshared) == HeapType::i31);
967+
assert(type.isRef() && type.getHeapType().isMaybeShared(HeapType::i31));
969968
}
970969
}
971970

0 commit comments

Comments
 (0)