Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a scripting language into a C application?

I have a C application and I want to include a Scripting Language to put certain functionality into scripts. I just have no experience with that and don't know exactly where to start (Still learning C and trying to understand the application).

How does embedding and communication between my app and the scripts actually work? I think I need the interpreter for the scripting language as a library (.dll on Windows or C Source Code that can be compiled into my application)? And then can I do something like

interpreter->run("myscript", some_object);

How would the script know about the properties of the object? Say my script wants to read or modify some_object->some_field?

Are there any scripting languages that are optimized for that sort of embedding? I know that there is Lua which is popular in game dev, and languages like Python, Perl, PHP or Ruby which seem to be more targeted as stand-alone applications, but my knowledge in the deep architecture does not allow more educated guesses :) (Tagged Lua and Python because they would be my favorites, but as long as it runs on x86 Windows, Linux and Mac OS X, I'm open for other scripting languages as long as they are easy to implement into a C application)

like image 612
Michael Stum Avatar asked Jul 21 '09 10:07

Michael Stum


2 Answers

Lua. It has a very small footprint, is rather fast, and I found it (subjectively) to have the most pleasant API to interact with C.

If you want to touch the Lua objects from C - it's quite easy using the built-in APIs. If you want to touch C data from Lua - it's a bit more work, typically you'd need to make wrapper methods to expose what you want to allow the Lua to modify.

Small code base and tight control over the amount of default libraries introduced into your embeded interpreter also means that you can make reasonable assumptions over the security.

The only odd part is the 1-based array numbering, however, it was not that big of a deal compared to what I thought, given the existence of the iterators.

How to integrate with C: the distribution tarball for Lua has a directory "etc" with a few very good useful examples that should quickly get you started. Specifically - etc/min.c shows how to start up an interpreter, make it interpret the file, and make it call the C function ('print' in that case). From there on you can go by reading the Lua documentation and the source of the standard libraries included with the distribution.

like image 161
Andrew Y Avatar answered Oct 05 '22 23:10

Andrew Y


Some useful links:

  • Embedding Python
  • Embedding Lua: http://www.lua.org/manual/5.1/manual.html#3, http://www.ibm.com/developerworks/opensource/library/l-embed-lua/index.html
  • Embedding Ruby
  • Embedding PLT Scheme
  • Embedding PERL
  • Embedding TCL
  • Embedding JavaScript
  • Embedding PHP

I am familiar with Python. Python is a very rich language, and has a huge number of libraries available.

like image 33
codeape Avatar answered Oct 05 '22 23:10

codeape