Skip to content

Commit c410a55

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 to the test `luaL_loadbuffer_proto`. Bitwise operators introduced in Lua 5.3 breaks parsing in Lua 5.1 and 5.2, to avoid this metamethods in preamble.lua uses a function `bitwise_op`. It was implemented for bitwise tests added in the commit d4b91f6 ("tests/lapi: add bitop tests"). 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 dd276a6 commit c410a55

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ local not_nan_and_nil = function(val)
88
return (val ~= val or val == nil) and DEFAULT_NUMBER or val
99
end
1010

11+
local function bitwise_op(op_name)
12+
return function(...)
13+
local n = select('#', ...)
14+
local chunk
15+
-- Bitwise exclusive OR and bitwise NOT have the same
16+
-- operator.
17+
if (op_name == '&' or op_name == '|') then
18+
assert(n > 1)
19+
end
20+
if n == 1 then
21+
local x = ...
22+
chunk = ('return %s %d'):format(op_name, x)
23+
else
24+
local op_name_ws = (' %s '):format(op_name)
25+
chunk = 'return ' .. table.concat({...}, op_name_ws)
26+
end
27+
return assert(load(chunk))()
28+
end
29+
end
30+
1131
local __add = function(v1, v2)
1232
return always_number(v1) + always_number(v2)
1333
end
@@ -76,6 +96,30 @@ local __idiv = function(v1, v2)
7696
local chunk = ('%d // %d'):format(always_number(v1), always_number(v2))
7797
return assert(loadstring(chunk))()
7898
end
99+
local __band = function(v1, v2)
100+
local band = bitwise_op('&')
101+
return band(always_number(v1), always_number(v2))
102+
end
103+
local __bor = function(v1, v2)
104+
local bor = bitwise_op('|')
105+
return bor(always_number(v1), always_number(v2))
106+
end
107+
local __bxor = function(v1, v2)
108+
local bxor = bitwise_op('~')
109+
return bxor(always_number(v1), always_number(v2))
110+
end
111+
local __bnot = function(v)
112+
local bnot = bitwise_op('~')
113+
return bnot(always_number(v))
114+
end
115+
local __shl = function(v1, v2)
116+
local shl = bitwise_op('<<')
117+
return shl(always_number(v1), always_number(v2))
118+
end
119+
local __shr = function(v1, v2)
120+
local shr = bitwise_op('>>')
121+
return shr(always_number(v1), always_number(v2))
122+
end
79123

80124
debug.setmetatable('string', {
81125
__add = __add,
@@ -89,6 +133,12 @@ debug.setmetatable('string', {
89133
__pow = __pow,
90134
__sub = __sub,
91135
__unm = __unm,
136+
__band = __band,
137+
__bor= __bor,
138+
__bxor = __bxor,
139+
__bnot = __bnot,
140+
__shl = __shl,
141+
__shr = __shr,
92142
})
93143
debug.setmetatable(0, {
94144
__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)