在 Objective-C 中将字符串转换/编译为可执行代码 [英] Convert/compile a string into executable code in Objective-C

查看:66
本文介绍了在 Objective-C 中将字符串转换/编译为可执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 ObjC 中获取一个字符串并将其作为代码进行评估.例如(这些是组合函数):

I want to take a string in ObjC and evaluate it as if it were code. For example (these are made-up functions):

NSString *Cmd=@" if (10>5) NSLog(@"Test");  ";
MyClass.Run(Cmd);

我希望测试"出现在输出日志中.我已经搜索并测试了很多代码示例和库,但仍然没有很好的结果.

I expect that "Test" appears in the output log. I have searched and tested a lot of code samples and library but still no good results.

我最终得到了这 2 个用于运行时编译的库:

I Ended Up with these 2 libraries for compilation at runtime:

1- libClang 框架和 clang_Cursor_Evaluate 函数.

1- libClang framework and clang_Cursor_Evaluate function .

https://www.mikeash.com/pyblog/friday-qa-2014-01-24-introduction-to-libclang.html

https://github.com/root-project/cling

2- objc-eval 框架.

2- objc-eval framework .

https://github.com/antipax/objc-eval/blob/master/README.mdown

我无法用 libClang 做任何事情.暂时保持原样,但是在第二个中,我成功地编译了框架并在一个简单的项目中实际使用了它,但仍然没有正确的输出!

I could not do anything with libClang. and left as it is for now, but with the second one, I successfully compiled the framework and actually used it in a simple project, however still no proper output!

#import <Foundation/Foundation.h>
#import "OEEvaluation.h"
void main()
{
Code1:OEEvaluation *evaluation = [OEEvaluation evaluationWithCode:@"retun @\"Hello World!\" ;" delegate:nil];
Code2://OEEvaluation *evaluation = [OEEvaluation evaluationWithCode:@"NSLog(@\"Hello World!\");" delegate:nil];
NSLog([evaluation evaluateSynchronously]);
NSLog(@"Test");
}

代码 1 和代码 2 没有输出:没有(空),没有错误,没有提示......什么都没有.

No output with Code1 and Code 2: no (null), no error, no hint.. nothing at all.

代码 1 是框架文档中的确切示例.

Code 1 is the exact sample from the framework's documentation.

也许我错了,但我认为我找到的解决方案与我的研究之间的答案最接近.

Maybe I am wrong but I think the solutions I found are the closest ones to the answer between my researches.

现在的问题是:

1- 我怎样才能实现这个想法(将字符串转换为可执行代码的可执行行)?

1- How can I accomplish this idea (convert a string into an executable line of executable code)?

2- 如何让 objc-eval.framework 工作?

2- How can I get the objc-eval.framework to work?

推荐答案

如何让 objc-eval.framework 工作?

How can I get the objc-eval.framework to work?

完整的源代码可用,请按照自己的方式进行操作.

The full source is available, work your way through it.

您会发现它通过将代码拼接到它创建的 Xcode 项目中来运行您的代码,该项目是通过解压 tar 存档创建的,调用 xcodebuild 来编译该项目,然后动态加载编译后的代码并执行.

You will discover that it runs your code by splicing it into an Xcode project that it creates by unpacking a tar archive, calling xcodebuild to compile that project, and then dynamically loading the compiled code and executing it.

您的第一个问题是 tar 压缩的存档相当陈旧,需要针对最新版本的 Xcode 进行更新 - Xcode 提供了执行此操作的选项.完成后,项目将编译,但您会发现编译的代码不在此框架期望的位置 - 这是因为默认情况下,设置产品位置的项目设置在最新版本的 Xcode 中被覆盖.

Your first problem is that the tar'ed archive is rather old and needs updating for the latest version of Xcode – Xcode provides the option to do this. Having done that the project will compile but you'll find the compiled code is not where this framework expects to find it – this is because by default the project settings which set the location of the products are overridden in the latest versions of Xcode.

您需要探索 Xcode 的 Preferences...、项目的 Build Locations 设置、xcodebuild 命令采用的参数以及加载已编译二进制文件的框架以决定哪些设置将在您想要的位置获取已编译的二进制文件,以便框架可以访问它.

You need to explore Xcode's Preferences…, the project's Build Locations settings, the arguments the xcodebuild command takes, and the code in the framework which loads the compiled binary to decide which settings will get the compiled binary where you want it so the framework can access it.

一旦你这样做了,你就会发现这个框架是有效的.请注意,要实现此功能,需要在您的应用运行的任何系统上安装 Xcode.

Once you do that you'll find the framework works. Note that for this to work at all requires Xcode to be installed on any system your app runs on.

玩得开心!

HTH

这篇关于在 Objective-C 中将字符串转换/编译为可执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆