lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Sat, Nov 5, 2011 at 8:05 PM, HyperHacker <hyperhacker@gmail.com> wrote:
On Sat, Nov 5, 2011 at 16:26, Maximilian Herkender
<static@brokenfunction.com> wrote:
> Hi, I wanted to share a Lua-related experiment I've written.
> https://github.com/mherkender/lua2js-experiment
> Basically, it converts a single Lua script into a _javascript_ equivalent.
> It works by parsing normal Lua code and wrapping pretty much every Lua
> operation around a function to maintain compatibility, and using _javascript_
> constructs wherever possible, the result being a pretty fast _javascript_
> version of a Lua script. It seems like there's only a handful of Lua
> features that seem to be completely incompatible with this style of
> translation.
>
> Still, this is better used as a proof-of-concept rather than a legitimate
> tool. I didn't plan much in advance so it's only clear to me now that it
> should've been written in _javascript_ instead of Python, which makes this
> something of a dead end. Since it works for what I've been using it for, and
> I'm having trouble finding an equivalent to ply for _javascript_, I decided to
> put it up online for now in case anyone would like to check it out.
> -Max

I've been curious about the possibility of this for a while. It sounds
like yours even goes as far as to provide JS equivalents of Lua's
standard functions? I had envisioned a tool that would mainly just

Most of them, yes.
 
translate Lua to _javascript_ at the syntax level, so you could write
things like:
local form = document.getElementById('myform')
if foo then form:submit() else doSomethingWith(form) end
and just pretend the browser understood that by translating it to
_javascript_ syntax.

There's a lot of fundamental differences between Lua and _javascript_, even at the syntax level. Fortunately, the majority of them were easy to wrap _javascript_ functions around, but I can see at least three things to consider when translating that chunk of code to _javascript_.

- What is "true" is different in Lua and _javascript_. If "foo" was 0, then form:submit() would be called in Lua, but doSomethingWith(form) would be called in _javascript_. In my implementation it is wrapped around a call that follows the Lua standard.
- form:submit() seems fine, but what happens if form.submit() is called? Is the first argument passed in as "this"? If so, some kind of function must intercept and handle this situation.
- Other operations, like getting a value from a table, calling a function, would still need to handle things like metatables if Lua functionality is to be preserved. It can be difficult to know if "form" is a _javascript_ object or Lua table at compile time.
 

--
Sent from my toaster.