lua-users home
lua-l archive

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

> int luaopen_ran2(lua_State *L) {
>    do whatever you want here
>    return 1;
> }
> Must you always return 1? 

According to the API docs for lua_Cfunction, the return value must be
the number of results that the function is returning. These results must
be on the top of the stack. All other values in the stack will be

I believe most library loading functions return 1 because they have a
single result on the stack: the table containing all the library
functions. But it doesn't have to be this way. If you have a look at
luaopen_base in lbaselib.c you will see it returns 2, because there are
two table results in this case: the globals table and the coroutine
table. At least that's how it looks to me.

All this is fresh in my mind because I am writing a lot of library
registration functions right now :)

- DC