Objective-C 运行时代码生成
我想创建一个 Objective-C 应用程序,它允许您在运行时指定类实现。
我希望用户输入一些代码(代码的正确性暂时超出了范围)。 当用户完成后,我想创建一个键入代码的类并在应用程序中使用它。
所以我想在应用程序的运行时动态添加代码。这可能吗? 如果是这样,我怎样才能实现这一目标?如果没有,为什么不呢?有没有其他方法可以创建我想要创建的相同效果?
谢谢。
I want to create an Objective-C application which lets you specify a class implementation at runtime.
I want the user to type some code (correctness of the code is out of scope for now).
When the user is done i want to create a class of the typed code and use it in the application.
So i want to dynamically add code in runtime of the application. Is this possible?
If so, how can i achieve this? If not, why not and are there any alternatives to create the same effect which i want to create?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在运行时动态加载类,但要到达那里您首先需要处理分发编译器、其编译依赖项(标头等)、设置其环境等。
通常应用程序使用可轻松嵌入的脚本语言(Lua、Python等)或已在平台(JavaScript、AppleScript,...)。
You can dynamically load classes at runtime, but to get there you'd first need to handle distributing a compiler, its compilation dependencies (headers, ...), setup its enviroment, etc.
Usually applications use scripting languages that are painlessly embeddable (Lua, Python, ...) or already available on the platform (JavaScript, AppleScript, ...).
我会查看 F-Script。它比 Obj-C 更接近 Smalltalk,但在我看来,它比 JavaScript 或 Lua 更接近 Obj-C :-)
I'd check out F-Script. It's closer to smalltalk than Obj-C, but IMO it's closer to Obj-C than JavaScript or Lua :-)
在 iOS 设备上,这是不可能的。我相信,在 Mac 上,您可以链接 clang+llvm 库并使用它们将代码生成到缓冲区中,然后 mprotect() 缓冲区可执行。
On iOS devices, this isn't possible. On a Mac you could link against the clang+llvm libraries and use them to generate code into a buffer, then mprotect() the buffer to be executable, I believe.