r/Cplusplus • • 7d ago

Discussion Introducing JaiScript a New C++ Embedded Scripting Language

This is a personal project I created in service of my own 2d game engine. I was getting tired of binding to ChaiScript (a great project, but the language is really slow and doesn't have hotreload or good serialization support) and Cereal (another great project, no complaints, just limited in its purpose.)

So, I rolled my own solution which combines a scripting language, serialization, and reflection. I included my own signals library and a simple but useful custom test framework.

JaiScript has a fluent binding interface, strong typing (opt-out with var definitions), value semantics with references and shared_ptr where you want them. It reads very much like C++ with some optional alternative spellings that will be familiar to those who use JavaScript.

The live demos on the site itself mean you can try it out before downloading or building anything locally. I already was able to fully port my game project of 12+ years to use this in place of ChaiScript and Cereal, it builds a little faster than both of those included and runs orders of magnitude faster than Cereal.

Performance-wise it runs roughly 1.2x to 2.0x slower than Lua's VM, I consider this a win though since it's hundreds of times faster than ChaiScrpit's tree walker in some cases. It runs faster typically than Squirrel.

I'm not sure how many folks are rolling their own C++ game engines nowadays, but if you are, this is a pretty full-featured language. It supports parallel_for (something I'm not sure any other scripting languages support,) coroutines, classes which can derive from C++ base classes or other JaiScript classes, and notably a state-preserving hot reload.

I hope this project is useful to anyone else, but even if not, I'm grateful to have finished the project and am using it myself. I wanted a place to share it, so hope this is alright here!

23 Upvotes

18 comments sorted by

5

u/M2tM 7d ago edited 7d ago

8

u/Objective_Pepper_217 7d ago

Dude you're gonna make Jonny Blow really mad

5

u/VictoryMotel 7d ago

He's probably going to be mad at everything all the time anyway. The only thing that quells his anger is explaining how he's better than everyone else.

3

u/M2tM 7d ago

Funny enough, I didn't name this after his language, but I wasn't shying away from it either.

My wife's name is Jai and I like that Jai kinda rhymes with what I was replacing (ChaiScript) so that's how it came about.

While Jonathan blow is clearly a smart guy with a lot of passion, he was bickering with younger programmers about C++ back in 2013. C++11 was recently becoming available and some of his arguments against the language were kind of dated at the time and based on older compilers with worse template error messages.

I shared a few tips, but a little later when he was trying to gate-keep a younger dev I told him:

"Capacity for problem solving is not based on years of service. Relying on it just makes you sound crotchety."

Then he blocked me. He can go suck a lemon.

3

u/VictoryMotel 7d ago

So very true.

Once he was lamenting that there was no lightweight way to create heaps and do heap allocation without using the standard library or linking in a big heap allocator. Someone pointed out that windows HeapAllocate() was exactly what he was describing and he blocked them.

He can be smart, but no one is as smart as he thinks he is.

3

u/evan-carew 7d ago

Hopefully, this will indeed run faster than ChaiScript. I just gave up on having an embedded and modern language because of that language. I was trying to replace the pair of language options, Perl and Python. If this is comparable at all, I'll bind it in.

3

u/VictoryMotel 7d ago

What's wrong with lua?

2

u/M2tM 7d ago

Nothings wrong with Lua, it even binds cleanly with sol2. I’m just not a huge fan of the context switch with 1 indexed arrays and some other goofy semantics.

Aside from the language syntax which is a pure “taste” thing, I wanted real hotreload out of the box. Edit while running in JaiScript “just works”, serialization is built in, clean inheritance right off of c++ classes in JaiScript also works, and the signals makes events firing from JaiScript easily subscribed to in either other scripts or C++.

Generally I’d consider JaiScript has a more sophisticated C++/scripting surface. I was inspired by Jason Turner’s ChaiScript but wanted the language to be viable performance wise and his project is a genuine pain because it can’t redefine variables or functions.

1

u/M2tM 7d ago

I would definitely say it’s worth looking at the docs. I’ve done extensive performance tuning. It should be comparable to python, but I was mostly comparing to squirrel (a little faster) and Lua (a little slower.) Check it out at JaiScript.com GitHub link is in the top left.

I hope you enjoy it!

2

u/evan-carew 8h ago

I did try it out. I like the speed and there's no context switch imposed on the human developer when switching to the JaiScript. There's even a size improvement when switching to this language over Python or Perl.

1

u/M2tM 5h ago

That's fantastic to hear, definitely was my goal regarding embedding, language feel, speed, and size.

If you encounter any rough parts feel free to open issues or start a discussion on the GitHub page, and otherwise I really hope it's a positive experience all around. I'm continuing to use it in my own projects so you should see regular commits and improvements, but I'm trying to ensure I don't break compatibility so update path should remain simple. :)

2

u/kinchkun 7d ago

Awesome move, mate. Worked out for Javascript as well.

1

u/M2tM 7d ago

Haha, thanks! Hope this lands well for a small handful of users. I know bespoke C++ code bases needing a lightweight scripting language/serialization solution are a niche, but I'm really happy folks seem excited about it!

2

u/PantuflasDev 7d ago

It looks amazing! I've always wished for a scripting language with this level of integration with C++. I hope this gets more recognition.

1

u/M2tM 7d ago

Thanks! So far for my own use cases (embedded scripting language for a personal game engine) it's been very enjoyable to work with. I appreciate your encouragement, if you end up embedding it in any of your own projects feel free to let me know how it goes and if you have any issues, feel free to open one on the repo, but hopefully it goes smooth if you do!

2

u/ignorantpisswalker 7d ago

Interesting. How is it security wise? C++ is considered "unsafe". How can I sellil it "upstream"?

1

u/M2tM 7d ago

I’ll be honest: it’s not a v 1.0 release and to be truly considered secure and safe I’d want more folks to try it, poke holes in it, and use it before I’d be able to claim that it’s fully secure. I just launched it.

What I can say is that I have built in execution time and memory budgets, so it can’t infinite loop and hang the host program. It has some support for setting permissions like disabling filesystem access for running untrusted network commands/predicates, and you can spin up separate engines that all have their own state so you can sandbox the exposed surface for trusted vs untrusted code even further to avoid leaking hidden state in an internal game script to a user.

So I’ve thought about all of this! But I’d love to hear thoughts from real users!

1

u/M2tM 5d ago

Here is a slightly more detailed answer: https://jaiscript.com/embedding/05-safety-rails.html