Chetan Singh | 2 Oct 01:34
Picon
Favicon

JavaScript to Lua compiler

Hi,

I am new to Lua and looking to compile JavaScript code to Lua so I can execute my JavaScript code in Kahlua VM on
J2ME devices. I heard there is a compiler named Fusion or something which does exactly what I am looking for
but could not find any information on it. Is this tool available or is there other tool that I can use to
covert javascript to Lua.

Thanks
Chetan

Mike Crowe | 2 Oct 15:21
Picon

Re: JavaScript to Lua compiler

Hi Chetan,

Nothing like this exists atm to my knowledge.

Mike

On Wed, Oct 1, 2008 at 7:35 PM, Chetan Singh <chetan_dhillon <at> yahoo.com> wrote:
Hi,

I am new to Lua and looking to compile JavaScript code to Lua so I can execute my JavaScript code in Kahlua VM on J2ME devices. I heard there is a compiler named Fusion or something which does exactly what I am looking for but could not find any information on it. Is this tool available or is there other tool that I can use to covert javascript to Lua.

Thanks
Chetan





steve donovan | 2 Oct 15:32
Picon

Re: JavaScript to Lua compiler

On the face of it, many JavaScript constructs are going to translate
nicely, like

obj.method(function() {
   dosomething();
})

to

obj:method(function()
    dosomething()
end)

But how to tell when something's being called as a method? Lua
requires us to know this upfront so we can say ':' and ensure that the
self argument is passed implicitly.

Also, from my limiited experience, JavaScript is very permissive about
conversions which in Lua have to be explicit.

In short, it is much more of a compiler (deep semantic mapping) than a
translator that's needed.

steve d.

On Thu, Oct 2, 2008 at 3:21 PM, Mike Crowe <drmikecrowe <at> gmail.com> wrote:
> Hi Chetan,
>
> Nothing like this exists atm to my knowledge.
>
> Mike
>
> On Wed, Oct 1, 2008 at 7:35 PM, Chetan Singh <chetan_dhillon <at> yahoo.com>
> wrote:
>>
>> Hi,
>>
>> I am new to Lua and looking to compile JavaScript code to Lua so I can
>> execute my JavaScript code in Kahlua VM on J2ME devices. I heard there is a
>> compiler named Fusion or something which does exactly what I am looking for
>> but could not find any information on it. Is this tool available or is there
>> other tool that I can use to covert javascript to Lua.
>>
>> Thanks
>> Chetan
>>
>>
>>
>>
>
>

David Given | 2 Oct 16:02

Re: JavaScript to Lua compiler

steve donovan wrote:
[...]
> But how to tell when something's being called as a method? Lua
> requires us to know this upfront so we can say ':' and ensure that the
> self argument is passed implicitly.

>From my limited knowledge of Javascript, you can tell from the syntax.
The three types of calls are:

Func()
object.Func()
new Func()

...which are very roughly equivalent to the Lua:

Func(this)
Func(object)
o={}; Func(o); return o

(this in Javascript is passed implicitly and is not a function argument.)

But life is made more complex by the way that Javascript variables don't
really work the same way that Lua ones do --- *all* 'var' variables end
up in the equivalent of the local environment, I believe, and I'm not
entirely sure of when new environments show up.

(I'd like to find out about this stuff at some point, but the Javascript
reference documentation is largely incomprehensible. Anyone know where I
can find a clear description of how it all works?)

--

-- 
David Given
dg <at> cowlark.com

Chetan Singh | 2 Oct 20:18
Picon
Favicon

Re: JavaScript to Lua compiler

I was referring to folloging thread on this mailing list when I said Fusion.
http://lua-users.org/lists/lua-l/2005-11/msg00266.html

At that time the work on compiling JavaScript to Lua was in progress but I could not find any update since then
so I was wondering if any one knows about it and have used it.

thanks

--- On Thu, 10/2/08, David Given <dg <at> cowlark.com> wrote:

> From: David Given <dg <at> cowlark.com>
> Subject: Re: JavaScript to Lua compiler
> To: "Lua list" <lua <at> bazar2.conectiva.com.br>
> Date: Thursday, October 2, 2008, 7:02 AM
> steve donovan wrote:
> [...]
> > But how to tell when something's being called as a
> method? Lua
> > requires us to know this upfront so we can say
> ':' and ensure that the
> > self argument is passed implicitly.
> 
> >From my limited knowledge of Javascript, you can tell
> from the syntax.
> The three types of calls are:
> 
> Func()
> object.Func()
> new Func()
> 
> ...which are very roughly equivalent to the Lua:
> 
> Func(this)
> Func(object)
> o={}; Func(o); return o
> 
> (this in Javascript is passed implicitly and is not a
> function argument.)
> 
> But life is made more complex by the way that Javascript
> variables don't
> really work the same way that Lua ones do --- *all*
> 'var' variables end
> up in the equivalent of the local environment, I believe,
> and I'm not
> entirely sure of when new environments show up.
> 
> (I'd like to find out about this stuff at some point,
> but the Javascript
> reference documentation is largely incomprehensible. Anyone
> know where I
> can find a clear description of how it all works?)
> 
> -- 
> David Given
> dg <at> cowlark.com

Fabio Mascarenhas | 2 Oct 20:33
Picon

Re: JavaScript to Lua compiler

On Thu, Oct 2, 2008 at 9:02 AM, David Given <dg <at> cowlark.com> wrote:
> steve donovan wrote:
> [...]
>
> But life is made more complex by the way that Javascript variables don't
> really work the same way that Lua ones do --- *all* 'var' variables end
> up in the equivalent of the local environment, I believe, and I'm not
> entirely sure of when new environments show up.

Mapping Lua's lexical scope into JavaScript's function scope is easy,
rename the variables so the scopes don't clash, and fix the
references. It's even easier if you are compiling to a newer
JavaScript implementation and use let instead of var.

Implementing all the rest of Lua's semantics is the hard part,
JavaScript's semantics are just superficially similar to Lua's, they
are in fact quite different. Basically every Lua operation will have
to be compiled to JavaScript code that checks the types of the values
involved and implement the correct semantics. Even a simple operation
as a+b would be compiled to the "add_event" code on section 2.8 of
Lua's reference manual, translated to JavaScript, of course. Likewise
for indexing, and even function calls (as functions are not the only
callable object in Lua).

It's pretty straightforward, though, and Mozilla's tracing JIT could
probably eliminate a lot of the overhead in the hot paths (if all the
extra code doesn't push the hot paths above their maximum size).
SquirrelFish and V8 wouldn't fare as well. In the end, JavaScript is
just as alien to Lua's semantics as the JVM or CLR.

> (I'd like to find out about this stuff at some point, but the Javascript
> reference documentation is largely incomprehensible. Anyone know where I
> can find a clear description of how it all works?)
>
> --
> David Given
> dg <at> cowlark.com
>

--
Fabio Mascarenhas

Fabio Mascarenhas | 2 Oct 21:49
Picon

Re: JavaScript to Lua compiler

Oops, just noticed it's JavaScript->Lua, not the reverse (if the
intention is runninng JavaScript on J2ME maybe http://minijoe.com/ is
more useful, though).

Compiling JavaScript function scope to Lua lexical scope is easy, too,
if your variable names are already unique (i.e. you know which
specific declaration covers each use). The semantic mismatch in all
operations cut both ways, too, but you can probably get away with
using Lua tables for JavaScript objects and have JavaScripts obj.foo
compile to Lua's obj.foo (metatables go a long way here).

JavaScript will run even slower in Kahlua than Lua code does, though,
but maybe your application won't care... sorry for immediately
assuming Lua->JavaScript, but you usually compile nicer languages to
uglier ones, so you can program in the nicer ones. :-)

--
Fabio Mascarenhas

On Thu, Oct 2, 2008 at 1:33 PM, Fabio Mascarenhas <mascarenhas <at> acm.org> wrote:
> On Thu, Oct 2, 2008 at 9:02 AM, David Given <dg <at> cowlark.com> wrote:
>> steve donovan wrote:
>> [...]
>>
>> But life is made more complex by the way that Javascript variables don't
>> really work the same way that Lua ones do --- *all* 'var' variables end
>> up in the equivalent of the local environment, I believe, and I'm not
>> entirely sure of when new environments show up.
>
> Mapping Lua's lexical scope into JavaScript's function scope is easy,
> rename the variables so the scopes don't clash, and fix the
> references. It's even easier if you are compiling to a newer
> JavaScript implementation and use let instead of var.
>
> Implementing all the rest of Lua's semantics is the hard part,
> JavaScript's semantics are just superficially similar to Lua's, they
> are in fact quite different. Basically every Lua operation will have
> to be compiled to JavaScript code that checks the types of the values
> involved and implement the correct semantics. Even a simple operation
> as a+b would be compiled to the "add_event" code on section 2.8 of
> Lua's reference manual, translated to JavaScript, of course. Likewise
> for indexing, and even function calls (as functions are not the only
> callable object in Lua).
>
> It's pretty straightforward, though, and Mozilla's tracing JIT could
> probably eliminate a lot of the overhead in the hot paths (if all the
> extra code doesn't push the hot paths above their maximum size).
> SquirrelFish and V8 wouldn't fare as well. In the end, JavaScript is
> just as alien to Lua's semantics as the JVM or CLR.
>
>> (I'd like to find out about this stuff at some point, but the Javascript
>> reference documentation is largely incomprehensible. Anyone know where I
>> can find a clear description of how it all works?)
>>
>> --
>> David Given
>> dg <at> cowlark.com
>>
>
> --
> Fabio Mascarenhas
>

Chetan Singh | 2 Oct 22:06
Picon
Favicon

Re: JavaScript to Lua compiler

Hi fabio, Thanks fo pointing me to MiniJoe. I will take a look. Yes I was looking for JavaScript -> Lua
assuming that that Lua VM would be faster than any other JavaScript engine that is avaialbe for J2ME
devices. 
Regarding your comment on running JacaScript in Kahlua, once it is compiled into Lua (off-device lets say
on a server), it would run as fast as Lua code on J2ME. Right?

I am not very much familar with either language so don;t know how much work is involved in translating JS to
Lua. That is why I am looking for a tool/complier.

thanks much for you help
CHetan

--- On Thu, 10/2/08, Fabio Mascarenhas <mascarenhas <at> acm.org> wrote:

> From: Fabio Mascarenhas <mascarenhas <at> acm.org>
> Subject: Re: JavaScript to Lua compiler
> To: "Lua list" <lua <at> bazar2.conectiva.com.br>
> Date: Thursday, October 2, 2008, 12:49 PM
> Oops, just noticed it's JavaScript->Lua, not the
> reverse (if the
> intention is runninng JavaScript on J2ME maybe
> http://minijoe.com/ is
> more useful, though).
> 
> Compiling JavaScript function scope to Lua lexical scope is
> easy, too,
> if your variable names are already unique (i.e. you know
> which
> specific declaration covers each use). The semantic
> mismatch in all
> operations cut both ways, too, but you can probably get
> away with
> using Lua tables for JavaScript objects and have
> JavaScripts obj.foo
> compile to Lua's obj.foo (metatables go a long way
> here).
> 
> JavaScript will run even slower in Kahlua than Lua code
> does, though,
> but maybe your application won't care... sorry for
> immediately
> assuming Lua->JavaScript, but you usually compile nicer
> languages to
> uglier ones, so you can program in the nicer ones. :-)
> 
> --
> Fabio Mascarenhas
> 
> On Thu, Oct 2, 2008 at 1:33 PM, Fabio Mascarenhas
> <mascarenhas <at> acm.org> wrote:
> > On Thu, Oct 2, 2008 at 9:02 AM, David Given
> <dg <at> cowlark.com> wrote:
> >> steve donovan wrote:
> >> [...]
> >>
> >> But life is made more complex by the way that
> Javascript variables don't
> >> really work the same way that Lua ones do ---
> *all* 'var' variables end
> >> up in the equivalent of the local environment, I
> believe, and I'm not
> >> entirely sure of when new environments show up.
> >
> > Mapping Lua's lexical scope into JavaScript's
> function scope is easy,
> > rename the variables so the scopes don't clash,
> and fix the
> > references. It's even easier if you are compiling
> to a newer
> > JavaScript implementation and use let instead of var.
> >
> > Implementing all the rest of Lua's semantics is
> the hard part,
> > JavaScript's semantics are just superficially
> similar to Lua's, they
> > are in fact quite different. Basically every Lua
> operation will have
> > to be compiled to JavaScript code that checks the
> types of the values
> > involved and implement the correct semantics. Even a
> simple operation
> > as a+b would be compiled to the "add_event"
> code on section 2.8 of
> > Lua's reference manual, translated to JavaScript,
> of course. Likewise
> > for indexing, and even function calls (as functions
> are not the only
> > callable object in Lua).
> >
> > It's pretty straightforward, though, and
> Mozilla's tracing JIT could
> > probably eliminate a lot of the overhead in the hot
> paths (if all the
> > extra code doesn't push the hot paths above their
> maximum size).
> > SquirrelFish and V8 wouldn't fare as well. In the
> end, JavaScript is
> > just as alien to Lua's semantics as the JVM or
> CLR.
> >
> >> (I'd like to find out about this stuff at some
> point, but the Javascript
> >> reference documentation is largely
> incomprehensible. Anyone know where I
> >> can find a clear description of how it all works?)
> >>
> >> --
> >> David Given
> >> dg <at> cowlark.com
> >>
> >
> > --
> > Fabio Mascarenhas
> >

Fabio Mascarenhas | 2 Oct 23:15
Picon

Re: JavaScript to Lua compiler

On Thu, Oct 2, 2008 at 3:06 PM, Chetan Singh <chetan_dhillon <at> yahoo.com> wrote:
> Hi fabio, Thanks fo pointing me to MiniJoe. I will take a look. Yes I was looking for JavaScript -> Lua
assuming that that Lua VM would be faster than any other JavaScript engine that is avaialbe for J2ME devices.

There is also something called mojax that embeds a JavaScript
interpreter, too (http://mfwiki.mfoundry.com/display/mojax/Main+Page).

> Regarding your comment on running JacaScript in Kahlua, once it is compiled into Lua (off-device lets say
on a server), it would run as fast as Lua code on J2ME. Right?

That was my point, it wouldn't. :-) JavaScript is not Lua, so you need
extra code around each operation to implement the correct semantics.
This code has to be in Lua (either source or bytecode), so it's bound
to be slower than a dedicated JavaScript interpreter written in the
native language of the platform (J2ME, in this case). Assuming both
interpreters have similar architecture, of course; compiling
JavaScript to Lua bytecode and running on Kahlua may be faster than an
interpreting JavaScript from source, for example.

> I am not very much familar with either language so don;t know how much work is involved in translating JS to
Lua. That is why I am looking for a tool/complier.

It's quite a bit of work, but relatively straightforward compiler
work. But it really is a job for someone who know every in and out of
JavaScript.

> thanks much for you help
> CHetan
>

--
Fabio Mascarenhas


Gmane