160 points · by rsp1984 · 7y ago · 89 comments
Why not just use Lua, one of JavaScript engines, Python, etc? They all have C++ bindings, all relatively easy to use. All are already used in many C++ apps for this exact purpose... (I guess it's not commonly known in all coder circles, but all those languages can just as easily be embedded in a C++ codebase as this.)
It uses a header-only approach, which makes it easy to integrate with existing projects.
It maintains type safety between your C++ application and the user scripts.
It supports a variety of C++ techniques including callbacks, overloaded functions, class methods, and stl containers."
Shipping header only is really admitting that modern C++ still has a lack of technique, sidestepping the lack of modules and packaging for instance.
Furthermore, it's ignorant to conflate headers-only libraries with a problem or a shortcoming.
I'm tired of that nitpicking. These tools happen to be outside of the language for historical reasons and probably C compatibility. It's not in the language because apparently not enough people care.
And I think that the ideal way is standardizing of the OS package management first, not having a package manager (or several) for each language. But it won't happen for OS business reasons.
In my case, it is Wren. Some people prefer Chai, AngelScript, Lua, mruby, etc.
Wren has exception-like error handling and concurrency. And the object model is very similar to C#.
EDIT: Sorry, I wrote this a little too quickly and ended up writing a bit too coarse of a description. I meant JS and Python are both heavyweight to include in a project, and Python is slow both to initialize and to run. I didn't mean JS is slow to run. I don't know how fast it starts up either.
Why do you believe it's ridiculous? Being able to integrate a third-party library by just adding a few source files to your source tree is as simple as it gets.
> The state of C++ build tools is very poor, and it is harming the language as a whole.
This assertion makes no sense, particularly in the light of this discussion. Installing a headers-only library is a solved problem, and even template-heavy libraries such as Eigen are already distributed and installed quite easily with standard linux package managers.
If you stick to Linux and Debian derivatives for example then you've got all the tools for dependencies, building and packaging.
Also then, if this would be the selling point of ChaiScript, I would want to see some benchmarks (I have a hard time believing it will outperform lua with the built-in JIT or one of the faster JS engines with optimizations (it's not like performance is a novel desire in embeddable scripting languages, tons of manhours have been put into them already))... The website doesn't seem to have any such benchmarks.
$ time python -c 'exit()'
python -c 'exit()' 0.01s user 0.01s system 98% cpu 0.021 total
Seems pretty fast to me...And though python is no speed daemon, for a scripting/embedded language, it's likely to be fast enough.
Now try importing some stuff, and also testing a little more thoroughly...
> python3 -c "import sys, timeit; n = 10; print('%.2f ms/spawn' % (1000 * timeit.timeit('subprocess.call([%r, %r, %r], stdin=subprocess.DEVNULL)' % (sys.executable, '-c', 'import re, argparse, subprocess'), 'import subprocess', number=n) / n))"
Ubuntu (native): 20.82 ms/spawn
Ubuntu (WSL): 43.16 ms/spawn
Windows: 89.45 ms/spawn (without imports: 74 ms/spawn)
For reference on Windows, here's PHP, taking half the time to start up: > python3 -c "import sys, timeit; n = 10; print('%.2f ms/spawn' % (1000 * timeit.timeit('subprocess.call([%r, %r, %r], stdin=subprocess.DEVNULL)' % ('php', '-R', ''), 'import subprocess', number=n) / n))"
45.14 ms/spawn
Now consider that you'll generally be doing a LOT more on initialization than just importing these packages, a lot of packages end up being extremely heavyweight, and very likely you'll be calling Python several times in a row... it can easily waste seconds on startup, even on Linux.But I stopped using it because it was insanely slow (in both compile times and runtime performance). Several hundred (if not thousand) times slower than the equivalent Lua code.
That was a few years ago, but skimming through the release notes, it doesn't seem to have been fixed.
You can put all your ChaiScript-related code into an isolated implementation file, and try to avoid touching that file as much as possible. But that only works well for long-term projects, where once you get all the bindings you need set up, you can coast for a while without touching those files.
For short-term projects it's a nightmare because you're constantly touching the ChaiScript files.
There's no inherent reason a header-only library should significantly impact compile times, aside from the fact that the authors usually don't have the foresight to make it efficient. It's also very difficult to find information about how to make templates more efficient at compile time.
If any hypothetical compilation time problem concerns you then rest assure that C++ enables you to develop submodules that wrap and instantiate your templates, eliminating the need to recompile them every single build.
Many of his videos / tutorials are worth a look if you are interested in C++.
I am worried though how memory management is handled under the hood. Is it possible to use traditional C++ memory management with any of the variables created in ChaiScript?
I'm not saying that I don't trust the implementation, I'd just like more control over it in some cases.
I believe the dynamic library loader requirement is a showstopper for AVR.
Codecov: 72%
Reality is often disappointing...
In all seriousness, looks pretty nice.
https://www.etymonline.com/word/chai
Edit: apparently the original source is China. Etymonline fail!
See: Tea if by sea, cha if by land: Why the world only has two words for tea
https://qz.com/1176962/map-how-the-word-tea-spread-over-land...
edit: For example, there's a fairly clear distinction in a game between engine code and gameplay code. Engine code is run all the time, and should be memory and computationally efficient, and robust. It won't likely need to be changed much, and is OK to spend some extra effort writing it to get all these properties. So C++ is great for this.
Gameplay code doesn't tend to have these requirements, and you may not want to pay the cost to write C++ if you don't need the benefits. And additionally, gameplay code often needs to be rewritten frequently while you experiment with gameplay to find what's fun, or do expansions, or support mods to the game, so a language that's very easy to write in helps a lot.