Introduction and Questions


#1

I’m very interested in ChaiScript. I noted it has been around since 2009, and I don’t know how I’ve missed it this entire time. In fact, I’ve been looking for the “right” embeddable language for quite some time for a tool we use at work (in the past, I’ve used AngelScript and most recently Python). The issue is difficulty extending or crossing the script/C/C++ boundary or complaints abound the language itself by the users (Python has been exceedingly unpopular for some reason, despite the plethora of tutorials, examples, and documentation available).

So for the next iteration of the tool, I’m intrigued by ChaiScript. Looks very simple to embed. The ChaiScript/C++ interface looks clean as well. And should be popular with the users because of the C++ type interface. I also like embedding the stdlib to eliminate external dependencies (the problem with Python was users had to have Python installed as well, not just for the shared Python library, but also the host of python modules).

Before I dive into the questions, it might help to describe my use case. The internal tool I have is for a scripted test environment. It’s intended to be multi-purpose, but is mostly used right now to control test environments. We wanted it to be script based so there is no need to recompile or rebuild the test environment (as is the case with some tools like LabView). And we wanted it to be easily extendable to add support for custom interfaces. The current architecture is organized as event driven. Basically upon startup the script runs and does some configuration then registers a series of callbacks upon events (e.g. temperature reaches a certain level, voltage on an input drops below a certain value, etc). So there are 2 stages: 1) setup and 2) runtime.

So, I have a few questions about ChaiScript that will help with my evaluation:

  • What are object lifetimes? Is there some sort of reference counting/garbage collection? After running the “setup” stage, do the created objects remain? Or do they need to be stored in order to be referenced during a callback?
  • I see that ChaiScript can extend C++ classes (and vice-versa). But I’ve not seen anything related to inheritance. For example, if I have a class in C++ that I want to expose to the Chai side, I don’t see a way to derive a new class in Chai (though I suppose I can do a “has a” type relationship with a Chai class). I can extend the class by adding methods, but I can’t change the interface, for example by hiding methods in the C++ class. I suppose not really a question, just a general statement that perhaps someone can comment on.
  • Does the multi-threaded support handle callbacks cleanly? That is, if I call back into a function from different threads, are these calls serialized? Or do I have to be worried about things changing?

Thanks,
Pete


#2

@PlayDough

  • Objects are reference counted, and their lifetimes are analogous to C++ object lifetimes. If you create a variable in a local scope and use it only for that scope, it is destroyed at the end of the scope, just like in C++. If you return that object back to C++ and keep a handle to it (either a std::shared_ptr to the stored data or a Boxed_Value wrapper), then the object lifetime is extended. I have a unit test that attempts to capture the lifetime of scoped objects to make sure the stack is being cleaned up as expected: https://github.com/ChaiScript/ChaiScript/blob/master/unittests/object_lifetime_test.cpp

    Note that this gives you something that I don’t believe ANY other language (besides C++) gives you: guaranteed object lifetimes. There is no GC to contend with. If you have objects that rely on RAII behavior in C++, they should behave the same in ChaiScript

  • There’s no direct support for inheritance in ChaiScript. It’s a feature people have started asking for recently. In case you are not already aware, here is an example for how to allow ChaiScript to implement virtual C++ methods in a faux-inheritance kind of way: https://gist.github.com/lefticus/65a6c23714537196a541

  • Multi-threaded support provides exactly one guarantee: the ChaiScript engine itself will not crash on you in a multi-threaded environment. You can call functions in parallel from within C++, but if have multiple writers on a thread-unsafe object, you’re going to have problems. If you want to make a ChaiScript defined object threadsafe, it would be up to you to expose a mutex to ChaiScript currently. I just added this issue to expose some threading primitives to the scripting engine https://github.com/ChaiScript/ChaiScript/issues/147

The general rule of thumb that we’ve aimed for is “assume that it works like C++.”

Jason