#include <unistd.h>
#include <time.h>
#include <math.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
static int api_Sleep(lua_State* L) {
int argc = lua_gettop(L);
if (argc != 1) {
lua_pushstring(L, "sleep function requres an argument");
lua_error(L);
}
lua_Number argv1 = lua_tonumber(L, 1);
if (argv1 <= 0) {
lua_pushstring(L, "sleep funtion requires number, greather than zero");
lua_error(L);
}
int sleepTime = floor(argv1 * 1000000);
usleep(sleepTime);
return 0;
}
static int api_Version(lua_State* L) {
lua_pushstring(L, "LibTest 1.0.0");
lua_pushnumber(L, 100);
return 2;
}
static const struct luaL_Reg LuaLibCtx [] = {
{"sleep", api_Sleep},
{"version", api_Version},
{NULL, NULL}
};
int extern luaopen_libtest(lua_State *L) {
luaL_newlib(L, LuaLibCtx);
return 1;
}