views:

1801

answers:

3

I need to find a good Lua to JavaScript converter; lua2js on luaforge.org is out of date (3 or so years old and looks like it doesn't work on Lua 5.1) and I haven't yet found anything on Google.

Does anyone have any experience with any other converters out there? It should work on Lua 5.1 and preferably be .NET based, but .NET is not a requirement. A javascript lua interpreter would work as well.

+3  A: 

This is a recurrent question on the Lua list, i guess because of the superficial similarity of the two languages.

Unfortunately, there are many important differences that are not so obvious. Making it work need either a full-blown compiler targeting JS instead of Lua's bytecode, or rewriting the Lua VM in JavaScript.

I don't know the original goals of Lua2js; but if it was simply a limited 'translator', then writing Lua code intended to be translated would deny most (all?) of the nice things about Lua. For instance, i don't think you could use a function as a table key, since in JavaScript the keys are only strings (and numbers? i'm not sure).

The .NET choice is more reasonable, it could be done changing the existing compiler to emit CLR bytecode instead of standard Lua bytecode. Even if CLR is designed and optimised for other kind of languages, it's definitely generic enough to run very well. The main hurdle would be the availability of libraries.

Another option I just found is this paper by Roberto Ierusalmschy and Fabio Mascarenhas, where they explore translating LuaVM bytecode into CLR bytecode.

As is usual on academic papers, there's no indication about the date when it was written, so i have no idea if it's new and revolutionary or old and forgotten.

Javier
I wonder what the real goal of people is? They know Lua, and want to write JavaScript without having to learn JavaScript? They have a large application written in Lua, and want to port it to be a webapp with minimal work? None of these seem very likely to me
davr
The interview at http://www.computerworld.com.au/index.php/id;1028768484 makes it sound like Lua.NET is a recent and ongoing project.
Mike G.
Greetings, one can use a function as a table key in javascript. Your opinion does not seem informed.
tomdemuyt
i just tried on both Firefox 3.6.6 and Rhino 1.7r2. any non-string value (functions, objects, booleans, even numbers) that i use as key is accepted, and seemed to work, until i did a `for (k in t) {console.log(k,typeof(k));}` it's all strings. For most cases, it doesn't matter, as long as it's consistent; but in Lua it's different (any value except `nil` is acceptable as key), and lots of code do depend on that. if you want a Lua->JS 'translator' you have to consider these things, or it will be just JS with Lua syntax.
Javier
A: 

Translation to javascript is interesting to allow for a javascript replacement on the browser-side. We could take a little type safety on the browser too. Targeting javascript as a platform is targeting one of the most pervasive platform, the browsers of the planet. GWT does java2js but I am not sure if I want to introduce GWT for only a few pages in an application. I have to think about it. For your function as a key in a table, there must be some magic to be done. Maybe just assign a unique name to each function at compilation and use that for your key. You can also add a prefix to all your keys for type checking and that is a nice start.

lacroix1547
A: 

One way of doing this could be using LuaSub and generating JavaScript instead of Lua output. This can be done, with reasonable effort (currently LuaSub does not do it).

Places where JS cannot be bent to Lua's requirements could be discovered at compile time, and cause an error.

I am going to be doing a lot of JS+SVG in the future and if the JS side turns out a headache this may be a thing to try. If anyone else wants to have a go, please do so. The LuaSub source is there for you.

Originally, LuaSub was crafted as a syntax extender for Lua 5.1, to introduce ease-of-use concepts (s.a. increment, type check) without braking compatibility with standard Lua or needing to patch it. It is similar to MetaLua in this (which has become more commonplace, it seems).

akauppi