Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add protocol information to native types #247

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading