Remove los.c and lmath.c, and put their functions in the bundled Lua source

This commit is contained in:
Jeremy Baxter 2023-07-09 16:18:10 +12:00
parent 05bdda5d05
commit f2ff542bcb
6 changed files with 61 additions and 107 deletions

View file

@ -6,7 +6,7 @@ CPPFLAGS = -D_DEFAULT_SOURCE -DLUA_USE_READLINE
LDFLAGS = -lm -lreadline LDFLAGS = -lm -lreadline
OBJS = csto.o lcallisto.o lcl.o lenviron.o lextra.o lfile.o \ 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 LIBS = liblua.a cjson.a socket.a
all: csto libcallisto.so all: csto libcallisto.so
@ -28,8 +28,6 @@ lextra.o: lextra.c lcallisto.h
lenviron.o: lenviron.c lcallisto.h lenviron.o: lenviron.c lcallisto.h
lfile.o: lfile.c lcallisto.h lfile.o: lfile.c lcallisto.h
ljson.o: ljson.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 lprocess.o: lprocess.c lcallisto.h
lsocket.o: lsocket.c lcallisto.h lsocket.o: lsocket.c lcallisto.h
util.o: util.c util.o: util.c

View file

@ -32,8 +32,6 @@ int luaopen_environ(lua_State *);
int luaopen_extra(lua_State *); int luaopen_extra(lua_State *);
int luaopen_file(lua_State *); int luaopen_file(lua_State *);
int luaopen_json(lua_State *); int luaopen_json(lua_State *);
int luaopen_math(lua_State *);
int luaopen_os(lua_State *);
int luaopen_process(lua_State *); int luaopen_process(lua_State *);
int luaopen_socket(lua_State *); int luaopen_socket(lua_State *);

48
lmath.c
View file

@ -1,48 +0,0 @@
/***
* Math and algorithms.
* @module math
*/
#include <lua.h>
#include <lauxlib.h>
#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;
}

54
los.c
View file

@ -1,54 +0,0 @@
/***
* Operating system related facilities.
* @module os
*/
#ifdef __linux__
# include <bits/local_lim.h>
#else
/* assume OpenBSD/NetBSD */
# include <sys/syslimits.h>
#endif
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <lua.h>
#include <lauxlib.h>
#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;
}

View file

@ -4,6 +4,11 @@
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
/***
* Math and algorithms.
* @module math
*/
#define lmathlib_c #define lmathlib_c
#define LUA_LIB #define LUA_LIB
@ -646,6 +651,26 @@ static void setrandfunc (lua_State *L) {
luaL_setfuncs(L, randfuncs, 1); 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}, {"floor", math_floor},
{"fmod", math_fmod}, {"fmod", math_fmod},
{"ult", math_ult}, {"ult", math_ult},
{"lerp", math_lerp},
{"log", math_log}, {"log", math_log},
{"max", math_max}, {"max", math_max},
{"min", math_min}, {"min", math_min},

View file

@ -4,6 +4,18 @@
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
/***
* Operating system related facilities.
* @module os
*/
#ifdef __linux__
# include <bits/local_lim.h>
#else
/* assume OpenBSD/NetBSD */
# include <sys/syslimits.h>
#endif
#define loslib_c #define loslib_c
#define LUA_LIB #define LUA_LIB
@ -14,6 +26,7 @@
#include <locale.h> #include <locale.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <time.h> #include <time.h>
#include "lua.h" #include "lua.h"
@ -403,6 +416,26 @@ static int os_exit (lua_State *L) {
return 0; 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[] = { static const luaL_Reg syslib[] = {
{"clock", os_clock}, {"clock", os_clock},
@ -411,6 +444,7 @@ static const luaL_Reg syslib[] = {
{"execute", os_execute}, {"execute", os_execute},
{"exit", os_exit}, {"exit", os_exit},
{"getenv", os_getenv}, {"getenv", os_getenv},
{"hostname", os_hostname},
{"remove", os_remove}, {"remove", os_remove},
{"rename", os_rename}, {"rename", os_rename},
{"setlocale", os_setlocale}, {"setlocale", os_setlocale},