How does the Objective-C runtime instantiate the root metaclass and other class descriptions?

社会主义新天地 提交于 2019-12-01 15:27:54

问题


I'm trying to implement a basic object-oriented ANSI C runtime and using Objective-C as a guide.

They're seems to be three parts. A Class Description, Class Interface, and Class Implementation. In order for the Class Interface to be instantiated, the familiar method of using the Class object to instantiate one's object can only happen if the runtime has already instantiated your class object using the class description.

So are all Class definitions allocated statically at first run to provide the ability to instantiate using the Class object? Or if they are allocated dynamically (on initial call), how? Is it a part of the run loop or is the Class actually a function that determines if it has already been allocated or not prior to forwarding the message?


回答1:


The runtime does some initialization via constructor functions that get called before actual program execution. They go by __attribute__((constructor)) in both gcc and clang.

In the case of Objective-C some of them are embedded in the binary by the compiler. You would have to include them in your headers for similar effect.

These functions use data automatically embedded by the compiler. They do things such as building hash tables for the class lookup function which are then used for the actual message passing.

Instances on the other hand are allocated dynamically.

I am doing something similar, so I don't really know a lot better than that, but this is as deep as I have dug.



来源:https://stackoverflow.com/questions/2219501/how-does-the-objective-c-runtime-instantiate-the-root-metaclass-and-other-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!