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.
I Ended Up with these 2 libraries for compilation at runtime:
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 framework .
https://github.com/antipax/objc-eval/blob/master/README.mdown
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");
}
No output with Code1 and Code 2: no (null), no error, no hint.. nothing at all.
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.
Now the questions are:
1- How can I accomplish this idea (convert a string into an executable line of executable code)?
2- How can I get the objc-eval.framework to work?