diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 257 | ||||
| -rw-r--r-- | src/compat.c | 17 | ||||
| -rw-r--r-- | src/compat.h | 11 | ||||
| -rw-r--r-- | src/core.c | 19 | ||||
| -rw-r--r-- | src/time.c | 89 |
5 files changed, 393 insertions, 0 deletions
diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..25f468d --- /dev/null +++ b/src/Makefile | |||
| @@ -0,0 +1,257 @@ | |||
| 1 | # luasystem | ||
| 2 | # | ||
| 3 | # Definitions in this section can be overriden on the command line or in the | ||
| 4 | # environment. | ||
| 5 | # | ||
| 6 | |||
| 7 | # PLAT: linux macosx win32 mingw | ||
| 8 | # platform to build for | ||
| 9 | PLAT?=linux | ||
| 10 | |||
| 11 | # LUA_VERSION: 5.1 5.2 5.3 | ||
| 12 | # lua version to build against | ||
| 13 | LUA_VERSION?=5.1 | ||
| 14 | |||
| 15 | # MYCFLAGS: to be set by user if needed | ||
| 16 | MYCFLAGS= | ||
| 17 | |||
| 18 | # MYLDFLAGS: to be set by user if needed | ||
| 19 | MYLDFLAGS= | ||
| 20 | |||
| 21 | # where lua headers are found for macosx builds | ||
| 22 | # LUAINC_macosx: | ||
| 23 | # /opt/local/include | ||
| 24 | LUAINC_macosx_base?=/opt/local/include | ||
| 25 | LUAINC_macosx?=$(LUAINC_macosx_base)/lua/$(LUA_VERSION) | ||
| 26 | # FIXME default should this default to fink or to macports? | ||
| 27 | # What happens when more than one Lua version is installed? | ||
| 28 | LUAPREFIX_macosx?=/opt/local | ||
| 29 | CDIR_macosx?=lib/lua/$(LUA_VERSION) | ||
| 30 | LDIR_macosx?=share/lua/$(LUA_VERSION) | ||
| 31 | |||
| 32 | # LUAINC_linux: | ||
| 33 | # /usr/include/lua$(LUA_VERSION) | ||
| 34 | # /usr/local/include | ||
| 35 | # /usr/local/include/lua$(LUA_VERSION) | ||
| 36 | # where lua headers are found for linux builds | ||
| 37 | LUAINC_linux_base?=/usr/include | ||
| 38 | LUAINC_linux?=$(LUAINC_linux_base)/lua/$(LUA_VERSION) | ||
| 39 | LUAPREFIX_linux?=/usr/local | ||
| 40 | CDIR_linux?=lib/lua/$(LUA_VERSION) | ||
| 41 | LDIR_linux?=share/lua/$(LUA_VERSION) | ||
| 42 | |||
| 43 | # LUAINC_freebsd: | ||
| 44 | # /usr/local/include/lua$(LUA_VERSION) | ||
| 45 | # where lua headers are found for linux builds | ||
| 46 | LUAINC_freebsd_base?=/usr/local/include/ | ||
| 47 | LUAINC_freebsd?=$(LUAINC_freebsd_base)/lua$(LUA_VERSION) | ||
| 48 | LUAPREFIX_freebsd?=/usr/local/ | ||
| 49 | CDIR_freebsd?=lib/lua/$(LUA_VERSION) | ||
| 50 | LDIR_freebsd?=share/lua/$(LUA_VERSION) | ||
| 51 | |||
| 52 | # where lua headers are found for mingw builds | ||
| 53 | # LUAINC_mingw: | ||
| 54 | # /opt/local/include | ||
| 55 | LUAINC_mingw_base?=/usr/include | ||
| 56 | LUAINC_mingw?=$(LUAINC_mingw_base)/lua/$(LUA_VERSION) | ||
| 57 | LUALIB_mingw_base?=/usr/bin | ||
| 58 | LUALIB_mingw?=$(LUALIB_mingw_base)/lua/$(LUA_VERSION)/lua$(subst .,,$(LUA_VERSION)).dll | ||
| 59 | LUAPREFIX_mingw?=/usr | ||
| 60 | CDIR_mingw?=lua/$(LUA_VERSION) | ||
| 61 | LDIR_mingw?=lua/$(LUA_VERSION)/lua | ||
| 62 | |||
| 63 | |||
| 64 | # LUAINC_win32: | ||
| 65 | # LUALIB_win32: | ||
| 66 | # where lua headers and libraries are found for win32 builds | ||
| 67 | LUAPREFIX_win32?= | ||
| 68 | LUAINC_win32?=$(LUAPREFIX_win32)/include/lua/$(LUA_VERSION) | ||
| 69 | PLATFORM_win32?=Release | ||
| 70 | CDIR_win32?=bin/lua/$(LUA_VERSION)/$(PLATFORM_win32) | ||
| 71 | LDIR_win32?=bin/lua/$(LUA_VERSION)/$(PLATFORM_win32)/lua | ||
| 72 | LUALIB_win32?=$(LUAPREFIX_win32)/lib/lua/$(LUA_VERSION)/$(PLATFORM_win32) | ||
| 73 | LUALIBNAME_win32?=lua$(subst .,,$(LUA_VERSION)).lib | ||
| 74 | |||
| 75 | |||
| 76 | # prefix: /usr/local /usr /opt/local /sw | ||
| 77 | # the top of the default install tree | ||
| 78 | prefix?=$(LUAPREFIX_$(PLAT)) | ||
| 79 | |||
| 80 | CDIR?=$(CDIR_$(PLAT)) | ||
| 81 | LDIR?=$(LDIR_$(PLAT)) | ||
| 82 | |||
| 83 | # DESTDIR: (no default) | ||
| 84 | # used by package managers to install into a temporary destination | ||
| 85 | DESTDIR= | ||
| 86 | |||
| 87 | #------ | ||
| 88 | # Definitions below can be overridden on the make command line, but | ||
| 89 | # shouldn't have to be. | ||
| 90 | |||
| 91 | |||
| 92 | #------ | ||
| 93 | # Install directories | ||
| 94 | # | ||
| 95 | |||
| 96 | INSTALL_DIR=install -d | ||
| 97 | INSTALL_DATA=install -m644 | ||
| 98 | INSTALL_EXEC=install | ||
| 99 | INSTALL_TOP=$(DESTDIR)$(prefix) | ||
| 100 | |||
| 101 | INSTALL_TOP_LDIR=$(INSTALL_TOP)/$(LDIR) | ||
| 102 | INSTALL_TOP_CDIR=$(INSTALL_TOP)/$(CDIR) | ||
| 103 | |||
| 104 | INSTALL_LDIR=$(INSTALL_TOP_LDIR)/system | ||
| 105 | INSTALL_CDIR=$(INSTALL_TOP_CDIR)/system | ||
| 106 | |||
| 107 | #------ | ||
| 108 | # Supported platforms | ||
| 109 | # | ||
| 110 | PLATS= macosx linux win32 mingw | ||
| 111 | |||
| 112 | #------ | ||
| 113 | # Compiler and linker settings | ||
| 114 | # for Mac OS X | ||
| 115 | SO_macosx=so | ||
| 116 | O_macosx=o | ||
| 117 | CC_macosx=gcc | ||
| 118 | DEF_macosx= | ||
| 119 | CFLAGS_macosx= -I$(LUAINC) $(DEF) -Wall -O2 -fno-common \ | ||
| 120 | -fvisibility=hidden | ||
| 121 | LDFLAGS_macosx= -bundle -undefined dynamic_lookup -o | ||
| 122 | LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc | ||
| 123 | |||
| 124 | #------ | ||
| 125 | # Compiler and linker settings | ||
| 126 | # for Linux | ||
| 127 | SO_linux=so | ||
| 128 | O_linux=o | ||
| 129 | CC_linux=gcc | ||
| 130 | DEF_linux= | ||
| 131 | CFLAGS_linux= -I$(LUAINC) $(DEF) -Wall -Wshadow -Wextra \ | ||
| 132 | -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden | ||
| 133 | LDFLAGS_linux=-O -shared -fpic -o | ||
| 134 | LD_linux=gcc | ||
| 135 | |||
| 136 | #------ | ||
| 137 | # Compiler and linker settings | ||
| 138 | # for FreeBSD | ||
| 139 | SO_freebsd=so | ||
| 140 | O_freebsd=o | ||
| 141 | CC_freebsd=gcc | ||
| 142 | DEF_freebsd= | ||
| 143 | CFLAGS_freebsd= -I$(LUAINC) $(DEF) -Wall -Wshadow -Wextra \ | ||
| 144 | -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden | ||
| 145 | LDFLAGS_freebsd=-O -shared -fpic -o | ||
| 146 | LD_freebsd=gcc | ||
| 147 | |||
| 148 | #------ | ||
| 149 | # Compiler and linker settings | ||
| 150 | # for MingW | ||
| 151 | SO_mingw=dll | ||
| 152 | O_mingw=o | ||
| 153 | CC_mingw=gcc | ||
| 154 | DEF_mingw=-DWINVER=0x0501 | ||
| 155 | CFLAGS_mingw= -I$(LUAINC) $(DEF) -Wall -O2 -fno-common \ | ||
| 156 | -fvisibility=hidden | ||
| 157 | LDFLAGS_mingw= $(LUALIB) -shared -Wl,-s -o | ||
| 158 | LD_mingw=gcc | ||
| 159 | |||
| 160 | |||
| 161 | #------ | ||
| 162 | # Compiler and linker settings | ||
| 163 | # for Win32 | ||
| 164 | SO_win32=dll | ||
| 165 | O_win32=obj | ||
| 166 | CC_win32=cl | ||
| 167 | DEF_win32= //D "WIN32" //D "NDEBUG" //D "_WINDOWS" //D "_USRDLL" \ | ||
| 168 | //D "_CRT_SECURE_NO_WARNINGS" //D "_WINDLL" | ||
| 169 | CFLAGS_win32=//I "$(LUAINC)" $(DEF) //O2 //Ot //MD //W3 //nologo | ||
| 170 | LDFLAGS_win32= //nologo //link //NOLOGO //DLL //INCREMENTAL:NO \ | ||
| 171 | //MANIFEST //MANIFESTFILE:"intermediate.manifest" \ | ||
| 172 | //MANIFESTUAC:"level='asInvoker' uiAccess='false'" \ | ||
| 173 | //SUBSYSTEM:WINDOWS //OPT:REF //OPT:ICF //DYNAMICBASE:NO \ | ||
| 174 | //MACHINE:X86 /LIBPATH:"$(shell cmd //c echo $(LUALIB))" \ | ||
| 175 | $(LUALIBNAME_win32) //OUT: | ||
| 176 | LD_win32=cl | ||
| 177 | |||
| 178 | .SUFFIXES: .obj | ||
| 179 | |||
| 180 | .c.obj: | ||
| 181 | $(CC) $(CFLAGS) //Fo"$@" //c $< | ||
| 182 | |||
| 183 | #------ | ||
| 184 | # Output file names | ||
| 185 | # | ||
| 186 | SO=$(SO_$(PLAT)) | ||
| 187 | O=$(O_$(PLAT)) | ||
| 188 | SOLIB=core.$(SO) | ||
| 189 | |||
| 190 | #------ | ||
| 191 | # Settings selected for platform | ||
| 192 | # | ||
| 193 | CC=$(CC_$(PLAT)) | ||
| 194 | DEF=$(DEF_$(PLAT)) | ||
| 195 | CFLAGS=$(MYCFLAGS) $(CFLAGS_$(PLAT)) | ||
| 196 | LDFLAGS=$(MYLDFLAGS) $(LDFLAGS_$(PLAT)) | ||
| 197 | LD=$(LD_$(PLAT)) | ||
| 198 | LUAINC= $(LUAINC_$(PLAT)) | ||
| 199 | LUALIB= $(LUALIB_$(PLAT)) | ||
| 200 | |||
| 201 | #------ | ||
| 202 | # Objects | ||
| 203 | # | ||
| 204 | OBJS=core.$(O) compat.$(O) time.$(O) | ||
| 205 | |||
| 206 | #------ | ||
| 207 | # Targets | ||
| 208 | # | ||
| 209 | default: $(PLAT) | ||
| 210 | |||
| 211 | freebsd: | ||
| 212 | $(MAKE) all PLAT=freebsd | ||
| 213 | |||
| 214 | macosx: | ||
| 215 | $(MAKE) all PLAT=macosx | ||
| 216 | |||
| 217 | win32: | ||
| 218 | $(MAKE) all PLAT=win32 | ||
| 219 | |||
| 220 | linux: | ||
| 221 | $(MAKE) all PLAT=linux | ||
| 222 | |||
| 223 | mingw: | ||
| 224 | $(MAKE) all PLAT=mingw | ||
| 225 | |||
| 226 | none: | ||
| 227 | @echo "Please run" | ||
| 228 | @echo " make PLATFORM" | ||
| 229 | @echo "where PLATFORM is one of these:" | ||
| 230 | @echo " $(PLATS)" | ||
| 231 | |||
| 232 | all: $(SOLIB) | ||
| 233 | |||
| 234 | $(SOLIB): $(OBJS) | ||
| 235 | $(LD) $(OBJS) $(LDFLAGS)$@ | ||
| 236 | |||
| 237 | install: all | ||
| 238 | $(INSTALL_DIR) $(INSTALL_TOP_LDIR) | ||
| 239 | $(INSTALL_DIR) $(INSTALL_CDIR) | ||
| 240 | $(INSTALL_EXEC) $(SOLIB) $(INSTALL_CDIR)/$(SOLIB) | ||
| 241 | |||
| 242 | local: | ||
| 243 | $(MAKE) install INSTALL_TOP_CDIR=.. INSTALL_TOP_LDIR=.. | ||
| 244 | |||
| 245 | clean: | ||
| 246 | rm -f $(SOLIB) $(OBJS) ../system/core.so | ||
| 247 | |||
| 248 | print: | ||
| 249 | @echo PLAT=$(PLAT) | ||
| 250 | @echo LUA_VERSION=$(LUA_VERSION) | ||
| 251 | @echo prefix=$(prefix) | ||
| 252 | @echo LUAINC_$(PLAT)=$(LUAINC_$(PLAT)) | ||
| 253 | @echo LUALIB_$(PLAT)=$(LUALIB_$(PLAT)) | ||
| 254 | @echo INSTALL_TOP_CDIR=$(INSTALL_TOP_CDIR) | ||
| 255 | @echo INSTALL_TOP_LDIR=$(INSTALL_TOP_LDIR) | ||
| 256 | |||
| 257 | .PHONY: all $(PLATS) default clean none | ||
diff --git a/src/compat.c b/src/compat.c new file mode 100644 index 0000000..d763c56 --- /dev/null +++ b/src/compat.c | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #include <lua.h> | ||
| 2 | #include <lauxlib.h> | ||
| 3 | |||
| 4 | #if LUA_VERSION_NUM == 501 | ||
| 5 | void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) { | ||
| 6 | luaL_checkstack(L, nup+1, "too many upvalues"); | ||
| 7 | for (; l->name != NULL; l++) { /* fill the table with given functions */ | ||
| 8 | int i; | ||
| 9 | lua_pushstring(L, l->name); | ||
| 10 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ | ||
| 11 | lua_pushvalue(L, -(nup+1)); | ||
| 12 | lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ | ||
| 13 | lua_settable(L, -(nup + 3)); | ||
| 14 | } | ||
| 15 | lua_pop(L, nup); /* remove upvalues */ | ||
| 16 | } | ||
| 17 | #endif | ||
diff --git a/src/compat.h b/src/compat.h new file mode 100644 index 0000000..f523fd9 --- /dev/null +++ b/src/compat.h | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | #ifndef COMPAT_H | ||
| 2 | #define COMPAT_H | ||
| 3 | |||
| 4 | #include <lua.h> | ||
| 5 | #include <lauxlib.h> | ||
| 6 | |||
| 7 | #if LUA_VERSION_NUM == 501 | ||
| 8 | void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup); | ||
| 9 | #endif | ||
| 10 | |||
| 11 | #endif | ||
diff --git a/src/core.c b/src/core.c new file mode 100644 index 0000000..d2acb95 --- /dev/null +++ b/src/core.c | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #include <lua.h> | ||
| 2 | #include <lauxlib.h> | ||
| 3 | |||
| 4 | #ifdef _WIN32 | ||
| 5 | #define LUAEXPORT __declspec(dllexport) | ||
| 6 | #else | ||
| 7 | #define LUAEXPORT __attribute__((visibility("default"))) | ||
| 8 | #endif | ||
| 9 | |||
| 10 | void time_open(lua_State *L); | ||
| 11 | |||
| 12 | /*------------------------------------------------------------------------- | ||
| 13 | * Initializes all library modules. | ||
| 14 | *-------------------------------------------------------------------------*/ | ||
| 15 | LUAEXPORT int luaopen_system_core(lua_State *L) { | ||
| 16 | lua_newtable(L); | ||
| 17 | time_open(L); | ||
| 18 | return 1; | ||
| 19 | } | ||
diff --git a/src/time.c b/src/time.c new file mode 100644 index 0000000..3f61001 --- /dev/null +++ b/src/time.c | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | #include <lua.h> | ||
| 2 | #include <lauxlib.h> | ||
| 3 | |||
| 4 | #ifdef _WIN32 | ||
| 5 | #include <windows.h> | ||
| 6 | #else | ||
| 7 | #include <time.h> | ||
| 8 | #include <sys/time.h> | ||
| 9 | #endif | ||
| 10 | |||
| 11 | #include "compat.h" | ||
| 12 | |||
| 13 | /*------------------------------------------------------------------------- | ||
| 14 | * Gets time in s, relative to January 1, 1970 (UTC) | ||
| 15 | * Returns | ||
| 16 | * time in s. | ||
| 17 | *-------------------------------------------------------------------------*/ | ||
| 18 | #ifdef _WIN32 | ||
| 19 | static double time_gettime(void) { | ||
| 20 | FILETIME ft; | ||
| 21 | double t; | ||
| 22 | GetSystemTimeAsFileTime(&ft); | ||
| 23 | /* Windows file time (time since January 1, 1601 (UTC)) */ | ||
| 24 | t = ft.dwLowDateTime/1.0e7 + ft.dwHighDateTime*(4294967296.0/1.0e7); | ||
| 25 | /* convert to Unix Epoch time (time since January 1, 1970 (UTC)) */ | ||
| 26 | return (t - 11644473600.0); | ||
| 27 | } | ||
| 28 | #else | ||
| 29 | static double time_gettime(void) { | ||
| 30 | struct timeval v; | ||
| 31 | gettimeofday(&v, (struct timezone *) NULL); | ||
| 32 | /* Unix Epoch time (time since January 1, 1970 (UTC)) */ | ||
| 33 | return v.tv_sec + v.tv_usec/1.0e6; | ||
| 34 | } | ||
| 35 | #endif | ||
| 36 | |||
| 37 | /*------------------------------------------------------------------------- | ||
| 38 | * Returns the time the system has been up, in secconds. | ||
| 39 | *-------------------------------------------------------------------------*/ | ||
| 40 | static int time_lua_gettime(lua_State *L) | ||
| 41 | { | ||
| 42 | lua_pushnumber(L, time_gettime()); | ||
| 43 | return 1; | ||
| 44 | } | ||
| 45 | |||
| 46 | /*------------------------------------------------------------------------- | ||
| 47 | * Sleep for n seconds. | ||
| 48 | *-------------------------------------------------------------------------*/ | ||
| 49 | #ifdef _WIN32 | ||
| 50 | static int time_lua_sleep(lua_State *L) | ||
| 51 | { | ||
| 52 | double n = luaL_checknumber(L, 1); | ||
| 53 | if (n < 0.0) n = 0.0; | ||
| 54 | if (n < DBL_MAX/1000.0) n *= 1000.0; | ||
| 55 | if (n > INT_MAX) n = INT_MAX; | ||
| 56 | Sleep((int)n); | ||
| 57 | return 0; | ||
| 58 | } | ||
| 59 | #else | ||
| 60 | static int time_lua_sleep(lua_State *L) | ||
| 61 | { | ||
| 62 | double n = luaL_checknumber(L, 1); | ||
| 63 | struct timespec t, r; | ||
| 64 | if (n < 0.0) n = 0.0; | ||
| 65 | if (n > INT_MAX) n = INT_MAX; | ||
| 66 | t.tv_sec = (int) n; | ||
| 67 | n -= t.tv_sec; | ||
| 68 | t.tv_nsec = (int) (n * 1000000000); | ||
| 69 | if (t.tv_nsec >= 1000000000) t.tv_nsec = 999999999; | ||
| 70 | while (nanosleep(&t, &r) != 0) { | ||
| 71 | t.tv_sec = r.tv_sec; | ||
| 72 | t.tv_nsec = r.tv_nsec; | ||
| 73 | } | ||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | #endif | ||
| 77 | |||
| 78 | static luaL_Reg func[] = { | ||
| 79 | { "gettime", time_lua_gettime }, | ||
| 80 | { "sleep", time_lua_sleep }, | ||
| 81 | { NULL, NULL } | ||
| 82 | }; | ||
| 83 | |||
| 84 | /*------------------------------------------------------------------------- | ||
| 85 | * Initializes module | ||
| 86 | *-------------------------------------------------------------------------*/ | ||
| 87 | void time_open(lua_State *L) { | ||
| 88 | luaL_setfuncs(L, func, 0); | ||
| 89 | } | ||
