From owner-svn-src-all@freebsd.org Sat Feb 17 22:17:22 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id C4603F1556B; Sat, 17 Feb 2018 22:17:21 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 9CC636FC0F; Sat, 17 Feb 2018 22:17:21 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 7D89C22BBD; Sat, 17 Feb 2018 22:17:21 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w1HMHLSl094375; Sat, 17 Feb 2018 22:17:21 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w1HMHL5O094373; Sat, 17 Feb 2018 22:17:21 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201802172217.w1HMHL5O094373@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer Date: Sat, 17 Feb 2018 22:17:21 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r329473 - head/stand/liblua X-SVN-Group: head X-SVN-Commit-Author: cem X-SVN-Commit-Paths: head/stand/liblua X-SVN-Commit-Revision: 329473 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Feb 2018 22:17:22 -0000 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 -void lregister(lua_State *, const char *, const char *, int (*fptr)(lua_State *)); void register_utils(lua_State *);