Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 17 Feb 2018 22:17:21 +0000 (UTC)
From:      Conrad Meyer <cem@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r329473 - head/stand/liblua
Message-ID:  <201802172217.w1HMHL5O094373@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: cem
Date: Sat Feb 17 22:17:21 2018
New Revision: 329473
URL: https://svnweb.freebsd.org/changeset/base/329473

Log:
  liblua: Clean up io/loader C module registration
  
  Utilize registration APIs Lua provides to make module definition a little
  cleaner.
  
  Discussed with:	imp
  Sponsored by:	Dell EMC Isilon

Modified:
  head/stand/liblua/lutils.c
  head/stand/liblua/lutils.h

Modified: head/stand/liblua/lutils.c
==============================================================================
--- head/stand/liblua/lutils.c	Sat Feb 17 21:47:15 2018	(r329472)
+++ head/stand/liblua/lutils.c	Sat Feb 17 22:17:21 2018	(r329473)
@@ -210,59 +210,34 @@ lua_readfile(lua_State *L)
 	return 2;
 }
 
-void
-lregister(lua_State *L, const char *tableName, const char *funcName, int (*funcPointer)(lua_State *))
-{
-	lua_getglobal(L, tableName);
-	if (!lua_istable(L, -1)) {
-		lua_pop(L, 1);
-		lua_newtable(L);
-		lua_setglobal(L, tableName);
-		lua_getglobal(L, tableName);
-	}
+#define REG_SIMPLE(n)	{ #n, lua_ ## n }
+static const struct luaL_Reg loaderlib[] = {
+	REG_SIMPLE(delay),
+	REG_SIMPLE(getenv),
+	REG_SIMPLE(perform),
+	REG_SIMPLE(printc),
+	REG_SIMPLE(setenv),
+	REG_SIMPLE(time),
+	REG_SIMPLE(unsetenv),
+	{ NULL, NULL },
+};
 
-	lua_pushcfunction(L, funcPointer);
-	lua_setfield(L, -2, funcName);
-	lua_pop(L, 1);
-}
+static const struct luaL_Reg iolib[] = {
+	{ "close", lua_closefile },
+	REG_SIMPLE(getchar),
+	REG_SIMPLE(gets),
+	REG_SIMPLE(ischar),
+	{ "open", lua_openfile },
+	{ "read", lua_readfile },
+	{ NULL, NULL },
+};
+#undef REG_SIMPLE
 
-
-typedef struct utils_func
-{
-	int (*func)(lua_State *);
-	const char *table;
-	const char *name;
-} utils_func;
-
-static utils_func reg_funcs[] = {
-			{lua_delay, "loader", "delay"},
-			{lua_getenv, "loader", "getenv"},
-			{lua_perform, "loader", "perform"},
-			{lua_printc, "loader", "printc"},
-			{lua_setenv, "loader", "setenv"},
-			{lua_time, "loader", "time"},
-			{lua_unsetenv, "loader", "unsetenv"},
-
-			{lua_closefile, "io", "close"},
-			{lua_getchar, "io", "getchar"},
-			{lua_gets, "io", "gets"},
-			{lua_ischar, "io", "ischar"},
-			{lua_openfile, "io", "open"},
-			{lua_readfile, "io", "read"},
-
-			{NULL, NULL, NULL},
-			};
-
 void
 register_utils(lua_State *L)
 {
-	utils_func	*f = reg_funcs;
-
-	while (f->func != NULL && f->name != NULL) {
-		if (f->table != NULL)
-			lregister(L, f->table, f->name, f->func);
-		else
-			lua_register(L, f->name, f->func);
-		++f;
-	}
+	luaL_newlib(L, loaderlib);
+	lua_setglobal(L, "loader");
+	luaL_newlib(L, iolib);
+	lua_setglobal(L, "io");
 }

Modified: head/stand/liblua/lutils.h
==============================================================================
--- head/stand/liblua/lutils.h	Sat Feb 17 21:47:15 2018	(r329472)
+++ head/stand/liblua/lutils.h	Sat Feb 17 22:17:21 2018	(r329473)
@@ -28,5 +28,4 @@
 
 #include <lua.h>
 
-void	lregister(lua_State *, const char *, const char *, int (*fptr)(lua_State *));
 void	register_utils(lua_State *);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201802172217.w1HMHL5O094373>