lua-users home
lua-l archive

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


Well... I had a little JS experience about a month ago. But since I am now working in a web browser game company, I am now extending my knowledge and learn a lot on JS. Before that, I also thought "JS is a modern language with similarities with Lua" - because I had not yet done much with it. How wrong... some concepts are similar, but then again - JS is completely wicked (something that is not found in Lua, in my opinion). Some of this is rooted in the language, some in the implementations, some in its API.

For a starter: the "with" syntax:

with (obj) {
  a = b;
}

this looks now weird, the initial idea of this weird construct was, to simplify accessing variables of objects. But the result is simply crazy. I am giving here the four different results of what can happen:

<_javascript_>
var obj = {}

var b = 1;

with (obj) {
  a = b;
}

alert(obj.a+" : "+obj.b); // undefined : undefined
alert(a+" : "+b); // 1 : 1
</_javascript_>


<_javascript_>
var obj = {a : null}

var b = 1;

with (obj) {
  a = b;
}

alert(obj.a+" : "+obj.b); // 1 : undefined
alert(a+" : "+b); // error (a/b not defined)
</_javascript_>


<_javascript_>
var obj = {a : null, b:2}

var b = 1;

with (obj) {
  a = b;
}

alert(obj.a+" : "+obj.b); // 2 : 2
alert(a+" : "+b); // error
</_javascript_>


<_javascript_>
var obj = {b:2}

with (obj) {
  a = b;
}

alert(obj.a+" : "+obj.b); // undefined : 2
alert(a); // 2
</_javascript_>

I have not thought much on this, but I wouldn't be surprised if this syntax produces paradoxons in some cases. It is not used normaly (and it is probably best not to know it at all...). Yet it exists and would needed to be implemented. The compiler would need to create all possible cases if I see this right... (Crockford has a code sample to illustrate what would be the JS equivalent without using the "with" statement - until then I was not understanding fully what this statement does...).

Then, there are other issues - i.e. the scoping. That could be solved by declaring all variables within a function statement as locals on top of the script. "for" loops with their implicit local variable declarations in Lua (which I appreciate a lot) might be only used in certain conditions by the bytecode compiler.

The prototyping system of JS that is a bit like the metatable system in Lua could probably be emulated with metatables.

I have not much experiences with compilers... I think it would work because I don't see features in JS that are not available in Lua or that would be difficult to emulate (the "with" statement is such a case...). But I also think that it would be a difficult project due to the implementation of JS. On top of that, there are all these JS quirks to keep it compatible with old JS code.

Check this out, this runs in Firefox this way:

<_javascript_>
if (document.all) {
  alert("IE check correct"); // NOT TRIGGERED
}

alert(document.all); // [object HTML document.all class]
alert(!document.all); // true
alert(document.all == false); // false
</_javascript_>

So the document.all value is considered a false value but is in fact an object. This behaviour again varies over the browsers. I have no idea how this stuff can work at all, it is all so weird and full of paradoxons. Just another sample:

<_javascript_>
function foo () {
  return /.*/; // regex
}
var a = foo();
var b = foo();

a.somevar = "weird";
alert(a.somevar); // weird
</_javascript_>

The returned regular _expression_ is the *same* object. With consequences: if a regex is used on a string, the object is mutated. So if I use a regex obtained from "foo", all my regexes that are returned by foo are mutated and will produce different results; because a regex can be used multiple times on a string to iterate over all results. This is done with an object variable in the regex object (startindex or something like that, I can't remember). So since all regexes in this case are the same object, the index is shifted for all.

Or another example: The semicolon insertion mode, once developed to make it easier to learn the language (but making it harder to understand it...). It's a thing that is broken but that works - in the typical "God moves in mysterious ways." manner that seems to be _javascript_'s mantra:

<_javascript_>
function foo() {
  return
    1+2;
}

alert(foo()); // undefined
</_javascript_>

One would expect "syntax error" or "3" as result, however the answer is "undefined" because the linebreak between the return state ment and the "1+2" makes... I don't know what.

The list goes on - Last week, I forgot to watch the clock and nearly missed my bus and I simply stopped coding at the line where I was. I thought - "Well, tomorrow, I'll remember the line because this should throw a syntax error" - I was wrong and when I stumbled over that line again the next day by random, I thought "wtf" (once again):


if (true) obj.foo


This is valid JS code. It compiles and it runs, even if the "obj" variable doesn't exist. (again, Firefox, I haven't tested that in other browsers).

My colleagues also told me stories on what they experienced when writing code. JS reminds me permanently of this cartoon that can be found here:
http://www.osnews.com/story/19266/WTFs_m

When I read the book "_javascript_: The Good Parts", I was constantly laughing or crying out loud on all these miserable code fragments I read ... If you wonder now "why does this book have the title "The Good Parts"?" (everyone who saw the book said "that's a thin book!") - well, the reason is that the author tries to give examples how to use JS in order to produce good code. I think, all the good parts that the author speaks of are effectively parts of Lua... and Lua has even other good parts ;)

If you really intend to develop a JS>Lua Bytcode compiler ... I hope you know what you are heading to. Not that I would not appreciate a browser with Lua support ;) - but I think there are lot's of pitfalls in that language that are simply insane and can drive one crazy.

Eike