Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Is Lua that much of an improvement over JS to justify that?

This depends on the project, of course. For existing software that's already written in Lua and now needs to run inside a browser, running the Lua VM in JS may be the best way to go. However, I've decided that for new code, Lua doesn't have enough advantages over JS to justify the problems with running a VM in a VM.

Here are the things that I consider significant advantages of Lua over JS, and my thoughts on each.

1. Coroutines: JS is getting a form of these via generators, though I don't know how soon that feature will become ubiquitous. In the meantime, a compiler can turn code that uses coroutines into continuation-passing style.

2. Weak references: It's unfortunate that JS doesn't have these. But as the OP pointed out, running a VM within a VM introduces other memory management problems, since the guest VM has its own GC. To avoid those issues, I can live without weak references in the language.

3. Metamethods: The most well-known metamethods are for overriding table operations, so one can implement properties, proxies, or other dynamic behavior. I've sometimes found these useful on previous Lua projects, but for new code, I can do without them in order to avoid the problems of running a VM in a VM.

4. Non-string keys for tables/objects: There are ways around this. For example, instead of using another object as a key, one can assign a unique ID to the object, then use that as the key.



0. Lua is much simpler, with a much smaller WAT-quotient, without loss of power.

(For what I'm doing that's outweighed by the convenience of direct browser support, but to me that's the big attraction.)


ES6 will introduce Maps and WeakMaps, at least, which addresses two and four; similarly, proxies will enable three.


WeakMaps don't quite give you everything that weak references do in other languages. Sometimes weak refs can be used to check if the object referred to has gone away (the weak ref becomes null in that case), that is not possible with WeakMaps. You also cannot create a list of weak references and iterate over them with a WeakMap.

There has been some discussion about adding more powerful weak refs to JavaScript, but I'm not sure where that discussion stands.


I wrote a parenthetical about weak references. I wonder why I deleted it. In short: weakrefs are unlikely to appear any time soon in ES, as they open up all kinds of fun around cross-origin objects (being able to detect their approximate life-time, and that's a fairly major side-channel attack) in some lovely edge-cases.


If you have any more details about this, or even if you don't, please post to es-discuss about it. Currently there is broad consensus on TC39 to add weak references in JS in the future.


I was imprecise; Lua has weak tables, not general weak references. So I'd be happy with weak maps in JS.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: