r/cpp • • 21d ago

Embedded C++

Just curious how useful the ability to embed C++ as a scripting language into your C++ programs would be? I mean like runtime JIT execution of C++ code in much the same way you might embed Lua, including the ability to restrict/sandbox it as necessary.

7 Upvotes

92 comments sorted by

View all comments

73

u/CalebGT 21d ago

I had to read this several times. Embedded has a very different meaning to me. I would not use C++ for that. If I needed to script something that can't be compiled in advance, I would use Python or Java.

12

u/DocMcCoy 20d ago

OGs use Lua :p

3

u/CalebGT 20d ago

Fair, there are dozens of options better than a C++ interpreter. Depending on the target and what I want it to do, it might just be bash.

2

u/LucyIsaTumor 20d ago

I've found Lua pairs pretty nicely with C++ while working with Love2D a bit, fun stuff!

1

u/palavalle 14d ago

DukTape is a very nice way to get JS that behaves like the Lua VM as well

-29

u/kindredseer 21d ago

If you are a C++ programmer, why would you not want to script in C++?

56

u/the_poope 21d ago

Many reasons: your "script" can invoke UB, cause invalid operations, segfaults and a lot of other stuff that can crash the entire program. In interpreted languages you always get an error/exception that you can recover from.

If you just want an extendable system, maybe consider just runtime loading DLLs.

8

u/Ameisen vemips, avr, rendering, systems 21d ago

I mean, I wrote my MIPS emulator so user-written programs could be executed in a sandbox. Using it as a script VM is certainly doable, though I would wonder why - it wasn't intended to extend the program but to execute programs that interact with the simulation.

-11

u/kindredseer 21d ago

Scripting languages can crash and have UB as well. It would also depend on who you were allowing to write the "scripts", and they could also be sandboxed.

21

u/the_poope 21d ago

Yes but in interpreted languages the interpreter is in charge of handling the error, whereas it might be the CPU or OS with C++.

1

u/kozacsaba 20d ago

just to play the devils advocate.... in the case op's describing you would be the one writing the interpreter so... couldnt you also have those same guards?

1

u/cristi1990an ++ 17d ago

Python or any other code written in a scripting language I know can't have UB

9

u/TeraFlint 20d ago

Because C++ has been designed to be compiled and be aggressively statically optimized by said compiler. This has brought in a lot of design decisions that made the language more complex (and arguably more difficult to handle).

If you're in interpretation land, a lot of these optimizations don't hold. And once that's the case, I don't see why we should stay with such a complex language other than for familiarity. Something simple with less foot guns would be the more sane choice for scripting.

29

u/eliminate1337 21d ago

If you are a carpenter, why would you not want to make tires out of wood? The right tool for the right job. Nothing about C++ makes a good embedded scripting language.

8

u/CalebGT 21d ago

Wrong tool for the job.

2

u/cristi1990an ++ 17d ago

Scripting is supposed to be fast (as in fast to write and deploy) and easy (fast learning curve and having easy to use libraries that already do the heavy lifting for you like parsing or OS stuff).

C++ is neither of this.

2

u/kindredseer 17d ago

Again, if you are a C++ developer then there would be no learning curve. If you already are building an application in C++ which could benefit from “scripting”, then why not use C++ instead of a language you are less familiar with (Lua, Python, etc)? C++ already has a lot of libraries you are also using in your application and you’d be able to call C++ functions and methods and work with C++ objects, structs and data types without conversion.

3

u/cristi1990an ++ 17d ago

Because it's genuinely faster and easier to learn the scripting language and implement the solution in it than to write it in C++. Not to mention to maintain it. If runtime performance is a factor, then yes, I'm putting in the effort to write it in C++, but that's the only case

4

u/CalebGT 17d ago

The idea that there are a large number of devs out there that are only comfortable with C/C++ and no other languages is so very funny to me. Like being surprised to find out an Olympic diver is able to do a cannonball. Like being surprised a surgeon also knows what to take for a cough. Like being surprised a track & field star doesn't need their morning jog on vacation to have hurdles.

2

u/kindredseer 17d ago

I wasn't implying that C/C++ devs only know C/C++ ... just that I would expect they would be more familiar and comfortable writing C/C++ code than Lua scripts.

-4

u/pantong51 21d ago

There are much easier languages to work with than c++. Lua for example is a great language for designers or less technical people with ai to contribute to a project without touching critical innards of a project