[SCM] Development fot GoFind! branch, master, updated. 719f35ffcdca1f012d846860c0abd2d10fc47b4e

Miriam Ruiz miriam at debian.org
Mon Nov 24 01:48:50 UTC 2008


The following commit has been merged in the master branch:
commit 719f35ffcdca1f012d846860c0abd2d10fc47b4e
Author: Miriam Ruiz <miriam at debian.org>
Date:   Mon Nov 24 02:51:29 2008 +0100

    Started skeleton for gui plugins coded in lua

diff --git a/Makefile b/Makefile
index 247d0b1..1fb3fe3 100644
--- a/Makefile
+++ b/Makefile
@@ -27,7 +27,7 @@ OBJS= Engine.o Environment.o filter.o field.o gofind.o \
 	taghandler.o cfgmanager.o boolparser.o \
 	utf8.o dll.o guiplugin.o pkgdata.o slre.o
 
-PLUGINS= gui_cli.so gui_fltk.so
+PLUGINS= gui_cli.so gui_fltk.so gui_lua.so
 
 all: gofind $(PLUGINS)
 
@@ -55,6 +55,12 @@ gui_fltk.so : fltk/ui.h gui_fltk.o fltk/aux.o fltk/windows.o fltk/pkgbrowser.o f
 fltk/ui.h fltk/ui.cpp: fltk/ui.fld
 	cd fltk && fluid -c -o ui.cpp -h ui.h ui.fld
 
+gui_lua.so : gui_lua.o
+	g++ $(LDFLAGS) -shared $^ -o $@ `pkg-config lua5.1 --libs` $(LIBS)
+
+gui_lua.o: gui_lua.cpp
+	g++ -o $@ -c $+ $(CFLAGS) `pkg-config lua5.1 --cflags`
+
 TEST_OBJS=  filter.test.o taghandler.test.o cfgmanager.test.o boolparser.test.o slre.test.o utf8.test.o CuTest.o dll.test.o test.o
 TEST_PLUGINS= testplugin.test.so
 
diff --git a/gofind.cpp b/gofind.cpp
index 49b3cfe..fd84eb1 100644
--- a/gofind.cpp
+++ b/gofind.cpp
@@ -90,7 +90,7 @@ int main(int argc, const char* argv[])
 	bindtextdomain ("gofind", NULL);
 #endif
 
-	GUIPlugIn gui_plugin( "./gui_fltk.so" );
+	GUIPlugIn gui_plugin( "./gui_lua.so" );
 
 	if( gui_plugin.LastError())
 	{
diff --git a/gui_lua.cpp b/gui_lua.cpp
new file mode 100644
index 0000000..b328610
--- /dev/null
+++ b/gui_lua.cpp
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2008  Miriam Ruiz <little_miry at yahoo.es>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "common.h"
+#include "filter.h"
+#include "slre.h"
+#include "pkgdata.h"
+#include "lua/luna.h"
+
+#include <typeinfo>
+#include <iostream>
+#include <string>
+
+#include <ept/apt/packagerecord.h>
+#include <ept/textsearch/textsearch.h>
+#include <wibble/regexp.h>
+#include <wibble/string.h>
+#include <xapian.h>
+
+#include <iostream>
+#include <cmath>
+
+#ifdef USE_GETTEXT
+#include <libintl.h>
+#include <locale.h>
+#else
+// Work-around until goplay is actually gettextised
+#define gettext(a) (a)
+#endif
+
+using namespace std;
+using namespace ept;
+using namespace ept::debtags;
+using namespace ept::apt;
+using namespace ept::textsearch;
+
+#include <stdlib.h>
+#include <stdio.h>
+
+extern "C" int init(int argc, const char *argv[])
+{
+	std::cout << _("Lua Plugin successfully loaded") << std::endl;
+	return 1; // true
+}
+
+extern "C" void comment(const char *text)
+{
+	std::cout << "# " << text << std::endl;
+}
+
+/* Declare the Lua libraries we wish to use. */
+/* Note: If you are opening and running a file containing Lua code */
+/* using 'lua_dofile(l, "myfile.lua") - you must delcare all the libraries */
+/* used in that file here also. */
+static const luaL_reg lualibs[] =
+{
+       	{ "base",       luaopen_base },
+       	{ NULL,         NULL }
+};
+
+/* A function to open up all the Lua libraries you declared above. */
+static void openlualibs(lua_State *l)
+{
+       	const luaL_reg *lib;
+
+	for (lib = lualibs; lib->func != NULL; lib++)
+	{
+               	lib->func(l);
+               	lua_settop(l, 0);
+       	}
+}
+
+extern "C" int go(PackageData &pkgdata)
+{
+	/* Declare a Lua State, open the Lua State and load the libraries (see above). */
+	lua_State *l;
+	l = lua_open();
+	openlualibs(l);
+
+	/* You can do what you want here. Note: Remember to update the libraries used (see above) */
+	/* if you add to your program and use new Lua libraries. */
+	/* In the lines below, I load and run the Lua code contained in the file */
+	/* "script.lua". */
+	/* Plus print some text directly from C. */
+	std::cout << "Starting Lua Script" << std::endl;
+	luaL_dofile(l, "lua/script.lua");
+	std::cout << "Finished Lua Script" << std::endl;
+
+	/* Remember to destroy the Lua State */
+	lua_close(l);
+
+	return 1;
+}
diff --git a/lua/luna.h b/lua/luna.h
new file mode 100644
index 0000000..055db11
--- /dev/null
+++ b/lua/luna.h
@@ -0,0 +1,154 @@
+#ifndef LUNA_H
+#define LUNA_H 1
+
+/*****************************************************************************
+ *     .:: Luna ::.                                                          *
+ *                                                                *   , *    *
+ *  C++ library for binding classes into Lua.     By nornagon.       ((   *  *
+ *                                                               *    `      *
+ *  Example:                                                                 *
+ *****************************************************************************
+
+    class Foo {
+      public:
+        Foo(lua_State *L) {
+          printf("in constructor\n");
+        }
+
+        int foo(lua_State *L) {
+          printf("in foo\n");
+        }
+
+        ~Foo() {
+          printf("in destructor\n");
+        }
+
+        static const char className[];
+        static const Luna<Foo>::RegType Register[];
+    };
+
+    const char Foo::className[] = "Foo";
+    const Luna<Foo>::RegType Foo::Register[] = {
+      { "foo", &Foo::foo },
+      { 0 }
+    };
+
+ *****************************************************************************
+ * Then:                                                                     *
+ *****************************************************************************
+
+    Luna<Foo>::Register(L);
+
+ *****************************************************************************
+ * From Lua:                                                                 *
+ *****************************************************************************
+
+    local foo = Foo()
+    foo:foo()
+
+ *****************************************************************************
+ * Clean up:                                                                 *
+ *****************************************************************************
+
+    lua_close(L);
+
+ *****************************************************************************
+ * Output:                                                                   *
+ *****************************************************************************
+
+    in constructor
+    in foo
+    in destructor
+
+ *****************************************************************************
+ * This program is free software. It comes without any warranty, to          *
+ * the extent permitted by applicable law. You can redistribute it           *
+ * and/or modify it under the terms of the Do What The Fuck You Want         *
+ * To Public License, Version 2, as published by Sam Hocevar. See            *
+ * http://sam.zoy.org/wtfpl/COPYING for more details.                        *
+ ****************************************************************************/
+ 
+
+// convenience macros
+#define luna_register(L, klass) (Luna<klass>::Register((L)))
+#define luna_registermetatable(L, klass) (Luna<klass>::RegisterMetatable((L)))
+#define luna_inject(L, klass, t) (Luna<klass>::inject((L), (t)))
+
+extern "C" {
+#include "lua.h"
+#include "lualib.h"
+#include "lauxlib.h"
+}
+
+template<class T> class Luna {
+  public:
+    static void Register(lua_State *L) {
+      lua_pushcfunction(L, &Luna<T>::constructor);
+      lua_setglobal(L, T::className); // T() in lua will make a new instance.
+
+      RegisterMetatable(L);
+    }
+
+    // register the metatable without registering the class constructor
+    static void RegisterMetatable(lua_State *L) {
+      luaL_newmetatable(L, T::className); // create a metatable in the registry
+      lua_pushstring(L, "__gc");
+      lua_pushcfunction(L, &Luna<T>::gc_obj);
+      lua_settable(L, -3); // metatable["__gc"] = Luna<T>::gc_obj
+      lua_pop(L, 1);
+    }
+
+    static int constructor(lua_State *L) {
+      return inject(L, new T(L));
+    }
+
+    static int inject(lua_State *L, T* obj) {
+      lua_newtable(L); // create a new table for the class object ('self')
+
+      lua_pushnumber(L, 0);
+
+      T** a = static_cast<T**>(lua_newuserdata(L, sizeof(T*))); // store a ptr to the ptr
+      *a = obj; // set the ptr to the ptr to point to the ptr... >.>
+      luaL_newmetatable(L, T::className); // get (or create) the unique metatable
+      lua_setmetatable(L, -2); // self.metatable = uniqe_metatable
+
+      lua_settable(L, -3); // self[0] = obj;
+
+      for (int i = 0; T::Register[i].name; i++) { // register the functions
+        lua_pushstring(L, T::Register[i].name);
+        lua_pushnumber(L, i); // let the thunk know which method we mean
+        lua_pushcclosure(L, &Luna<T>::thunk, 1);
+        lua_settable(L, -3); // self["function"] = thunk("function")
+      }
+      return 1;
+    }
+
+    static int thunk(lua_State *L) {
+      // redirect method call to the real thing
+      int i = (int)lua_tonumber(L, lua_upvalueindex(1)); // which function?
+      lua_pushnumber(L, 0);
+      lua_gettable(L, 1); // get the class table (i.e, self)
+
+      T** obj = static_cast<T**>(luaL_checkudata(L, -1, T::className));
+      lua_remove(L, -1); // remove the userdata from the stack
+
+      return ((*obj)->*(T::Register[i].mfunc))(L); // execute the thunk
+    }
+
+    static int gc_obj(lua_State *L) {
+      // clean up
+      //printf("GC called: %s\n", T::className);
+      T** obj = static_cast<T**>(luaL_checkudata(L, -1, T::className));
+      delete (*obj);
+      return 0;
+    }
+
+    struct RegType {
+      const char *name;
+      int(T::*mfunc)(lua_State*);
+    };
+};
+
+
+#endif /* LUNA_H */
+
diff --git a/lua/script.lua b/lua/script.lua
new file mode 100644
index 0000000..62c813a
--- /dev/null
+++ b/lua/script.lua
@@ -0,0 +1 @@
+print "Hello world"

-- 
Development fot GoFind!



More information about the Pkg-games-commits mailing list