So I recently found out about ChaiScript, and subsequently spent both days this past weekend tinkering with it. I was using a checkout of the ‘develop’ branch, which I believe is the 6.0.0 development version. Anyways, I have a couple of first impressions, comments, and questions.
First off, I’ll say this this is hands-down the easiest integration into C++ I’ve ever seen for a scripting language. So kudos for that. I wish more languages were this easy to plug into C++.
C1: For a first task, I spent some time adding (reflecting?) the standard math library into ChaiScript, only to realize that someone has already done that ( https://github.com/ChaiScript/ChaiScript_Extras/tree/master/include/chaiscript/extras ). A link to additional modules on the documentation page ( http://chaiscript.com/docs.html ) would have been helpful to save me from re-inventing the wheel on that one. If it is linked somewhere, I must have missed it. No problem. I learned something about binding functions to it.
Q1: Regarding that, what’s the best way to implement/add a C/C++ function to chaiscript that operates on any numeric type? For example, if I have sin(x), I’d like to be able to have that work in chaiscript regardless of if the user passes in an unsigned int, int16_t, float, etc. For starters, I wound up just adding in the provided overloads (sin, sinl, sinf, etc) and naming them all “sin”. Though I don’t think that will allow implicit conversion from integer types. I could wrap it in a template, and then add overloads for each of the integer types, but I’m not sure if that’s the best way to do that. And there are a lot of possible permutations, when you consider signed and unsigned types and length modifiers. Since most of those functions already have corresponding C implementations, it doesn’t make sense to implement them in chaiscript itself. If there’s a recommended practice here, perhaps a FAQ on ChaiScript.com would be helpful.
C2: I saw a question on either a ticket or the discourse forum somewhere, where someone was asking for random number support. And there was subsequent discussion about adding in the C++ random number facilities, distributions, etc. One thing I really like about ChaiScript, is that out of the box, it’s fairly minimal, and doesn’t provide an excessively huge standard library. So my 2 cents on that are, if such things are added in the future, it’d be nice to put them in a separate module that could be optionally added to the interpreter by the user. I did wind up adding in random number support, but I didn’t need everything that’s in the C++11/14 random number library, and so I provided a much smaller/simpler interface than what’s in the standard. That seems fine for my needs. I definitely liked having the flexibility to be able to make my own decisions about that.
Q2: I noticed that the built-in map type can only operate with strings as the key type. I assume there’s probably a reason that’s it’s not a map of object to object. Can you enlighten me on that decision? I think it would be useful to allow other types as the key type. And, assuming that’s possible (e.g. there’s not some technical limitation), it’d be nice to have a Set type as well. How hard do you think it would be to implement these (scale of 1-10)? I’d be willing to submit patch(es) for them if they are feasible.
Q3: I also noticed that there’s an old ticket in the tracker about enumeration support. Again, I’ll put in my vote that it would be a useful thing. And I’d be willing to try my hand at implementing it, though you might have to point me in the right direction, as I’m not familiar with the code base yet. Similar question applies about difficulty level of adding such a capability.
C3: I ran a really simple script under valgrind, and hit a couple of “use of uninitialized value” messages. Those may have been fixed already with the most recent commit, but I thought I’d mention it. Basically, I created a chai script with one empty function, and then did an eval in C++ to get a std::function to it. That’s it. I poked around in the internals a bit to see if I could figure it out in short order, but the couple of ideas that I tried didn’t fix it. I can file a bug for that one if that would be useful. I’ll also check if I can reproduce it after updating to the newest version.
C4: I ran into an issue where the constructor for the ChaiScript object was private. That caught me off guard. Adding “public:” fixed it. Looks like this was recently fixed in the mainline, so my copy must have been from just before that.
Q4: I also came to the realization that ChaiScript doesn’t currently support inheritance of ChaiScript-defined classes. I did find the pseudo-inheritance sample in the samples/ directory. Is that currently the recommended way to achieve polymorphic-like behavior? Or are there other implementation patterns that people use in practice to make their objects extensible? To be honest, of everything I’ve brought up, this is the one that has me wondering if ChaiScript will be appropriate for my application or not. I’ll continue to tinker with it and see if I think I can do what I need to do. What would be the likelihood of seeing support for inheritance, interfaces, or mixins in a future version? I do realize that multiple inheritance is a really hard problem. But even single inheritance would be useful. Back to the difficulty of implementation scale, what are we talking? 10/10?
C5: I’d also be really interested to see a “development roadmap” page added to ChaiScript.com, so I have some idea of where the project is headed.
Anyways, I realize that’s a lot of stuff. I think ChaiScript is a really interesting project, and I’m glad to see that there’s active development going on. I’ve definitely had fun tinkering with it, and I think it sets a high bar for ease of scripting language integration.