summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/BLua.cc16
-rw-r--r--lib/LuaHandle.cc97
2 files changed, 94 insertions, 19 deletions
diff --git a/lib/BLua.cc b/lib/BLua.cc
index bdb35d2..7ee0bc4 100644
--- a/lib/BLua.cc
+++ b/lib/BLua.cc
@@ -219,12 +219,20 @@ void Lua::newtable() {
lua_newtable(L);
}
-void Lua::settable(int i) {
- lua_settable(L, i);
+void Lua::settable(int i, bool raw) {
+ if (raw) {
+ lua_rawset(L, i);
+ } else {
+ lua_settable(L, i);
+ }
}
-void Lua::gettable(int i) {
- lua_gettable(L, i);
+void Lua::gettable(int i, bool raw) {
+ if (raw) {
+ lua_rawget(L, i);
+ } else {
+ lua_gettable(L, i);
+ }
}
int Lua::gettop() {
diff --git a/lib/LuaHandle.cc b/lib/LuaHandle.cc
index f0e0706..fb0a228 100644
--- a/lib/LuaHandle.cc
+++ b/lib/LuaHandle.cc
@@ -2,16 +2,9 @@
LuaInput::LuaInput(Input * h) : LuaHandle(h) { }
LuaOutput::LuaOutput(Output * h) : LuaHandle(h) { }
+LuaBuffer::LuaBuffer(Buffer * h) : LuaHandle(h) { }
LuaHandle::LuaHandle(Handle * _h) : h(_h) { }
-void LuaInput::pushmembers(Lua * L) {
- LuaHandle::pushmembers(L);
-}
-
-void LuaOutput::pushmembers(Lua * L) {
- LuaHandle::pushmembers(L);
-}
-
class sLuaHandle : public Base {
public:
static int newinput(lua_State * L);
@@ -43,6 +36,8 @@ class sLuaHandle : public Base {
static int flush(lua_State * L);
static int seek(lua_State * L);
static int setz(lua_State * L);
+ static int bindex(lua_State * L);
+ static int bnewindex(lua_State * L);
private:
static int read(lua_State * L, int);
static int write(lua_State * L, int);
@@ -417,14 +412,14 @@ int sLuaHandle::action(lua_State * _L, int act) {
return r;
}
-int sLuaHandle::seek(lua_State * _L) {
+int sLuaHandle::setz(lua_State * _L) {
Lua * L = Lua::find(_L);
int n = L->gettop();
int z = 9;
Handle * h;
- if ((n < 2) || (n > 3) || !L->isnumber(2) || ((n == 3) && !L->isnumber(3))) {
- L->error("Incorrect arguments to method `Handle::seek'");
+ if ((n < 1) || (n > 2) || ((n == 2) && !L->isnumber(2))) {
+ L->error("Incorrect arguments to method `Handle::setz'");
}
h = (Handle *) LuaObject::getme(L);
@@ -438,15 +433,15 @@ int sLuaHandle::seek(lua_State * _L) {
return 1;
}
-int sLuaHandle::setz(lua_State * _L) {
+int sLuaHandle::seek(lua_State * _L) {
Lua * L = Lua::find(_L);
int n = L->gettop();
off_t off;
- int wheel = SEEK_SET;
+ int wheel;
Handle * h;
- if ((n < 1) || (n > 2) || ((n == 2) && !L->isnumber(2))) {
- L->error("Incorrect arguments to method `Handle::setz'");
+ if ((n < 2) || (n > 3) || !L->isnumber(2) || ((n == 3) && !L->isnumber(3))) {
+ L->error("Incorrect arguments to method `Handle::seek'");
}
h = (Handle *) LuaObject::getme(L);
@@ -455,11 +450,83 @@ int sLuaHandle::setz(lua_State * _L) {
if (n == 3) {
wheel = L->tonumber(3);
+ } else {
+ wheel = SEEK_SET;
+ }
+
+ L->push((lua_Number) h->seek(off, wheel));
+
+ return 1;
+}
+
+int sLuaHandle::bindex(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ const Buffer * b;
+
+ if (n != 2) {
+ L->error("Inconsistant call to metamethod Buffer::index");
}
+ if (!L->isnumber()) {
+ L->push();
+ return 1;
+ }
+
+ b = (Buffer *) LuaObject::getme(L);
+
+ L->push((lua_Number) (*b)[L->tonumber()]);
+
+ return 1;
+}
+
+int sLuaHandle::bnewindex(lua_State * _L) {
+ Lua * L = Lua::find(_L);
+ int n = L->gettop();
+ Buffer * b;
+
+ if (n != 3) {
+ L->error("Inconsistant call to metamethod Buffer::newindex");
+ }
+
+ if (!L->isnumber(2)) {
+ L->settable(1, true);
+ return 0;
+ }
+
+ if (!L->isnumber(3)) {
+ L->error("Can't write a non-number to buffer");
+ }
+
+ b = (Buffer *) LuaObject::getme(L);
+
+ (*b)[L->tonumber(2)] = L->tonumber(3);
+
return 0;
}
+void LuaInput::pushmembers(Lua * L) {
+ LuaHandle::pushmembers(L);
+}
+
+void LuaOutput::pushmembers(Lua * L) {
+ LuaHandle::pushmembers(L);
+}
+
+void LuaBuffer::pushmembers(Lua * L) {
+ LuaHandle::pushmembers(L);
+ if (!L->getmetatable(1)) {
+ L->newtable();
+ }
+ L->push("__index");
+ L->push(sLuaHandle::bindex);
+ L->settable();
+ L->push("__newindex");
+ L->push(sLuaHandle::bnewindex);
+ L->settable();
+ L->setmetatable(1);
+}
+
void LuaHandle::pushmembers(Lua * L) {
pushme(L, h);