Skip to content

Commit

Permalink
feat: add protocol information to native types (#247)
Browse files Browse the repository at this point in the history
* feat: add protocol information to native types

* fix: correct typing for C String return type
  • Loading branch information
edusperoni committed Jun 25, 2024
1 parent daceac1 commit 6286203
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions metadata-generator/src/TypeScript/DefinitionWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,11 +883,9 @@ std::string DefinitionWriter::tsifyType(const Type& type, const bool isFuncParam
case TypeSelector:
return "string";
case TypeCString: {
std::string res = "string";
if (isFuncParam) {
Type typeVoid(TypeVoid);
res += " | " + tsifyType(::Meta::PointerType(&typeVoid), isFuncParam);
}
std::string res = isFuncParam ? "string | " : "";
Type typeVoid(TypeVoid);
res += tsifyType(::Meta::PointerType(&typeVoid), isFuncParam);
return res;
}
case TypeProtocol:
Expand Down Expand Up @@ -976,7 +974,26 @@ std::string DefinitionWriter::tsifyType(const Type& type, const bool isFuncParam
}
}

if (interface.name == "NSArray" && isFuncParam) {
std::vector<std::string> protocols;
if (type.is(TypeType::TypeInterface) && type.as<InterfaceType>().protocols.size() > 0) {
for (auto & protocol : type.as<InterfaceType>().protocols) {
if (protocol->jsName != "NSCopying") {
protocols.push_back(protocol->jsName);
}
}
}

if (protocols.size() > 0) {
// Example: -(NSObject<Option> *) getOption;
// Expected: getOption(): NSObject & Option;
for (auto & protocol : protocols) {
output << " & " << protocol;
}
} else if (interface.name == "NSArray" && isFuncParam) {
// In this case, NSArray<string> maps into NSArray<string> | string[]
// We only do this if there are no protocols, though
// for the very rare case where someone would do NSArray with a protocol
// as we can't marshal a JS array into NSArray + protocol
if (hasClosedGenerics) {
std::string arrayType = firstElementType;
output << " | " << arrayType << "[]";
Expand Down

0 comments on commit 6286203

Please sign in to comment.