diff options
author | Thibault Charbonnier <thibaultcha@me.com> | 2015-11-07 01:53:33 -0800 |
---|---|---|
committer | Thibault Charbonnier <thibaultcha@me.com> | 2015-11-07 02:02:03 -0800 |
commit | 03617b9a8cede011d5dbd7009bf7dab00ba67806 (patch) | |
tree | f2fbc2aab8f30bffdf8e2c3b6012de443e3d0274 | |
parent | b1696f33dd7ae2b621ad8d04f1547df57731d6d0 (diff) | |
download | lua_uuid-03617b9a8cede011d5dbd7009bf7dab00ba67806.tar.gz lua_uuid-03617b9a8cede011d5dbd7009bf7dab00ba67806.tar.bz2 lua_uuid-03617b9a8cede011d5dbd7009bf7dab00ba67806.zip |
feat: made the module return a function
- also add lua lib path to the .so creation to ensure compilation
(generally lua header files are not in the standrard compiler's search
paths)
- remove boilerplate
- bare minimum of tests additions
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | README.md | 12 | ||||
-rw-r--r-- | lua_uuid.c | 28 | ||||
-rw-r--r-- | test/lua_uuid_test.lua | 13 |
4 files changed, 23 insertions, 33 deletions
@@ -1,5 +1,6 @@ LUA ?= lua5.1 LUA_PC ?= lua5.1 +LUA_LIBS ?= $(shell pkg-config $(LUA_PC) --libs) LUA_CFLAGS = $(shell pkg-config $(LUA_PC) --cflags) CFLAGS ?= -O3 -Wall -Werror @@ -10,7 +11,7 @@ all: lua_uuid.so $(CC) -c $(CFLAGS) -fPIC $(LUA_CFLAGS) -o $@ $< lua_uuid.so: lua_uuid.o test/lua_uuid_test.lua - $(CC) -shared lua_uuid.o -o $@ + $(CC) -shared lua_uuid.o $(LUA_LIBS) -o $@ $(LUA) test/lua_uuid_test.lua clean: @@ -1,14 +1,14 @@ # lua_uuid -Lua library that generate UUIDs leveraging libuuid. +Lua library that generate UUIDs leveraging [libuuid](http://linux.die.net/man/3/libuuid). ## Usage -To generate an unique UUID string: +To generate a new UUID string: ```lua -local lua_uuid = require "lua_uuid" -local uuid_str = lua_uuid.generate() +local uuid = require "lua_uuid" +local uuid_str = uuid() -print("The UUID is "..uuid_str) -```
\ No newline at end of file +print("New UUID: "..uuid_str) +``` @@ -1,42 +1,28 @@ /*** -Utility for generating UUIDs +Utility for generating UUIDs by wrapping libuuid's generate(). @license MIT @module lua_uuid */ -#define LUA_LIB #include <lua.h> #include <lauxlib.h> #include <uuid/uuid.h> -#if LUA_VERSION_NUM < 502 -# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l)) -# define lua_rawlen lua_objlen -#endif - -/// Generate an UUID +/// Generate a UUID // @return uuid_str // @function generate() static int generate(lua_State *L) { - - // Generate UUID uuid_t uuid; - uuid_generate(uuid); + char uuid_str[37]; - // Unparse to a string - char uuid_str[37]; // For example: "1b4e28ba-2fa1-11d2-883f-0016d3cca427" + "\0" + uuid_generate(uuid); uuid_unparse_lower(uuid, uuid_str); lua_pushstring(L, uuid_str); return 1; } -static const luaL_Reg lua_uuid[] = { - {"generate", generate}, - {NULL, NULL} -}; - -int luaopen_lua_uuid(lua_State *L){ - luaL_newlib(L, lua_uuid); +int luaopen_lua_uuid(lua_State *L) { + lua_pushcfunction(L, generate); return 1; -}
\ No newline at end of file +} diff --git a/test/lua_uuid_test.lua b/test/lua_uuid_test.lua index 3cc44c2..19ebf67 100644 --- a/test/lua_uuid_test.lua +++ b/test/lua_uuid_test.lua @@ -1,8 +1,11 @@ -local lua_uuid = require "lua_uuid" +package.path = package.path..";./?.lua" -assert(lua_uuid.generate()) +local uuid = require "lua_uuid" -local first = lua_uuid.generate() -local second = lua_uuid.generate() +assert(type(uuid) == "function") -assert(first ~= second)
\ No newline at end of file +local first = uuid() +local second = uuid() + +assert(first ~= second) +assert(type(uuid()) == "string") |