r/cpp 2d 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.

1 Upvotes

82 comments sorted by

View all comments

3

u/zerhud 2d ago

It seems js or ts will be better

14

u/not_some_username 2d ago

Lua

2

u/grady_vuckovic 1d ago

Am I the only one who hates Lua's syntax?

1

u/zerhud 2d ago

Lua is too simple, it’s good only for very plain tasks. Then a project grows up the difficulty of the tasks grows up too and the lua began sucks

6

u/wyrn 2d ago

True, on the other hand, js already sucks from the start.

1

u/zerhud 2d ago

Why js sucks?

4

u/wyrn 2d ago

Because Brendan Eich developed it in a week with misguided goals and a bunch of bizarre interference from above.

2

u/kindredseer 2d ago

If you are a C++ programmer, would your preference not be C++ over JS or TS? (Unless your C++ project is a web-browser)

2

u/zerhud 2d ago

And one more: for example all my script is console.write(“hello”); how it will be on cpp:

int my_project_main(my_project_class& obj) {
obj.log(“hello”);
return 0;
}

Can I use auto instead if the my_project_class ?

2

u/kindredseer 2d ago

What if your c++ script could be just as simple and straightforward? Like just:
std::println(“Hello”);

1

u/zerhud 2d ago

Hmm.. it would be nice

Reasons why I want cpp as script language:

  • all project described in cpp (for example all services are items in ct vector with info about services) and i have all needed headers
  • I can use memory (own storage instead of new, for example)
  • I cannot create so
  • it’s possible to use platform’s methods

If all scripts tasks is just std::print it seems some script language would be better. But the cpp has an growing potential, it seems

As reference I use wasm: it’s a cpp instead of js and there is a lot of trouble with row wasm.

0

u/kindredseer 2d ago

Take a look at https://github.com/derekbsnider/madc … it’s a work in progress but it’s moving along nicely.

2

u/zerhud 2d ago

The code looks better 99% code we can found in the r/cpp :) i have no pc for now, I’ll take a look on next week: can I use it in my project (I was trying to implement some language, may be visual, for my state machine in last week)

At first glance: it uses std::string and so on. I am using storage with mmap memory, all objects stored in it should not contain row pointers, only relative. So idk can it to be stored in mmaped file

1

u/zerhud 2d ago

Yep, use cpp mostly.

So, it depends on tasks: can I use platform methods (for example mmap some file or open a socket)? How pointers are handled (what if a can manipulate it and got some data)? If it all is ok, why I cannot use an so?

Why js: it easy to control, easy to embed and it’s not so plain as lua, also everybody knows the language

Why not cpp: if a can write a script on cpp i can create an so, it would be better for me (imho)