summaryrefslogtreecommitdiffstats
path: root/mbbsd/bbsluaext.c
diff options
context:
space:
mode:
authorpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-01-07 14:12:13 +0800
committerpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-01-07 14:12:13 +0800
commit904c180e590f365afe8dc40fade7e887d7485133 (patch)
tree85d239d81339a8786dd6024eee0abeb5ce5b7f7a /mbbsd/bbsluaext.c
parent98a6e74fcf2ee0eadce2e706f2167dc8efdac21b (diff)
downloadpttbbs-904c180e590f365afe8dc40fade7e887d7485133.tar
pttbbs-904c180e590f365afe8dc40fade7e887d7485133.tar.gz
pttbbs-904c180e590f365afe8dc40fade7e887d7485133.tar.bz2
pttbbs-904c180e590f365afe8dc40fade7e887d7485133.tar.lz
pttbbs-904c180e590f365afe8dc40fade7e887d7485133.tar.xz
pttbbs-904c180e590f365afe8dc40fade7e887d7485133.tar.zst
pttbbs-904c180e590f365afe8dc40fade7e887d7485133.zip
- bbs: enhance title editing
- bbslua: add more APIs, fix svn prop - bbsluaext: put non-standard modules in our framework git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@3801 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'mbbsd/bbsluaext.c')
-rw-r--r--mbbsd/bbsluaext.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/mbbsd/bbsluaext.c b/mbbsd/bbsluaext.c
new file mode 100644
index 00000000..ee3521ea
--- /dev/null
+++ b/mbbsd/bbsluaext.c
@@ -0,0 +1,83 @@
+/* This file contains non-standard modules which
+ * are fundamental in BBSLua framework.
+ */
+
+/* Bitwise operations library */
+/* (c) Reuben Thomas 2000-2007 */
+/*
+ bitlib release 24
+ -----------------
+ by Reuben Thomas <rrt@sc3d.org>
+ http://luaforge.net/projects/bitlib
+
+
+bitlib is a C library for Lua 5.x that provides bitwise operations. It
+is copyright Reuben Thomas 2000-2007, and is released under the MIT
+license, like Lua (see http://www.lua.org/copyright.html; it's
+basically the same as the BSD license). There is no warranty.
+
+Please report bugs and make suggestions to the email address above, or
+use the LuaForge trackers.
+
+Thanks to John Passaniti for his bitwise operations library, some of
+whose ideas I used, to Shmuel Zeigerman for the test suite, to
+Thatcher Ulrich for portability fixes, and to John Stiles for a bug
+fix.
+*/
+
+#include <inttypes.h>
+#include <lauxlib.h>
+#include <lua.h>
+
+typedef int32_t Integer;
+typedef uint32_t UInteger;
+
+#define checkUInteger(L, n) ((UInteger)luaL_checknumber((L), (n)))
+
+#define TDYADIC(name, op, type1, type2) \
+ static int bit_ ## name(lua_State* L) { \
+ lua_pushnumber(L, (Integer)((type1)checkUInteger(L, 1) op (type2)checkUInteger(L, 2))); \
+ return 1; \
+ }
+
+#define MONADIC(name, op, type) \
+ static int bit_ ## name(lua_State* L) { \
+ lua_pushnumber(L, (Integer)(op (type)checkUInteger(L, 1))); \
+ return 1; \
+ }
+
+#define VARIADIC(name, op, type) \
+ static int bit_ ## name(lua_State *L) { \
+ int n = lua_gettop(L), i; \
+ Integer w = (type)checkUInteger(L, 1); \
+ for (i = 2; i <= n; i++) \
+ w op (type)checkUInteger(L, i); \
+ lua_pushnumber(L, (Integer)w); \
+ return 1; \
+ }
+
+MONADIC(cast, +, Integer)
+MONADIC(bnot, ~, Integer)
+VARIADIC(band, &=, Integer)
+VARIADIC(bor, |=, Integer)
+VARIADIC(bxor, ^=, Integer)
+TDYADIC(lshift, <<, Integer, UInteger)
+TDYADIC(rshift, >>, UInteger, UInteger)
+TDYADIC(arshift, >>, Integer, UInteger)
+
+static const struct luaL_reg bitlib[] = {
+ {"cast", bit_cast},
+ {"bnot", bit_bnot},
+ {"band", bit_band},
+ {"bor", bit_bor},
+ {"bxor", bit_bxor},
+ {"lshift", bit_lshift},
+ {"rshift", bit_rshift},
+ {"arshift", bit_arshift},
+ {NULL, NULL}
+};
+
+LUALIB_API int luaopen_bit (lua_State *L) {
+ luaL_openlib(L, "bit", bitlib, 0);
+ return 1;
+}