pax_global_header00006660000000000000000000000064127743770330014527gustar00rootroot0000000000000052 comment=16a5314335cfefb803478239b52bde6fe42e13af luasystem-0.2.1/000077500000000000000000000000001277437703300135555ustar00rootroot00000000000000luasystem-0.2.1/.gitignore000066400000000000000000000000511277437703300155410ustar00rootroot00000000000000**/.*.swp **/.*.swo *.o *.obj *.dll *.so luasystem-0.2.1/.travis.yml000066400000000000000000000006421277437703300156700ustar00rootroot00000000000000language: c sudo: false os: - linux - osx env: global: - LUAROCKS=2.3.0 matrix: - LUA=lua5.1 - LUA=lua5.2 - LUA=lua5.3 - LUA=luajit - LUA=luajit2.0 - LUA=luajit2.1 before_install: - source .travis/setenv_lua.sh install: - luarocks install busted - luarocks make luasystem-scm-0.rockspec script: busted spec branches: only: - master notifications: email: false luasystem-0.2.1/.travis/000077500000000000000000000000001277437703300151435ustar00rootroot00000000000000luasystem-0.2.1/.travis/platform.sh000066400000000000000000000003711277437703300173240ustar00rootroot00000000000000if [ -z "${PLATFORM:-}" ]; then PLATFORM=$TRAVIS_OS_NAME; fi if [ "$PLATFORM" == "osx" ]; then PLATFORM="macosx"; fi if [ -z "$PLATFORM" ]; then if [ "$(uname)" == "Linux" ]; then PLATFORM="linux"; else PLATFORM="macosx"; fi; fi luasystem-0.2.1/.travis/setenv_lua.sh000066400000000000000000000002231277437703300176410ustar00rootroot00000000000000export PATH=${PATH}:$HOME/.lua:$HOME/.local/bin:${TRAVIS_BUILD_DIR}/install/luarocks/bin bash .travis/setup_lua.sh eval `$HOME/.lua/luarocks path` luasystem-0.2.1/.travis/setup_lua.sh000066400000000000000000000056551277437703300175130ustar00rootroot00000000000000#! /bin/bash # A script for setting up environment for travis-ci testing. # Sets up Lua and Luarocks. # LUA must be "lua5.1", "lua5.2" or "luajit". # luajit2.0 - master v2.0 # luajit2.1 - master v2.1 set -eufo pipefail LUAJIT_VERSION="2.0.4" LUAJIT_BASE="LuaJIT-$LUAJIT_VERSION" source .travis/platform.sh LUA_HOME_DIR=$TRAVIS_BUILD_DIR/install/lua LR_HOME_DIR=$TRAVIS_BUILD_DIR/install/luarocks mkdir $HOME/.lua LUAJIT="no" if [ "$PLATFORM" == "macosx" ]; then if [ "$LUA" == "luajit" ]; then LUAJIT="yes"; fi if [ "$LUA" == "luajit2.0" ]; then LUAJIT="yes"; fi if [ "$LUA" == "luajit2.1" ]; then LUAJIT="yes"; fi; elif [ "$(expr substr $LUA 1 6)" == "luajit" ]; then LUAJIT="yes"; fi mkdir -p "$LUA_HOME_DIR" if [ "$LUAJIT" == "yes" ]; then if [ "$LUA" == "luajit" ]; then curl --location https://github.com/LuaJIT/LuaJIT/archive/v$LUAJIT_VERSION.tar.gz | tar xz; else git clone https://github.com/LuaJIT/LuaJIT.git $LUAJIT_BASE; fi cd $LUAJIT_BASE if [ "$LUA" == "luajit2.1" ]; then git checkout v2.1; # force the INSTALL_TNAME to be luajit perl -i -pe 's/INSTALL_TNAME=.+/INSTALL_TNAME= luajit/' Makefile fi make && make install PREFIX="$LUA_HOME_DIR" ln -s $LUA_HOME_DIR/bin/luajit $HOME/.lua/luajit ln -s $LUA_HOME_DIR/bin/luajit $HOME/.lua/lua; else if [ "$LUA" == "lua5.1" ]; then curl http://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz cd lua-5.1.5; elif [ "$LUA" == "lua5.2" ]; then curl http://www.lua.org/ftp/lua-5.2.4.tar.gz | tar xz cd lua-5.2.4; elif [ "$LUA" == "lua5.3" ]; then curl http://www.lua.org/ftp/lua-5.3.2.tar.gz | tar xz cd lua-5.3.2; fi # Build Lua without backwards compatibility for testing perl -i -pe 's/-DLUA_COMPAT_(ALL|5_2)//' src/Makefile make $PLATFORM make INSTALL_TOP="$LUA_HOME_DIR" install; ln -s $LUA_HOME_DIR/bin/lua $HOME/.lua/lua ln -s $LUA_HOME_DIR/bin/luac $HOME/.lua/luac; fi cd $TRAVIS_BUILD_DIR lua -v LUAROCKS_BASE=luarocks-$LUAROCKS curl --location http://luarocks.org/releases/$LUAROCKS_BASE.tar.gz | tar xz cd $LUAROCKS_BASE if [ "$LUA" == "luajit" ]; then ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.0" --prefix="$LR_HOME_DIR"; elif [ "$LUA" == "luajit2.0" ]; then ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.0" --prefix="$LR_HOME_DIR"; elif [ "$LUA" == "luajit2.1" ]; then ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.1" --prefix="$LR_HOME_DIR"; else ./configure --with-lua="$LUA_HOME_DIR" --prefix="$LR_HOME_DIR" fi make build && make install ln -s $LR_HOME_DIR/bin/luarocks $HOME/.lua/luarocks cd $TRAVIS_BUILD_DIR luarocks --version rm -rf $LUAROCKS_BASE if [ "$LUAJIT" == "yes" ]; then rm -rf $LUAJIT_BASE; elif [ "$LUA" == "lua5.1" ]; then rm -rf lua-5.1.5; elif [ "$LUA" == "lua5.2" ]; then rm -rf lua-5.2.4; elif [ "$LUA" == "lua5.3" ]; then rm -rf lua-5.3.2; fi luasystem-0.2.1/LICENSE000066400000000000000000000020601277437703300145600ustar00rootroot00000000000000MIT License Terms ================= Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. luasystem-0.2.1/Makefile000066400000000000000000000020021277437703300152070ustar00rootroot00000000000000# busted_time makefile # # see csrc/makefile for description of how to customize the build # # Targets: # install install system independent support # install-all install for lua51 lua52 lua53 # print print the build settings ifeq ($(origin PLAT),undefined) UNAME_S:=$(shell uname -s) ifeq ($(UNAME_S),Linux) PLAT=linux endif ifeq ($(UNAME_S),Darwin) PLAT=macosx endif ifeq ($(UNAME_S),FreeBSD) PLAT=freebsd endif ifeq ($(patsubst MINGW%,MINGW,$(UNAME_S)),MINGW) PLAT=mingw endif endif PLAT?= linux PLATS= macosx linux win32 mingw freebsd all: $(PLAT) $(PLATS) none install local clean: $(MAKE) -C src $@ print: $(MAKE) -C src $@ install-all: $(MAKE) clean @cd src && $(MAKE) $(PLAT) LUA_VERSION=5.1 @cd src && $(MAKE) install LUA_VERSION=5.1 $(MAKE) clean @cd src && $(MAKE) $(PLAT) LUA_VERSION=5.2 @cd src && $(MAKE) install LUA_VERSION=5.2 $(MAKE) clean @cd src && $(MAKE) $(PLAT) LUA_VERSION=5.3 @cd src && $(MAKE) install LUA_VERSION=5.3 .PHONY: test luasystem-0.2.1/README.md000066400000000000000000000007431277437703300150400ustar00rootroot00000000000000LuaSystem ====== [![travis-ci status](https://travis-ci.org/o-lim/luasystem.svg?branch=master)](https://travis-ci.org/o-lim/luasystem/builds) luasystem is a platform independent system call library for Lua. Supports Lua >= 5.1 and luajit >= 2.0.0. Currently the following functions are supported: * gettime * monotime * sleep License ------- This code and its accompanying README are [MIT licensed](http://www.opensource.org/licenses/mit-license.php). See LICENSE for details. luasystem-0.2.1/luasystem-0.2.1-0.rockspec000066400000000000000000000024331277437703300201310ustar00rootroot00000000000000package = 'luasystem' version = '0.2.1-0' source = { url = "https://github.com/o-lim/luasystem/archive/v0.2.1.tar.gz", dir = "luasystem-0.2.1" } description = { summary = 'Platform independent system calls for Lua.', detailed = [[ Adds a Lua API for making platform independent system calls. ]], homepage = 'http://olivinelabs.com/luasystem/', license = 'MIT ' } dependencies = { 'lua >= 5.1', } local function make_platform(plat) local defines = { linux = { }, unix = { }, macosx = { }, win32 = { "WINVER=0x0600", "_WIN32_WINNT=0x0600" }, mingw32 = { "WINVER=0x0600", "_WIN32_WINNT=0x0600" }, } local libraries = { linux = { "rt" }, unix = { }, macosx = { }, win32 = { }, mingw32 = { }, } return { modules = { ['system.core'] = { sources = { 'src/core.c', 'src/compat.c', 'src/time.c', }, defines = defines[plat], libraries = libraries[plat], }, }, } end build = { type = 'builtin', platforms = { linux = make_platform('linux'), unix = make_platform('unix'), macosx = make_platform('macosx'), win32 = make_platform('win32'), mingw32 = make_platform('mingw32'), }, modules = { ['system.init'] = 'system/init.lua', }, } luasystem-0.2.1/luasystem-scm-0.rockspec000066400000000000000000000023731277437703300202600ustar00rootroot00000000000000package = 'luasystem' version = 'scm-0' source = { url = "git://github.com/o-lim/luasystem", branch = "master" } description = { summary = 'Platform independent system calls for Lua.', detailed = [[ Adds a Lua API for making platform independent system calls. ]], homepage = 'http://olivinelabs.com/luasystem/', license = 'MIT ' } dependencies = { 'lua >= 5.1', } local function make_platform(plat) local defines = { linux = { }, unix = { }, macosx = { }, win32 = { "WINVER=0x0600", "_WIN32_WINNT=0x0600" }, mingw32 = { "WINVER=0x0600", "_WIN32_WINNT=0x0600" }, } local libraries = { linux = { "rt" }, unix = { }, macosx = { }, win32 = { }, mingw32 = { }, } return { modules = { ['system.core'] = { sources = { 'src/core.c', 'src/compat.c', 'src/time.c', }, defines = defines[plat], libraries = libraries[plat], }, }, } end build = { type = 'builtin', platforms = { linux = make_platform('linux'), unix = make_platform('unix'), macosx = make_platform('macosx'), win32 = make_platform('win32'), mingw32 = make_platform('mingw32'), }, modules = { ['system.init'] = 'system/init.lua', }, } luasystem-0.2.1/spec/000077500000000000000000000000001277437703300145075ustar00rootroot00000000000000luasystem-0.2.1/spec/time_spec.lua000066400000000000000000000020611277437703300171610ustar00rootroot00000000000000local system = require 'system.core' describe('Test time functions', function() it('gettime returns current time', function() local starttime = system.gettime() local expected = os.time() local endtime = system.gettime() local delta = endtime - starttime local avg = starttime + delta/2 assert.is_true(expected >= math.floor(starttime)) assert.is_true(expected <= math.ceil(endtime)) assert.is_near(expected, avg, 1 + delta) end) it('monottime returns monotonically increasing time', function() local starttime = system.monotime() local endtime = system.monotime() local delta = endtime - starttime assert.is_true(starttime > 0) assert.is_true(delta >= 0) assert.is_true(system.monotime() - endtime >= 0) end) it('sleep will wait for specified amount of time', function() local starttime = system.gettime() local starttick = system.monotime() system.sleep(0.5) assert.is_near(0.5, system.gettime() - starttime, 0.1) assert.is_near(0.5, system.monotime() - starttick, 0.1) end) end) luasystem-0.2.1/src/000077500000000000000000000000001277437703300143445ustar00rootroot00000000000000luasystem-0.2.1/src/Makefile000066400000000000000000000143321277437703300160070ustar00rootroot00000000000000# luasystem # # Definitions in this section can be overriden on the command line or in the # environment. # # PLAT: linux macosx win32 mingw freebsd # platform to build for ifeq ($(origin PLAT),undefined) UNAME_S:=$(shell uname -s) ifeq ($(UNAME_S),Linux) PLAT=linux endif ifeq ($(UNAME_S),Darwin) PLAT=macosx endif ifeq ($(UNAME_S),FreeBSD) PLAT=freebsd endif ifeq ($(patsubst MINGW%,MINGW,$(UNAME_S)),MINGW) PLAT=mingw endif endif PLAT?=linux # LUA_VERSION: 5.1 5.2 5.3 # lua version to build against LUA_VERSION?=$(basename $(word 2,$(shell lua -v))) # MYCFLAGS: to be set by user if needed MYCFLAGS= # MYLDFLAGS: to be set by user if needed MYLDFLAGS= # where lua headers are found for macosx builds # LUAINC_macosx: # /opt/local/include LUAINC_macosx_base?=/opt/local/include LUAINC_macosx?=$(LUAINC_macosx_base)/lua/$(LUA_VERSION) # FIXME default should this default to fink or to macports? # What happens when more than one Lua version is installed? LUAPREFIX_macosx?=/opt/local CDIR_macosx?=lib/lua/$(LUA_VERSION) LDIR_macosx?=share/lua/$(LUA_VERSION) # LUAINC_linux: # /usr/include/lua$(LUA_VERSION) # /usr/local/include # /usr/local/include/lua$(LUA_VERSION) # where lua headers are found for linux builds LUAINC_linux_base?=/usr/include LUAINC_linux?=$(LUAINC_linux_base)/lua/$(LUA_VERSION) LUAPREFIX_linux?=/usr/local CDIR_linux?=lib/lua/$(LUA_VERSION) LDIR_linux?=share/lua/$(LUA_VERSION) # LUAINC_freebsd: # /usr/local/include/lua$(LUA_VERSION) # where lua headers are found for linux builds LUAINC_freebsd_base?=/usr/local/include/ LUAINC_freebsd?=$(LUAINC_freebsd_base)/lua$(LUA_VERSION) LUAPREFIX_freebsd?=/usr/local/ CDIR_freebsd?=lib/lua/$(LUA_VERSION) LDIR_freebsd?=share/lua/$(LUA_VERSION) # where lua headers are found for mingw builds # LUAINC_mingw: # /opt/local/include LUAINC_mingw_base?=/usr/include LUAINC_mingw?=$(LUAINC_mingw_base)/lua/$(LUA_VERSION) LUALIB_mingw_base?=/usr/bin LUALIB_mingw?=$(LUALIB_mingw_base)/lua/$(LUA_VERSION)/lua$(subst .,,$(LUA_VERSION)).dll LUAPREFIX_mingw?=/usr CDIR_mingw?=lua/$(LUA_VERSION) LDIR_mingw?=lua/$(LUA_VERSION)/lua # LUAINC_win32: # LUALIB_win32: # where lua headers and libraries are found for win32 builds LUAPREFIX_win32?= LUAINC_win32?=$(LUAPREFIX_win32)/include/lua/$(LUA_VERSION) PLATFORM_win32?=Release CDIR_win32?=bin/lua/$(LUA_VERSION)/$(PLATFORM_win32) LDIR_win32?=bin/lua/$(LUA_VERSION)/$(PLATFORM_win32)/lua LUALIB_win32?=$(LUAPREFIX_win32)/lib/lua/$(LUA_VERSION)/$(PLATFORM_win32) LUALIBNAME_win32?=lua$(subst .,,$(LUA_VERSION)).lib # prefix: /usr/local /usr /opt/local /sw # the top of the default install tree prefix?=$(LUAPREFIX_$(PLAT)) CDIR?=$(CDIR_$(PLAT)) LDIR?=$(LDIR_$(PLAT)) # DESTDIR: (no default) # used by package managers to install into a temporary destination DESTDIR= #------ # Definitions below can be overridden on the make command line, but # shouldn't have to be. #------ # Install directories # INSTALL_DIR=install -d INSTALL_DATA=install -m644 INSTALL_EXEC=install INSTALL_TOP=$(DESTDIR)$(prefix) INSTALL_TOP_LDIR=$(INSTALL_TOP)/$(LDIR) INSTALL_TOP_CDIR=$(INSTALL_TOP)/$(CDIR) INSTALL_LDIR=$(INSTALL_TOP_LDIR)/system INSTALL_CDIR=$(INSTALL_TOP_CDIR)/system #------ # Supported platforms # PLATS= macosx linux win32 mingw #------ # Compiler and linker settings # for Mac OS X SO_macosx=so O_macosx=o CC_macosx=gcc DEF_macosx= CFLAGS_macosx= -I$(LUAINC) $(DEF) -Wall -O2 -fno-common \ -fvisibility=hidden LDFLAGS_macosx= -bundle -undefined dynamic_lookup -o LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc #------ # Compiler and linker settings # for Linux SO_linux=so O_linux=o CC_linux=gcc DEF_linux= CFLAGS_linux= -I$(LUAINC) $(DEF) -Wall -Wshadow -Wextra \ -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden LDFLAGS_linux=-lrt -O -shared -fpic -o LD_linux=gcc #------ # Compiler and linker settings # for FreeBSD SO_freebsd=so O_freebsd=o CC_freebsd=gcc DEF_freebsd= CFLAGS_freebsd= -I$(LUAINC) $(DEF) -Wall -Wshadow -Wextra \ -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden LDFLAGS_freebsd=-O -shared -fpic -o LD_freebsd=gcc #------ # Compiler and linker settings # for MingW SO_mingw=dll O_mingw=o CC_mingw=gcc DEF_mingw=-DWINVER=0x0600 -D_WIN32_WINNT=0x0600 CFLAGS_mingw= -I$(LUAINC) $(DEF) -Wall -O2 -fno-common \ -fvisibility=hidden LDFLAGS_mingw= $(LUALIB) -shared -Wl,-s -o LD_mingw=gcc #------ # Compiler and linker settings # for Win32 SO_win32=dll O_win32=obj CC_win32=cl DEF_win32= //D "WIN32" //D "NDEBUG" //D "_WINDOWS" //D "_USRDLL" \ //D "_CRT_SECURE_NO_WARNINGS" //D "_WINDLL" \ //D"WINVER=0x0600" //D"_WIN32_WINNT=0x0600" CFLAGS_win32=//I "$(LUAINC)" $(DEF) //O2 //Ot //MD //W3 //nologo LDFLAGS_win32= //nologo //link //NOLOGO //DLL //INCREMENTAL:NO \ //MANIFEST //MANIFESTFILE:"intermediate.manifest" \ //MANIFESTUAC:"level='asInvoker' uiAccess='false'" \ //SUBSYSTEM:WINDOWS //OPT:REF //OPT:ICF //DYNAMICBASE:NO \ //MACHINE:X86 /LIBPATH:"$(shell cmd //c echo $(LUALIB))" \ $(LUALIBNAME_win32) //OUT: LD_win32=cl .SUFFIXES: .obj .c.obj: $(CC) $(CFLAGS) //Fo"$@" //c $< #------ # Output file names # SO=$(SO_$(PLAT)) O=$(O_$(PLAT)) SOLIB=core.$(SO) #------ # Settings selected for platform # CC=$(CC_$(PLAT)) DEF=$(DEF_$(PLAT)) CFLAGS=$(MYCFLAGS) $(CFLAGS_$(PLAT)) LDFLAGS=$(MYLDFLAGS) $(LDFLAGS_$(PLAT)) LD=$(LD_$(PLAT)) LUAINC= $(LUAINC_$(PLAT)) LUALIB= $(LUALIB_$(PLAT)) #------ # Objects # OBJS=core.$(O) compat.$(O) time.$(O) #------ # Targets # default: $(PLAT) freebsd: $(MAKE) all PLAT=freebsd macosx: $(MAKE) all PLAT=macosx win32: $(MAKE) all PLAT=win32 linux: $(MAKE) all PLAT=linux mingw: $(MAKE) all PLAT=mingw none: @echo "Please run" @echo " make PLATFORM" @echo "where PLATFORM is one of these:" @echo " $(PLATS)" all: $(SOLIB) $(SOLIB): $(OBJS) $(LD) $(OBJS) $(LDFLAGS)$@ install: all $(INSTALL_DIR) $(INSTALL_TOP_LDIR) $(INSTALL_DIR) $(INSTALL_CDIR) $(INSTALL_EXEC) $(SOLIB) $(INSTALL_CDIR)/$(SOLIB) local: $(MAKE) install INSTALL_TOP_CDIR=.. INSTALL_TOP_LDIR=.. clean: rm -f $(SOLIB) $(OBJS) ../system/core.so print: @echo PLAT=$(PLAT) @echo LUA_VERSION=$(LUA_VERSION) @echo prefix=$(prefix) @echo LUAINC_$(PLAT)=$(LUAINC_$(PLAT)) @echo LUALIB_$(PLAT)=$(LUALIB_$(PLAT)) @echo INSTALL_TOP_CDIR=$(INSTALL_TOP_CDIR) @echo INSTALL_TOP_LDIR=$(INSTALL_TOP_LDIR) .PHONY: all $(PLATS) default clean none luasystem-0.2.1/src/compat.c000066400000000000000000000010541277437703300157730ustar00rootroot00000000000000#include #include #if LUA_VERSION_NUM == 501 void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) { luaL_checkstack(L, nup+1, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ int i; lua_pushstring(L, l->name); for (i = 0; i < nup; i++) /* copy upvalues to the top */ lua_pushvalue(L, -(nup+1)); lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ lua_settable(L, -(nup + 3)); } lua_pop(L, nup); /* remove upvalues */ } #endif luasystem-0.2.1/src/compat.h000066400000000000000000000002621277437703300160000ustar00rootroot00000000000000#ifndef COMPAT_H #define COMPAT_H #include #include #if LUA_VERSION_NUM == 501 void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup); #endif #endif luasystem-0.2.1/src/core.c000066400000000000000000000011751277437703300154440ustar00rootroot00000000000000#include #include #define LUASYSTEM_VERSION "LuaSystem 0.2.1" #ifdef _WIN32 #define LUAEXPORT __declspec(dllexport) #else #define LUAEXPORT __attribute__((visibility("default"))) #endif void time_open(lua_State *L); /*------------------------------------------------------------------------- * Initializes all library modules. *-------------------------------------------------------------------------*/ LUAEXPORT int luaopen_system_core(lua_State *L) { lua_newtable(L); lua_pushstring(L, "_VERSION"); lua_pushstring(L, LUASYSTEM_VERSION); lua_rawset(L, -3); time_open(L); return 1; } luasystem-0.2.1/src/time.c000066400000000000000000000072141277437703300154520ustar00rootroot00000000000000#include #include #ifdef _WIN32 #include #include #include #else #include #include #endif #ifdef __APPLE__ #include #ifndef MAC_OS_X_VERSION_10_12 #define MAC_OS_X_VERSION_10_12 101200 #endif #define HAVE_CLOCK_GETTIME (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 || defined(CLOCK_MONOTONIC)) #if !(HAVE_CLOCK_GETTIME) #include "time_osx.h" #endif #endif #include "compat.h" /*------------------------------------------------------------------------- * Gets time in s, relative to January 1, 1970 (UTC) * Returns * time in s. *-------------------------------------------------------------------------*/ #ifdef _WIN32 static double time_gettime(void) { FILETIME ft; double t; GetSystemTimeAsFileTime(&ft); /* Windows file time (time since January 1, 1601 (UTC)) */ t = ft.dwLowDateTime*1.0e-7 + ft.dwHighDateTime*(4294967296.0*1.0e-7); /* convert to Unix Epoch time (time since January 1, 1970 (UTC)) */ return (t - 11644473600.0); } #else static double time_gettime(void) { struct timeval v; gettimeofday(&v, (struct timezone *) NULL); /* Unix Epoch time (time since January 1, 1970 (UTC)) */ return v.tv_sec + v.tv_usec*1.0e-6; } #endif /*------------------------------------------------------------------------- * Gets monotonic time in s * Returns * time in s. *-------------------------------------------------------------------------*/ #ifdef _WIN32 WINBASEAPI ULONGLONG WINAPI GetTickCount64(VOID); static double time_monotime(void) { ULONGLONG ms = GetTickCount64(); return ms*1.0e-3; } #else static double time_monotime(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return ts.tv_sec + ts.tv_nsec*1.0e-9; } #endif /*------------------------------------------------------------------------- * Returns the current system time, 1970 (UTC), in secconds. *-------------------------------------------------------------------------*/ static int time_lua_gettime(lua_State *L) { lua_pushnumber(L, time_gettime()); return 1; } /*------------------------------------------------------------------------- * Returns the monotonic time the system has been up, in secconds. *-------------------------------------------------------------------------*/ static int time_lua_monotime(lua_State *L) { lua_pushnumber(L, time_monotime()); return 1; } /*------------------------------------------------------------------------- * Sleep for n seconds. *-------------------------------------------------------------------------*/ #ifdef _WIN32 static int time_lua_sleep(lua_State *L) { double n = luaL_checknumber(L, 1); if (n < 0.0) n = 0.0; if (n < DBL_MAX/1000.0) n *= 1000.0; if (n > INT_MAX) n = INT_MAX; Sleep((int)n); return 0; } #else static int time_lua_sleep(lua_State *L) { double n = luaL_checknumber(L, 1); struct timespec t, r; if (n < 0.0) n = 0.0; if (n > INT_MAX) n = INT_MAX; t.tv_sec = (int) n; n -= t.tv_sec; t.tv_nsec = (int) (n * 1000000000); if (t.tv_nsec >= 1000000000) t.tv_nsec = 999999999; while (nanosleep(&t, &r) != 0) { t.tv_sec = r.tv_sec; t.tv_nsec = r.tv_nsec; } return 0; } #endif static luaL_Reg func[] = { { "gettime", time_lua_gettime }, { "monotime", time_lua_monotime }, { "sleep", time_lua_sleep }, { NULL, NULL } }; /*------------------------------------------------------------------------- * Initializes module *-------------------------------------------------------------------------*/ void time_open(lua_State *L) { luaL_setfuncs(L, func, 0); } luasystem-0.2.1/src/time_osx.h000066400000000000000000000033051277437703300163450ustar00rootroot00000000000000#ifndef TIME_OSX_H #define TIME_OSX_H /* * clock_gettime() * * OS X doesn't implement the clock_gettime() POSIX interface, but does * provide a monotonic clock through mach_absolute_time(). On i386 and * x86_64 architectures this clock is in nanosecond units, but not so on * other devices. mach_timebase_info() provides the conversion parameters. * */ #include /* struct timespec */ #include /* errno EINVAL */ #include /* TIMEVAL_TO_TIMESPEC struct timeval gettimeofday(3) */ #include /* mach_timebase_info_data_t mach_timebase_info() mach_absolute_time() */ #define CLOCK_REALTIME 0 #define CLOCK_VIRTUAL 1 #define CLOCK_PROF 2 #define CLOCK_MONOTONIC 3 static mach_timebase_info_data_t clock_timebase = { .numer = 1, .denom = 1, }; /* clock_timebase */ void clock_gettime_init(void) __attribute__((constructor)); void clock_gettime_init(void) { if (mach_timebase_info(&clock_timebase) != KERN_SUCCESS) __builtin_abort(); } /* clock_gettime_init() */ static int clock_gettime(int clockid, struct timespec *ts) { switch (clockid) { case CLOCK_REALTIME: { struct timeval tv; if (0 != gettimeofday(&tv, 0)) return -1; TIMEVAL_TO_TIMESPEC(&tv, ts); return 0; } case CLOCK_MONOTONIC: { unsigned long long abt; abt = mach_absolute_time(); abt = abt * clock_timebase.numer / clock_timebase.denom; ts->tv_sec = abt / 1000000000UL; ts->tv_nsec = abt % 1000000000UL; return 0; } default: errno = EINVAL; return -1; } /* switch() */ } /* clock_gettime() */ #endif /* TIME_OSX_H */ luasystem-0.2.1/system/000077500000000000000000000000001277437703300151015ustar00rootroot00000000000000luasystem-0.2.1/system/init.lua000066400000000000000000000000631277437703300165460ustar00rootroot00000000000000local system = require 'system.core' return system