32

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.

Kevlar
  • 8,804
  • 9
  • 55
  • 81

9 Answers9

10

A new challenger appears: Lua.js https://github.com/mherkender/lua.js

For some awesome demos proving its maturity, see https://github.com/ghoulsblade/love-webplayer

Lua.js works by converting Lua code directly to ECMAscript (including JavaScript, ActionScript), which gives it an important speed advantage over solutions which attempt to implement the Lua VM in JavaScript.

SheetJS
  • 22,470
  • 12
  • 65
  • 75
Breton
  • 15,401
  • 3
  • 59
  • 76
  • @Akavel attempted to edit this answer to add some information. The edit failed the approval process, but I think it's useful nonetheless. I quote: "For some awesome demos proving its maturity, see: https://github.com/ghoulsblade/love-webplayer Lua.js works by converting Lua code directly to ECMAscript (including JavaScript, ActionScript), which gives it an important speed advantage over solutions which attempt to implement the Lua VM in JavaScript." – Breton Mar 04 '13 at 04:11
  • i figured that you probably forgot about this, so i hope you come back and update this – SheetJS Sep 26 '13 at 20:31
  • @Breton I just get blue background for all demos here ? https://ghoulsblade.schattenkind.net/love-webplayer/ – user310291 Oct 10 '20 at 04:47
8

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.

Community
  • 1
  • 1
Javier
  • 60,510
  • 8
  • 78
  • 126
  • 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 Oct 08 '08 at 16:15
  • 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. Oct 09 '08 at 19:22
  • Greetings, one can use a function as a table key in javascript. Your opinion does not seem informed. – tomdemuyt Jul 06 '10 at 15:43
  • 4
    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 Jul 06 '10 at 19:43
  • 1
    To be more precise, in Javascript values are converted to their string representations before being used as keys. This can cause problems since its very easy to make things clash: for example, both the number 1 and the string "1" have the same string representation and, by default, all objects serialize to the same "[object Object]" string. Another difference between JS tables and Lua tables is that while both give undefined/nil when reading unused keys, in Javascript setting a key to undefined will not remove it from the object (it will still be iterable and hasOwnProperty will return true). – hugomg May 28 '12 at 14:47
  • @davr If you're using Lua for other parts of your stack -- for example, scripting Nginx or Redis -- the idea of using it for the front-end is pretty attractive. – solidsnack Jan 29 '13 at 16:34
  • The http://www.lua.inf.puc-rio.br/luanet/lua2il_jucs.pdf link is 404 as of 2016. – Arto Bendiken Jun 24 '16 at 22:47
4

Emscripten contains the Lua interpreter converted to JavaScript. You can try it in your browser. However the size is 1.5MB.

Jakob
  • 3,570
  • 3
  • 36
  • 49
4

ljs has a working demo of a lua vm in javascript. Source code can even be compiled in the browser using a lua interpreter written in lua.

https://github.com/humbletim/ljs

2

visit http://luajs.org to see my work.

There are demos and benchmarks on site menu.

It's only 35.5K before compressing and 12.2K after compressing. It can access Javascript objects/arrays directly. It's updated nearly every day.

It's faster than lua.vm.js. (Not so quick because of metatable implementation), and I keep optimizing it.

dda
  • 6,030
  • 2
  • 25
  • 34
tdzl2003
  • 224
  • 2
  • 4
2

I've been using this implementation https://github.com/mherkender/lua.js that was mentioned in a previous answer. It does either offline and load-time parsing and I've found it very easy to use.

My Lua scripts were calling through C-functions, and converting that C code to Javascript and getting it hooked up with lua.js has been pretty straightforward.

Tom Gaulton
  • 393
  • 1
  • 7
0

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
  • 17,018
  • 15
  • 95
  • 120
0

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.

0

I've written a partially complete Lua to ECMAscript static compiler.

https://github.com/mherkender/lua2js-experiment

It might not meet your needs, as the project is missing many key features and I'm not rushing to complete it because it's a dead-end. Read more in the README for details.

Despite some of the concerns I've seen around the Internet, there's only a few Lua-specific features (like the *fenv functions, or most of the debug library) that Javascript can't really support. Others, like the file library, are limited by the features that the VM has access to.

I also personally tried the Lua + Emscripten option, and while it's impressive, it is also very slow.

max
  • 676
  • 1
  • 5
  • 10