Skip to content

Commit e0dab88

Browse files
committed
tests/capi: support bitop in luaL_loadbuffer_proto
The `bit32` library has been deprecated in PUC Rio Lua 5.3 [1]. Missed bitwise functions have been replaced by appropriate bitwise operations [2]. The patch adds support of bitwise operations to the test `luaL_loadbuffer_proto`. 1. https://www.lua.org/manual/5.3/manual.html#8.2 2. https://www.lua.org/manual/5.3/manual.html#3.4.2
1 parent f29c1c2 commit e0dab88

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

tests/capi/luaL_loadbuffer_proto/lua_grammar.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,13 @@ message BinaryOperator {
386386

387387
/* Arithmetic operators (5.3+). */
388388
uint32 idiv = 16;
389+
390+
/* Bitwise operators (5.3+). */
391+
uint32 band = 17;
392+
uint32 bor = 18;
393+
uint32 bxor = 19;
394+
uint32 shl = 20;
395+
uint32 shr = 21;
389396
}
390397
}
391398

@@ -395,6 +402,9 @@ message UnaryOperator {
395402
uint32 negate = 1;
396403
uint32 not = 2;
397404
uint32 length = 3;
405+
406+
/* Bitwise operators (5.3+). */
407+
uint32 bnot = 4;
398408
}
399409
}
400410

tests/capi/luaL_loadbuffer_proto/preamble.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@ local __idiv = load([[
7676
local v1, v2 = ...
7777
return always_number(v1) // always_number(v2)
7878
]])
79+
local __band = load([[
80+
local v1, v2 = ...
81+
return always_number(v1) & always_number(v2)
82+
]])
83+
local __bor = load([[
84+
local v1, v2 = ...
85+
return always_number(v1) | always_number(v2)
86+
]])
87+
local __bxor = load([[
88+
local v1, v2 = ...
89+
return always_number(v1) ~ always_number(v2)
90+
]])
91+
local __bnot = load("return ~always_number(v)")
92+
local __shl = load([[
93+
local v1, v2 = ...
94+
return always_number(v1) << always_number(v2)
95+
]])
96+
local __shr = load([[
97+
local v1, v2 = ...
98+
return always_number(v1) >> always_number(v2)
99+
]])
79100

80101
_G.always_number = always_number
81102

@@ -91,6 +112,12 @@ debug.setmetatable('string', {
91112
__pow = __pow,
92113
__sub = __sub,
93114
__unm = __unm,
115+
__band = __band,
116+
__bor= __bor,
117+
__bxor = __bxor,
118+
__bnot = __bnot,
119+
__shl = __shl,
120+
__shr = __shr,
94121
})
95122
debug.setmetatable(0, {
96123
__add = __add,

tests/capi/luaL_loadbuffer_proto/serializer.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,19 @@ PROTO_TOSTRING(BinaryOperator, op)
11851185
return "and";
11861186
case BinopType::kOr:
11871187
return "or";
1188+
1189+
#if LUA_VERSION_NUM >= 503
1190+
case BinopType::kBAnd:
1191+
return "&";
1192+
case BinopType::kBOr:
1193+
return "|";
1194+
case BinopType::kBXor:
1195+
return "~";
1196+
case BinopType::kBShl:
1197+
return "<<";
1198+
case BinopType::kBShr:
1199+
return ">>";
1200+
#endif
11881201
default:
11891202
/* Works in most cases. */
11901203
return "==";
@@ -1201,6 +1214,10 @@ PROTO_TOSTRING(UnaryOperator, op)
12011214
return "not ";
12021215
case UnaryopType::kLength:
12031216
return "#";
1217+
#if LUA_VERSION_NUM >= 503
1218+
case UnaryopType::kBNot:
1219+
return "~";
1220+
#endif
12041221
default:
12051222
/* Works in most cases. */
12061223
return "not ";

0 commit comments

Comments
 (0)