Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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!

share|improve this question
    
Why not delegate runtime user scripting to dedicated tools such as python, bash or lua? –  mouviciel May 24 '13 at 7:51
    
So you want to do this in C++, but you did C# before? Can't you implement this in C#? Would be much easier, since the C# environment allows easy dynamic compilation at runtime. –  Doc Brown May 24 '13 at 11:46
    
@DocBrown Actually I didn't try it in C#, sorry for the confusion. Are you talking about the Dynamic Method? That could work but it seems to need IL code. I'm a new programmer really, so I don't know much about C# either. –  Dimension May 24 '13 at 11:59
1  
@mouviciel And that's why scripts are now called compiled programs? No wait, they are still just scripts. I may not know the technicalities behind some of them but I do know the performance of those languages. They just aren't good enough for what I had in mind. Thanks for the suggestion though! –  Dimension May 24 '13 at 12:35
1  
@DocBrown Right, good point I understand. However what is most important is being able to call all the scripts (compiled methods) in a tight loop over and over. That's where I need the speed. For this it seems C# is a bad idea: msdn.microsoft.com/en-us/magazine/cc163759.aspx#S3 - Sorry for not saying this earlier, I just can't put everything in the question. –  Dimension May 24 '13 at 13:41

2 Answers 2

up vote 1 down vote accepted

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!

share|improve this answer
    
This could work! It seems to use this to specify types though: "Type::getInt32Ty(Context)", I'm not sure if this will work with any class type. –  Dimension May 24 '13 at 12:11
    
Well, I did the easy bit :) its up to you to read and understand the documentation. You can use derived types though. –  gbjbaanb May 24 '13 at 17:24

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.

share|improve this answer
    
The application is only for myself, it will be a very specific kind fo data research application. I need it compiled for speed, it's either compiled for user interactions, or an endless loop of programming and compiling. There's a very good reason I want the application, but my usage example above won't show it. Finally, the future will have these kinds of programs, it does not make sense to not recompile the program when it needs to change, and it is possible. Therefore I will do it. And the dynamic loading library as far as I can tell is a really simple, well supported way of doing things. –  Dimension May 24 '13 at 11:46
    
So it's not only technically possible but it is well supported to load and unload libraries during runtime. Thanks for the answer though, but I want the cutting edge. –  Dimension May 24 '13 at 11:48
    
@Dimension: It is certainly well-supported to dynamically load and unload libraries. It is the basis for DLL/so libraries and extensively leveraged for plug-in systems. The less-supported part is the assumption that a C++ compiler exists in the runtime environment, but that is also not a problem for your target audience. –  Bart van Ingen Schenau May 24 '13 at 12:17
    
Fair enough, it was a good point to raise when I hadn't clarified the type of user. Though luckily for everyone it appears LLVM can be embedded as demonstrated by gbjbaanb. I also want to point out that the DLL solution was only the first thing that popped to mind, I have no clue if there are different solutions though the dll one looks to be the simplest. –  Dimension May 24 '13 at 12:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.