Add Scripting To Your C++ Programs With ChaiScript

If you are writing a program that has a technical user base, it is a nice touch to make the program scriptable. In fact, you might want to do the hard work in a programming language and then use your scripting language to build out features. In theory, this should be easy. There are plenty of embedded scripting libraries and they provide some way for your code to access script resources and for script resources to access selected host variables and functions. If you use C++, one of the easier ways to do this is with ChaiScript.

ChaiScript is BSD licensed and — assuming your compiler supports C++ 14 — it is as easy as including a header file and making a few calls. There are no special tools or libraries required. The code is portable between operating systems, including both 32-bit and 64-bit Windows. It is also threadsafe unless you turn that feature off.

How simple is ChaiScript? Here’s their example of exposing a function (HelloWorld, of course). The function takes an argument and returns a value. The main program sets up the link between the function and script and then runs a simple script.

#include <chaiscript/chaiscript.hpp>

std::string helloWorld(const std::string &t_name) {
   return "Hello " + t_name + "!";
}

int main() {
   chaiscript::ChaiScript chai;
   chai.add(chaiscript::fun(&helloWorld), "helloWorld");

   chai.eval(R"(
      puts(helloWorld("Bob"));
       )");
}

A real program is probably more likely to read its script from a file or some other user-specified thing, but that’s easy enough to imagine. According to the documentation, your script can call C++ and C++ can call into scripts and all in a type-safe manner. You can also propagate exceptions.

The scripting language itself is straightforward. Instead of the formal documentation, you might appreciate the cheat sheet. There are also quite a few examples.

It is easy to dismiss scripting languages, but they are perfect for some applications. ChaiScript and similar tools let you build the hard parts the hard way and the easy parts the easy way. After all, you probably use bash and it is nothing more than a scripting language.

23 thoughts on “Add Scripting To Your C++ Programs With ChaiScript

  1. If one is thinking of using a scripting language, one might be better off using one of the older languages such as bash or perl.

    Taking perl as an example, it’s got more functionality than any recently-designed scripting language, it’s been tested for decades, and it’s the most portable of the languages – the same perl program will run on any system(*).

    Bash is the same way – it’s been around forever, very well tested, and available for many systems including Windows.

    Lots of documentation for both languages are online, and tutorials as well. If you don’t understand something you can find an alternate explanation that might explain it better. There’s a ton of stack exchange experts in perl and bash who can answer questions.

    The way new languages come about is: some programmer doesn’t like the way a language does something, thinks “I can make something better”, and goes off and starts coding. Languages are complex and subtle, and unless you are a language expert your new system will have flaws and faults that will be hard to deal with. Java string comparison comes to mind.

    The newest, hottest language on the market, at version 1.0, “we’re working on adding features”, might not be popular 2 years from now.

    For scripting, it’s better to use an older well-entrenched system that’s stood the test of time.

    (*) Yes, it’s possible to write hard-to-understand code in perl and bash. It’s possible to write clean and clear code as well. The difference lies in the programmer, not the language.

    1. Python and JS are probably better choices than Chai, but I don’t know if Bash would be a good fit.

      Bash is pretty optimized for being an interactive command language. It doesn’t seem to have any OOP, and a generally odd idea of what a data type is. I don’t know if they added real arrays at some point, but the closes thing to a standard library it has seems to think a list is just a string.

      Perl might be better, but it has a complicated confusing syntax that seems to be aimed more for one liners than large readable programs.

      1. As a Python programmer I have to say that it’s horrible for embedding. JavaScript is not great either. I think at the moment your best bet is Lua. It was *designed* for that particular use case, it’s mature and widespread, and there are lots of programmers and tools around.

    1. Well that’s an apple orange comparison because the eight lines code are also creating an API for the one line of script. So the actual script isn’t large with the one time set up is only six or seven lines of code which is really not bad.

    2. Yes it is a terrible example…..

      It is very convoluted to use a scripting language like chaiscript instead of just doing the following:

      std::string name = “”;
      cout <> name;
      helloWorld(name);

      It would have been better to see a different example that couldn’t be done far faster in c++ directly.

    1. I wouldn’t without serious characterization. But for a program that talks to a microcontroller I’ve used v8 to add JavaScript to those programs very successfully to let end users do things like engineering unit conversions etc without coding.

  2. Duktape is pretty awesome for javascript embedding and even supports a debug api if you are ambitious. Can run on embedded systems up to windows. Let me build the toys I always wanted.

  3. How much resources are required for the example? Would it work as embedded scripting engine on constrained SOCs like ESP32 or ESP8266 and leave RAM for the main C++ program?

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.