Can metatype objects be represented by a pointer?

I have a set of types which all conform to a protocol P with no associated types.

protocol P 
{
    init()
}

The metatype objects, of type P.Type, appear to be pairs of pointers, which I retrieved off the stack using unsafeBitCast(_:to:):

let (type, _):(UnsafeMutableRawPointer, UnsafeMutableRawPointer) = 
        unsafeBitCast(T.self as P.Type, 
            to: (UnsafeMutableRawPointer, UnsafeMutableRawPointer).self)

I’m trying to register types that conform to P with Godot Nativescript, a C API. This API allows you to associate one user pointer with each type that is registered with the C API, so I took the first metatype pointer, and discarded the second. For structs, I found that the second pointer is always at an offset +24 from the first pointer, so it can be computed dynamically. But this doesn’t seem to be the case for classes. What is the meaning of the second pointer in this case? I looked at TypeMetadata.rst but I was not able to understand it.

The first pointer is going to be the conforming type; the second is going to be the actual conformance. The fact that the conformance is being emitted immediately after the metadata for the type is a coincidence. So unfortunately you won’t be able to cram this into a single pointer.

2 Likes
Terms of Service

Privacy Policy

Cookie Policy