Skip to content

Commit

Permalink
v0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
luau-project committed Nov 12, 2024
1 parent aa43ed9 commit 7ca2733
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 42 deletions.
21 changes: 10 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -31,7 +31,7 @@ jobs:
- 5.2
- 5.3
- 5.4
- luajit-master
- luajit

os:
- windows-latest
Expand All @@ -40,7 +40,7 @@ jobs:

exclude:
- os: macos-latest
lua-version: luajit-master
lua-version: luajit

steps:

Expand All @@ -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
Expand Down Expand Up @@ -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:

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile.macosx
Original file line number Diff line number Diff line change
@@ -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
Expand Down
78 changes: 77 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
100 changes: 100 additions & 0 deletions rockspecs/lua-uuid-0.0.3-0.rockspec
Original file line number Diff line number Diff line change
@@ -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")
}
}
2 changes: 1 addition & 1 deletion rockspecs/lua-uuid-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion samples/tostring.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ local s1 = tostring(id1)
local s2 = tostring(id2)

assert(type(s1) == 'string')
assert(type(s2) == 'string')
assert(type(s2) == 'string')

-- print each string
print(s1)
print(s2)
65 changes: 38 additions & 27 deletions src/lua-uuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 7ca2733

Please sign in to comment.