浏览代码

better way to open libraries

Roberto Ierusalimschy 23 年之前
父节点
当前提交
9957f7d598
共有 1 个文件被更改,包括 25 次插入12 次删除
  1. 25 12
      lua.c

+ 25 - 12
lua.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.c,v 1.105 2002/09/20 13:32:56 roberto Exp roberto $
+** $Id: lua.c,v 1.106 2002/10/21 20:43:38 roberto Exp roberto $
 ** Lua stand-alone interpreter
 ** Lua stand-alone interpreter
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -47,6 +47,11 @@ static int isatty (int x) { return x==0; }  /* assume stdin is a tty */
 #endif
 #endif
 
 
 
 
+#ifndef LUA_EXTRALIBS
+#define LUA_EXTRALIBS	/* empty */
+#endif
+
+
 static lua_State *L = NULL;
 static lua_State *L = NULL;
 
 
 static const char *progname;
 static const char *progname;
@@ -56,6 +61,20 @@ static lua_Hook old_hook = NULL;
 static unsigned long old_mask = 0;
 static unsigned long old_mask = 0;
 
 
 
 
+static const luaL_reg lualibs[] = {
+  {"baselib", lua_baselibopen},
+  {"tablib", lua_tablibopen},
+  {"iolib", lua_iolibopen},
+  {"strlib", lua_strlibopen},
+  {"mathlib", lua_mathlibopen},
+  {"dblib", lua_dblibopen},
+  /* add your libraries here */
+  LUA_EXTRALIBS
+  {NULL, NULL}
+};
+
+
+
 static void lstop (lua_State *l, lua_Debug *ar) {
 static void lstop (lua_State *l, lua_Debug *ar) {
   (void)ar;  /* unused arg. */
   (void)ar;  /* unused arg. */
   lua_sethook(l, old_hook, old_mask);
   lua_sethook(l, old_hook, old_mask);
@@ -341,16 +360,10 @@ static int handle_argv (char *argv[], int *interactive) {
 }
 }
 
 
 
 
-static int openstdlibs (lua_State *l) {
-  int res = 0;
-  res += lua_baselibopen(l);
-  res += lua_tablibopen(l);
-  res += lua_iolibopen(l);
-  res += lua_strlibopen(l);
-  res += lua_mathlibopen(l);
-  res += lua_dblibopen(l);
-  /* add your libraries here */
-  return res;
+static void openstdlibs (lua_State *l) {
+  const luaL_reg *lib = lualibs;
+  for (; lib->name; lib++)
+    lua_pop(l, lib->func(l));  /* open library, discard any results */
 }
 }
 
 
 
 
@@ -371,7 +384,7 @@ int main (int argc, char *argv[]) {
   progname = argv[0];
   progname = argv[0];
   L = lua_open();  /* create state */
   L = lua_open();  /* create state */
   lua_atpanic(L, l_panic);
   lua_atpanic(L, l_panic);
-  lua_pop(L, lua_userinit(L));  /* open libraries, discard any results */
+  lua_userinit(L);  /* open libraries */
   status = handle_luainit();
   status = handle_luainit();
   if (status != 0) return status;
   if (status != 0) return status;
   status = handle_argv(argv, &interactive);
   status = handle_argv(argv, &interactive);