lua-users home
lua-l archive

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




On Thu, Nov 17, 2011 at 3:52 AM, Axel Kittenberger <axkibe@gmail.com> wrote:
> 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.

This is accounted for. I have _javascript_ code in place to simulate tables, since _javascript_ objects are very very different from Lua tables.
 

* 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.

lua.js doesn't convert "#x" to "x.length", it converts it to "lua_len(x)" which is a replicates the lua version of length. The length property does not behave like the # operator anyway, it's actually more like the table.maxn function, which will actually use the length operator in certain situations.


* 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?

Each key is grouped into a section based on type, since _javascript_ objects can't handle anything but strings in its objects. That object would look something like this in _javascript_.

{floats: {}, uints: {}, strs: {}, bools:{'true': 'yes', 'false': 'no'}}

This code...

print(a[true])

will be translated (and I'm simplifying a bit here) to this...

console.log(a.bools[true])
 

Additionally
for k, v in a do
 will result in
true , 'yes'
false, 'no'

It returns the real keys, not the pseudo IDs.

I think you mean "for k,v in pairs(a) do"?

The real keys are returned. Although currently the behavior of "pairs" differs slightly from the real version (something that should be fixed in the future) in this case it should behave the same in the Lua VM.

The lua.js version of pairs ensures that each key returned is converted to its original type.
 

* 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.

Basically every lua operator has an equivalent lua_* function to handle the differences. They also handle checking the metatables for any functions that are supposed to handle the 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.

Prototypes are not used to simulate metatables at all in lua.js. They aren't particularly useful for how I manage metatables.
 

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.


I like Lua, it suits my needs perfectly right now and I don't know of anything that could offer a more productive approach.