wickwirew / Runtime

A Swift Runtime library for viewing type info, and the dynamic getting and setting of properties.
MIT License
1.08k stars 94 forks source link

Initialize Enums and Tuple #91

Closed PSchmiedmayer closed 1 year ago

PSchmiedmayer commented 3 years ago

This PR adds the functionality to initialize enums and tuples using the createInstance function that is currently missing in the runtime framework as documented in #90 .

Testing

Adds unit tests to create enums and tuples

PSchmiedmayer commented 3 years ago

As documented in the description this PR aims to address #90 in creating enums and tuples.

I am currently struggling with creating single-payload enums as well as multi-payload enums. I have taken a look at https://github.com/apple/swift/blob/main/docs/ABI/TypeLayout.rst#fragile-enum-layout as a reference for the enum layout in the Swift runtime and have tried to understand the concept of https://github.com/apple/swift/blob/main/stdlib/public/runtime/Enum.cpp but am still struggling with the fact that I can currently not determine the spare bits of a type to create single-payload enums. In addition I would need to get detailed type information about an enum and the extra inhabitants of an enum type.

@wickwirew I would be grateful if you can point me in some direction if and how I would be able to find that information in the Swift Runtime and how you approached these problems before. I was in contact with @varungandhi-apple who was very helpful and friendly and who pointed me towards https://github.com/apple/swift/tree/main/tools/swift-inspect but I think that I am a bit over my head here and am not sure how to approach this with my current knowledge about the Swift runtime.

EricRabil commented 3 years ago

Just whipped up a very rough POC based on your work, the framework itself, and a lot of staring at the IRGen of enums. https://github.com/ericrabil/enummagic Feel free to adapt this as you'd like, or if you give me push access to your fork I could help finalize yours.

PSchmiedmayer commented 3 years ago

@EricRabil Very cool, great news! I will take a look at your implementation in the next days. In addition I have also invited you as a collaborator on my fork at https://github.com/PSchmiedmayer/Runtime, it would be wonderful if you can incorporate insights into my draft and we can work together to finalize the Runtime PR! 💪