News for hackers. Stuff that matters.
Re:Lua Is So Underrated by fsmv on Thursday December 26, 2024 @05:49PM (#42518469)
I built a chatbot with lua plugin commands and I really had an awful experience trying to incorporate it in my C++ program. I really don't understand why people say it's easy to embed because you have to do a ton of stack manipulation to do anything. Not only that but the language itself is cumbersome, frustrating, and does nothing at all to help you when you make a mistake.

I will never use lua again and avoid any project that uses it.

Re:Lua Is So Underrated by bogwog on Thursday December 26, 2024 @06:40PM (#42518741)
I am in the "Lua sucks" camp. I also found it weird how clunky it is to embed, but my biggest gripe is with the language itself. It just sucks, as its barely a programming language at all. Every Lua project ends up reinventing its own class system or other language feature to get anything done.

I've also never seen a clean (non trivial) Lua codebase before. They're all spaghetti messes and difficult to follow. On the contrary, some of the worst, least maintainable and difficult to read code I've seen has been in Lua.

For configuration (what it was originally designed for) or for very small, non-critical stuff, then it can be a good choice.

With that said, I know some people are very productive with it, and have made impressive things in Lua. It's just not for me.

Re:Lua Is So Underrated by kragen on Thursday December 26, 2024 @07:26PM (#42518970)
> It just sucks, as it's barely a programming language at all.

I remember when I thought like this when I was first learning C++ and thought it was awesome because of all the features built into the language. Since then, I've learned that things that are "barely a programming language at all" like Lua—where you can easily learn the whole language and then write, if necessary, reflective object-oriented effortlessly parametrically polymorphic code—are actually the best languages of all. (Such extreme liberalism does require a compensating amount of discipline, as you say.)

Lua has its flaws (implicit nils, the nil/# interaction, default global, 1-based indexing, etc.) but minimalism is one of its major virtues.

Re:Lua Is So Underrated by wruza on Thursday December 26, 2024 @07:45PM (#42519055)
Then you get hold of a language that doesn’t have all this mess as a foundation and it feels like you stopped self-suffocating.

you can easily learn the whole language and then write, if necessary, reflective object-oriented effortlessly parametrically polymorphic code

This is all you can do in these languages. Anything above it will share the fragility and ad-hocness of this meaningless language para-olympics.

Re:Lua Is So Underrated by kragen on Thursday December 26, 2024 @08:47PM (#42519311)
Nope, that's completely wrong, to the point that you cannot have possibly thought it was correct. You can avoid using reflection, object orientation, and parametric polymorphism in Lua, just as you can in almost any other language, and stick to simple imperative code. And most of the time you should.
Re:Lua Is So Underrated by wruza on Thursday December 26, 2024 @09:07PM (#42519404)
I think you misunderstood my second paragraph. What I’m saying is that in order to do anything complex in it, you’ll have to reimplement important parts that are lacking, using all these meta/parametric things in your “mylualib”, and all this mud will drown you when you’re most vulnerable.

Toying with it with all the free time on one’s hands is okay.

Avoiding useful techniques is an option, but doing it when the world of saner alternatives exists feels like self-sabotaging.

Re:Lua Is So Underrated by kragen on Thursday December 26, 2024 @09:12PM (#42519419)
Hmm, that makes a little more sense than what I thought you were saying. I still don't think it's correct, though. Lua has those facilities already; you don't have to implement them yourself. My point is that it isn't like programming in assembly or C where you either implement them yourself or do without. When OO or reflection is the saner alternative, in Lua, you can just use it.
Re:Lua Is So Underrated by wruza on Thursday December 26, 2024 @09:33PM (#42519494)
I see your point and have even shared it, but my experience with real-business Lua was miserable and actively laughed at. Which was, I must admit now, absolutely deserved.
Re:Lua Is So Underrated by kragen on Thursday December 26, 2024 @09:50PM (#42519561)
Condolences! Fortunately, my own Lua experience has entirely been real-fun Lua instead of real-business Lua.
Re:Lua Is So Underrated by garbagelang on Thursday December 26, 2024 @10:17PM (#42519671)
> It just sucks, as its barely a programming language at all So very true.

This entire comment accurately depicts what it’s like to actually use Lua.

The worst, host horrific codebase I’ve ever worked on was in Lua for some godawful reason.

Re:Lua Is So Underrated by zaik on Friday December 27, 2024 @03:53AM (#42520803)
I enjoyed writing a small module for the Prosody XMPP server, which is written in Lua. Everytime I had to look into the internals I found what I was looking for pretty easily.
Re:Lua Is So Underrated by Etheryte on Thursday December 26, 2024 @06:26PM (#42518680)
My experience has been similar, even though not as drastic. Lua is fine for small extensions and such (e.g. Hammerspoon), but a lack of any kind of a reasonable standard library really bites me the wrong way. Sure, I can write a function to log an array, but why can't I just log an array like a decent human being?
Re:Lua Is So Underrated by kragen on Thursday December 26, 2024 @06:34PM (#42518719)
What language did you find was easier to embed, and how does it handle the lifetime management that Lua's embedding API handles with its stack?

The language itself may be a matter of taste. I prefer languages which, "in the face of ambiguity, refuse the temptation to guess," but certainly I've spent enough time using Perl, bash, JS, and BASIC to be able to deal with Lua's relatively mild level of DWIMminess.

Re:Lua Is So Underrated by int_19h on Thursday December 26, 2024 @08:08PM (#42519160)
Python is arguably easier since you don't need to handle non-native stacks at all; you just have a straightforward mapping of Python operations to C functions that you call with regular arguments, as usual.
Re:Lua Is So Underrated by kragen on Thursday December 26, 2024 @08:50PM (#42519328)
With Python's extension API, you have to carefully incref and decref Python values to ensure that you don't either leak memory or deallocate data that's still in use. That's what the Lua stack interface saves you. It's not a call stack, which seems to be what you're thinking of.
Re:Lua Is So Underrated by wruza on Thursday December 26, 2024 @09:22PM (#42519456)
Lifetimes and references are things to consider in Lua integration code too. See luaL_ref() rationale and why you can’t lua_pop() a string that is in use. The fact that gc doesn’t happen instantly 99.99999% of the times doesn’t save you from thinking about it.

Not that this was a hard topic in a language where lifetimes are the main concern and business as usual. In C/etc you can’t hold anything without a ref or some sort of a lifetime guarantee. What makes it special wrt Lua/Python?

Re:Lua Is So Underrated by kragen on Thursday December 26, 2024 @09:29PM (#42519480)
Yeah, I didn't mean to say you didn't have to think about lifetimes at all when embedding Lua, just that the stack, weird as it is, seems to simplify that kind of thing rather than complicating it (even if you do occasionally have to luaL_ref). But if there's a clearly better alternative, I'd like to know about it. Constantly calling Py_INCREF and Py_DECREF isn't it.

Delayed GC seems like it might add headaches for this kind of thing rather than removing them, because your bugs may take a while to show up in testing.

Re:Lua Is So Underrated by wruza on Thursday December 26, 2024 @09:40PM (#42519524)
Lua stack basically works as an arena if you aren’t eager to clean it.

I guess in C one could macro hack himself a similar arena to borrow objects from python runtime.

In C++ it’s a matter of befriending a smart pointer with ref/unref calls. It was probably done hundreds of times in all sorts of python embedding wrappers.

Re:Lua Is So Underrated by int_19h on Friday December 27, 2024 @09:51PM (#42528178)
Indeed, but the rules for doing so are straightforward and easy to apply almost mechanically. Indeed, if you are writing in C++ or Rust, you just use a smart pointer that does it for you.
Re:Lua Is So Underrated by le-mark on Thursday December 26, 2024 @08:08PM (#42519159)
I would use quickjs today for embedding over lua. Everyone know js nowadays and quickjs is really nice. The advantage lua has is library support via Lua rocks. Maybe someone can comment on the quickjs ecosystem?
Re:Lua Is So Underrated by kragen on Thursday December 26, 2024 @08:51PM (#42519336)
It sounds like you're saying quickjs is what you'd try if you wanted to embed a language, but you haven't tried embedding quickjs yet?
Re:Lua Is So Underrated by garbagelang on Thursday December 26, 2024 @10:22PM (#42519694)
Luarocks is an advantage? It’s full of thousands of variations of the same poorly copypasted code from idiotic Lua forums. All of them basically abandoned.

Lua rocks is a wart on Lua’s piss poor existence, not an advantage. Lol.

Re:Lua Is So Underrated by feznyng on Thursday December 26, 2024 @11:51PM (#42520023)
I think there’s a fork called quickjs-ng that’s actively maintained on GH. Should be able to use it as a lib maybe with some externing.

I’ve also had luck using it in a wasm runtime thanks to quickjs-emscripten. Good if you need sandboxing sans full process isolation.

Re:Lua Is So Underrated by kragen on Friday December 27, 2024 @08:24AM (#42521867)
Thank you for sharing your experience! Wasm provides full process isolation, doesn't it? Do you mean that QuickJS provides cheaper sandboxing than wasm?
Re:Lua Is So Underrated by feznyng on Friday December 27, 2024 @12:56PM (#42524360)
I’m not sure if wasm would be considered full process isolation but it does provide software based sandboxing. I’m referring to running quickjs in wasm by compiling it with emscripten.

https://github.com/justjake/quickjs-emscripten

Re:Lua Is So Underrated by kragen on Friday December 27, 2024 @08:38PM (#42527796)
I see, thanks!
Re:Lua Is So Underrated by dchest on Friday December 27, 2024 @02:09AM (#42520455)
I have experience of embedding and modifying both QuickJS and Lua. QuickJS is not bad, but Lua is a lot easier and has fewer bugs. JavaScript is a very complicated language under the surface; I think very few people, if any, know and remember all its quirks.
Re:Lua Is So Underrated by ga2mer on Friday December 27, 2024 @03:21AM (#42520684)
Amazfit's Zepp OS uses quickjs to run 3rd party watchfaces and apps in their smartwatch

I was surprised to find out I'd been wearing javascript on my arm all this time

Re:Lua Is So Underrated by xigoi on Friday December 27, 2024 @07:31AM (#42521643)
While QuickJS is nice, JavaScript as a language is much uglier and more complicated than Lua.
Re:Lua Is So Underrated by wruza on Thursday December 26, 2024 @06:54PM (#42518814)
It’s easy to embed compared to e.g. python, js or perl, and iff you only push/call/pop and it’s all functions with primitive arguments. Otherwise it’s ~as hard.