mherkender / lua.js

An ECMAscript framework to compile and run Lua code, allowing Lua to run in a browser or in Flash
http://blog.brokenfunction.com/
600 stars 73 forks source link

assert(), rawset(), select() #21

Closed ggcrunchy closed 11 years ago

ggcrunchy commented 11 years ago

Hi. A couple things that tripped me up.

In the parser, "assert" returns value, where it should return [value]. This was a bit puzzling for a while. :smile:

According to the manual, "rawset" (and lua_rawset()) take "any value" for index, which would include nil. (This is sometimes handy.)

Not so much an issue, but I'm using this for "select"... so far so good:

    "select": function(n) {
        if (n === "#")
            return [arguments.length - 1];
        return slice(arguments, n);
    }
mherkender commented 11 years ago

Nice catch on the assert issue, I've committed a fix.

I misread the documentation for select(), I thought it used the arguments from the function calling select(), which is pretty difficult to recreate in Javascript, but also not how the actual function behaves. I've committed an implementation of select(). Thanks for noticing.

According to the Lua documentation, nil is not a valid index for rawset.

rawset (table, index, value) Sets the real value of table[index] to value, without invoking any metamethod. table must be a table, index any value different from nil, and value any Lua value. This function returns table.

Try opening a lua console and typing a = {}; rawset(a, nil, 1).

ggcrunchy commented 11 years ago

Whoops, sorry, I meant rawget()

mherkender commented 11 years ago

You are indeed correct, I've updated lua_rawget() and rawget() to return nil/null instead of throwing an error when a nil/null index is used. Thanks!