Embedding versus FFI
I read articles about Lua and Guile, and it looks like one thing that separates these languages from other popular scripting languages is the ease of embedding an interpreter into your C program (for example). I don't have any experience with this kind of embedding, but I have used FFIs for a couple different languages to call out to C APIs. What sorts of design considerations would favor the embedded approach over writing a C library with a clean API?
Posted Sep 25, 2011, 8:03 PM UTC
82
The main case for embedding a script interpreter is that you allow extensibility and customizability by the end-user without having to recompile. If you want your application to be adaptable to a varied set of use cases, you don't want people to have to download the source and recompile it to tailor its behavior, as that is cumbersome and difficult, and potentially requires tools they don't have. Scripting allows the user to just edit/create a text file that implements the functionality, and the program recognizes it instantly.
I don't really see how this is comparable to implementing something as a C library. Or am I misunderstanding the question. You do implement the scripting engine as a C library with a clean API, that's how it becomes embedded in a larger application. The larger application then uses that interface to expose its internals to the scripting engine, so that users can write scripts that manipulate the program.
Thanks for your response. I guess my confusion comes from something like this Guile tutorial. It seems that -- based on this tutorial -- one use of Guile is to write some core functionality in C and then write some bridge code that exposes that functionality to Guile (after which point you can write Scheme code that calls out to C functions). After reading this tutorial, it wasn't clear to me how this might be an improvement over calling those same C functions via some language's FFI.
So if my understanding is correct, in either approach you write some core program functionality in the system language and you write other parts of the program in your preferred higher-level language. Your point about customizing without having to recompile makes sense to me. If I've made my question clearer and you have any additional thoughts, I'd enjoy hearing them.