From f2ff542bcb5b4b098e9210ebec20d7ecefac3bd0 Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Sun, 9 Jul 2023 16:18:10 +1200 Subject: [PATCH] Remove los.c and lmath.c, and put their functions in the bundled Lua source --- Makefile | 4 +--- lcallisto.h | 2 -- lmath.c | 48 ----------------------------------------- los.c | 54 ---------------------------------------------- lua-5.4/lmathlib.c | 26 ++++++++++++++++++++++ lua-5.4/loslib.c | 34 +++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 107 deletions(-) delete mode 100644 lmath.c delete mode 100644 los.c diff --git a/Makefile b/Makefile index bfb557d..a9eb9ce 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ CPPFLAGS = -D_DEFAULT_SOURCE -DLUA_USE_READLINE LDFLAGS = -lm -lreadline OBJS = csto.o lcallisto.o lcl.o lenviron.o lextra.o lfile.o \ - ljson.o lmath.o los.o lprocess.o lsocket.o util.o + ljson.o lprocess.o lsocket.o util.o LIBS = liblua.a cjson.a socket.a all: csto libcallisto.so @@ -28,8 +28,6 @@ lextra.o: lextra.c lcallisto.h lenviron.o: lenviron.c lcallisto.h lfile.o: lfile.c lcallisto.h ljson.o: ljson.c lcallisto.h -lmath.o: lmath.c lcallisto.h -los.o: los.c lcallisto.h lprocess.o: lprocess.c lcallisto.h lsocket.o: lsocket.c lcallisto.h util.o: util.c diff --git a/lcallisto.h b/lcallisto.h index 7371141..1c9e70e 100644 --- a/lcallisto.h +++ b/lcallisto.h @@ -32,8 +32,6 @@ int luaopen_environ(lua_State *); int luaopen_extra(lua_State *); int luaopen_file(lua_State *); int luaopen_json(lua_State *); -int luaopen_math(lua_State *); -int luaopen_os(lua_State *); int luaopen_process(lua_State *); int luaopen_socket(lua_State *); diff --git a/lmath.c b/lmath.c deleted file mode 100644 index ee305c8..0000000 --- a/lmath.c +++ /dev/null @@ -1,48 +0,0 @@ -/*** - * Math and algorithms. - * @module math - */ - -#include -#include - -#include "lcallisto.h" -#include "util.h" - - -/*** - * Linearly interpolates using the values given. - * - * Returns `x + (y - x) * z` - * - * @function lerp - * @tparam number x - * @tparam number y - * @tparam number z - */ -static int -math_lerp(lua_State *L) -{ - double x; /* parameter 1 (number) */ - double y; /* parameter 2 (number) */ - double z; /* parameter 3 (number) */ - - x = luaL_checknumber(L, 1); - y = luaL_checknumber(L, 2); - z = luaL_checknumber(L, 3); - - lua_pushnumber(L, x + (y - x) * z); - return 1; -} - -static const luaL_Reg mathlib[] = { - {"lerp", math_lerp}, - {NULL, NULL} -}; - -int -luaopen_math(lua_State *L) -{ - newoverride(L, mathlib, CALLISTO_MATHLIBNAME); - return 0; -} diff --git a/los.c b/los.c deleted file mode 100644 index 21fcc97..0000000 --- a/los.c +++ /dev/null @@ -1,54 +0,0 @@ -/*** - * Operating system related facilities. - * @module os - */ - -#ifdef __linux__ -# include -#else -/* assume OpenBSD/NetBSD */ -# include -#endif - -#include -#include -#include - -#include -#include - -#include "lcallisto.h" -#include "util.h" - - -/*** - * Returns the system hostname. - * - * @function hostname - * @usage local hostname = os.hostname() - */ -static int -os_hostname(lua_State *L) -{ - char *buffer; - - buffer = malloc(HOST_NAME_MAX * sizeof(char *)); - - gethostname(buffer, HOST_NAME_MAX); /* get hostname */ - lua_pushstring(L, buffer); - free(buffer); - - return 1; -} - -static const luaL_Reg oslib[] = { - {"hostname", os_hostname}, - {NULL, NULL} -}; - -int -luaopen_os(lua_State *L) -{ - newoverride(L, oslib, CALLISTO_OSLIBNAME); - return 1; -} diff --git a/lua-5.4/lmathlib.c b/lua-5.4/lmathlib.c index e0c61a1..51c2934 100644 --- a/lua-5.4/lmathlib.c +++ b/lua-5.4/lmathlib.c @@ -4,6 +4,11 @@ ** See Copyright Notice in lua.h */ +/*** + * Math and algorithms. + * @module math + */ + #define lmathlib_c #define LUA_LIB @@ -646,6 +651,26 @@ static void setrandfunc (lua_State *L) { luaL_setfuncs(L, randfuncs, 1); } +/*** + * Linearly interpolates using the values given. + * + * Returns `x + (y - x) * z` + * + * @function lerp + * @tparam number x + * @tparam number y + * @tparam number z + */ +static int +math_lerp(lua_State *L) +{ + double x; /* parameter 1 (number) */ + + x = luaL_checknumber(L, 1); + lua_pushnumber(L, x + (luaL_checknumber(L, 2) - x) * luaL_checknumber(L, 3)); + return 1; +} + /* }================================================================== */ @@ -715,6 +740,7 @@ static const luaL_Reg mathlib[] = { {"floor", math_floor}, {"fmod", math_fmod}, {"ult", math_ult}, + {"lerp", math_lerp}, {"log", math_log}, {"max", math_max}, {"min", math_min}, diff --git a/lua-5.4/loslib.c b/lua-5.4/loslib.c index 3e20d62..1947d3b 100644 --- a/lua-5.4/loslib.c +++ b/lua-5.4/loslib.c @@ -4,6 +4,18 @@ ** See Copyright Notice in lua.h */ +/*** + * Operating system related facilities. + * @module os + */ + +#ifdef __linux__ +# include +#else +/* assume OpenBSD/NetBSD */ +# include +#endif + #define loslib_c #define LUA_LIB @@ -14,6 +26,7 @@ #include #include #include +#include #include #include "lua.h" @@ -403,6 +416,26 @@ static int os_exit (lua_State *L) { return 0; } +/*** + * Returns the system hostname. + * + * @function hostname + * @usage local hostname = os.hostname() + */ +static int +os_hostname(lua_State *L) +{ + char *buffer; + + buffer = malloc(HOST_NAME_MAX * sizeof(char *)); + + gethostname(buffer, HOST_NAME_MAX); /* get hostname */ + lua_pushstring(L, buffer); + free(buffer); + + return 1; +} + static const luaL_Reg syslib[] = { {"clock", os_clock}, @@ -411,6 +444,7 @@ static const luaL_Reg syslib[] = { {"execute", os_execute}, {"exit", os_exit}, {"getenv", os_getenv}, + {"hostname", os_hostname}, {"remove", os_remove}, {"rename", os_rename}, {"setlocale", os_setlocale},