From 03617b9a8cede011d5dbd7009bf7dab00ba67806 Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Sat, 7 Nov 2015 01:53:33 -0800 Subject: 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 --- Makefile | 3 ++- README.md | 12 ++++++------ lua_uuid.c | 28 +++++++--------------------- test/lua_uuid_test.lua | 13 ++++++++----- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 0b872e0..cb124b3 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/README.md b/README.md index cfa7c92..ee80f5d 100644 --- a/README.md +++ b/README.md @@ -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) +``` diff --git a/lua_uuid.c b/lua_uuid.c index e0eca64..94d76b1 100644 --- a/lua_uuid.c +++ b/lua_uuid.c @@ -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 #include #include -#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") -- cgit v1.2.3-70-g09d2