Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add lua modules #273

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
add_library(dfly_core compact_object.cc dragonfly_core.cc extent_tree.cc
external_alloc.cc interpreter.cc mi_memory_resource.cc
segment_allocator.cc small_string.cc tx_queue.cc)
cxx_link(dfly_core base absl::flat_hash_map absl::str_format redis_lib TRDP::lua
cxx_link(dfly_core base absl::flat_hash_map absl::str_format redis_lib TRDP::lua lua_modules
Boost::fiber crypto)


Expand Down
17 changes: 17 additions & 0 deletions src/core/interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ extern "C" {
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>

LUALIB_API int (luaopen_cjson) (lua_State *L);
LUALIB_API int (luaopen_struct) (lua_State *L);
LUALIB_API int (luaopen_cmsgpack) (lua_State *L);
LUALIB_API int (luaopen_bit) (lua_State *L);
}

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

void LoadLibrary(lua_State *lua, const char *libname, lua_CFunction luafunc) {
lua_pushcfunction(lua, luafunc);
lua_pushstring(lua, libname);
lua_call(lua, 1, 0);
}

void InitLua(lua_State* lua) {
Require(lua, "", luaopen_base);
Require(lua, LUA_TABLIBNAME, luaopen_table);
Require(lua, LUA_STRLIBNAME, luaopen_string);
Require(lua, LUA_MATHLIBNAME, luaopen_math);
Require(lua, LUA_DBLIBNAME, luaopen_debug);

LoadLibrary(lua, "cjson", luaopen_cjson);
LoadLibrary(lua, "struct", luaopen_struct);
LoadLibrary(lua, "cmsgpack", luaopen_cmsgpack);
LoadLibrary(lua, "bit", luaopen_bit);


/* Add a helper function we use for pcall error reporting.
* Note that when the error is in the C function we want to report the
* information about the caller, that's what makes sense from the point
Expand Down
20 changes: 20 additions & 0 deletions src/core/interpreter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,24 @@ TEST_F(InterpreterTest, ArgKeys) {
EXPECT_EQ("[str(foo) str(key1) str(key2)]", ser_.res);
}

TEST_F(InterpreterTest, Modules) {
// cjson module
EXPECT_TRUE(Execute("return cjson.encode({1, 2, 3})"));
EXPECT_EQ("str([1,2,3])", ser_.res);
EXPECT_TRUE(Execute("return cjson.decode('{\"a\": 1}')['a']"));
EXPECT_EQ("d(1)", ser_.res);

// cmsgpack module
EXPECT_TRUE(Execute("return cmsgpack.pack('ok', true)"));
EXPECT_EQ("str(\xA2ok\xC3)", ser_.res);

// bit module
EXPECT_TRUE(Execute("return bit.bor(8, 4, 5)"));
EXPECT_EQ("i(13)", ser_.res);

// struct module
EXPECT_TRUE(Execute("return struct.pack('bbc4', 1, 2, 'test')"));
EXPECT_EQ("str(\x1\x2test)", ser_.res);
}

} // namespace dfly
2 changes: 2 additions & 0 deletions src/redis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ target_compile_options(redis_lib PRIVATE -Wno-maybe-uninitialized)
if (REDIS_ZMALLOC_MI)
target_compile_definitions(redis_lib PUBLIC USE_ZMALLOC_MI)
endif()

add_subdirectory(lua)
11 changes: 11 additions & 0 deletions src/redis/lua/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_library(lua_modules STATIC
cjson/fpconv.c cjson/strbuf.c cjson/lua_cjson.c
cmsgpack/lua_cmsgpack.c
struct/lua_struct.c
bit/bit.c
)

target_compile_options(lua_modules PRIVATE
-Wno-sign-compare -Wno-misleading-indentation -Wno-implicit-fallthrough -Wno-undefined-inline)

target_link_libraries(lua_modules TRDP::lua)
3 changes: 3 additions & 0 deletions src/redis/lua/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.

To provide the modules globally, `luaL_newlib` is followed by a `lua_setglobal` for bit and struct.
196 changes: 196 additions & 0 deletions src/redis/lua/bit/bit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
** http://bitop.luajit.org/
**
** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
*/

#define LUA_BITOP_VERSION "1.0.3"

#define LUA_LIB
#include "lua.h"
#include "lauxlib.h"

#ifdef _MSC_VER
/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif

typedef int32_t SBits;
typedef uint32_t UBits;

typedef union {
lua_Number n;
#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
uint64_t b;
#else
UBits b;
#endif
} BitNum;

/* Convert argument to bit type. */
static UBits barg(lua_State *L, int idx)
{
BitNum bn;
UBits b;
#if LUA_VERSION_NUM < 502
bn.n = lua_tonumber(L, idx);
#else
bn.n = luaL_checknumber(L, idx);
#endif
#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
bn.n += 6755399441055744.0; /* 2^52+2^51 */
#ifdef SWAPPED_DOUBLE
b = (UBits)(bn.b >> 32);
#else
b = (UBits)bn.b;
#endif
#elif defined(LUA_NUMBER_INT) || defined(LUA_INT_INT) || \
defined(LUA_NUMBER_LONG) || defined(LUA_INT_LONG) || \
defined(LUA_NUMBER_LONGLONG) || defined(LUA_INT_LONGLONG) || \
defined(LUA_NUMBER_LONG_LONG) || defined(LUA_NUMBER_LLONG)
if (sizeof(UBits) == sizeof(lua_Number))
b = bn.b;
else
b = (UBits)(SBits)bn.n;
#elif defined(LUA_NUMBER_FLOAT) || defined(LUA_FLOAT_FLOAT)
#error "A 'float' lua_Number type is incompatible with this library"
#else
#error "Unknown number type, check LUA_NUMBER_*, LUA_FLOAT_*, LUA_INT_* in luaconf.h"
#endif
#if LUA_VERSION_NUM < 502
if (b == 0 && !lua_isnumber(L, idx)) {
luaL_typerror(L, idx, "number");
}
#endif
return b;
}

/* Return bit type. */
#if LUA_VERSION_NUM < 503
#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
#else
#define BRET(b) lua_pushinteger(L, (lua_Integer)(SBits)(b)); return 1;
#endif

static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }

#define BIT_OP(func, opr) \
static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
BIT_OP(bit_band, &=)
BIT_OP(bit_bor, |=)
BIT_OP(bit_bxor, ^=)

#define bshl(b, n) (b << n)
#define bshr(b, n) (b >> n)
#define bsar(b, n) ((SBits)b >> n)
#define brol(b, n) ((b << n) | (b >> (32-n)))
#define bror(b, n) ((b << (32-n)) | (b >> n))
#define BIT_SH(func, fn) \
static int func(lua_State *L) { \
UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
BIT_SH(bit_lshift, bshl)
BIT_SH(bit_rshift, bshr)
BIT_SH(bit_arshift, bsar)
BIT_SH(bit_rol, brol)
BIT_SH(bit_ror, bror)

static int bit_bswap(lua_State *L)
{
UBits b = barg(L, 1);
b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
BRET(b)
}

static int bit_tohex(lua_State *L)
{
UBits b = barg(L, 1);
SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
const char *hexdigits = "0123456789abcdef";
char buf[8];
int i;
if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
if (n > 8) n = 8;
for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
lua_pushlstring(L, buf, (size_t)n);
return 1;
}

static const struct luaL_Reg bit_funcs[] = {
{ "tobit", bit_tobit },
{ "bnot", bit_bnot },
{ "band", bit_band },
{ "bor", bit_bor },
{ "bxor", bit_bxor },
{ "lshift", bit_lshift },
{ "rshift", bit_rshift },
{ "arshift", bit_arshift },
{ "rol", bit_rol },
{ "ror", bit_ror },
{ "bswap", bit_bswap },
{ "tohex", bit_tohex },
{ NULL, NULL }
};

/* Signed right-shifts are implementation-defined per C89/C99.
** But the de facto standard are arithmetic right-shifts on two's
** complement CPUs. This behaviour is required here, so test for it.
*/
#define BAD_SAR (bsar(-8, 2) != (SBits)-2)

LUALIB_API int luaopen_bit(lua_State *L)
{
UBits b;
#if LUA_VERSION_NUM < 503
lua_pushnumber(L, (lua_Number)1437217655L);
#else
lua_pushinteger(L, (lua_Integer)1437217655L);
#endif
b = barg(L, -1);
if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
const char *msg = "compiled with incompatible luaconf.h";
#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
#ifdef _WIN32
if (b == (UBits)1610612736L)
msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
#endif
if (b == (UBits)1127743488L)
msg = "not compiled with SWAPPED_DOUBLE";
#endif
if (BAD_SAR)
msg = "arithmetic right-shift broken";
luaL_error(L, "bit library self-test failed (%s)", msg);
}

luaL_newlib(L, bit_funcs);
lua_setglobal(L, "bit");

return 1;
}
Loading