Skip to content

Commit

Permalink
use a connected type rather than one single type
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbrix committed Jan 23, 2025
1 parent 35e2ea1 commit 1ffd224
Show file tree
Hide file tree
Showing 4 changed files with 599 additions and 437 deletions.
7 changes: 4 additions & 3 deletions swiftwinrt/Resources/Support/MakeFromAbi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import Foundation
// when we cast to `any MakeFromAbi`, plus that requires a lot more exported
// simples than we want
public protocol MakeFromAbi {
static func from(typeName: String, abi: SUPPORT_MODULE.IInspectable) -> Any?
associatedtype SwiftType
static func from(abi: SUPPORT_MODULE.IInspectable) -> SwiftType?
}

func make(typeName: SwiftTypeName, from abi: SUPPORT_MODULE.IInspectable) -> Any? {
guard let makerType = NSClassFromString("\(typeName.module).__MakeFromAbi") as? any MakeFromAbi.Type else {
guard let makerType = NSClassFromString("\(typeName.module).\(typeName.typeName)Maker") as? any MakeFromAbi.Type else {
return nil
}
return makerType.from(typeName: typeName.typeName, abi: abi)
return makerType.from(abi: abi)
}

func makeFrom(abi: SUPPORT_MODULE.IInspectable) -> Any? {
Expand Down
34 changes: 20 additions & 14 deletions swiftwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1203,31 +1203,37 @@ bind_bridge_fullname(type));
return true;
}

static void write_make_from_abi_case(writer& w, metadata_type const& type)
{
if (skip_write_from_abi(w, type)) return;
w.write("case \"%\": return make%From(abi: abi)\n", type.swift_type_name(), type.swift_type_name());
}

static void write_make_from_abi(writer& w, metadata_type const& type)
{
if (skip_write_from_abi(w, type)) return;

w.write("fileprivate func make%From(abi: %.IInspectable) -> Any {\n", type.swift_type_name(), w.support);

std::string fromAbi;
std::string swiftType;
if (is_interface(type))
{
auto indent = w.push_indent();
w.write("let swiftAbi: %.% = try! abi.QueryInterface()\n", abi_namespace(type),
fromAbi = w.write_temp("let swiftAbi: %.% = try! abi.QueryInterface()\n", abi_namespace(type),
type.swift_type_name());
w.write("return %.from(abi: RawPointer(swiftAbi))!\n", bind_bridge_fullname(type));
fromAbi += w.write_temp(" return %.from(abi: RawPointer(swiftAbi))", bind_bridge_fullname(type));
swiftType = w.write_temp("%", bind<write_swift_interface_existential_identifier>(type));
}
else if (is_class(&type))
{
auto indent = w.push_indent();
w.write("return %(fromAbi: abi)\n", type.swift_type_name());
fromAbi = w.write_temp("return %(fromAbi: abi)", type.swift_type_name());
swiftType = w.write_temp("%", bind<write_swift_type_identifier>(type));
}
w.write("}\n\n");
else
{
throw std::exception("Invalid type for MakeFromAbi");
}

w.write(R"(^@_spi(WinRTInternal)
public class %Maker: MakeFromAbi {
public typealias SwiftType = %
public static func from(abi: %.IInspectable) -> SwiftType? {
%
}
}
)", type.swift_type_name(), swiftType, w.support, fromAbi);
}

static void write_interface_bridge(writer& w, metadata_type const& type)
Expand Down
15 changes: 0 additions & 15 deletions swiftwinrt/file_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,6 @@ namespace swiftwinrt
w.write("%", w.filter.bind_each<write_make_from_abi>(members.interfaces));
w.write("%", w.filter.bind_each<write_make_from_abi>(members.classes));

w.write("@_spi(__MakeFromAbi_DoNotImport)\n");
w.write("public class __MakeFromAbi: MakeFromAbi {\n");
w.write(" public static func from(typeName: String, abi: %.IInspectable) -> Any? {\n", w.support);
w.write(" switch typeName {\n");
{
auto indent_guard = w.push_indent(indent{ 3 });
w.write("%", w.filter.bind_each<write_make_from_abi_case>(members.interfaces));
w.write("%", w.filter.bind_each<write_make_from_abi_case>(members.classes));
}

w.write(" default: return nil\n");
w.write(" }\n");
w.write(" }\n");
w.write("}\n");

w.swap();
write_preamble(w, /* swift_code: */ true);
w.save_file("MakeFromAbi");
Expand Down
Loading

0 comments on commit 1ffd224

Please sign in to comment.