diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-11-11 11:28:06 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-11-11 11:28:06 -0200 |
| commit | 9957f7d59825ef4b99e2968b5f8a55cf0d698004 (patch) | |
| tree | 48ec6cca76e75b3770f121899d02903085ae2ba6 | |
| parent | ac27b7a84287a80488e3e34d4fa179df0b67c37c (diff) | |
| download | lua-9957f7d59825ef4b99e2968b5f8a55cf0d698004.tar.gz lua-9957f7d59825ef4b99e2968b5f8a55cf0d698004.tar.bz2 lua-9957f7d59825ef4b99e2968b5f8a55cf0d698004.zip | |
better way to open libraries
| -rw-r--r-- | lua.c | 37 |
1 files changed, 25 insertions, 12 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.105 2002/09/20 13:32:56 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.106 2002/10/21 20:43:38 roberto Exp roberto $ |
| 3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -47,6 +47,11 @@ static int isatty (int x) { return x==0; } /* assume stdin is a tty */ | |||
| 47 | #endif | 47 | #endif |
| 48 | 48 | ||
| 49 | 49 | ||
| 50 | #ifndef LUA_EXTRALIBS | ||
| 51 | #define LUA_EXTRALIBS /* empty */ | ||
| 52 | #endif | ||
| 53 | |||
| 54 | |||
| 50 | static lua_State *L = NULL; | 55 | static lua_State *L = NULL; |
| 51 | 56 | ||
| 52 | static const char *progname; | 57 | static const char *progname; |
| @@ -56,6 +61,20 @@ static lua_Hook old_hook = NULL; | |||
| 56 | static unsigned long old_mask = 0; | 61 | static unsigned long old_mask = 0; |
| 57 | 62 | ||
| 58 | 63 | ||
| 64 | static const luaL_reg lualibs[] = { | ||
| 65 | {"baselib", lua_baselibopen}, | ||
| 66 | {"tablib", lua_tablibopen}, | ||
| 67 | {"iolib", lua_iolibopen}, | ||
| 68 | {"strlib", lua_strlibopen}, | ||
| 69 | {"mathlib", lua_mathlibopen}, | ||
| 70 | {"dblib", lua_dblibopen}, | ||
| 71 | /* add your libraries here */ | ||
| 72 | LUA_EXTRALIBS | ||
| 73 | {NULL, NULL} | ||
| 74 | }; | ||
| 75 | |||
| 76 | |||
| 77 | |||
| 59 | static void lstop (lua_State *l, lua_Debug *ar) { | 78 | static void lstop (lua_State *l, lua_Debug *ar) { |
| 60 | (void)ar; /* unused arg. */ | 79 | (void)ar; /* unused arg. */ |
| 61 | lua_sethook(l, old_hook, old_mask); | 80 | lua_sethook(l, old_hook, old_mask); |
| @@ -341,16 +360,10 @@ static int handle_argv (char *argv[], int *interactive) { | |||
| 341 | } | 360 | } |
| 342 | 361 | ||
| 343 | 362 | ||
| 344 | static int openstdlibs (lua_State *l) { | 363 | static void openstdlibs (lua_State *l) { |
| 345 | int res = 0; | 364 | const luaL_reg *lib = lualibs; |
| 346 | res += lua_baselibopen(l); | 365 | for (; lib->name; lib++) |
| 347 | res += lua_tablibopen(l); | 366 | lua_pop(l, lib->func(l)); /* open library, discard any results */ |
| 348 | res += lua_iolibopen(l); | ||
| 349 | res += lua_strlibopen(l); | ||
| 350 | res += lua_mathlibopen(l); | ||
| 351 | res += lua_dblibopen(l); | ||
| 352 | /* add your libraries here */ | ||
| 353 | return res; | ||
| 354 | } | 367 | } |
| 355 | 368 | ||
| 356 | 369 | ||
| @@ -371,7 +384,7 @@ int main (int argc, char *argv[]) { | |||
| 371 | progname = argv[0]; | 384 | progname = argv[0]; |
| 372 | L = lua_open(); /* create state */ | 385 | L = lua_open(); /* create state */ |
| 373 | lua_atpanic(L, l_panic); | 386 | lua_atpanic(L, l_panic); |
| 374 | lua_pop(L, lua_userinit(L)); /* open libraries, discard any results */ | 387 | lua_userinit(L); /* open libraries */ |
| 375 | status = handle_luainit(); | 388 | status = handle_luainit(); |
| 376 | if (status != 0) return status; | 389 | if (status != 0) return status; |
| 377 | status = handle_argv(argv, &interactive); | 390 | status = handle_argv(argv, &interactive); |
