Skip to content

Commit fb0a5f4

Browse files
authored
feat: add more null types for types the runtime accept or returns as null (#363)
1 parent a9894b1 commit fb0a5f4

11 files changed

Lines changed: 152 additions & 79 deletions

metadata-generator/src/Binary/binaryTypeEncodingSerializer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ binary::BinaryTypeEncodingSerializer::visitNullable(
292292
return type.innerType->visit(*this);
293293
}
294294

295+
unique_ptr<binary::TypeEncoding>
296+
binary::BinaryTypeEncodingSerializer::visitNonNullable(
297+
const ::Meta::NonNullableType& type) {
298+
return type.innerType->visit(*this);
299+
}
300+
295301
unique_ptr<binary::TypeEncoding>
296302
binary::BinaryTypeEncodingSerializer::serializeRecordEncoding(
297303
const binary::BinaryTypeEncodingType encodingType,

metadata-generator/src/Binary/binaryTypeEncodingSerializer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,8 @@ class BinaryTypeEncodingSerializer
118118

119119
virtual unique_ptr<TypeEncoding> visitNullable(
120120
const ::Meta::NullableType& type) override;
121+
122+
virtual unique_ptr<TypeEncoding> visitNonNullable(
123+
const ::Meta::NonNullableType& type) override;
121124
};
122125
} // namespace binary

metadata-generator/src/Meta/NameRetrieverVisitor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ std::string NameRetrieverVisitor::visitNullable(
178178
return type.innerType->visit(*this);
179179
}
180180

181+
std::string NameRetrieverVisitor::visitNonNullable(
182+
const ::Meta::NonNullableType& type) {
183+
return type.innerType->visit(*this);
184+
}
185+
181186
std::string NameRetrieverVisitor::generateFixedArray(const Type* el_type,
182187
size_t size) {
183188
std::stringstream ss(el_type->visit(*this));

metadata-generator/src/Meta/NameRetrieverVisitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ class NameRetrieverVisitor : public ::Meta::TypeVisitor<std::string> {
9999

100100
virtual std::string visitNullable(const ::Meta::NullableType& type);
101101

102+
virtual std::string visitNonNullable(const ::Meta::NonNullableType& type);
103+
102104
private:
103105
NameRetrieverVisitor(bool tsNames) : tsNames(tsNames) {}
104106

metadata-generator/src/Meta/TypeEntities.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ enum TypeType {
5050
TypeEnum,
5151
TypeTypeArgument,
5252
TypeExtVector,
53-
TypeNullable
53+
TypeNullable,
54+
TypeNonNullable
5455
};
5556

5657
class Type {
@@ -150,6 +151,8 @@ class Type {
150151
return visitor.visitExtVector(as<ExtVectorType>());
151152
case TypeNullable:
152153
return visitor.visitNullable(as<NullableType>());
154+
case TypeNonNullable:
155+
return visitor.visitNonNullable(as<NonNullableType>());
153156
}
154157
}
155158

@@ -324,4 +327,12 @@ class NullableType : public Type {
324327

325328
Type* innerType;
326329
};
330+
331+
class NonNullableType : public Type {
332+
public:
333+
NonNullableType(Type* innerType)
334+
: Type(TypeType::TypeNonNullable), innerType(innerType) {}
335+
336+
Type* innerType;
337+
};
327338
} // namespace Meta

metadata-generator/src/Meta/TypeFactory.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ shared_ptr<Type> TypeFactory::createFromAttributedType(
508508
if (auto nullability = type->getImmediateNullability()) {
509509
if (*nullability == clang::NullabilityKind::Nullable) {
510510
return make_shared<NullableType>(innerType.get());
511+
} else if (*nullability == clang::NullabilityKind::NonNull) {
512+
return make_shared<NonNullableType>(innerType.get());
511513
}
512514
}
513515
return innerType;

metadata-generator/src/Meta/TypeVisitor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class EnumType;
1919
class TypeArgumentType;
2020
class ExtVectorType;
2121
class NullableType;
22+
class NonNullableType;
2223

2324
/*
2425
* \class TypeVisitor<T>
@@ -107,5 +108,7 @@ class TypeVisitor {
107108
virtual T_RESULT visitTypeArgument(const ::Meta::TypeArgumentType& type) = 0;
108109

109110
virtual T_RESULT visitNullable(const ::Meta::NullableType& type) = 0;
111+
112+
virtual T_RESULT visitNonNullable(const ::Meta::NonNullableType& type) = 0;
110113
};
111114
} // namespace Meta

metadata-generator/src/Meta/ValidateMetaTypeVisitor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,10 @@ bool ValidateMetaTypeVisitor::visitNullable(const NullableType& typeDetails) {
171171

172172
return true;
173173
}
174+
175+
bool ValidateMetaTypeVisitor::visitNonNullable(
176+
const NonNullableType& typeDetails) {
177+
typeDetails.innerType->visit(*this);
178+
179+
return true;
180+
}

metadata-generator/src/Meta/ValidateMetaTypeVisitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ class ValidateMetaTypeVisitor : public TypeVisitor<bool> {
9393

9494
virtual bool visitNullable(const ::Meta::NullableType& type);
9595

96+
virtual bool visitNonNullable(const ::Meta::NonNullableType& type);
97+
9698
private:
9799
MetaFactory& _metaFactory;
98100
};

metadata-generator/src/TypeScript/DefinitionWriter.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,8 @@ bool DefinitionWriter::hasClosedGenerics(const Type& type) {
949949
}
950950

951951
std::string DefinitionWriter::tsifyType(const Type& type,
952-
const bool isFuncParam) {
952+
const bool isFuncParam,
953+
const bool suppressNull) {
953954
switch (type.getType()) {
954955
case TypeVoid:
955956
return "void";
@@ -1011,6 +1012,9 @@ std::string DefinitionWriter::tsifyType(const Type& type,
10111012
if (isFuncParam) {
10121013
result += " | ArrayBufferLike | ArrayBufferView";
10131014
}
1015+
if (!suppressNull) {
1016+
result += " | null";
1017+
}
10141018
return result;
10151019
}
10161020
case TypeBlock:
@@ -1130,8 +1134,16 @@ std::string DefinitionWriter::tsifyType(const Type& type,
11301134
return type.as<TypeArgumentType>().name;
11311135
case TypeNullable: {
11321136
const NullableType& nullableType = type.as<NullableType>();
1137+
if (nullableType.innerType->is(TypePointer)) {
1138+
return tsifyType(*nullableType.innerType, isFuncParam);
1139+
}
11331140
return tsifyType(*nullableType.innerType, isFuncParam) + " | null";
11341141
}
1142+
case TypeNonNullable: {
1143+
const NonNullableType& nonNullType = type.as<NonNullableType>();
1144+
bool suppress = isFuncParam;
1145+
return tsifyType(*nonNullType.innerType, isFuncParam, suppress);
1146+
}
11351147
case TypeVaList:
11361148
case TypeInstancetype:
11371149
default:

0 commit comments

Comments
 (0)