Skip to content

Commit b905138

Browse files
authoredNov 26, 2021
Add Lua bitop library (#9847)
1 parent 7a1464d commit b905138

File tree

8 files changed

+251
-1
lines changed

8 files changed

+251
-1
lines changed
 

Diff for: ‎CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ endif()
261261
find_package(GMP REQUIRED)
262262
find_package(Json REQUIRED)
263263
find_package(Lua REQUIRED)
264+
if(NOT USE_LUAJIT)
265+
set(LUA_BIT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/bitop)
266+
set(LUA_BIT_LIBRARY bitop)
267+
add_subdirectory(lib/bitop)
268+
endif()
264269

265270
# JsonCpp doesn't compile well on GCC 4.8
266271
if(NOT USE_SYSTEM_JSONCPP)

Diff for: ‎doc/lua_api.txt

+7
Original file line numberDiff line numberDiff line change
@@ -8825,3 +8825,10 @@ Used by `minetest.register_authentication_handler`.
88258825
-- Returns an iterator (use with `for` loops) for all player names
88268826
-- currently in the auth database
88278827
}
8828+
8829+
Bit Library
8830+
-----------
8831+
8832+
Functions: bit.tobit, bit.tohex, bit.bnot, bit.band, bit.bor, bit.bxor, bit.lshift, bit.rshift, bit.arshift, bit.rol, bit.ror, bit.bswap
8833+
8834+
See http://bitop.luajit.org/ for advanced information.

Diff for: ‎lib/bitop/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_library(bitop bit.c)
2+
target_link_libraries(bitop)
3+
4+
include_directories(${LUA_INCLUDE_DIR})

Diff for: ‎lib/bitop/bit.c

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
#include "bit.h"
30+
31+
#define LUA_BITOP_VERSION "1.0.2"
32+
33+
#define LUA_LIB
34+
#include "lauxlib.h"
35+
36+
#ifdef _MSC_VER
37+
/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
38+
typedef __int32 int32_t;
39+
typedef unsigned __int32 uint32_t;
40+
typedef unsigned __int64 uint64_t;
41+
#else
42+
#include <stdint.h>
43+
#endif
44+
45+
typedef int32_t SBits;
46+
typedef uint32_t UBits;
47+
48+
typedef union {
49+
lua_Number n;
50+
#ifdef LUA_NUMBER_DOUBLE
51+
uint64_t b;
52+
#else
53+
UBits b;
54+
#endif
55+
} BitNum;
56+
57+
/* Convert argument to bit type. */
58+
static UBits barg(lua_State *L, int idx)
59+
{
60+
BitNum bn;
61+
UBits b;
62+
#if LUA_VERSION_NUM < 502
63+
bn.n = lua_tonumber(L, idx);
64+
#else
65+
bn.n = luaL_checknumber(L, idx);
66+
#endif
67+
#if defined(LUA_NUMBER_DOUBLE)
68+
bn.n += 6755399441055744.0; /* 2^52+2^51 */
69+
#ifdef SWAPPED_DOUBLE
70+
b = (UBits)(bn.b >> 32);
71+
#else
72+
b = (UBits)bn.b;
73+
#endif
74+
#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \
75+
defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \
76+
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)
82+
#error "A 'float' lua_Number type is incompatible with this library"
83+
#else
84+
#error "Unknown number type, check LUA_NUMBER_* 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+
#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
96+
97+
static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
98+
static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
99+
100+
#define BIT_OP(func, opr) \
101+
static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
102+
for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
103+
BIT_OP(bit_band, &=)
104+
BIT_OP(bit_bor, |=)
105+
BIT_OP(bit_bxor, ^=)
106+
107+
#define bshl(b, n) (b << n)
108+
#define bshr(b, n) (b >> n)
109+
#define bsar(b, n) ((SBits)b >> n)
110+
#define brol(b, n) ((b << n) | (b >> (32-n)))
111+
#define bror(b, n) ((b << (32-n)) | (b >> n))
112+
#define BIT_SH(func, fn) \
113+
static int func(lua_State *L) { \
114+
UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
115+
BIT_SH(bit_lshift, bshl)
116+
BIT_SH(bit_rshift, bshr)
117+
BIT_SH(bit_arshift, bsar)
118+
BIT_SH(bit_rol, brol)
119+
BIT_SH(bit_ror, bror)
120+
121+
static int bit_bswap(lua_State *L)
122+
{
123+
UBits b = barg(L, 1);
124+
b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
125+
BRET(b)
126+
}
127+
128+
static int bit_tohex(lua_State *L)
129+
{
130+
UBits b = barg(L, 1);
131+
SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
132+
const char *hexdigits = "0123456789abcdef";
133+
char buf[8];
134+
int i;
135+
if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
136+
if (n > 8) n = 8;
137+
for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
138+
lua_pushlstring(L, buf, (size_t)n);
139+
return 1;
140+
}
141+
142+
static const struct luaL_Reg bit_funcs[] = {
143+
{ "tobit", bit_tobit },
144+
{ "bnot", bit_bnot },
145+
{ "band", bit_band },
146+
{ "bor", bit_bor },
147+
{ "bxor", bit_bxor },
148+
{ "lshift", bit_lshift },
149+
{ "rshift", bit_rshift },
150+
{ "arshift", bit_arshift },
151+
{ "rol", bit_rol },
152+
{ "ror", bit_ror },
153+
{ "bswap", bit_bswap },
154+
{ "tohex", bit_tohex },
155+
{ NULL, NULL }
156+
};
157+
158+
/* Signed right-shifts are implementation-defined per C89/C99.
159+
** But the de facto standard are arithmetic right-shifts on two's
160+
** complement CPUs. This behaviour is required here, so test for it.
161+
*/
162+
#define BAD_SAR (bsar(-8, 2) != (SBits)-2)
163+
164+
LUALIB_API int luaopen_bit(lua_State *L)
165+
{
166+
UBits b;
167+
lua_pushnumber(L, (lua_Number)1437217655L);
168+
b = barg(L, -1);
169+
if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
170+
const char *msg = "compiled with incompatible luaconf.h";
171+
#ifdef LUA_NUMBER_DOUBLE
172+
#ifdef _WIN32
173+
if (b == (UBits)1610612736L)
174+
msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
175+
#endif
176+
if (b == (UBits)1127743488L)
177+
msg = "not compiled with SWAPPED_DOUBLE";
178+
#endif
179+
if (BAD_SAR)
180+
msg = "arithmetic right-shift broken";
181+
luaL_error(L, "bit library self-test failed (%s)", msg);
182+
}
183+
#if LUA_VERSION_NUM < 502
184+
luaL_register(L, "bit", bit_funcs);
185+
#else
186+
luaL_newlib(L, bit_funcs);
187+
#endif
188+
return 1;
189+
}

Diff for: ‎lib/bitop/bit.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
#pragma once
30+
31+
#include "lua.h"
32+
33+
#define LUA_BITLIBNAME "bit"
34+
LUALIB_API int luaopen_bit(lua_State *L);

Diff for: ‎src/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ include_directories(
500500
${LUA_INCLUDE_DIR}
501501
${GMP_INCLUDE_DIR}
502502
${JSON_INCLUDE_DIR}
503+
${LUA_BIT_INCLUDE_DIR}
503504
${X11_INCLUDE_DIR}
504505
${PROJECT_SOURCE_DIR}/script
505506
)
@@ -537,6 +538,7 @@ if(BUILD_CLIENT)
537538
${LUA_LIBRARY}
538539
${GMP_LIBRARY}
539540
${JSON_LIBRARY}
541+
${LUA_BIT_LIBRARY}
540542
${PLATFORM_LIBS}
541543
)
542544
if(NOT USE_LUAJIT)
@@ -619,6 +621,7 @@ if(BUILD_SERVER)
619621
${SQLITE3_LIBRARY}
620622
${JSON_LIBRARY}
621623
${LUA_LIBRARY}
624+
${LUA_BIT_LIBRARY}
622625
${GMP_LIBRARY}
623626
${PLATFORM_LIBS}
624627
)

Diff for: ‎src/script/cpp_api/s_base.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ extern "C" {
3737
#include "lualib.h"
3838
#if USE_LUAJIT
3939
#include "luajit.h"
40+
#else
41+
#include "bit.h"
4042
#endif
4143
}
4244

@@ -88,6 +90,11 @@ ScriptApiBase::ScriptApiBase(ScriptingType type):
8890
else
8991
luaL_openlibs(m_luastack);
9092

93+
// Load bit library
94+
lua_pushcfunction(m_luastack, luaopen_bit);
95+
lua_pushstring(m_luastack, LUA_BITLIBNAME);
96+
lua_call(m_luastack, 1, 0);
97+
9198
// Make the ScriptApiBase* accessible to ModApiBase
9299
#if INDIRECT_SCRIPTAPI_RIDX
93100
*(void **)(lua_newuserdata(m_luastack, sizeof(void *))) = this;

Diff for: ‎src/script/cpp_api/s_security.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ void ScriptApiSecurity::initializeSecurity()
106106
"string",
107107
"table",
108108
"math",
109+
"bit"
109110
};
110111
static const char *io_whitelist[] = {
111112
"close",
@@ -298,6 +299,7 @@ void ScriptApiSecurity::initializeSecurityClient()
298299
"string",
299300
"table",
300301
"math",
302+
"bit",
301303
};
302304
static const char *os_whitelist[] = {
303305
"clock",
@@ -834,4 +836,3 @@ int ScriptApiSecurity::sl_os_remove(lua_State *L)
834836
lua_call(L, 1, 2);
835837
return 2;
836838
}
837-

0 commit comments

Comments
 (0)
Please sign in to comment.