summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpixel <pixel>2003-11-29 00:48:21 +0000
committerpixel <pixel>2003-11-29 00:48:21 +0000
commitb60005df986a901ed2b91a96529a7f7d6937ce52 (patch)
treebe4aee78e498f2e3f143cfc5e2b1dc5a62fa999e
parent85cc12b6fae31e8f2d7014c5061c915c036d9369 (diff)
Fixed __gc (humf)
Added LuaBuffer
-rw-r--r--include/BLua.h4
-rw-r--r--include/LuaHandle.h9
-rw-r--r--lib/BLua.cc16
-rw-r--r--lib/LuaHandle.cc97
4 files changed, 105 insertions, 21 deletions
diff --git a/include/BLua.h b/include/BLua.h
index 19c65c4..d9b809e 100644
--- a/include/BLua.h
+++ b/include/BLua.h
@@ -28,8 +28,8 @@ class Lua : public Base {
void push(lua_CFunction, int = 0);
void pop(int = 1);
void newtable();
- void settable(int = -3);
- void gettable(int = -2);
+ void settable(int = -3, bool = false);
+ void gettable(int = -2, bool = false);
int gettop();
void error(const String &);
int type(int = -1);
diff --git a/include/LuaHandle.h b/include/LuaHandle.h
index 629b57f..f8fc1ab 100644
--- a/include/LuaHandle.h
+++ b/include/LuaHandle.h
@@ -4,6 +4,7 @@
#include <Exceptions.h>
#include <Input.h>
#include <Output.h>
+#include <Buffer.h>
#include <BLua.h>
class LuaHandle : public LuaObject {
@@ -30,4 +31,12 @@ class LuaOutput : public LuaHandle {
virtual void pushmembers(Lua *);
};
+class LuaBuffer : public LuaHandle {
+ public:
+ static void pushconstruct(Lua *);
+ LuaBuffer(Buffer *);
+ protected:
+ virtual void pushmembers(Lua *);
+};
+
#endif
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);