I want to compile custom functions during run time based on user written scripts. I’ll give a hypothetical example that should demonstrate exactly what I need to do. This is the best way for me to describe the problem. I’d like a little input/ideas in how I can solve this. At the bottom I give you my ideas.
I have 2 classes, one with public int x and y. Another with public int x,y,z. There can be more than one instance of each class running.
The thing with these classes though (and here comes my problem) is that they need to be updatable via a user written and then compiled function during run time, that then gets added to a list of update functions that gets run once per cycle/event. Which could be at the touch of some button or something.
Example: The user has written a script to update the public members of Class A. He has written A.X = B.Y + C.Z. The variables here are public, never private. This method needs to be compiled, and added to some list of functions to call each cycle. Obviously this method needs access to class B and C, as well as A. So pointers to those need to be passed in. The function returns nothing.
When I say class A, B, and C. I mean these as some instances of any classes. ‘A’ could be class Q, while both B and C could be from class T. Or any other variation. These classes do not need recompilation/user written or anything like that.
I just need help with coming up with options and how to compile a function that gets passed in some pointers and does something with them. But with all of that defined run time, what the function does, what gets passed into it, ect.
—- What I think I can do —
I’m jumping extremely far in knowledge but it is what I always do. So I am just switching from C# to C++ and have little to go on for now while I know little. So all i can do is guess solutions and research them. I’m guessing I could compile a late loading dll for each function, and I’m guessing the dll has to know something about the data structures passed into it so I think it could work to have them in a separate normal dll usable by any program. (The A,B, and C class thingies, or rather Q, and T classes which A,B,and C were instances of in my little demo). So now both the normal program and the dll functions knows all about the classes, and all I have to do is compile the dlls during runtime, load them, and somehow get a pointer to them that my main application has and can call with the appropriate arguments. Then if the user changes the script, the dll unloads, recompiles, loads again, and a new pointer is gotten?
Any help is greatly appreciated. If you can help direct my learning/research I’ll learn a lot faster and be able to implement this. Thank you so much!
16
Leverage an existing compiler, like LLVM. This can be embedded into your application to compile custom code at runtime using the JIT feature of LLVM – see the cpp example that appears to do exactly what you need – compile a user-supplied function at runtime and then call it!
2
Technically, it is possible to let your program build a library and dynamically load that into your program. But it is very unlikely that such a program will be a success.
For starters, unless you are targeting C++ developers as your users, you should not assume that there is a C++ compiler present.
A far better approach is to incorporate a scripting engine in your program and to let the users write the extensions in the scripting language.
Possibilities here are for example Boost.Python or Lua.
That way you get the best of both worlds: the speed of C++ for your main program with the flexibility of scripting languages for the extensions.
4