From 7ca273335ecc2251442847480dd5578b4ec0eccf Mon Sep 17 00:00:00 2001 From: luau-project Date: Tue, 12 Nov 2024 05:25:25 -0300 Subject: [PATCH] v0.0.3 --- .github/workflows/ci.yml | 21 +++--- Makefile.macosx | 2 +- README.md | 78 +++++++++++++++++++++- rockspecs/lua-uuid-0.0.3-0.rockspec | 100 ++++++++++++++++++++++++++++ rockspecs/lua-uuid-dev-1.rockspec | 2 +- samples/tostring.lua | 6 +- src/lua-uuid.c | 65 ++++++++++-------- 7 files changed, 232 insertions(+), 42 deletions(-) create mode 100644 rockspecs/lua-uuid-0.0.3-0.rockspec diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5bc46a4..853cfad 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ on: - "docs/**" env: - ROCKSPEC_VERSION: 0.0.2 + ROCKSPEC_VERSION: 0.0.3 DEV_ROCKSPEC: lua-uuid-dev-1.rockspec jobs: @@ -31,7 +31,7 @@ jobs: - 5.2 - 5.3 - 5.4 - - luajit-master + - luajit os: - windows-latest @@ -40,7 +40,7 @@ jobs: exclude: - os: macos-latest - lua-version: luajit-master + lua-version: luajit steps: @@ -65,7 +65,7 @@ jobs: run: sudo apt install -y uuid-dev - name: Setup MSVC dev-prompt - if: ${{ matrix.os == 'windows-latest' && matrix.lua-version != 'luajit-master' }} + if: ${{ matrix.os == 'windows-latest' && matrix.lua-version != 'luajit' }} uses: ilammy/msvc-dev-cmd@v1 - name: Setup Lua @@ -114,19 +114,18 @@ jobs: matrix: MSYS2_CONFIG: - - { sys: mingw32, env: i686 } - { sys: mingw64, env: x86_64 } - { sys: ucrt64, env: ucrt-x86_64 } - { sys: clang64, env: clang-x86_64 } Lua: - - { version: '5.4', msys2_pkg_name: 'lua', msys2_lua_interpreter: 'lua' } - - { version: '5.3', msys2_pkg_name: 'lua53', msys2_lua_interpreter: 'lua5.3' } - - { version: '5.1', msys2_pkg_name: 'lua51', msys2_lua_interpreter: 'lua5.1' } - - { version: '5.1', msys2_pkg_name: 'luajit', msys2_lua_interpreter: 'luajit' } + - { version: '5.4', msys2-pkg-name: 'lua', msys2-lua-interpreter: 'lua' } + - { version: '5.3', msys2-pkg-name: 'lua53', msys2-lua-interpreter: 'lua5.3' } + - { version: '5.1', msys2-pkg-name: 'lua51', msys2-lua-interpreter: 'lua5.1' } + - { version: '5.1', msys2-pkg-name: 'luajit', msys2-lua-interpreter: 'luajit' } env: - LUA_INTERPRETER: /${{ matrix.MSYS2_CONFIG.sys }}/bin/${{ matrix.Lua.msys2_lua_interpreter }} + LUA_INTERPRETER: /${{ matrix.MSYS2_CONFIG.sys }}/bin/${{ matrix.Lua.msys2-lua-interpreter }} steps: @@ -138,7 +137,7 @@ jobs: base-devel git mingw-w64-${{ matrix.MSYS2_CONFIG.env }}-cc - mingw-w64-${{ matrix.MSYS2_CONFIG.env }}-${{ matrix.Lua.msys2_pkg_name }} + mingw-w64-${{ matrix.MSYS2_CONFIG.env }}-${{ matrix.Lua.msys2-pkg-name }} mingw-w64-${{ matrix.MSYS2_CONFIG.env }}-lua-luarocks - name: Set environment variable to hold the rockspec name diff --git a/Makefile.macosx b/Makefile.macosx index e0e558d..72ffc62 100644 --- a/Makefile.macosx +++ b/Makefile.macosx @@ -1,6 +1,6 @@ OBJ_EXTENSION = o LIB_EXTENSION = so -CFLAGS_EXTRA = -DLUA_UUID_USE_APPLE +CFLAGS_EXTRA = -DLUA_UUID_BUILD_SHARED -DLUA_UUID_USE_APPLE LIBFLAG_EXTRA = -framework CoreFoundation LUA_DIR = /usr/local diff --git a/README.md b/README.md index 9282c0a..811ba4e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # lua-uuid -[![CI](https://github.com/luau-project/lua-uuid/actions/workflows/ci.yml/badge.svg)](./.github/workflows/ci.yml) +[![CI](https://github.com/luau-project/lua-uuid/actions/workflows/ci.yml/badge.svg)](./.github/workflows/ci.yml) [![LuaRocks](https://img.shields.io/luarocks/v/luau-project/lua-uuid?label=LuaRocks&color=2c3e67)](https://luarocks.org/modules/luau-project/lua-uuid) ## Overview @@ -18,6 +18,15 @@ * [Parse GUIDs / UUIDs from string](#parse-guids--uuids-from-string) * [Compare GUIDs / UUIDs](#compare-guids--uuids) * [Verify GUIDs / UUIDs nullity](#verify-guids--uuids-nullity) +* [Static Methods](#static-methods) + * [new](#new) + * [parse](#parse) +* [Instance Methods](#instance-methods) + * [isnil](#isnil) +* [Metamethods](#metamethods) + * [__eq](#__eq) + * [__tostring](#__tostring) +* [Change log](#change-log) * [Future works](#future-works) ## Installation @@ -79,6 +88,10 @@ luarocks install lua-uuid assert(type(s1) == 'string') assert(type(s2) == 'string') + + -- print each string + print(s1) + print(s2) ``` ### Parse GUIDs / UUIDs from string @@ -141,6 +154,69 @@ local id3 = uuid.parse("00000000-0000-0000-0000-000000000000") print(id3:isnil()) ``` +## Static Methods + +### new + +* *Description*: Generates a new GUID / UUID +* *Signature*: ```new()``` + * *return*: ```(userdata)``` +* *Usage*: See [here](#generate-guids--uuids) + +### parse + +* *Description*: Generates a new GUID / UUID from a string value +* *Signature*: ```parse(value)``` + * *value* (string): the string to be parsed + * *return*: ```(userdata)``` +* *Usage*: See [here](#parse-guids--uuids-from-string) + +## Instance Methods + +### isnil + +* *Description*: Verifies whether the GUID / UUID is considered null or not. + +> [!NOTE] +> +> a GUID / UUID is considered null when its string representation is equal to ```00000000-0000-0000-0000-000000000000```. + +* *Signature*: ```instance:isnil()``` + * *instance* (userdata): the GUID / UUID instance to check for nullity + * *return*: ```(boolean)``` +* *Usage*: See [here](#verify-guids--uuids-nullity) + +## Metamethods + +### __eq + +* *Description*: Compares two GUIDs / UUIDs for equality +* *Signature*: ```left == right``` + * *left* (any): the left-side element + * *right* (any): the right-side element + * *return*: ```(boolean)``` +* *Usage*: See [here](#compare-guids--uuids) + +### __tostring + +* *Description*: Converts the GUID / UUID to string +* *Signature*: ```tostring(value)``` + * *value* (userdata): the GUID / UUID to perform the conversion + * *return*: ```(string)``` +* *Usage*: See [here](#generate-guids--uuids) + +## Change log + +* v0.0.3: + * Changed to throw issue when ```lua_newuserdata``` returns ```NULL```; + * Added macro ```LUA_UUID_BUILD_SHARED``` to ```CFLAGS_EXTRA``` on macos; + * Changed ```luajit-master``` to ```luajit``` on CI when testing for ```LuaJIT```; + * Added print statements on [tostring.lua](./samples/tostring.lua) sample; + * Removed build / testing from CI for x86 packages on MSYS2; + * Added documentation for static, instance and metamethods to the README. +* v0.0.2: + * Fixed syntax issue in the rockspec lua-uuid-0.0.1-0.rockspec + ## Future works * Add CMake as build system \ No newline at end of file diff --git a/rockspecs/lua-uuid-0.0.3-0.rockspec b/rockspecs/lua-uuid-0.0.3-0.rockspec new file mode 100644 index 0000000..26207ed --- /dev/null +++ b/rockspecs/lua-uuid-0.0.3-0.rockspec @@ -0,0 +1,100 @@ +package = "lua-uuid" +version = "0.0.3-0" + +source = { + url = "git://github.com/luau-project/lua-uuid.git", + tag = "v0.0.3" +} + +description = { + homepage = "https://github.com/luau-project/lua-uuid", + summary = [[Lightweight, native GUID / UUID library for Lua]], + detailed = [=[ +lua-uuid is a lightweight, native library for Lua (5.1 and newer) to deal with Universally Unique Id (UUID). + +* On Linux, it uses libuuid to generate UUIDs; +* On Windows, it uses the WINAPI rpcrt4 library; +* On MacOS / iOS, it uses the CoreFoundation framework. + +Visit the GitHub repository for more information.]=], + license = "MIT" +} + +supported_platforms = { "linux", "win32", "mingw", "cygwin", "macosx" } + +dependencies = { + "lua >= 5.1" +} + +external_dependencies = { + platforms = { + linux = { + ["UUID"] = { + header = "uuid/uuid.h" + } + } + } +} + +local function make_plat(plat) + if (plat == "linux") then + return { + type = "builtin", + modules = { + ["lua-uuid"] = { + sources = { "src/lua-uuid.c" }, + libraries = { "uuid" }, + defines = { "LUA_UUID_BUILD_SHARED", "LUA_UUID_USE_LIBUUID" }, + incdirs = { "src", "$(UUID_INCDIR)" }, + libdirs = { "$(UUID_LIBDIR)" } + } + } + } + elseif (plat == "win32" or plat == "mingw" or plat == "cygwin") then + return { + type = "builtin", + modules = { + ["lua-uuid"] = { + sources = { "src/lua-uuid.c" }, + libraries = { "rpcrt4" }, + defines = { "LUA_UUID_BUILD_SHARED", "LUA_UUID_USE_WIN32" }, + incdirs = { "src" }, + libdirs = {} + } + } + } + elseif (plat == "macosx") then + return { + type = "make", + makefile = "Makefile.macosx", + build_variables = { + CFLAGS = "$(CFLAGS)", + LIBFLAG = "$(LIBFLAG)", + CFLAGS_EXTRA = "-DLUA_UUID_BUILD_SHARED -DLUA_UUID_USE_APPLE", + LIBFLAG_EXTRA = "-framework CoreFoundation", + LUA_INCDIR = "$(LUA_INCDIR)", + LUA_LIBDIR = "$(LUA_INCDIR)/../lib", + OBJ_EXTENSION = "$(OBJ_EXTENSION)", + LIB_EXTENSION = "$(LIB_EXTENSION)" + }, + install_variables = { + INSTALL_PREFIX = "$(PREFIX)", + INSTALL_LIBDIR = "$(LIBDIR)", + LUA_VERSION = "$(LUA_VERSION)", + LIB_EXTENSION = "$(LIB_EXTENSION)" + } + } + else + error("Unknown platform", 2) + end +end + +build = { + platforms = { + linux = make_plat("linux"), + win32 = make_plat("win32"), + mingw = make_plat("mingw"), + cygwin = make_plat("cygwin"), + macosx = make_plat("macosx") + } +} diff --git a/rockspecs/lua-uuid-dev-1.rockspec b/rockspecs/lua-uuid-dev-1.rockspec index b89ad75..3b45b24 100644 --- a/rockspecs/lua-uuid-dev-1.rockspec +++ b/rockspecs/lua-uuid-dev-1.rockspec @@ -69,7 +69,7 @@ local function make_plat(plat) build_variables = { CFLAGS = "$(CFLAGS)", LIBFLAG = "$(LIBFLAG)", - CFLAGS_EXTRA = "-DLUA_UUID_USE_APPLE", + CFLAGS_EXTRA = "-DLUA_UUID_BUILD_SHARED -DLUA_UUID_USE_APPLE", LIBFLAG_EXTRA = "-framework CoreFoundation", LUA_INCDIR = "$(LUA_INCDIR)", LUA_LIBDIR = "$(LUA_INCDIR)/../lib", diff --git a/samples/tostring.lua b/samples/tostring.lua index d8ac504..ae83521 100644 --- a/samples/tostring.lua +++ b/samples/tostring.lua @@ -10,4 +10,8 @@ local s1 = tostring(id1) local s2 = tostring(id2) assert(type(s1) == 'string') -assert(type(s2) == 'string') \ No newline at end of file +assert(type(s2) == 'string') + +-- print each string +print(s1) +print(s2) \ No newline at end of file diff --git a/src/lua-uuid.c b/src/lua-uuid.c index 7018c03..879fb59 100644 --- a/src/lua-uuid.c +++ b/src/lua-uuid.c @@ -64,34 +64,35 @@ static void *lua_uuid_testudata(lua_State *L, int ud, const char *tname) static int lua_uuid_new(lua_State *L) { - int res = 1; - void *ud = lua_newuserdata(L, sizeof(LuaUuid)); - if (ud != NULL) + if (ud == NULL) { - luaL_getmetatable(L, LUA_UUID_METATABLE); - lua_setmetatable(L, -2); - LuaUuid *uuid = (LuaUuid *)ud; + luaL_error(L, "Failed to create userdata"); + } + + luaL_getmetatable(L, LUA_UUID_METATABLE); + lua_setmetatable(L, -2); + LuaUuid *uuid = (LuaUuid *)ud; #if defined(LUA_UUID_USE_WIN32) - RPC_STATUS create_status = UuidCreate(&(uuid->data)); - - if (create_status != RPC_S_OK) - { - luaL_error(L, "Failed to create UUID"); - } + RPC_STATUS create_status = UuidCreate(&(uuid->data)); + + if (create_status != RPC_S_OK) + { + luaL_error(L, "Failed to create UUID"); + } #elif defined(LUA_UUID_USE_LIBUUID) - uuid_generate(uuid->data); + uuid_generate(uuid->data); #elif defined(LUA_UUID_USE_APPLE) - uuid->data = CFUUIDCreate(NULL); + uuid->data = CFUUIDCreate(NULL); - if (uuid->data == NULL) - { - luaL_error(L, "Failed to create UUID"); - } -#endif + if (uuid->data == NULL) + { + luaL_error(L, "Failed to create UUID"); } - return res; +#endif + + return 1; } static int lua_uuid_parse(lua_State *L) @@ -136,19 +137,29 @@ static int lua_uuid_parse(lua_State *L) #endif void *ud = lua_newuserdata(L, sizeof(LuaUuid)); - if (ud != NULL) + if (ud == NULL) { - luaL_getmetatable(L, LUA_UUID_METATABLE); - lua_setmetatable(L, -2); - LuaUuid *uuid = (LuaUuid *)ud; #if defined(LUA_UUID_USE_WIN32) - memcpy(&(uuid->data), &data, sizeof(data)); + // do nothing #elif defined(LUA_UUID_USE_LIBUUID) - memcpy(uuid->data, data, sizeof(data)); + // do nothing #elif defined(LUA_UUID_USE_APPLE) - memcpy(&(uuid->data), &data, sizeof(CFUUIDRef)); + CFRelease(data); #endif + + luaL_error(L, "Failed to create userdata"); } + + luaL_getmetatable(L, LUA_UUID_METATABLE); + lua_setmetatable(L, -2); + LuaUuid *uuid = (LuaUuid *)ud; +#if defined(LUA_UUID_USE_WIN32) + memcpy(&(uuid->data), &data, sizeof(UUID)); +#elif defined(LUA_UUID_USE_LIBUUID) + memcpy(uuid->data, data, sizeof(uuid_t)); +#elif defined(LUA_UUID_USE_APPLE) + memcpy(&(uuid->data), &data, sizeof(CFUUIDRef)); +#endif return 1; }