diff options
author | Oscar Lim <olim@ucla.edu> | 2016-01-24 22:53:37 -0800 |
---|---|---|
committer | Oscar Lim <olim@ucla.edu> | 2016-02-10 23:45:49 -0800 |
commit | 7499b422e3f07bf25704cbf7989e845f89697fa8 (patch) | |
tree | 524d8a262a9d61486b858e70c64f8aedbda43095 | |
download | luasystem-7499b422e3f07bf25704cbf7989e845f89697fa8.tar.gz luasystem-7499b422e3f07bf25704cbf7989e845f89697fa8.tar.bz2 luasystem-7499b422e3f07bf25704cbf7989e845f89697fa8.zip |
Support for gettime and sleep functions
Provide `gettime` and `sleep` functions with at least 1 millisecond
resolution.
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | .travis.yml | 23 | ||||
-rw-r--r-- | .travis_setup.sh | 45 | ||||
-rw-r--r-- | LICENSE | 20 | ||||
-rw-r--r-- | Makefile | 32 | ||||
-rw-r--r-- | README.md | 19 | ||||
-rw-r--r-- | luasystem-scm-0.rockspec | 47 | ||||
-rw-r--r-- | spec/time_spec.lua | 13 | ||||
-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 | ||||
-rw-r--r-- | system/init.lua | 2 |
14 files changed, 600 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..65b45dd --- /dev/null +++ b/.gitignore | |||
@@ -0,0 +1,6 @@ | |||
1 | **/.*.swp | ||
2 | **/.*.swo | ||
3 | *.o | ||
4 | *.obj | ||
5 | *.dll | ||
6 | *.so | ||
diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..efc632b --- /dev/null +++ b/.travis.yml | |||
@@ -0,0 +1,23 @@ | |||
1 | language: c | ||
2 | |||
3 | env: | ||
4 | - LUA='Lua 5.1' | ||
5 | - LUA='Lua 5.2' | ||
6 | - LUA='Lua 5.3' | ||
7 | - LUA='LuaJIT 2.0' | ||
8 | |||
9 | before_install: | ||
10 | - bash .travis_setup.sh | ||
11 | |||
12 | install: | ||
13 | - sudo luarocks install busted | ||
14 | - sudo luarocks make | ||
15 | |||
16 | script: busted spec | ||
17 | |||
18 | branches: | ||
19 | only: | ||
20 | - master | ||
21 | |||
22 | notifications: | ||
23 | email: false | ||
diff --git a/.travis_setup.sh b/.travis_setup.sh new file mode 100644 index 0000000..f186230 --- /dev/null +++ b/.travis_setup.sh | |||
@@ -0,0 +1,45 @@ | |||
1 | # A script for setting up environment for travis-ci testing. | ||
2 | # Sets up Lua and Luarocks. | ||
3 | # LUA must be "Lua 5.1", "Lua 5.2", "Lua 5.3" or "LuaJIT 2.0". | ||
4 | |||
5 | set -e | ||
6 | |||
7 | echo 'rocks_servers = { | ||
8 | "http://luarocks.org/manifests/olivine-labs", | ||
9 | "http://rocks.moonscript.org/", | ||
10 | "http://luarocks.org/repositories/rocks", | ||
11 | "http://luarocks.logiceditor.com/rocks", | ||
12 | "http://liblua.so/luarocks/repositories/rocks" | ||
13 | }' >> ~/config.lua | ||
14 | |||
15 | |||
16 | if [ "$LUA" == "LuaJIT 2.0" ]; then | ||
17 | wget -O - http://luajit.org/download/LuaJIT-2.0.4.tar.gz | tar xz | ||
18 | cd LuaJIT-2.0.4 | ||
19 | make && sudo make install INSTALL_TSYMNAME=lua; | ||
20 | else | ||
21 | if [ "$LUA" == "Lua 5.1" ]; then | ||
22 | wget -O - http://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz | ||
23 | cd lua-5.1.5; | ||
24 | elif [ "$LUA" == "Lua 5.2" ]; then | ||
25 | wget -O - http://www.lua.org/ftp/lua-5.2.4.tar.gz | tar xz | ||
26 | cd lua-5.2.4; | ||
27 | elif [ "$LUA" == "Lua 5.3" ]; then | ||
28 | wget -O - http://www.lua.org/ftp/lua-5.3.2.tar.gz | tar xz | ||
29 | cd lua-5.3.2; | ||
30 | fi | ||
31 | sudo make linux install; | ||
32 | fi | ||
33 | |||
34 | cd .. | ||
35 | wget -O - http://luarocks.org/releases/luarocks-2.3.0.tar.gz | tar xz || wget -O - http://keplerproject.github.io/luarocks/releases/luarocks-2.3.0.tar.gz | tar xz | ||
36 | cd luarocks-2.3.0 | ||
37 | |||
38 | if [ "$LUA" == "LuaJIT 2.0" ]; then | ||
39 | ./configure --with-lua-include=/usr/local/include/luajit-2.0; | ||
40 | else | ||
41 | ./configure; | ||
42 | fi | ||
43 | |||
44 | make build && sudo make install | ||
45 | cd .. | ||
@@ -0,0 +1,20 @@ | |||
1 | MIT License Terms | ||
2 | ================= | ||
3 | |||
4 | Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
5 | this software and associated documentation files (the "Software"), to deal in | ||
6 | the Software without restriction, including without limitation the rights to | ||
7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
8 | of the Software, and to permit persons to whom the Software is furnished to do | ||
9 | so, subject to the following conditions: | ||
10 | |||
11 | The above copyright notice and this permission notice shall be included in all | ||
12 | copies or substantial portions of the Software. | ||
13 | |||
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
20 | SOFTWARE. | ||
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4d83c2a --- /dev/null +++ b/Makefile | |||
@@ -0,0 +1,32 @@ | |||
1 | # busted_time makefile | ||
2 | # | ||
3 | # see csrc/makefile for description of how to customize the build | ||
4 | # | ||
5 | # Targets: | ||
6 | # install install system independent support | ||
7 | # install-all install for lua51 lua52 lua53 | ||
8 | # print print the build settings | ||
9 | |||
10 | PLAT?= linux | ||
11 | PLATS= macosx linux win32 mingw freebsd | ||
12 | |||
13 | all: $(PLAT) | ||
14 | |||
15 | $(PLATS) none install local clean: | ||
16 | $(MAKE) -C src $@ | ||
17 | |||
18 | print: | ||
19 | $(MAKE) -C src $@ | ||
20 | |||
21 | install-all: | ||
22 | $(MAKE) clean | ||
23 | @cd src && $(MAKE) $(PLAT) LUA_VERSION=5.1 | ||
24 | @cd src && $(MAKE) install LUA_VERSION=5.1 | ||
25 | $(MAKE) clean | ||
26 | @cd src && $(MAKE) $(PLAT) LUA_VERSION=5.2 | ||
27 | @cd src && $(MAKE) install LUA_VERSION=5.2 | ||
28 | $(MAKE) clean | ||
29 | @cd src && $(MAKE) $(PLAT) LUA_VERSION=5.3 | ||
30 | @cd src && $(MAKE) install LUA_VERSION=5.3 | ||
31 | |||
32 | .PHONY: test | ||
diff --git a/README.md b/README.md new file mode 100644 index 0000000..240c478 --- /dev/null +++ b/README.md | |||
@@ -0,0 +1,19 @@ | |||
1 | LuaSystem | ||
2 | ====== | ||
3 | |||
4 | [](https://travis-ci.org/o-lim/luasystem/builds) | ||
5 | |||
6 | |||
7 | luasystem is a platform independent system call library for Lua. | ||
8 | Supports Lua >= 5.1 and luajit >= 2.0.0. | ||
9 | |||
10 | Currently the following functions are supported: | ||
11 | * gettime | ||
12 | * sleep | ||
13 | |||
14 | License | ||
15 | ------- | ||
16 | |||
17 | This code and its accompanying README are | ||
18 | [MIT licensed](http://www.opensource.org/licenses/mit-license.php). | ||
19 | See LICENSE for details. | ||
diff --git a/luasystem-scm-0.rockspec b/luasystem-scm-0.rockspec new file mode 100644 index 0000000..83f4172 --- /dev/null +++ b/luasystem-scm-0.rockspec | |||
@@ -0,0 +1,47 @@ | |||
1 | package = 'luasystem' | ||
2 | version = 'scm-0' | ||
3 | source = { | ||
4 | url = "git://github.com/o-lim/luasystem", | ||
5 | branch = "master" | ||
6 | } | ||
7 | description = { | ||
8 | summary = 'Platform independent system calls for Lua.', | ||
9 | detailed = [[ | ||
10 | Adds a Lua API for making platform independent system calls. | ||
11 | ]], | ||
12 | homepage = 'http://olivinelabs.com/luasystem/', | ||
13 | license = 'MIT <http://opensource.org/licenses/MIT>' | ||
14 | } | ||
15 | dependencies = { | ||
16 | 'lua >= 5.1', | ||
17 | } | ||
18 | |||
19 | local function make_platform(plat) | ||
20 | local defines = { | ||
21 | unix = { }, | ||
22 | macosx = { }, | ||
23 | win32 = { }, | ||
24 | mingw32 = { "WINVER=0x0501" }, | ||
25 | } | ||
26 | return { | ||
27 | modules = { | ||
28 | ['system.core'] = { | ||
29 | sources = { 'src/core.c', 'src/compat.c', 'src/time.c', }, | ||
30 | defines = defines[plat], | ||
31 | }, | ||
32 | }, | ||
33 | } | ||
34 | end | ||
35 | |||
36 | build = { | ||
37 | type = 'builtin', | ||
38 | platforms = { | ||
39 | unix = make_platform('unix'), | ||
40 | macosx = make_platform('macosx'), | ||
41 | win32 = make_platform('win32'), | ||
42 | mingw32 = make_platform('mingw32'), | ||
43 | }, | ||
44 | modules = { | ||
45 | ['system.init'] = 'system/init.lua', | ||
46 | }, | ||
47 | } | ||
diff --git a/spec/time_spec.lua b/spec/time_spec.lua new file mode 100644 index 0000000..8bee5f7 --- /dev/null +++ b/spec/time_spec.lua | |||
@@ -0,0 +1,13 @@ | |||
1 | local system = require 'system.core' | ||
2 | |||
3 | describe('Test time functions', function() | ||
4 | it('gettime returns current time', function() | ||
5 | assert.is_near(os.time(), system.gettime(), 1.0) | ||
6 | end) | ||
7 | |||
8 | it('sleep will wait for specified amount of time', function() | ||
9 | local starttime = system.gettime() | ||
10 | system.sleep(0.5) | ||
11 | assert.is_near(0.5, system.gettime() - starttime, 0.1) | ||
12 | end) | ||
13 | end) | ||
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 | } | ||
diff --git a/system/init.lua b/system/init.lua new file mode 100644 index 0000000..77e0c3b --- /dev/null +++ b/system/init.lua | |||
@@ -0,0 +1,2 @@ | |||
1 | local system = require 'system.core' | ||
2 | return system | ||