Skip to content

Instantly share code, notes, and snippets.

@neonux
Created November 17, 2011 12:02
Show Gist options
  • Save neonux/1373000 to your computer and use it in GitHub Desktop.
Save neonux/1373000 to your computer and use it in GitHub Desktop.
Lua to JS gotchas
> There's a lot of fundamental differences between Lua and Javascript, even at
> the syntax level.
Syntax differences are easy to come by. The problem are semantics.
Don't get me wrong, but I got the impressions this approach is a bit
naive missing the experience from larger projects in both languages.
* JS arrays are indexed with 0. Lua with 1, resulting at least with
issues with length operators.
* JS differs between arrays and tables. Lua does not. (Albeit the
object that holds a JS Array can have string keys also, there are
differences:
var x = {};
x[0] = 3;
x[1] = 2;
x[2] = 1;
#x
-> will be 2 in Lua
var x = {}
x[0] = 3;
x[1] = 2;
x[2] = 1;
x.length
-> will be undefined in JS.
* In Lua not only can tables be key of an array, but any primitive. e.g.
a[true] = 'yes'
a[false] = 'no'
How you translate this to JS? Pseuso IDs for primitives too?
Additionally
for k, v in a do
will result in
true , 'yes'
false, 'no'
It returns the real keys, not the pseudo IDs.
* Lua has a string concat and arithmetic add operator where both auto
convert variables to number respective string. JS has only '+' where
it converts the other variable to a string if one of them is a string,
or does an arithmetic add when both are numbers. So you need some code
hacking around every + operation.
* Finally prototypes, good Javascript code makes heavy use of
prototypes (So they become vritual classes in the V8). While
metatables can't be easily transformed into prototypes.
Personally I'd see yet another language translates to Lua as well as
to JS but like 99% of computer languages nobody will use a more
productive approach, but maybe i'm just wrong.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment