summaryrefslogtreecommitdiff
path: root/lib/BLua.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/BLua.cc')
-rw-r--r--lib/BLua.cc89
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/BLua.cc b/lib/BLua.cc
new file mode 100644
index 0000000..286857e
--- /dev/null
+++ b/lib/BLua.cc
@@ -0,0 +1,89 @@
+#include "BLua.h"
+
+#ifndef LUAL_BUFFERSIZE
+#define LUAL_BUFFERSIZE BUFSIZ
+#endif
+
+class LuaStatics : public Base {
+ public:
+ static const char * getF(lua_State *, void *, size_t *);
+};
+
+std::map<lua_State *, Lua *> Lua::lualist;
+
+Lua::Lua() : L(lua_open()) {
+ // error setting ? **TODO**
+ lualist[L] = this;
+}
+
+Lua::Lua(lua_State * _L) : L(_L) {
+ // error setting ? **TODO**
+ lualist[L] = this;
+}
+
+Lua::~Lua() {
+ lua_close(L);
+}
+
+Lua::Lua(const Lua & l) throw (GeneralException) {
+ throw GeneralException("Error: can't duplicate a Lua object.");
+}
+
+void Lua::push() {
+ lua_pushnil(L);
+}
+
+void Lua::push(double n) {
+ lua_pushnumber(L, n);
+}
+
+void Lua::push(const String & s) {
+ lua_pushlstring(L, s.to_charp(), s.strlen());
+}
+
+void Lua::push(bool b) {
+ lua_pushboolean(L, b);
+}
+
+void Lua::push(void * p) {
+ lua_pushlightuserdata(L, p);
+}
+
+void Lua::push(lua_CFunction f, int n) {
+ lua_pushcclosure(L, f, n);
+}
+
+struct LoadF {
+ Handle * f;
+ char buff[LUAL_BUFFERSIZE];
+};
+
+const char * LuaStatics::getF(lua_State * L, void * ud, size_t * size) {
+ LoadF *lf = (LoadF *)ud;
+ (void)L;
+
+ *size = lf->f->read(lf->buff, LUAL_BUFFERSIZE);
+ return (*size > 0) ? lf->buff : NULL;
+}
+
+void Lua::load(Handle * h) throw (GeneralException) {
+ LoadF lf;
+ int status;
+
+ lf.f = h;
+
+ status = lua_load(L, LuaStatics::getF, &lf, h->GetName().to_charp());
+
+ if (status) {
+ // displaying error **TODO**
+ throw GeneralException("Error loading lua chunk `" + h->GetName() + "'");
+ }
+}
+
+Lua * Lua::thread() {
+ return new Lua(lua_newthread(L));
+}
+
+Lua * Lua::find(lua_State * _L) {
+ return lualist.find(_L)->second;
+}