Running arbitrary code at runtime

前端 未结 2 1430
名媛妹妹
名媛妹妹 2020-12-18 14:59

I know this is an odd question, but I\'m wondering if this is possible. Is there any method by which code (which would be typed by a user) could be run during runtime?

相关标签:
2条回答
  • 2020-12-18 15:17

    I see a few solutions:

    1. Create a language of your own, and parse it in-application
    2. If on mac, you could theoretically, create a function stub from what they enter in, and use GCC shipped with the application to compile the code at runtime into a dylib, and then use dylib functions to run the function you created.
    0 讨论(0)
  • 2020-12-18 15:31

    On a Mac, you can have your app send text to the compiler (several come with Xcode), have the code compiled, and run the compiled result as a slave app (controlled via a socket, for instance, and copying the preview pane image pixels back via a pipe). If needed you could convert the source code text using some sort of preprocessor and wrap it in your own run-time shell.

    Alternatively you could write or port a C language interpreter (there are several open source interpreters for various subsets of C), and plug Core Graphics library calls into the C interpreter's parser and run-time engine.

    I do not know of a full interpreter for Objective C.

    0 讨论(0)
提交回复
热议问题