[swift-dev] Understanding runtime entry points
Austin Zheng
austinzheng at gmail.com
Wed Jan 6 14:46:10 CST 2016
Thanks Joe, this is incredibly helpful, especially the note about how enums
are passed in 'piecewise'.
re. your question: I was indeed calling the C++ function from Swift and
directly passing in the enum (in pseudo-Swift):
let myEnum : SomeEnumType = ...
cplusplusFunction(value: myEnum, type: SomeEnumType.self) // incorrect
How would I pass a pointer to the enum's value from Swift? I don't think
Swift exposes any mechanism to get the memory address of a Swift object.
Perhaps I should create an UnsafeMutablePointer<SomeEnumType>, set its
memory to (a copy of) the enum value, and pass that pointer object in
instead?
Austin
On Wed, Jan 6, 2016 at 12:04 PM, Joe Groff <jgroff at apple.com> wrote:
>
> > On Jan 1, 2016, at 10:58 PM, Austin Zheng via swift-dev <
> swift-dev at swift.org> wrote:
> >
> > Hello,
> > (...)
>
> Swift structs are (currently) passed by passing each stored property
> individually. _EnumMirror is defined as a struct with owner/value/type
> fields, and the 'self' parameter to its methods gets broken down this way.
>
> > * What I really want to do is to get the tag of an enum. I wrote a
> different entry point that omits the unused "owner" property and simply
> calls swift_EnumMirror_caseName with nullptr as the first argument. This
> other C++ function takes 'value' (an OpaqueValue*) and 'type' (a
> Metadata*). I've surmised that 'type' should be the Swift metatype of the
> enum instance (e.g. myEnum.dynamicType), and I do get the case names table.
> However, if I pass in the enum instance itself as 'value', my tag is always
> retrieved as 0. I noticed that there's some sort of indirection in the form
> of "vw_getEnumTag", which goes through something called the "value
> witness". Is there somewhere I can read up about the value witness concept?
> I assume the reason the 'original' code worked was because it was passing
> in a different object as 'value', maybe one that could serve as a value
> witness for the reflected-upon instance's type.
>
> 'value' is a pointer to the value in memory, not the value itself. Are you
> passing the enum's literal representation by value instead of passing a
> pointer to it?
>
> -Joe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160106/df987d02/attachment.html>
More information about the swift-dev
mailing list