News for hackers. Stuff that matters.
Re:Fengari – Lua for the Browser by friedturkey on Sunday February 20, 2022 @06:44AM (#30404992)
Lua is the only language I can truly say I love. If more people used it (and used it responsibly, not letting it become a mess of odd libraries), the world would be a better place.
Re:Fengari – Lua for the Browser by Sebb767 on Sunday February 20, 2022 @07:32AM (#30405194)
> Lua is the only language I can truly say I love

Why? I've worked with Lua before and to me it's not a bad language, but nothing special either. I'm honestly wondering.

Re:Fengari – Lua for the Browser by gilgad13 on Sunday February 20, 2022 @09:35AM (#30406005)
One aspect of Lua that stands out to me is how every feature is carefully designed both in isolation and in composition with the others. The language has relatively few features, but none of them are hanging off the side, the all lean on each other to make a cohesive whole.

I think Lua is a bit unique in this for two reasons. First, they have in intentional open-source but not open development model. Second, because of the way that Lua is embedded inside other projects, there is more willingness to implement backwards-incompatable changes. I'm sure this is a negative for some who want to build a larger, less fractured community, but it has advantages for language cohesiveness.

Re:Fengari – Lua for the Browser by friedturkey on Sunday February 20, 2022 @07:49AM (#30405286)
It has all the power of something like Python, but is much faster and with far less cruft and basically no confusing elements.

You’re given a small set of tools and it’s incredibly easy to build off them.

Re:Fengari – Lua for the Browser by chii on Sunday February 20, 2022 @07:58AM (#30405330)
> basically no confusing elements.

the one confusing element - one-based indexing of arrays! that is something i found hard to adjust, as it makes off-by-one errors more prominent...

but otherwise it's a nice language.

Re:Fengari – Lua for the Browser by DonHopkins on Sunday February 20, 2022 @09:05AM (#30405769)
I agree, that's the one unfortunate flaw of Lua I'd go back in time and change if I were Hitler With a Time Machine, but I've used Lua and other 1-based languages like ScriptX, and you totally get used to it, and finally realize that 0-based languages have their own confusing quirks and inconveniences that you got used to when you learned them, and you just don't think about them any more once you've internalized them, just like 1-based languages. It's just a matter of moving the confusing quirks and complexity around, not that 0-based languages are less confusing, complex, or quirky than 1-based languages, or the other way around.

But that said, I'd prefer that Lua had 0-based indexes, simply because that's what most other languages have, not because it's superior.

Re:Fengari – Lua for the Browser by chii on Sunday February 20, 2022 @08:56PM (#30411123)
> It's just a matter of moving the confusing quirks and complexity around

unless you can exclusively program in 1 language only, these confusions would be the cause of various bugs, or at least, increase the cognitive load. Programming is already hard enough, without artificially increasing the cognitive load!

Re:Fengari – Lua for the Browser by donatj on Sunday February 20, 2022 @08:56AM (#30405693)
I certainly prefer 0-indexed these days, but starting on 1-indexed languages (A variety of BASIC’s) I found 0-indexing just as weird.
Re:Fengari – Lua for the Browser by otikik on Sunday February 20, 2022 @06:51PM (#30410417)
If you work with pure Lua that’s a non issue. The ipairs function abstracts that away, and for numerical loops are written different than in C, with a start value (included), an end value (included) and a step (optional), and no exit condition. You can’t put i<len there as a result like you would do in C.

Agreed that interaction with C or other 0-based languages or systems (eg screen coords) makes things more difficult.

Re:Fengari – Lua for the Browser by _huayra_ on Sunday February 20, 2022 @10:01AM (#30406202)
iirc, one other confusing element is that tables (hash tables? I don't remember what they're called) return null when a lookup is done for a nonexistent key.

This is not necessarily a bad choice. Exceptions and such can be a real pain. However, accidentally getting a null value because you didn't check and then have it propagate much further in your program is extremely difficult to debug. Instead of blowing up at the site of the bad lookup, you only see the distant effects of it (e.g. put the null value into another table, which gets put in another table, and then is attempted to be called as a function, etc).

Re:Fengari – Lua for the Browser by boardwaalk on Sunday February 20, 2022 @10:47AM (#30406574)
It even does this for globals (which are just another table), so there’s never an “undefined variable” error (even though there really is).

It’s pretty unfortunate. You can mitigate it using meta tables though.

Re:Fengari – Lua for the Browser by otikik on Sunday February 20, 2022 @06:40PM (#30410344)
Give Teal [1] a look. It’s basically typescript for Lua

https://github.com/teal-language/tl

Re:Fengari – Lua for the Browser by naasking on Sunday February 20, 2022 @12:05PM (#30407179)
Storing a null value is a legitimate operation. How do you distinguish "never stored a value under key K", and "stored null under key K"?
Re:Fengari – Lua for the Browser by scgtrp on Sunday February 20, 2022 @12:30PM (#30407370)
I'm pretty sure you're agreeing with them. Current Lua does this, which is wonky:

    > t = {}
    > t['a'] = nil
    > t['a']
    nil
    > t['b']
    nil
Re:Fengari – Lua for the Browser by CollinEMac on Sunday February 20, 2022 @09:48AM (#30406105)
Same here. It's not a big deal once you get used to it but after years of always having indexes start at 0 it just seems... off.
Re:Fengari – Lua for the Browser by throwawayboise on Sunday February 20, 2022 @01:13PM (#30407807)
If you learned arrays in C where indexing is the same as pointer arithemetic then zero-based arrays seem natural. If you are coming from the real-world concept of "a list of things" then the "zero-th item" in the list seems odd; one-based indexing feels natural.
Re:Fengari – Lua for the Browser by keb_ on Sunday February 20, 2022 @08:33AM (#30405534)
That's not really confusing though, just takes a bit of unlearning the muscle memory of 0-based indexing.
Re:Fengari – Lua for the Browser by BiteCode_dev on Sunday February 20, 2022 @09:30AM (#30405980)
Lua definitely does not have the power of something like Python.

That's usually what people like about Lua: it's barebone, yet high level and clean.

If one likes Python, then the chance of liking Lua are low.

E.G:

Both python and lua can open something (a file, a socket, a transaction...) in one line.

But only Python has the `with` construct that means it's easy to guaranty you close it in case of an error.

Lua is then easier to learn: one less concept to master. But the high level tool of Python, that you had to learn, make your life easier.

They have very different trade off.

Re:Fengari – Lua for the Browser by johnmaguire on Sunday February 20, 2022 @09:52AM (#30406135)
> If one likes Python, then the chance of liking Lua are low.

I don't know. I quite like both as well. The design ethos feels similar although Python has certainly added more features over the years.

Re:Fengari – Lua for the Browser by BiteCode_dev on Sunday February 20, 2022 @10:07AM (#30406239)
"with" existed in python 2. In python 2.6 in fact, like comprehension lists, decorators, generators, descriptor protocol, exotic slicing assignation, advanced nested unpacking, infinite parameters...

Python was always chock-full of advanced features, people just usually don't notice because they get productive in 3 days with the basic features and don't need to go further. It's has the quality of a very smooth learning curve, but a very long one if you care.

Re:Fengari – Lua for the Browser by johnmaguire on Sunday February 20, 2022 @09:36PM (#30411363)
"with" and "defer" (Go) are nice but not crucial to me.
Re:Fengari – Lua for the Browser by BiteCode_dev on Monday February 21, 2022 @04:56AM (#30413828)
My point exactly.
Re:Fengari – Lua for the Browser by pygy_ on Sunday February 20, 2022 @03:34PM (#30409038)
Lua 5.4 has to-be-closed variables that are similar in functions to with blocks in python.

Unkike the __gc hook which provides no guarantee as to when it will be called, if ever, the __close hook is called when a value goes out of scope.

http://www.lua.org/manual/5.4/manual.html#3.3.8

Re:Fengari – Lua for the Browser by webmobdev on Sunday February 20, 2022 @08:42AM (#30405605)
Isn't it LuaJIT that's really fast? Last I remember reading about it, there was some version fragmentation going on with Lua advancing and LuaJIT stuck on an older version? (That was a long time ago, and I don't know what been happening since.)
Re:Fengari – Lua for the Browser by DonHopkins on Sunday February 20, 2022 @09:11AM (#30405813)
And LuaJIT has historically been extremely fast (initially much faster than JavaScript's early JITs) because it didn't have nearly as many optimizer-busting design flaws to work around as JavaScript JITs did, because Lua's language design is so much simpler and cleaner than JavaScript's, which wasn't originally designed to be compiled (cough cough "with" cough "this"). But because JavaScript was the "Chosen Language", a whole lot of effort has been put into developing JITs that worked around JavaScript's flaws over the decades since. But all that effort could have been put to much better uses if it didn't have all those flaws in the first place.
Re:Fengari – Lua for the Browser by pansa2 on Sunday February 20, 2022 @09:03AM (#30405751)
Yes, Lua’s now on version 5.4 and LuaJIT is stuck half-way between 5.1 and 5.2.
Re:Fengari – Lua for the Browser by fouc on Sunday February 20, 2022 @08:38AM (#30405576)
Why would Lua be significantly faster than Python? Isn't it an interpreted language too?
Re:Fengari – Lua for the Browser by harpiaharpyja on Sunday February 20, 2022 @08:59AM (#30405720)
The Lua interpreter is really lightweight, and for decades has been the go to choice for when you need dynamic code and speed (for example, it's been popular in the games industry for this reason).

Probably someone else can shed light on exact numbers, but Lua is faster than Python.

Re:Fengari – Lua for the Browser by scruple on Sunday February 20, 2022 @11:05AM (#30406706)
It's also very small. We used it extensively on embedded Linux devices in the 2000s for these reasons as well as easy interop with C/C++.
Re:Fengari – Lua for the Browser by aa-jv on Monday February 21, 2022 @08:34AM (#30415696)
I'm still using Lua on embedded Linux devices, and in fact completed a project last year that was deemed impossible by the Java devs, but easily within our memory/time budget as a Lua group. The success of that embedded Lua project converted a large number of Java diehards into Lua accolytes...

Folks who sniff at putting a scripted/interpreted language into an embedded environment really need to think twice with Lua. It is fast, tight and highly performant - and if you bundle it along with LuaJIT and Turbo.lua, it'll give you the best of all worlds - aynsc i/o, coroutines, very, very fast performance and a great execution environment upon which to build truly useful apps.

Re:Fengari – Lua for the Browser by leephillips on Sunday February 20, 2022 @08:57AM (#30405701)
The JIT version of LUA, according to some benchmarks I just saw, is similar to C in performance.
Re:Fengari – Lua for the Browser by pansa2 on Sunday February 20, 2022 @07:40AM (#30405234)
Not OP, but for me, I love Lua because it’s both easy-to-use and simple. Python is the former and Go is the latter, but I don’t know any other mainstream language that’s both.
Re:Fengari – Lua for the Browser by DonHopkins on Sunday February 20, 2022 @08:13AM (#30405409)
I wish Netscape had chosen Lua instead of bothering to invent JavaScript. Python would have also been much better than JavaScript, but Lua would have been perfect. But at least they didn't choose TCL, as Sun was pushing before they switched gears to Java after the Great TCL War. And personally, I would have preferred PostScript (which was the basis of NeWS, with a Smalltalk-like OOP system) or ScriptX (which was like Scheme or Dylan with "normal" infix syntax and a CLOS-like OOP system) to JavaScript.

If all the time and effort and treasure that was wasted cobbling together JavaScript from scratch with all its naive unforced design flaws and security holes and interoperability issues -- and then everyone else pissing away so much effort working around and fixing subtle bugs in JavaScript code due to those terrible design flaws (cough cough "this" cough cough "equality") -- had been put into Lua instead, which was cleanly designed from the start by incredibly smart people who actually had a clue about what they were doing, the world would be a much better place.

People who are that confused about equality shouldn't design programming languages:

https://dorey.github.io/JavaScript-Equality-Table/

"All animals are equal, but some animals are more equal than others." -George Orwell, Animal Farm

https://news.ycombinator.com/item?id=29379272

>For Netscape in 1995, I believe Python would have been a better choice than JavaScript, but Lua would have been an even better choice, given how much smaller, simpler, and more efficient Lua is, and the eventual excellence of LuaJIT. (If only Lua indexed its array from 0 instead of 1...) But Python and Lua didn't "look like Java" enough for Netscape.

https://news.ycombinator.com/item?id=17061967

[...]

>But if not PostScript, Python, or Lua, then at least Netscape didn't use TCL in the browser. Around 1994, long after NeWS and right before Java, Sun announced they were going to make TCL the official scripting language of the world wide web, which triggered RMS into kicking off the Great TCL War:

>RMS's "Why you should not use Tcl" flame:

https://wiki.tcl.tk/16730

https://news.ycombinator.com/item?id=17061858

>And with that diplomatically worded message, RMS kicked of The Infamous TCL War. That was Stallman's response to Sun bombastically pushing TCL as the official scripting language of the web, BEFORE Live Oak / Java was a widely known (or evangelized) thing.

>At the point anybody started talking about a Java/TCL bridge, it was already all over for TCL becoming the "ubiquitous scripting language of the Internet".

>Sun's unilateral anointment of TCL as the official Internet scripting language trigged RMS's "Why you should not use Tcl" message, which triggered the TCL War, which triggered Sun to switch to Java.

>After the TCL war finally subsided, Sun quietly pushed TCL aside and loudly evangelize Java instead. The TCL community was quite flustered and disappointed after first winning the title "ubiquitous scripting language of the Internet" and then having the title yanked away and given to Java.

>Any talk of bridges were just table scraps for TCL, the redheaded bastard stepchild sitting outside on the back porch in the rain, smoking a cigarette and commiserating with NeWS and Self.

>Tom Lord's description of what happened is insightful and accurate:

https://web.archive.org/web/20110102015130/http://basiscraft...

Re:Fengari – Lua for the Browser by zkldi on Sunday February 20, 2022 @08:25AM (#30405485)
> People who are confused about equality shouldn't design programming languages:

This is a very dull criticism of JavaScript, everybody uses ===. While it's definitely true the language has some poor decisions (`with`, `==`, etc.). You can write JS without using any of these features (and almost everyone does).

Infact, just using a decently strict ESLint config will get you most of the way there.

It's controversial, but I think somewhere under the cruft of JS is a good language. The syntax is dead simple, functions are first class, and with something like TS you can get static typing ontop of all of that.

Re:Fengari – Lua for the Browser by cardanome on Sunday February 20, 2022 @11:14AM (#30406775)
> The syntax is dead simple

It really annoys me that people confuse ease of use due to familiarity with simplicity.

Many programmers will be familiar with the typical curly-braced C-style syntax many mainstream programming languages have and so find JS syntax easy to get into but that has nothing to do with simplicity.

Building a parser for JS is not exactly simple nor is teaching new programmers the syntax. Lua is vastly superior in both aspects. People just tend to forget the pain most new programmers have to go through learning a curly braced languages, having to figure what all these weird symbols and different kinds of braces and how to type them.

JS is arguable even worse than other curly braced languages because of weird exceptions like automatically adding semicolons to line endings or these fancy arrow functions. Honestly JS is one of very few languages were I actually need to look up syntax after having not used it for a while.

Re:Fengari – Lua for the Browser by zkldi on Sunday February 20, 2022 @02:45PM (#30408656)
I don't personally think there's anything complicates about arrow functions. They're syntactic sugar for `function() {}`, with the exception that they have sensible binding of `this`.

I do agree that Lua is simpler, but I don't think this is an area where JS is that bad.

Re:Fengari – Lua for the Browser by user3939382 on Sunday February 20, 2022 @09:05AM (#30405761)
I’ve used JavaScript for about 25 years, sometimes more seriously, and always hated it. I’ve always found it unpredictable, I don’t like the turtles-all-the-way-down thing going on with its objects. I’d like to leverage my experience with regular class definitions and instantiation from Java workalikes. I’m sure many people love JS’s take on OO but I find it annoying.

A perfect example: I read hey there’s a cool simpler way to write small anonymous functions called arrow syntax. Nice let me try it, hm it’s not working —> google —> arrow functions have no access to ‘this’.

Re:Fengari – Lua for the Browser by capableweb on Sunday February 20, 2022 @10:15AM (#30406319)
> I’d like to leverage my experience with regular class definitions and instantiation from Java workalikes. I’m sure many people love JS’s take on OO but I find it annoying.

That's a bit like complaining about a language without types not having interfaces. I mean, sure, but it's also clearly not what that language is trying to do. Prototype-based programming is object-oriented programming without defining classes, that's basically the thing that sets it apart. If you're trying to do classical OOP with prototypes, you're not really embracing the paradigm of the language you're using.

> A perfect example: I read hey there’s a cool simpler way to write small anonymous functions called arrow syntax. Nice let me try it, hm it’s not working —> google —> arrow functions have no access to ‘this’.

Not sure what this has to do with OOP vs prototypes, or even Objects. But yeah, new language features sometimes modifies behavior like in this case with arrow functions (which are not just a different syntax to write anonymous functions). You're also wrong that they don't have access to `this`. They do, but arrow functions don't define a new lexical scope so `this` is referring to the closest part that did define the bindings for `this`, `super`, `arguments` et al.

Re:Fengari – Lua for the Browser by user3939382 on Sunday February 20, 2022 @04:09PM (#30409289)
> That's a bit like complaining about a language without types not having interfaces

Totally agree. I wouldn't make the mistake I see often of saying "for this reason JavaScript is objectively bad" -- but it's bad for me.

> you're not really embracing the paradigm of the language you're using

That's true, and related to the problem. I don't want to learn another paradigm, I want to get my work done. JavaScript's paradigm isn't intuitive to me, and at this point I assume will never click since I've been using it fairly consistently for 25 years and it still makes me uncomfortable.

> Not sure what this has to do with OOP vs prototypes, or even Objects

It doesn't. It's an example where JS often doesn't behave the way I expect it to. Some examples of this unpredictability have been fixed with newer versions over the last 25 years but this just illustrates the problem I often have where I make what I feel are reasonable assumptions about how its scoping or execution model works and have to trial-and-error my way into getting it to work right. This is after countless time spent reading JS books and posts over the years.

I accept that this may just be a personal issue, but I also don't think I'm the only one.

Re:Fengari – Lua for the Browser by djur on Sunday February 20, 2022 @01:58PM (#30408274)
> A perfect example: I read hey there’s a cool simpler way to write small anonymous functions called arrow syntax. Nice let me try it, hm it’s not working —> google —> arrow functions have no access to ‘this’.

If you try to use a new language feature without reading the documentation first, you will probably run into difficulties. This is not a unique problem to JS.

Re:Fengari – Lua for the Browser by ludamad on Sunday February 20, 2022 @10:38AM (#30406523)
There's a few ways of doing anonymous functions now, and some capture `this` and some don't. This is not a nice part of the language perhaps, but it's not like e.g. Java doesn't have similar complexity with inner classes and outer 'this'
Re:Fengari – Lua for the Browser by jamil7 on Sunday February 20, 2022 @08:44AM (#30405621)
I think your answer just solidifies their point since your proposed solutions are to avoid parts of the language and bolt two pieces of tooling on top.
Re:Fengari – Lua for the Browser by zkldi on Sunday February 20, 2022 @02:46PM (#30408665)
Fair enough -- Like I said, I think somewhere under the cruft of JS is a good language, but I don't think it's as bad as OP implies.
Re:Fengari – Lua for the Browser by nmz on Sunday February 20, 2022 @10:05PM (#30411515)
Of course you'd think that, there's even a famous book called "Javascript, The good parts".
Re:Fengari – Lua for the Browser by harpiaharpyja on Sunday February 20, 2022 @09:06AM (#30405778)
The point is that we wouldn't have needed decades of work on tooling to achieve what could have been done from that start with intelligent language design.
Re:Fengari – Lua for the Browser by DonHopkins on Sunday February 20, 2022 @08:38AM (#30405570)
Decades later you may believe that "everybody" uses "===", but actually "==" is still there, and many people still use that regardless of what you choose to believe, simply because it's there and 33% shorter and looks like C and Stackoverflow is full of examples of it, and it STILL commonly causes many subtle hard-to-find bugs.

People like Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, James Gosling, Guido van Rossum, and Anders Hejlsberg are enlightened, experienced programming language designers who actually know what they're doing, and they don't make stupid amateur-hour mistakes like JavaScript was cursed and riddled with from day one, that make it difficult for compilers to optimize code, and which we're still using tooling and linters and IDEs and compilers to work around, because they're still in the language and will never go away.

"My favorite is always the billion dollar mistake of having null in the language. And since JavaScript has both null and undefined, it's the two billion dollar mistake." -Anders Hejlsberg

"It is by far the most problematic part of language design. And it's a single value that -- ha ha ha ha -- that if only that wasn't there, imagine all the problems we wouldn't have, right? If type systems were designed that way. And some type systems are, and some type systems are getting there, but boy, trying to retrofit that on top of a type system that has null in the first place is quite an undertaking." -Anders Hejlsberg

But you also missed the reference to how deeply and tragically confused the bigoted designer of JavaScript is about equality when it comes to human beings, not just programming languages. And the terrible damage his confusion about equality and his promotion of inequality did to Mozilla and his co-workers and his own reputation and legacy.

I love what JavaScript finally evolved into after decades of intense development and revision and optimization, which took the precious time and effort of uncountable extremely talented people, but all that effort that was pissed away working around JavaScript's original stupid unforced flaws could have been applied in so many much more productive and useful ways, instead of wasted the way it was, and the world would be a much better place if a language like Lua that wasn't so naively and incoherently designed in the first place was used instead of JavaScript.

Re:Fengari – Lua for the Browser by Banana699 on Sunday February 20, 2022 @10:27AM (#30406430)
>the bigoted designer of JavaScript is about equality when it comes to human beings

Zero relevance to programming language design, makes your argument look very weak and emotionally-motivated. If you have to sink to bringing up the personal views of the language designer you're criticizing on an entirely unrelated matter to make a point, you don't really have a point.

>his own reputation and legacy

His legacy is quite fine, I love Brave browser and use it everyday and so are countless individuals. Not all of us are so fragile to the point of not using good software because its author just so happens to disagree on a completely irrelevant social issue.

As for Javascript, it's a terrible mistake but I would love seeing the ones you will make when you're forced to design and implement a language in 10 days.

Re:Fengari – Lua for the Browser by DonHopkins on Sunday February 20, 2022 @02:22PM (#30408465)
I would have used Lua in 10 days. That's the point.

My reasons for not using brave have nothing to do with the author, and everything to do with not wanting to participate in cryptocurrency bullshit.

Re:Fengari – Lua for the Browser by leephillips on Sunday February 20, 2022 @11:49AM (#30407049)
> But you also missed the reference to how deeply and tragically confused

I couldn’t find this reference in your little essays here. Where is it?

Re:Fengari – Lua for the Browser by samatman on Sunday February 20, 2022 @01:59PM (#30408281)
I share a similar vague desire, but the specifics are all off.

If Netscape had even heard of an obscure two-year-old scripting language from Brazil, and chosen to use it, we wouldn't have gotten the Lua we know and love in the bargain. 1995 Lua wasn't the obviously superior Javascript alternative that it's been since the early 200s.

Re:Fengari – Lua for the Browser by aasasd on Sunday February 20, 2022 @11:57AM (#30407107)
One of the best illustrations of how JS equality works: https://www.codewiz.org/wiki/pictures/jokes/computers/js-tri...
Re:Fengari – Lua for the Browser by aa-jv on Monday February 21, 2022 @08:29AM (#30415649)
Cannot agree with you more. It is just so elegant and powerful, and yet simple.

Easily one of the most productive tools in my suite, and I made a lot of money as a Lua developer last year, using Lua in a realtime analysis application.

Would love to see a Lua-only browser arise from these efforts. Its just such a delightful language to code in ..