summaryrefslogtreecommitdiff
path: root/lib/BLua.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/BLua.cc')
-rw-r--r--lib/BLua.cc98
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/BLua.cc b/lib/BLua.cc
index c15e6d9..bdb35d2 100644
--- a/lib/BLua.cc
+++ b/lib/BLua.cc
@@ -14,6 +14,12 @@ class LuaStatics : public Base {
static int putF(lua_State *, const void *, size_t, void *);
static int luapanic(lua_State *) throw(GeneralException);
static int destructor(lua_State *);
+
+ static int andB(lua_State *);
+ static int orB(lua_State *);
+ static int xorB(lua_State *);
+ static int notB(lua_State *);
+ static int hex(lua_State *);
};
std::map<lua_State *, Lua *> Lua::lualist;
@@ -23,9 +29,101 @@ int LuaStatics::luapanic(lua_State * L) throw (GeneralException) {
throw LuaException("Error running Lua code, bailing out.");
}
+int LuaStatics::andB(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ int a, b;
+
+ if ((n != 2) && !L->isnumber(1) && !L->isnumber(2)) {
+ L->error("Incorrect arguments to function `andB'");
+ }
+
+ a = L->tonumber(1);
+ b = L->tonumber(2);
+
+ L->push((lua_Number) (a & b));
+
+ return 1;
+}
+
+int LuaStatics::orB(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ int a, b;
+
+ if ((n != 2) && !L->isnumber(1) && !L->isnumber(2)) {
+ L->error("Incorrect arguments to function `orB'");
+ }
+
+ a = L->tonumber(1);
+ b = L->tonumber(2);
+
+ L->push((lua_Number) (a | b));
+
+ return 1;
+}
+
+int LuaStatics::xorB(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ int a, b;
+
+ if ((n != 2) && !L->isnumber(1) && !L->isnumber(2)) {
+ L->error("Incorrect arguments to function `xorB'");
+ }
+
+ a = L->tonumber(1);
+ b = L->tonumber(2);
+
+ L->push((lua_Number) (a ^ b));
+
+ return 1;
+}
+
+int LuaStatics::notB(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ int x;
+
+ if ((n != 1) && !L->isnumber()) {
+ L->error("Incorrect arguments to function `notB'");
+ }
+
+ x = L->tonumber();
+
+ L->push((lua_Number) (~x));
+
+ return 1;
+}
+
+int LuaStatics::hex(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ int x;
+ String r;
+
+ if (((n != 1) || (n != 2)) && !L->isnumber(1) && ((n == 2) && !L->isstring(2))) {
+ L->error("Incorrect arguments to function `hex'");
+ }
+
+ x = L->tonumber(1);
+ String fmt = n == 2 ? L->tostring() : "%x";
+ r.set(fmt.to_charp(), x);
+
+ L->push(r);
+
+ return 1;
+}
+
Lua::Lua() : L(lua_open()) {
lua_atpanic(L, LuaStatics::luapanic);
lualist[L] = this;
+
+ declarefunc("andB", LuaStatics::andB);
+ declarefunc("orB", LuaStatics::orB);
+ declarefunc("xorB", LuaStatics::xorB);
+ declarefunc("notB", LuaStatics::notB);
+ declarefunc("hex", LuaStatics::hex);
}
Lua::Lua(lua_State * _L) : L(_L) {