lua-users home
lua-l archive

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


Hi!

I must admit that I don't really understand the documentation of
require (as given by
http://www.lua.org/manual/5.2/manual.html#pdf-require).

After reading it several times I gave up and decided to help myself by
some example code. I wrote the following simple module:

#include <stdio.h>
#include <lua5.2/lua.h>

int luaopen_mod( lua_State *L ) {
   printf( "stack size  : %d\n", lua_gettop( L ) );
   printf( "typename   1: %s\n", lua_typename( L, 1 ) );
   printf( "typename   2: %s\n", lua_typename( L, 2 ) );
   printf( "value      1: %d\n", lua_toboolean( L, 1 ) );
   printf( "isuserdata 2: %d\n", lua_isuserdata( L, 2 ) );
   printf( "isstring   2: %d\n", lua_isstring( L, 2 ) );
   printf( "value      2: %s\n", lua_tolstring( L, 2, 0 ) );
   lua_newtable( L );
   return 1;
}

and compiled it with gcc -shared -o mod.so mod.c.

When called by `require 'mod'` the output is

stack size  : 2
typename   1: boolean
typename   2: userdata
value      1: 1
isuserdata 2: 0
isstring   2: 1
value      2: ./mod.so

Ok, two arguments as mentioned in the manual. But:

On position 2 I've got a userdata which is none according to
isuserdata(). This argument may be the modname. I didn't understand
what this is exactly, but somehow I expected 'mod' rather than
'./mod.so'.

On position 1 I've got a boolean true, which means 'the loader does
not return a non-nil value and has not assigned any value to
package.loaded[modname]'? What's the "loader" in this case exactly?

Regards,
Matthias