Earlier quoted context omitted.
I think it once was taught, but was considered passé in various places during the "Java" revolution, and in general I think there is a degradation of capabilities in most comp-sci programs, a consequence of interference by market players who invest in the schools in order to keep their technologies relevant. If anyone reading this wants to see what the fuss is, you can get started very easily with antirez' (Redis fam…
There's also the book "Creating Solid APIs with Lua" by Tyler Neylon [1,2] which seems to be quite specifically addressing this idea of embedding lua in something so as to implement highly-movable application logic in lua rather than the something you started out with. [1] https://www.oreilly.com/library/view/creating-solid-apis/978... [2] https://github.com/tylerneylon/APIsWithLua
Using Lua with C++
11–20 of 43 posts
Re: Using Lua with C++
#12Lua is a joy to work with, one of my favorite (scripting) languages. Having tables as the primary underlying data structure gives it a very LISP-y feel despite having an ALGOL syntax, providing a nice middle ground between the two styles.
Lua really is a joy, and after 40 years of software development is still one of the top ways, in my personal list, to get serious software built. I've done quite a few projects where I built a library of C modules to do the heavy lifting for some particular purpose (data in/out mostly), glommed these functions into the LuaVM, then built the application logic, very rapidly and smoothly, in Lua - outcompeting others wo…
We can also have a small core team writing C/C++, and then let a wider team loose to build applications with Lua.
Re: Using Lua with C++
#13Re: Using Lua with C++
#14Can untrusted Lua code be executed safely, with bounds on time and space (e.g. memory used)? Or if a threshold is exceeded, the host environment is notified?
Re: Using Lua with C++
#15Can untrusted Lua code be executed safely, with bounds on time and space (e.g. memory used)? Or if a threshold is exceeded, the host environment is notified?
Not sure about time, but you can pass a custom allocator to Lua and stop execution if the program is allocating too much.
Another way would be to make a minor interpreter modification to have it count byte code instructions executed and terminate execution that way. But then you might run into problems where calls into native functions might take a long time (e.g. string.rep("x", 1000000000)).
So it's not easy and the only solid approach is to have lua run in some external process and kill that using OS limits. You might get away using a thread instead of a process if you use a custom memory allocator to help with cleaning up resources, but I'm not sure if it's safe to terminate Lua that way.
Re: Using Lua with C++
#16Lua is a joy to work with, one of my favorite (scripting) languages. Having tables as the primary underlying data structure gives it a very LISP-y feel despite having an ALGOL syntax, providing a nice middle ground between the two styles.
Lua really is a joy, and after 40 years of software development is still one of the top ways, in my personal list, to get serious software built. I've done quite a few projects where I built a library of C modules to do the heavy lifting for some particular purpose (data in/out mostly), glommed these functions into the LuaVM, then built the application logic, very rapidly and smoothly, in Lua - outcompeting others wo…
I think Python is an example of the same pattern, you write your glue code in Python and the high performance code is in C called by C FFI from Python.
I just wonder how we can write code with development velocity of Python or Lua but actually evaluate extremely fast.
Re: Using Lua with C++
#17Earlier quoted context omitted.
Lua really is a joy, and after 40 years of software development is still one of the top ways, in my personal list, to get serious software built. I've done quite a few projects where I built a library of C modules to do the heavy lifting for some particular purpose (data in/out mostly), glommed these functions into the LuaVM, then built the application logic, very rapidly and smoothly, in Lua - outcompeting others wo…
I imagine this speeds up development velocity and is easy to adapt to a changing environment. I think Python is an example of the same pattern, you write your glue code in Python and the high performance code is in C called by C FFI from Python. I just wonder how we can write code with development velocity of Python or Lua but actually evaluate extremely fast.
Okay, not workable for every Lua project, but for most it'll be fine.
Re: Using Lua with C++
#18Earlier quoted context omitted.
Not sure about time, but you can pass a custom allocator to Lua and stop execution if the program is allocating too much.
Time is difficult to do. You can use hooks to have a function called for events like entering a function or executing a new line of code and then decide in that callback if execution should continue. That's fairly expensive and might not catch a tight infinite loop (while true do end). Another way would be to make a minor interpreter modification to have it count byte code instructions executed and terminate executio…
Lua provides the LUA_MASKCOUNT hook type since at least Lua 5.1, which calls the hook every N instructions. See https://www.lua.org/manual/5.1/manual.html#lua_sethook and https://www.lua.org/manual/5.4/manual.html#lua_sethook
The following prints 1 through 10 and then exits using Lua 5.3 and 5.4:
local i = 0
debug.sethook(function ()
i = i + 1
print(i)
if i >= 10 then
os.exit()
end
end, "", 1)
while true do
-- nothing
end
I've never used hooks in practice, though, so I may be missing some caveats beyond the obvious.Regarding time, IIRC the Lua implementation very carefully implements lua_sethook (and related internals) such that you can safely install a hook from a signal handler, at least on common hardware like x86, ARM, etc where pointer assignments are naturally atomic. It can be tricky to make your lua_State context available to a signal handler, though.
Also, unless you isolate the process using system facilities, Lua probably isn't suited to running potentially malicious code as the VM doesn't see enough eyeballs. Looking through the bug history at https://www.lua.org/bugs.html it's apparent that even if you disable all APIs there are occasionally exploitable bugs in the interpreter. I suppose the same is true in practice regarding, e.g., Node--many more eyeballs but the JIT engines are so much more complex that you end up in the same place. (The jury is still out on dedicated WebAssembly engines, but JIT engines are notoriously complex and "memory safe" languages don't help with JIT bugs.)
Re: Using Lua with C++
#19Especially when bringing up and testing new hardware and algorithms, it is incredibly useful to to be able to go from "wouldn't it be useful if we could test " to a fully automated system test with a few lines of Lua.
Re: Using Lua with C++
#20I have yet to find similar packages for C SWIG can be used to ease the c-binding coding.
Lua with C/C++ works basically like Python with CFFI, but Lua is much simpler and light weight that you can embed into your projects directly. A perfect combination between glue scripting and heavy lifting code that is small and easy to maintain.