Friday, November 26, 2010

Lua Improvements

It seems there are people interested in running Lua on the web :) The Lua demo was on Hacker News, and some people noticed that certain things don't work - thanks for the feedback! - so I spent a few hours figuring out what was wrong. After some small fixes, the demo will now handle these statements:
  • for k,v in pairs({'foo','bar'}) do print(k,v) end
  • for x = 1,10 do print(x) end
  • for k,v in pairs(_G) do print("->", k,v) end
which people mentioned did not work before.

The output seems ok, but I'm not totally sure that it's 100% correct, since my Lua skills are pretty rusty. That's also why I didn't notice the issues in the beginning - I saw that 'hello world' worked, and thought all was well ;)

If people really want to see this work properly, let me know about any other problems. I'm happy to work on it, I just can't do it without feedback since I don't know where to start testing. (Is there a standard test suite for Lua?)

The issues that had to be fixed in Emscripten were
  • Lua uses varargs in ways that the existing code didn't support. So I improved that. Varargs are now emulated in a way that is basically identical to C.
  • A few missing stdlib functions.
So, small stuff. In fact I'd be surprised if there were any big issues left, at this point, given the amount of code known to compile properly, but there are probably a lot of small things left to fix.


  1. Still doesn't work. I tried with this code:
    io.write("Hello world, from ",_VERSION,"!\n")
    Found on the Lua Web Demo:

  2. For loops only seem to work in Firefox, broken in chrome and opera.

    lazza: I'd be pretty scared if io functions worked..

  3. Why? I don't know LUA so please be patient with me. :)
    I found the example on the official website so I tought it was appropriate...

  4. Thanks for the feedback Lazza and TheAncientGoat!

    @Lazza: Hmm, to be honest I don't know Lua much either ;) But if you replace |io.write| with |print| then that code will work.

    I suspect as the TheAncientGoat says, it is trying to do IO to a file or something, and running on the web won't allow that...

    @TheAncientGoat: Ah, it seems to only work in browsers that support typed arrays right now... will have a fix up shortly.

  5. Ok, uploaded a fix. Stuff should now work in all browsers, including those without typed arrays. Tested on Chrome and Firefox. Please let me know if something is broken.

  6. Just tried a few examples from and many of the functions didn't work (but some did). The following didn't work:

    math.pow (x^y doesn't work either)

    and "/bin/this.program: (error object is not a string)" was displayed in response on the webpage. contains tests for Lua 5.1. If you want to add tests for non-C/C++ languages, Lua might be a candidate.

    Using Emscripten to run the Lua interpreter (not LuaJIT?) in the browser seems like a fun way to learn the language. I'm a Lua newbie.

    Maybe I'll give the Lua LLVM front end a try and feed the generated bitcode into Emscripten...not as fun as running the entire VM but maybe it'll be a faster and smaller way of running Lua apps in the browser. Accessing the DOM is going to be a problem...unless I'm missing something.

    Thanks for the great work.

  7. @Dave

    Going through the Lua LLVM frontend would be better, yeah. Let me know if you get that working, or if I can help!

  8. The Lua demo page ( ) mentions the needs for a Lua syntax highlighter. Codemirror ( ) supports this now, so switching Skywriter to Codemirror will do the trick.