Skip to content

Commit 7ca2733

Browse files
committed
v0.0.3
1 parent aa43ed9 commit 7ca2733

File tree

7 files changed

+232
-42
lines changed

7 files changed

+232
-42
lines changed

.github/workflows/ci.yml

+10-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111
- "docs/**"
1212

1313
env:
14-
ROCKSPEC_VERSION: 0.0.2
14+
ROCKSPEC_VERSION: 0.0.3
1515
DEV_ROCKSPEC: lua-uuid-dev-1.rockspec
1616

1717
jobs:
@@ -31,7 +31,7 @@ jobs:
3131
- 5.2
3232
- 5.3
3333
- 5.4
34-
- luajit-master
34+
- luajit
3535

3636
os:
3737
- windows-latest
@@ -40,7 +40,7 @@ jobs:
4040

4141
exclude:
4242
- os: macos-latest
43-
lua-version: luajit-master
43+
lua-version: luajit
4444

4545
steps:
4646

@@ -65,7 +65,7 @@ jobs:
6565
run: sudo apt install -y uuid-dev
6666

6767
- name: Setup MSVC dev-prompt
68-
if: ${{ matrix.os == 'windows-latest' && matrix.lua-version != 'luajit-master' }}
68+
if: ${{ matrix.os == 'windows-latest' && matrix.lua-version != 'luajit' }}
6969
uses: ilammy/msvc-dev-cmd@v1
7070

7171
- name: Setup Lua
@@ -114,19 +114,18 @@ jobs:
114114
matrix:
115115

116116
MSYS2_CONFIG:
117-
- { sys: mingw32, env: i686 }
118117
- { sys: mingw64, env: x86_64 }
119118
- { sys: ucrt64, env: ucrt-x86_64 }
120119
- { sys: clang64, env: clang-x86_64 }
121120

122121
Lua:
123-
- { version: '5.4', msys2_pkg_name: 'lua', msys2_lua_interpreter: 'lua' }
124-
- { version: '5.3', msys2_pkg_name: 'lua53', msys2_lua_interpreter: 'lua5.3' }
125-
- { version: '5.1', msys2_pkg_name: 'lua51', msys2_lua_interpreter: 'lua5.1' }
126-
- { version: '5.1', msys2_pkg_name: 'luajit', msys2_lua_interpreter: 'luajit' }
122+
- { version: '5.4', msys2-pkg-name: 'lua', msys2-lua-interpreter: 'lua' }
123+
- { version: '5.3', msys2-pkg-name: 'lua53', msys2-lua-interpreter: 'lua5.3' }
124+
- { version: '5.1', msys2-pkg-name: 'lua51', msys2-lua-interpreter: 'lua5.1' }
125+
- { version: '5.1', msys2-pkg-name: 'luajit', msys2-lua-interpreter: 'luajit' }
127126

128127
env:
129-
LUA_INTERPRETER: /${{ matrix.MSYS2_CONFIG.sys }}/bin/${{ matrix.Lua.msys2_lua_interpreter }}
128+
LUA_INTERPRETER: /${{ matrix.MSYS2_CONFIG.sys }}/bin/${{ matrix.Lua.msys2-lua-interpreter }}
130129

131130
steps:
132131

@@ -138,7 +137,7 @@ jobs:
138137
base-devel
139138
git
140139
mingw-w64-${{ matrix.MSYS2_CONFIG.env }}-cc
141-
mingw-w64-${{ matrix.MSYS2_CONFIG.env }}-${{ matrix.Lua.msys2_pkg_name }}
140+
mingw-w64-${{ matrix.MSYS2_CONFIG.env }}-${{ matrix.Lua.msys2-pkg-name }}
142141
mingw-w64-${{ matrix.MSYS2_CONFIG.env }}-lua-luarocks
143142
144143
- name: Set environment variable to hold the rockspec name

Makefile.macosx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
OBJ_EXTENSION = o
22
LIB_EXTENSION = so
3-
CFLAGS_EXTRA = -DLUA_UUID_USE_APPLE
3+
CFLAGS_EXTRA = -DLUA_UUID_BUILD_SHARED -DLUA_UUID_USE_APPLE
44
LIBFLAG_EXTRA = -framework CoreFoundation
55

66
LUA_DIR = /usr/local

README.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# lua-uuid
22

3-
[![CI](https://github.com/luau-project/lua-uuid/actions/workflows/ci.yml/badge.svg)](./.github/workflows/ci.yml)
3+
[![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)
44

55
## Overview
66

@@ -18,6 +18,15 @@
1818
* [Parse GUIDs / UUIDs from string](#parse-guids--uuids-from-string)
1919
* [Compare GUIDs / UUIDs](#compare-guids--uuids)
2020
* [Verify GUIDs / UUIDs nullity](#verify-guids--uuids-nullity)
21+
* [Static Methods](#static-methods)
22+
* [new](#new)
23+
* [parse](#parse)
24+
* [Instance Methods](#instance-methods)
25+
* [isnil](#isnil)
26+
* [Metamethods](#metamethods)
27+
* [__eq](#__eq)
28+
* [__tostring](#__tostring)
29+
* [Change log](#change-log)
2130
* [Future works](#future-works)
2231

2332
## Installation
@@ -79,6 +88,10 @@ luarocks install lua-uuid
7988

8089
assert(type(s1) == 'string')
8190
assert(type(s2) == 'string')
91+
92+
-- print each string
93+
print(s1)
94+
print(s2)
8295
```
8396

8497
### Parse GUIDs / UUIDs from string
@@ -141,6 +154,69 @@ local id3 = uuid.parse("00000000-0000-0000-0000-000000000000")
141154
print(id3:isnil())
142155
```
143156

157+
## Static Methods
158+
159+
### new
160+
161+
* *Description*: Generates a new GUID / UUID
162+
* *Signature*: ```new()```
163+
* *return*: ```(userdata)```
164+
* *Usage*: See [here](#generate-guids--uuids)
165+
166+
### parse
167+
168+
* *Description*: Generates a new GUID / UUID from a string value
169+
* *Signature*: ```parse(value)```
170+
* *value* (string): the string to be parsed
171+
* *return*: ```(userdata)```
172+
* *Usage*: See [here](#parse-guids--uuids-from-string)
173+
174+
## Instance Methods
175+
176+
### isnil
177+
178+
* *Description*: Verifies whether the GUID / UUID is considered null or not.
179+
180+
> [!NOTE]
181+
>
182+
> a GUID / UUID is considered null when its string representation is equal to ```00000000-0000-0000-0000-000000000000```.
183+
184+
* *Signature*: ```instance:isnil()```
185+
* *instance* (userdata): the GUID / UUID instance to check for nullity
186+
* *return*: ```(boolean)```
187+
* *Usage*: See [here](#verify-guids--uuids-nullity)
188+
189+
## Metamethods
190+
191+
### __eq
192+
193+
* *Description*: Compares two GUIDs / UUIDs for equality
194+
* *Signature*: ```left == right```
195+
* *left* (any): the left-side element
196+
* *right* (any): the right-side element
197+
* *return*: ```(boolean)```
198+
* *Usage*: See [here](#compare-guids--uuids)
199+
200+
### __tostring
201+
202+
* *Description*: Converts the GUID / UUID to string
203+
* *Signature*: ```tostring(value)```
204+
* *value* (userdata): the GUID / UUID to perform the conversion
205+
* *return*: ```(string)```
206+
* *Usage*: See [here](#generate-guids--uuids)
207+
208+
## Change log
209+
210+
* v0.0.3:
211+
* Changed to throw issue when ```lua_newuserdata``` returns ```NULL```;
212+
* Added macro ```LUA_UUID_BUILD_SHARED``` to ```CFLAGS_EXTRA``` on macos;
213+
* Changed ```luajit-master``` to ```luajit``` on CI when testing for ```LuaJIT```;
214+
* Added print statements on [tostring.lua](./samples/tostring.lua) sample;
215+
* Removed build / testing from CI for x86 packages on MSYS2;
216+
* Added documentation for static, instance and metamethods to the README.
217+
* v0.0.2:
218+
* Fixed syntax issue in the rockspec lua-uuid-0.0.1-0.rockspec
219+
144220
## Future works
145221

146222
* Add CMake as build system

rockspecs/lua-uuid-0.0.3-0.rockspec

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package = "lua-uuid"
2+
version = "0.0.3-0"
3+
4+
source = {
5+
url = "git://github.com/luau-project/lua-uuid.git",
6+
tag = "v0.0.3"
7+
}
8+
9+
description = {
10+
homepage = "https://github.com/luau-project/lua-uuid",
11+
summary = [[Lightweight, native GUID / UUID library for Lua]],
12+
detailed = [=[
13+
lua-uuid is a lightweight, native library for Lua (5.1 and newer) to deal with Universally Unique Id (UUID).
14+
15+
* On Linux, it uses libuuid to generate UUIDs;
16+
* On Windows, it uses the WINAPI rpcrt4 library;
17+
* On MacOS / iOS, it uses the CoreFoundation framework.
18+
19+
Visit the GitHub repository for more information.]=],
20+
license = "MIT"
21+
}
22+
23+
supported_platforms = { "linux", "win32", "mingw", "cygwin", "macosx" }
24+
25+
dependencies = {
26+
"lua >= 5.1"
27+
}
28+
29+
external_dependencies = {
30+
platforms = {
31+
linux = {
32+
["UUID"] = {
33+
header = "uuid/uuid.h"
34+
}
35+
}
36+
}
37+
}
38+
39+
local function make_plat(plat)
40+
if (plat == "linux") then
41+
return {
42+
type = "builtin",
43+
modules = {
44+
["lua-uuid"] = {
45+
sources = { "src/lua-uuid.c" },
46+
libraries = { "uuid" },
47+
defines = { "LUA_UUID_BUILD_SHARED", "LUA_UUID_USE_LIBUUID" },
48+
incdirs = { "src", "$(UUID_INCDIR)" },
49+
libdirs = { "$(UUID_LIBDIR)" }
50+
}
51+
}
52+
}
53+
elseif (plat == "win32" or plat == "mingw" or plat == "cygwin") then
54+
return {
55+
type = "builtin",
56+
modules = {
57+
["lua-uuid"] = {
58+
sources = { "src/lua-uuid.c" },
59+
libraries = { "rpcrt4" },
60+
defines = { "LUA_UUID_BUILD_SHARED", "LUA_UUID_USE_WIN32" },
61+
incdirs = { "src" },
62+
libdirs = {}
63+
}
64+
}
65+
}
66+
elseif (plat == "macosx") then
67+
return {
68+
type = "make",
69+
makefile = "Makefile.macosx",
70+
build_variables = {
71+
CFLAGS = "$(CFLAGS)",
72+
LIBFLAG = "$(LIBFLAG)",
73+
CFLAGS_EXTRA = "-DLUA_UUID_BUILD_SHARED -DLUA_UUID_USE_APPLE",
74+
LIBFLAG_EXTRA = "-framework CoreFoundation",
75+
LUA_INCDIR = "$(LUA_INCDIR)",
76+
LUA_LIBDIR = "$(LUA_INCDIR)/../lib",
77+
OBJ_EXTENSION = "$(OBJ_EXTENSION)",
78+
LIB_EXTENSION = "$(LIB_EXTENSION)"
79+
},
80+
install_variables = {
81+
INSTALL_PREFIX = "$(PREFIX)",
82+
INSTALL_LIBDIR = "$(LIBDIR)",
83+
LUA_VERSION = "$(LUA_VERSION)",
84+
LIB_EXTENSION = "$(LIB_EXTENSION)"
85+
}
86+
}
87+
else
88+
error("Unknown platform", 2)
89+
end
90+
end
91+
92+
build = {
93+
platforms = {
94+
linux = make_plat("linux"),
95+
win32 = make_plat("win32"),
96+
mingw = make_plat("mingw"),
97+
cygwin = make_plat("cygwin"),
98+
macosx = make_plat("macosx")
99+
}
100+
}

rockspecs/lua-uuid-dev-1.rockspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ local function make_plat(plat)
6969
build_variables = {
7070
CFLAGS = "$(CFLAGS)",
7171
LIBFLAG = "$(LIBFLAG)",
72-
CFLAGS_EXTRA = "-DLUA_UUID_USE_APPLE",
72+
CFLAGS_EXTRA = "-DLUA_UUID_BUILD_SHARED -DLUA_UUID_USE_APPLE",
7373
LIBFLAG_EXTRA = "-framework CoreFoundation",
7474
LUA_INCDIR = "$(LUA_INCDIR)",
7575
LUA_LIBDIR = "$(LUA_INCDIR)/../lib",

samples/tostring.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ local s1 = tostring(id1)
1010
local s2 = tostring(id2)
1111

1212
assert(type(s1) == 'string')
13-
assert(type(s2) == 'string')
13+
assert(type(s2) == 'string')
14+
15+
-- print each string
16+
print(s1)
17+
print(s2)

src/lua-uuid.c

+38-27
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,35 @@ static void *lua_uuid_testudata(lua_State *L, int ud, const char *tname)
6464

6565
static int lua_uuid_new(lua_State *L)
6666
{
67-
int res = 1;
68-
6967
void *ud = lua_newuserdata(L, sizeof(LuaUuid));
70-
if (ud != NULL)
68+
if (ud == NULL)
7169
{
72-
luaL_getmetatable(L, LUA_UUID_METATABLE);
73-
lua_setmetatable(L, -2);
74-
LuaUuid *uuid = (LuaUuid *)ud;
70+
luaL_error(L, "Failed to create userdata");
71+
}
72+
73+
luaL_getmetatable(L, LUA_UUID_METATABLE);
74+
lua_setmetatable(L, -2);
75+
LuaUuid *uuid = (LuaUuid *)ud;
7576

7677
#if defined(LUA_UUID_USE_WIN32)
77-
RPC_STATUS create_status = UuidCreate(&(uuid->data));
78-
79-
if (create_status != RPC_S_OK)
80-
{
81-
luaL_error(L, "Failed to create UUID");
82-
}
78+
RPC_STATUS create_status = UuidCreate(&(uuid->data));
79+
80+
if (create_status != RPC_S_OK)
81+
{
82+
luaL_error(L, "Failed to create UUID");
83+
}
8384
#elif defined(LUA_UUID_USE_LIBUUID)
84-
uuid_generate(uuid->data);
85+
uuid_generate(uuid->data);
8586
#elif defined(LUA_UUID_USE_APPLE)
86-
uuid->data = CFUUIDCreate(NULL);
87+
uuid->data = CFUUIDCreate(NULL);
8788

88-
if (uuid->data == NULL)
89-
{
90-
luaL_error(L, "Failed to create UUID");
91-
}
92-
#endif
89+
if (uuid->data == NULL)
90+
{
91+
luaL_error(L, "Failed to create UUID");
9392
}
94-
return res;
93+
#endif
94+
95+
return 1;
9596
}
9697

9798
static int lua_uuid_parse(lua_State *L)
@@ -136,19 +137,29 @@ static int lua_uuid_parse(lua_State *L)
136137
#endif
137138

138139
void *ud = lua_newuserdata(L, sizeof(LuaUuid));
139-
if (ud != NULL)
140+
if (ud == NULL)
140141
{
141-
luaL_getmetatable(L, LUA_UUID_METATABLE);
142-
lua_setmetatable(L, -2);
143-
LuaUuid *uuid = (LuaUuid *)ud;
144142
#if defined(LUA_UUID_USE_WIN32)
145-
memcpy(&(uuid->data), &data, sizeof(data));
143+
// do nothing
146144
#elif defined(LUA_UUID_USE_LIBUUID)
147-
memcpy(uuid->data, data, sizeof(data));
145+
// do nothing
148146
#elif defined(LUA_UUID_USE_APPLE)
149-
memcpy(&(uuid->data), &data, sizeof(CFUUIDRef));
147+
CFRelease(data);
150148
#endif
149+
150+
luaL_error(L, "Failed to create userdata");
151151
}
152+
153+
luaL_getmetatable(L, LUA_UUID_METATABLE);
154+
lua_setmetatable(L, -2);
155+
LuaUuid *uuid = (LuaUuid *)ud;
156+
#if defined(LUA_UUID_USE_WIN32)
157+
memcpy(&(uuid->data), &data, sizeof(UUID));
158+
#elif defined(LUA_UUID_USE_LIBUUID)
159+
memcpy(uuid->data, data, sizeof(uuid_t));
160+
#elif defined(LUA_UUID_USE_APPLE)
161+
memcpy(&(uuid->data), &data, sizeof(CFUUIDRef));
162+
#endif
152163

153164
return 1;
154165
}

0 commit comments

Comments
 (0)