eval in Objective-C [duplicate]

2019-01-27 04:05发布

Possible Duplicate:
Is there something like Matlab's eval statement in Objective-C 2.0?

Is there an eval function (as in lisp, javascript, python, ruby...) in Objective-C?

What I mean by eval is a function that can take in arbitrary Objective-C code (possibly having class definitions, side effects, instrospection, etc) as a string (not a block or an NSInvocation or IMP or something like that), and evaluate each expression, respecting the current runtime environment state, side effects, class definitions, etc.

If not, is it possible to implement within the confines of the existing runtime?

3条回答
时光不老,我们不散
2楼-- · 2019-01-27 04:43

Neither the language nor Apple's frameworks directly support such a thing. However, one of the goals of LLVM is to be an embeddable compiler suite. I'm pretty sure it can generate executable code right into memory. The hard part would be providing that code with access to the pre-existing environment of the calling code. For example, compiling code which references a local variable or something like that.

(Mind you, this approach is forbidden for the iOS App Store, but it could maybe be workable on Mac OS X.)

查看更多
Viruses.
3楼-- · 2019-01-27 04:57

No. Code eval is the feature for dynamic language. Although objective-C has dynamic feature , and even Cocoa runtime , it is still considered as a static language (generally).

查看更多
倾城 Initia
4楼-- · 2019-01-27 05:05

Absolutely not. Objective-C is a fully compiled language. Only interpreted languages can do that sort of thing.

查看更多
登录 后发表回答