Bitwise operations in Lua

pull/3226/head
Petr Mikheev 3 years ago
parent a0590d91ce
commit eca64b48e8

@ -51,7 +51,7 @@ namespace LuaUtil
LuaState::LuaState(const VFS::Manager* vfs, const ScriptsConfiguration* conf) : mConf(conf), mVFS(vfs)
{
mLua.open_libraries(sol::lib::base, sol::lib::coroutine, sol::lib::math,
mLua.open_libraries(sol::lib::base, sol::lib::coroutine, sol::lib::math, sol::lib::bit32,
sol::lib::string, sol::lib::table, sol::lib::os, sol::lib::debug);
mLua["math"]["randomseed"](static_cast<unsigned>(std::time(nullptr)));

@ -216,6 +216,37 @@ namespace LuaUtil
util["normalizeAngle"] = &Misc::normalizeAngle;
util["makeReadOnly"] = &makeReadOnly;
if (lua["bit32"] != sol::nil)
{
sol::table bit = lua["bit32"];
util["bitOr"] = bit["bor"];
util["bitAnd"] = bit["band"];
util["bitXor"] = bit["bxor"];
util["bitNot"] = bit["bnot"];
}
else
{
util["bitOr"] = [](unsigned a, sol::variadic_args va)
{
for (auto v : va)
a |= v.as<unsigned>();
return a;
};
util["bitAnd"] = [](unsigned a, sol::variadic_args va)
{
for (auto v : va)
a &= v.as<unsigned>();
return a;
};
util["bitXor"] = [](unsigned a, sol::variadic_args va)
{
for (auto v : va)
a ^= v.as<unsigned>();
return a;
};
util["bitNot"] = [](unsigned a) { return ~a; };
}
return util;
}

@ -25,6 +25,33 @@
-- @param #table table Any table.
-- @return #table The same table wrapped with read only userdata.
---
-- Bitwise And (supports any number of arguments).
-- @function [parent=#util] bitAnd
-- @param #number A First argument (integer).
-- @param #number B Second argument (integer).
-- @return #number Bitwise And of A and B.
---
-- Bitwise Or (supports any number of arguments).
-- @function [parent=#util] bitOr
-- @param #number A First argument (integer).
-- @param #number B Second argument (integer).
-- @return #number Bitwise Or of A and B.
---
-- Bitwise Xor (supports any number of arguments).
-- @function [parent=#util] bitXor
-- @param #number A First argument (integer).
-- @param #number B Second argument (integer).
-- @return #number Bitwise Xor of A and B.
---
-- Bitwise inversion.
-- @function [parent=#util] bitNot
-- @param #number A Argument (integer).
-- @return #number Bitwise Not of A.
---
-- Immutable 2D vector

Loading…
Cancel
Save