Skip to content

Commit e1d1dde

Browse files
committed
tests/capi: support bitwise operations 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 in 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 8198e38 commit e1d1dde

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

tests/capi/luaL_loadbuffer_proto/lua_grammar.proto

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

387387
/* Arithmetic operators (5.3+). */
388388
uint32 idiv = 16;
389+
390+
/* Bitwise operators (5.3+). */
391+
uint32 bor = 17;
392+
uint32 band = 18;
393+
uint32 bxor = 19;
394+
uint32 bnot = 20;
395+
uint32 shl = 21;
396+
uint32 shr = 22;
389397
}
390398
}
391399

@@ -395,6 +403,9 @@ message UnaryOperator {
395403
uint32 negate = 1;
396404
uint32 not = 2;
397405
uint32 length = 3;
406+
407+
/* Bitwise operators (5.3+). */
408+
uint32 bnot = 4;
398409
}
399410
}
400411

tests/capi/luaL_loadbuffer_proto/preamble.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ end
7373
local __idiv = function(v1, v2)
7474
return always_number(v1) // always_number(v2)
7575
end
76+
local __band = function(v1, v2)
77+
return always_number(v1) & always_number(v2)
78+
end
79+
local __bor = function(v1, v2)
80+
return always_number(v1) | always_number(v2)
81+
end
82+
local __bxor = function(v1, v2)
83+
return always_number(v1) ~ always_number(v2)
84+
end
85+
local __bnot = function(v)
86+
return ~always_number(v)
87+
end
88+
local __shl = function(v1, v2)
89+
return always_number(v1) << always_number(v2)
90+
end
91+
local __shr = function(v1, v2)
92+
return always_number(v1) >> always_number(v2)
93+
end
7694

7795
debug.setmetatable('string', {
7896
__add = __add,
@@ -86,6 +104,12 @@ debug.setmetatable('string', {
86104
__pow = __pow,
87105
__sub = __sub,
88106
__unm = __unm,
107+
__band = __band,
108+
__bor= __bor,
109+
__bxor = __bxor,
110+
__bnot = __bnot,
111+
__shl = __shl,
112+
__shr = __shr,
89113
})
90114
debug.setmetatable(0, {
91115
__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)