Skip to content

Commit 8964864

Browse files
committed
tests/capi: support floor division operation
The PUC Rio Lua 5.3 has introduced a floor division (//) operation. It is a division that rounds the quotient towards minus infinity, that is, the floor of the division of its operands. The patch adds support of the aforementioned operation to a protobuf schema, serializer and preamble.
1 parent 0fb2c93 commit 8964864

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

tests/capi/luaL_loadbuffer_proto/lua_grammar.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ message BinaryOperator {
383383
uint32 notEqual = 13;
384384
uint32 and = 14;
385385
uint32 or = 15;
386+
387+
/* Arithmetic operators (5.3+). */
388+
uint32 idiv = 16;
386389
}
387390
}
388391

tests/capi/luaL_loadbuffer_proto/preamble.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ local not_nan_and_nil = function(val)
88
return (val ~= val or val == nil) and DEFAULT_NUMBER or val
99
end
1010

11+
_G.always_number = always_number
12+
1113
local __add = function(v1, v2)
1214
return always_number(v1) + always_number(v2)
1315
end
@@ -70,11 +72,16 @@ end
7072
local __unm = function(v)
7173
return - always_number(v)
7274
end
75+
local __idiv = load([[
76+
local v1, v2 = ...
77+
return always_number(v1) // always_number(v2)
78+
]])
7379

7480
debug.setmetatable('string', {
7581
__add = __add,
7682
__call = __call,
7783
__div = __div,
84+
__idiv = __idiv,
7885
__index = __index,
7986
__mod = __mod,
8087
__mul = __mul,
@@ -88,6 +95,7 @@ debug.setmetatable(0, {
8895
__call = __call,
8996
__concat = __concat,
9097
__div = __div,
98+
__idiv = __idiv,
9199
__index = __index,
92100
__len = __len,
93101
__newindex = __newindex,
@@ -97,6 +105,7 @@ debug.setmetatable(nil, {
97105
__call = __call,
98106
__concat = __concat,
99107
__div = __div,
108+
__idiv = __idiv,
100109
__index = __index,
101110
__le = __le,
102111
__len = __len,
@@ -112,6 +121,7 @@ debug.setmetatable(function() end, {
112121
__add = __add,
113122
__concat = __concat,
114123
__div = __div,
124+
__idiv = __idiv,
115125
__index = __index,
116126
__le = __le,
117127
__len = __len,
@@ -128,6 +138,7 @@ debug.setmetatable(true, {
128138
__call = __call,
129139
__concat = __concat,
130140
__div = __div,
141+
__idiv = __idiv,
131142
__index = __index,
132143
__le = __le,
133144
__len = __len,
@@ -144,6 +155,7 @@ local table_mt = {
144155
__call = __call,
145156
__concat = __concat,
146157
__div = __div,
158+
__idiv = __idiv,
147159
__le = __le,
148160
__len = __len,
149161
__lt = __lt,

tests/capi/luaL_loadbuffer_proto/serializer.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,10 @@ PROTO_TOSTRING(BinaryOperator, op)
11571157
return "*";
11581158
case BinopType::kDiv:
11591159
return "/";
1160+
#if LUA_VERSION_NUM >= 503
1161+
case BinopType::kIDiv:
1162+
return "//";
1163+
#endif
11601164
case BinopType::kExp:
11611165
return "^";
11621166
case BinopType::kMod:

0 commit comments

Comments
 (0)