Skip to content

Commit a43badf

Browse files
authored
feat(core): Add lua modules (#273)
feat(server): Add modules to lua interpreter
1 parent cfa1265 commit a43badf

File tree

14 files changed

+3700
-1
lines changed

14 files changed

+3700
-1
lines changed

src/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
add_library(dfly_core compact_object.cc dragonfly_core.cc extent_tree.cc
22
external_alloc.cc interpreter.cc mi_memory_resource.cc
33
segment_allocator.cc small_string.cc tx_queue.cc)
4-
cxx_link(dfly_core base absl::flat_hash_map absl::str_format redis_lib TRDP::lua
4+
cxx_link(dfly_core base absl::flat_hash_map absl::str_format redis_lib TRDP::lua lua_modules
55
Boost::fiber crypto)
66

77

src/core/interpreter.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ extern "C" {
1414
#include <lauxlib.h>
1515
#include <lua.h>
1616
#include <lualib.h>
17+
18+
LUALIB_API int (luaopen_cjson) (lua_State *L);
19+
LUALIB_API int (luaopen_struct) (lua_State *L);
20+
LUALIB_API int (luaopen_cmsgpack) (lua_State *L);
21+
LUALIB_API int (luaopen_bit) (lua_State *L);
1722
}
1823

1924
#include <absl/strings/str_format.h>
@@ -196,13 +201,25 @@ int RaiseError(lua_State* lua) {
196201
return lua_error(lua);
197202
}
198203

204+
void LoadLibrary(lua_State *lua, const char *libname, lua_CFunction luafunc) {
205+
lua_pushcfunction(lua, luafunc);
206+
lua_pushstring(lua, libname);
207+
lua_call(lua, 1, 0);
208+
}
209+
199210
void InitLua(lua_State* lua) {
200211
Require(lua, "", luaopen_base);
201212
Require(lua, LUA_TABLIBNAME, luaopen_table);
202213
Require(lua, LUA_STRLIBNAME, luaopen_string);
203214
Require(lua, LUA_MATHLIBNAME, luaopen_math);
204215
Require(lua, LUA_DBLIBNAME, luaopen_debug);
205216

217+
LoadLibrary(lua, "cjson", luaopen_cjson);
218+
LoadLibrary(lua, "struct", luaopen_struct);
219+
LoadLibrary(lua, "cmsgpack", luaopen_cmsgpack);
220+
LoadLibrary(lua, "bit", luaopen_bit);
221+
222+
206223
/* Add a helper function we use for pcall error reporting.
207224
* Note that when the error is in the C function we want to report the
208225
* information about the caller, that's what makes sense from the point

src/core/interpreter_test.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,24 @@ TEST_F(InterpreterTest, ArgKeys) {
301301
EXPECT_EQ("[str(foo) str(key1) str(key2)]", ser_.res);
302302
}
303303

304+
TEST_F(InterpreterTest, Modules) {
305+
// cjson module
306+
EXPECT_TRUE(Execute("return cjson.encode({1, 2, 3})"));
307+
EXPECT_EQ("str([1,2,3])", ser_.res);
308+
EXPECT_TRUE(Execute("return cjson.decode('{\"a\": 1}')['a']"));
309+
EXPECT_EQ("d(1)", ser_.res);
310+
311+
// cmsgpack module
312+
EXPECT_TRUE(Execute("return cmsgpack.pack('ok', true)"));
313+
EXPECT_EQ("str(\xA2ok\xC3)", ser_.res);
314+
315+
// bit module
316+
EXPECT_TRUE(Execute("return bit.bor(8, 4, 5)"));
317+
EXPECT_EQ("i(13)", ser_.res);
318+
319+
// struct module
320+
EXPECT_TRUE(Execute("return struct.pack('bbc4', 1, 2, 'test')"));
321+
EXPECT_EQ("str(\x1\x2test)", ser_.res);
322+
}
323+
304324
} // namespace dfly

src/redis/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ target_compile_options(redis_lib PRIVATE -Wno-maybe-uninitialized)
2020
if (REDIS_ZMALLOC_MI)
2121
target_compile_definitions(redis_lib PUBLIC USE_ZMALLOC_MI)
2222
endif()
23+
24+
add_subdirectory(lua)

src/redis/lua/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_library(lua_modules STATIC
2+
cjson/fpconv.c cjson/strbuf.c cjson/lua_cjson.c
3+
cmsgpack/lua_cmsgpack.c
4+
struct/lua_struct.c
5+
bit/bit.c
6+
)
7+
8+
target_compile_options(lua_modules PRIVATE
9+
-Wno-sign-compare -Wno-misleading-indentation -Wno-implicit-fallthrough -Wno-undefined-inline)
10+
11+
target_link_libraries(lua_modules TRDP::lua)

src/redis/lua/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Since version 5.2 `luaL_register` is deprecated and removed. The new `luaL_newlib` function doesn't make the module globally available upon registration and is ment to be used with the `require` function.
2+
3+
To provide the modules globally, `luaL_newlib` is followed by a `lua_setglobal` for bit and struct.

src/redis/lua/bit/bit.c

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
3+
** http://bitop.luajit.org/
4+
**
5+
** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
6+
**
7+
** Permission is hereby granted, free of charge, to any person obtaining
8+
** a copy of this software and associated documentation files (the
9+
** "Software"), to deal in the Software without restriction, including
10+
** without limitation the rights to use, copy, modify, merge, publish,
11+
** distribute, sublicense, and/or sell copies of the Software, and to
12+
** permit persons to whom the Software is furnished to do so, subject to
13+
** the following conditions:
14+
**
15+
** The above copyright notice and this permission notice shall be
16+
** included in all copies or substantial portions of the Software.
17+
**
18+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19+
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20+
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21+
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22+
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23+
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24+
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25+
**
26+
** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
27+
*/
28+
29+
#define LUA_BITOP_VERSION "1.0.3"
30+
31+
#define LUA_LIB
32+
#include "lua.h"
33+
#include "lauxlib.h"
34+
35+
#ifdef _MSC_VER
36+
/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
37+
typedef __int32 int32_t;
38+
typedef unsigned __int32 uint32_t;
39+
typedef unsigned __int64 uint64_t;
40+
#else
41+
#include <stdint.h>
42+
#endif
43+
44+
typedef int32_t SBits;
45+
typedef uint32_t UBits;
46+
47+
typedef union {
48+
lua_Number n;
49+
#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
50+
uint64_t b;
51+
#else
52+
UBits b;
53+
#endif
54+
} BitNum;
55+
56+
/* Convert argument to bit type. */
57+
static UBits barg(lua_State *L, int idx)
58+
{
59+
BitNum bn;
60+
UBits b;
61+
#if LUA_VERSION_NUM < 502
62+
bn.n = lua_tonumber(L, idx);
63+
#else
64+
bn.n = luaL_checknumber(L, idx);
65+
#endif
66+
#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
67+
bn.n += 6755399441055744.0; /* 2^52+2^51 */
68+
#ifdef SWAPPED_DOUBLE
69+
b = (UBits)(bn.b >> 32);
70+
#else
71+
b = (UBits)bn.b;
72+
#endif
73+
#elif defined(LUA_NUMBER_INT) || defined(LUA_INT_INT) || \
74+
defined(LUA_NUMBER_LONG) || defined(LUA_INT_LONG) || \
75+
defined(LUA_NUMBER_LONGLONG) || defined(LUA_INT_LONGLONG) || \
76+
defined(LUA_NUMBER_LONG_LONG) || defined(LUA_NUMBER_LLONG)
77+
if (sizeof(UBits) == sizeof(lua_Number))
78+
b = bn.b;
79+
else
80+
b = (UBits)(SBits)bn.n;
81+
#elif defined(LUA_NUMBER_FLOAT) || defined(LUA_FLOAT_FLOAT)
82+
#error "A 'float' lua_Number type is incompatible with this library"
83+
#else
84+
#error "Unknown number type, check LUA_NUMBER_*, LUA_FLOAT_*, LUA_INT_* in luaconf.h"
85+
#endif
86+
#if LUA_VERSION_NUM < 502
87+
if (b == 0 && !lua_isnumber(L, idx)) {
88+
luaL_typerror(L, idx, "number");
89+
}
90+
#endif
91+
return b;
92+
}
93+
94+
/* Return bit type. */
95+
#if LUA_VERSION_NUM < 503
96+
#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
97+
#else
98+
#define BRET(b) lua_pushinteger(L, (lua_Integer)(SBits)(b)); return 1;
99+
#endif
100+
101+
static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
102+
static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
103+
104+
#define BIT_OP(func, opr) \
105+
static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
106+
for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
107+
BIT_OP(bit_band, &=)
108+
BIT_OP(bit_bor, |=)
109+
BIT_OP(bit_bxor, ^=)
110+
111+
#define bshl(b, n) (b << n)
112+
#define bshr(b, n) (b >> n)
113+
#define bsar(b, n) ((SBits)b >> n)
114+
#define brol(b, n) ((b << n) | (b >> (32-n)))
115+
#define bror(b, n) ((b << (32-n)) | (b >> n))
116+
#define BIT_SH(func, fn) \
117+
static int func(lua_State *L) { \
118+
UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
119+
BIT_SH(bit_lshift, bshl)
120+
BIT_SH(bit_rshift, bshr)
121+
BIT_SH(bit_arshift, bsar)
122+
BIT_SH(bit_rol, brol)
123+
BIT_SH(bit_ror, bror)
124+
125+
static int bit_bswap(lua_State *L)
126+
{
127+
UBits b = barg(L, 1);
128+
b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
129+
BRET(b)
130+
}
131+
132+
static int bit_tohex(lua_State *L)
133+
{
134+
UBits b = barg(L, 1);
135+
SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
136+
const char *hexdigits = "0123456789abcdef";
137+
char buf[8];
138+
int i;
139+
if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
140+
if (n > 8) n = 8;
141+
for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
142+
lua_pushlstring(L, buf, (size_t)n);
143+
return 1;
144+
}
145+
146+
static const struct luaL_Reg bit_funcs[] = {
147+
{ "tobit", bit_tobit },
148+
{ "bnot", bit_bnot },
149+
{ "band", bit_band },
150+
{ "bor", bit_bor },
151+
{ "bxor", bit_bxor },
152+
{ "lshift", bit_lshift },
153+
{ "rshift", bit_rshift },
154+
{ "arshift", bit_arshift },
155+
{ "rol", bit_rol },
156+
{ "ror", bit_ror },
157+
{ "bswap", bit_bswap },
158+
{ "tohex", bit_tohex },
159+
{ NULL, NULL }
160+
};
161+
162+
/* Signed right-shifts are implementation-defined per C89/C99.
163+
** But the de facto standard are arithmetic right-shifts on two's
164+
** complement CPUs. This behaviour is required here, so test for it.
165+
*/
166+
#define BAD_SAR (bsar(-8, 2) != (SBits)-2)
167+
168+
LUALIB_API int luaopen_bit(lua_State *L)
169+
{
170+
UBits b;
171+
#if LUA_VERSION_NUM < 503
172+
lua_pushnumber(L, (lua_Number)1437217655L);
173+
#else
174+
lua_pushinteger(L, (lua_Integer)1437217655L);
175+
#endif
176+
b = barg(L, -1);
177+
if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
178+
const char *msg = "compiled with incompatible luaconf.h";
179+
#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
180+
#ifdef _WIN32
181+
if (b == (UBits)1610612736L)
182+
msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
183+
#endif
184+
if (b == (UBits)1127743488L)
185+
msg = "not compiled with SWAPPED_DOUBLE";
186+
#endif
187+
if (BAD_SAR)
188+
msg = "arithmetic right-shift broken";
189+
luaL_error(L, "bit library self-test failed (%s)", msg);
190+
}
191+
192+
luaL_newlib(L, bit_funcs);
193+
lua_setglobal(L, "bit");
194+
195+
return 1;
196+
}

0 commit comments

Comments
 (0)