Skip to content

Support bitwise operations in luaL_loadbuffer_proto #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions extra/errors.awk
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ BEGIN { matched = 0
err_pat["'for' step is zero"] = 0
err_pat["attempt to assign to const variable"] = 0
err_pat["got a non-closable value"] = 0
err_pat["unfinished string near"] = 0
err_pat["syntax error near"] = 0
}

# String that function report_error() prints with every error message.
Expand Down
13 changes: 13 additions & 0 deletions tests/capi/luaL_loadbuffer_proto/lua_grammar.proto
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ message BinaryOperator {
uint32 notEqual = 13;
uint32 and = 14;
uint32 or = 15;

/* Arithmetic operators (5.3+). */
uint32 idiv = 16;

/* Bitwise operators (5.3+). */
uint32 band = 17;
uint32 bor = 18;
uint32 bxor = 19;
uint32 shl = 20;
uint32 shr = 21;
}
}

Expand All @@ -392,6 +402,9 @@ message UnaryOperator {
uint32 negate = 1;
uint32 not = 2;
uint32 length = 3;

/* Bitwise operators (5.3+). */
uint32 bnot = 4;
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/capi/luaL_loadbuffer_proto/preamble.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,53 @@ end
local __unm = function(v)
return - always_number(v)
end
local __idiv = function(v1, v2)
return always_number(v1) // always_number(v2)
end
local __band = function(v1, v2)
return always_number(v1) & always_number(v2)
end
local __bor = function(v1, v2)
return always_number(v1) | always_number(v2)
end
local __bxor = function(v1, v2)
return always_number(v1) ~ always_number(v2)
end
local __bnot = function(v)
return ~always_number(v)
end
local __shl = function(v1, v2)
return always_number(v1) << always_number(v2)
end
local __shr = function(v1, v2)
return always_number(v1) >> always_number(v2)
end

debug.setmetatable('string', {
__add = __add,
__call = __call,
__div = __div,
__idiv = __idiv,
__index = __index,
__mod = __mod,
__mul = __mul,
__newindex = __newindex,
__pow = __pow,
__sub = __sub,
__unm = __unm,
__band = __band,
__bor= __bor,
__bxor = __bxor,
__bnot = __bnot,
__shl = __shl,
__shr = __shr,
})
debug.setmetatable(0, {
__add = __add,
__call = __call,
__concat = __concat,
__div = __div,
__idiv = __idiv,
__index = __index,
__len = __len,
__newindex = __newindex,
Expand All @@ -97,6 +126,7 @@ debug.setmetatable(nil, {
__call = __call,
__concat = __concat,
__div = __div,
__idiv = __idiv,
__index = __index,
__le = __le,
__len = __len,
Expand All @@ -112,6 +142,7 @@ debug.setmetatable(function() end, {
__add = __add,
__concat = __concat,
__div = __div,
__idiv = __idiv,
__index = __index,
__le = __le,
__len = __len,
Expand All @@ -128,6 +159,7 @@ debug.setmetatable(true, {
__call = __call,
__concat = __concat,
__div = __div,
__idiv = __idiv,
__index = __index,
__le = __le,
__len = __len,
Expand All @@ -144,6 +176,7 @@ local table_mt = {
__call = __call,
__concat = __concat,
__div = __div,
__idiv = __idiv,
__le = __le,
__len = __len,
__lt = __lt,
Expand Down
21 changes: 21 additions & 0 deletions tests/capi/luaL_loadbuffer_proto/serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,10 @@ PROTO_TOSTRING(BinaryOperator, op)
return "*";
case BinopType::kDiv:
return "/";
#if LUA_VERSION_NUM >= 503
case BinopType::kIDiv:
return "//";
#endif
case BinopType::kExp:
return "^";
case BinopType::kMod:
Expand All @@ -1181,6 +1185,19 @@ PROTO_TOSTRING(BinaryOperator, op)
return "and";
case BinopType::kOr:
return "or";

#if LUA_VERSION_NUM >= 503
case BinopType::kBAnd:
return "&";
case BinopType::kBOr:
return "|";
case BinopType::kBXor:
return "~";
case BinopType::kBShl:
return "<<";
case BinopType::kBShr:
return ">>";
#endif
default:
/* Works in most cases. */
return "==";
Expand All @@ -1197,6 +1214,10 @@ PROTO_TOSTRING(UnaryOperator, op)
return "not ";
case UnaryopType::kLength:
return "#";
#if LUA_VERSION_NUM >= 503
case UnaryopType::kBNot:
return "~";
#endif
default:
/* Works in most cases. */
return "not ";
Expand Down