Objective-C运行时如何实例化根元类和其他类描述?
我正在尝试实现一个基本的面向对象的 ANSI C 运行时并使用 Objective-C 作为指导。
它们似乎分为三个部分。类描述、类接口和类实现。为了实例化类接口,只有当运行时已经使用类描述实例化您的类对象时,才会发生使用类对象实例化对象的熟悉方法。
那么,所有类定义是否在第一次运行时都静态分配,以提供使用类对象实例化的能力?或者,如果它们是动态分配的(在初始调用时),如何分配?它是运行循环的一部分,还是该类实际上是一个在转发消息之前确定它是否已被分配的函数?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
运行时通过在实际程序执行之前调用的构造函数进行一些初始化。它们在 gcc 和 clang 中都遵循 __attribute__((constructor)) 。
对于 Objective-C,其中一些由编译器嵌入到二进制文件中。您必须将它们包含在标题中才能达到类似的效果。
这些函数使用编译器自动嵌入的数据。他们做一些事情,例如为类查找函数构建哈希表,然后将其用于实际的消息传递。
另一方面,实例是动态分配的。
我正在做类似的事情,所以我真的不知道比这更好的了,但这与我挖掘的一样深。
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.