summaryrefslogtreecommitdiff
path: root/tolua/src/lib
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2014-10-09 08:59:02 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2014-10-09 08:59:02 +0200
commit7d8b1ff684b412da292e0fc734748975188a0f10 (patch)
tree2673e3da51cc80bfc38a426048b30a4d71c31d4c /tolua/src/lib
parent62c5bb90525baf0d82c23892c2666f611750d63c (diff)
downloadcrawler-7d8b1ff684b412da292e0fc734748975188a0f10.tar.gz
crawler-7d8b1ff684b412da292e0fc734748975188a0f10.tar.bz2
first trials with a Google normalizer called from Lua, std::string is the problem currently
and the missing wrapper for the URL class also added a local 'tolua', we will have to hack it
Diffstat (limited to 'tolua/src/lib')
-rw-r--r--tolua/src/lib/.DS_Storebin0 -> 6148 bytes
-rw-r--r--tolua/src/lib/._.DS_Storebin0 -> 82 bytes
-rw-r--r--tolua/src/lib/._tolua_event.cbin0 -> 225 bytes
-rw-r--r--tolua/src/lib/._tolua_map.cbin0 -> 225 bytes
-rw-r--r--tolua/src/lib/Makefile27
-rw-r--r--tolua/src/lib/tolua_event.c449
-rw-r--r--tolua/src/lib/tolua_event.h24
-rwxr-xr-xtolua/src/lib/tolua_is.c567
-rw-r--r--tolua/src/lib/tolua_map.c529
-rwxr-xr-xtolua/src/lib/tolua_push.c144
-rwxr-xr-xtolua/src/lib/tolua_to.c125
11 files changed, 1865 insertions, 0 deletions
diff --git a/tolua/src/lib/.DS_Store b/tolua/src/lib/.DS_Store
new file mode 100644
index 0000000..5008ddf
--- /dev/null
+++ b/tolua/src/lib/.DS_Store
Binary files differ
diff --git a/tolua/src/lib/._.DS_Store b/tolua/src/lib/._.DS_Store
new file mode 100644
index 0000000..321346b
--- /dev/null
+++ b/tolua/src/lib/._.DS_Store
Binary files differ
diff --git a/tolua/src/lib/._tolua_event.c b/tolua/src/lib/._tolua_event.c
new file mode 100644
index 0000000..4f881ff
--- /dev/null
+++ b/tolua/src/lib/._tolua_event.c
Binary files differ
diff --git a/tolua/src/lib/._tolua_map.c b/tolua/src/lib/._tolua_map.c
new file mode 100644
index 0000000..4f881ff
--- /dev/null
+++ b/tolua/src/lib/._tolua_map.c
Binary files differ
diff --git a/tolua/src/lib/Makefile b/tolua/src/lib/Makefile
new file mode 100644
index 0000000..b2091f3
--- /dev/null
+++ b/tolua/src/lib/Makefile
@@ -0,0 +1,27 @@
+# makefile for tolua library
+
+TOLUA=../..
+
+include $(TOLUA)/config
+
+OBJS= \
+ tolua_event.o \
+ tolua_is.o \
+ tolua_map.o \
+ tolua_push.o \
+ tolua_to.o
+
+T= $(TOLUA)/lib/libtolua.a
+
+all: $T
+
+$T: $(OBJS)
+ $(AR) $@ $(OBJS)
+ $(RANLIB) $@
+
+clean:
+ rm -f $(OBJS)
+
+klean:
+ rm -f $T
+
diff --git a/tolua/src/lib/tolua_event.c b/tolua/src/lib/tolua_event.c
new file mode 100644
index 0000000..40f20ff
--- /dev/null
+++ b/tolua/src/lib/tolua_event.c
@@ -0,0 +1,449 @@
+/* tolua: event functions
+** Support code for Lua bindings.
+** Written by Waldemar Celes
+** TeCGraf/PUC-Rio
+** Apr 2003
+** $Id: tolua_event.c,v 1.7 2011/01/13 13:43:46 fabraham Exp $
+*/
+
+/* This code is free software; you can redistribute it and/or modify it.
+** The software provided hereunder is on an "as is" basis, and
+** the author has no obligation to provide maintenance, support, updates,
+** enhancements, or modifications.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "tolua.h"
+
+/* Store at peer
+ * It stores, creating the corresponding table if needed,
+ * the pair key/value in the corresponding peer table
+*/
+static void storeatpeer (lua_State* L, int index)
+{
+ /* stack: key value (to be stored) */
+ lua_pushstring(L,"tolua_peer");
+ lua_rawget(L,LUA_REGISTRYINDEX); /* stack: k v peer */
+ lua_pushvalue(L, index);
+ lua_rawget(L,-2); /* stack: k v peer peer[u] */
+ if (!lua_istable(L,-1))
+ {
+ lua_pop(L,1); /* stack: k v peer */
+ lua_newtable(L); /* stack: k v peer table */
+ lua_pushvalue(L,index);
+ lua_pushvalue(L,-2); /* stack: k v peer table u table */
+ lua_rawset(L,-4); /* stack: k v peer peer[u]=table */
+ }
+ lua_insert(L,-4); /* put table before k */
+ lua_pop(L,1); /* pop peer */
+ lua_rawset(L,-3); /* store at table */
+ lua_pop(L,1); /* pop peer[u] */
+}
+
+/* Module index function
+*/
+static int module_index_event (lua_State* L)
+{
+ lua_pushstring(L,".get");
+ lua_rawget(L,-3);
+ if (lua_istable(L,-1))
+ {
+ lua_pushvalue(L,2); /* key */
+ lua_rawget(L,-2);
+ if (lua_iscfunction(L,-1))
+ {
+ lua_call(L,0,1);
+ return 1;
+ }
+ else if (lua_istable(L,-1))
+ return 1;
+ }
+ /* call old index meta event */
+ if (lua_getmetatable(L,1))
+ {
+ lua_pushstring(L,"__index");
+ lua_rawget(L,-2);
+ lua_pushvalue(L,1);
+ lua_pushvalue(L,2);
+ if (lua_isfunction(L,-1))
+ {
+ lua_call(L,2,1);
+ return 1;
+ }
+ else if (lua_istable(L,-1))
+ {
+ lua_gettable(L,-3);
+ return 1;
+ }
+ }
+ lua_pushnil(L);
+ return 1;
+}
+
+/* Module newindex function
+*/
+static int module_newindex_event (lua_State* L)
+{
+ lua_pushstring(L,".set");
+ lua_rawget(L,-4);
+ if (lua_istable(L,-1))
+ {
+ lua_pushvalue(L,2); /* key */
+ lua_rawget(L,-2);
+ if (lua_iscfunction(L,-1))
+ {
+ lua_pushvalue(L,1); /* only to be compatible with non-static vars */
+ lua_pushvalue(L,3); /* value */
+ lua_call(L,2,0);
+ return 0;
+ }
+ }
+ /* call old newindex meta event */
+ if (lua_getmetatable(L,1) && lua_getmetatable(L,-1))
+ {
+ lua_pushstring(L,"__newindex");
+ lua_rawget(L,-2);
+ if (lua_isfunction(L,-1))
+ {
+ lua_pushvalue(L,1);
+ lua_pushvalue(L,2);
+ lua_pushvalue(L,3);
+ lua_call(L,3,0);
+ }
+ }
+ lua_settop(L,3);
+ lua_rawset(L,-3);
+ return 0;
+}
+
+/* Class index function
+ * If the object is a userdata (ie, an object), it searches the field in
+ * the alternative table stored in the corresponding "peer" table.
+*/
+static int class_index_event (lua_State* L)
+{
+ int t = lua_type(L,1);
+ if (t == LUA_TUSERDATA)
+ {
+ /* Access alternative table */
+ lua_pushstring(L,"tolua_peer");
+ lua_rawget(L,LUA_REGISTRYINDEX); /* stack: obj key peer */
+ lua_pushvalue(L,1);
+ lua_rawget(L,-2); /* stack: obj key peer peer[u] */
+ if (lua_istable(L,-1))
+ {
+ lua_pushvalue(L,2); /* key */
+ lua_rawget(L,-2); /* stack: obj key peer peer[u] value */
+ if (!lua_isnil(L,-1))
+ return 1;
+ }
+ lua_settop(L,2); /* stack: obj key */
+ /* Try metatables */
+ lua_pushvalue(L,1); /* stack: obj key obj */
+ while (lua_getmetatable(L,-1))
+ { /* stack: obj key obj mt */
+ lua_remove(L,-2); /* stack: obj key mt */
+ if (lua_isnumber(L,2)) /* check if key is a numeric value */
+ {
+ /* try operator[] */
+ lua_pushstring(L,".geti");
+ lua_rawget(L,-2); /* stack: obj key mt func */
+ if (lua_isfunction(L,-1))
+ {
+ lua_pushvalue(L,1);
+ lua_pushvalue(L,2);
+ lua_call(L,2,1);
+ return 1;
+ }
+ }
+ else
+ {
+ lua_pushvalue(L,2); /* stack: obj key mt key */
+ lua_rawget(L,-2); /* stack: obj key mt value */
+ if (!lua_isnil(L,-1))
+ return 1;
+ else
+ lua_pop(L,1);
+ /* try C/C++ variable */
+ lua_pushstring(L,".get");
+ lua_rawget(L,-2); /* stack: obj key mt tget */
+ if (lua_istable(L,-1))
+ {
+ lua_pushvalue(L,2);
+ lua_rawget(L,-2); /* stack: obj key mt value */
+ if (lua_iscfunction(L,-1))
+ {
+ lua_pushvalue(L,1);
+ lua_pushvalue(L,2);
+ lua_call(L,2,1);
+ return 1;
+ }
+ else if (lua_istable(L,-1))
+ {
+ /* deal with array: create table to be returned and cache it in peer */
+ void* u = *((void**)lua_touserdata(L,1));
+ lua_newtable(L); /* stack: obj key mt value table */
+ lua_pushstring(L,".self");
+ lua_pushlightuserdata(L,u);
+ lua_rawset(L,-3); /* store usertype in ".self" */
+ lua_insert(L,-2); /* stack: obj key mt table value */
+ lua_setmetatable(L,-2); /* set stored value as metatable */
+ lua_pushvalue(L,-1); /* stack: obj key met table table */
+ lua_pushvalue(L,2); /* stack: obj key mt table table key */
+ lua_insert(L,-2); /* stack: obj key mt table key table */
+ storeatpeer(L,1); /* stack: obj key mt table */
+ return 1;
+ }
+ }
+ }
+ lua_settop(L,3);
+ }
+ lua_pushnil(L);
+ return 1;
+ }
+ else if (t== LUA_TTABLE)
+ {
+ module_index_event(L);
+ return 1;
+ }
+ lua_pushnil(L);
+ return 1;
+}
+
+/* Newindex function
+ * It first searches for a C/C++ varaible to be set.
+ * Then, it either stores it in the alternative peer table (in the case it is
+ * an object) or in the own table (that represents the class or module).
+*/
+static int class_newindex_event (lua_State* L)
+{
+ int t = lua_type(L,1);
+ if (t == LUA_TUSERDATA)
+ {
+ /* Try accessing a C/C++ variable to be set */
+ lua_getmetatable(L,1);
+ while (lua_istable(L,-1)) /* stack: t k v mt */
+ {
+ if (lua_isnumber(L,2)) /* check if key is a numeric value */
+ {
+ /* try operator[] */
+ lua_pushstring(L,".seti");
+ lua_rawget(L,-2); /* stack: obj key mt func */
+ if (lua_isfunction(L,-1))
+ {
+ lua_pushvalue(L,1);
+ lua_pushvalue(L,2);
+ lua_pushvalue(L,3);
+ lua_call(L,3,0);
+ return 0;
+ }
+ }
+ else
+ {
+ lua_pushstring(L,".set");
+ lua_rawget(L,-2); /* stack: t k v mt tset */
+ if (lua_istable(L,-1))
+ {
+ lua_pushvalue(L,2);
+ lua_rawget(L,-2); /* stack: t k v mt tset func */
+ if (lua_iscfunction(L,-1))
+ {
+ lua_pushvalue(L,1);
+ lua_pushvalue(L,3);
+ lua_call(L,2,0);
+ return 0;
+ }
+ lua_pop(L,1); /* stack: t k v mt tset */
+ }
+ lua_pop(L,1); /* stack: t k v mt */
+ if (!lua_getmetatable(L,-1)) /* stack: t k v mt mt */
+ lua_pushnil(L);
+ lua_remove(L,-2); /* stack: t k v mt */
+ }
+ }
+ lua_settop(L,3); /* stack: t k v */
+
+ /* then, store as a new field */
+ storeatpeer(L,1);
+ }
+ else if (t== LUA_TTABLE)
+ {
+ module_newindex_event(L);
+ }
+ return 0;
+}
+
+static int do_operator (lua_State* L, const char* op)
+{
+ if (lua_isuserdata(L,1))
+ {
+ /* Try metatables */
+ lua_pushvalue(L,1); /* stack: op1 op2 */
+ while (lua_getmetatable(L,-1))
+ { /* stack: op1 op2 op1 mt */
+ lua_remove(L,-2); /* stack: op1 op2 mt */
+ lua_pushstring(L,op); /* stack: op1 op2 mt key */
+ lua_rawget(L,-2); /* stack: obj key mt func */
+ if (lua_isfunction(L,-1))
+ {
+ lua_pushvalue(L,1);
+ lua_pushvalue(L,2);
+ lua_call(L,2,1);
+ return 1;
+ }
+ lua_settop(L,3);
+ }
+ }
+ if (strcmp(op,".eq")==0)
+ {
+ lua_pushboolean(L,lua_rawequal(L,1,2));
+ return 1;
+ }
+ else
+ {
+ tolua_error(L,"Attempt to perform operation on an invalid operand",NULL);
+ return 0;
+ }
+}
+
+static int class_add_event (lua_State* L)
+{
+ return do_operator(L,".add");
+}
+
+static int class_sub_event (lua_State* L)
+{
+ return do_operator(L,".sub");
+}
+
+static int class_mul_event (lua_State* L)
+{
+ return do_operator(L,".mul");
+}
+
+static int class_div_event (lua_State* L)
+{
+ return do_operator(L,".div");
+}
+
+static int class_lt_event (lua_State* L)
+{
+ return do_operator(L,".lt");
+}
+
+static int class_le_event (lua_State* L)
+{
+ return do_operator(L,".le");
+}
+
+static int class_eq_event (lua_State* L)
+{
+ return do_operator(L,".eq");
+}
+
+static int class_gc_event (lua_State* L)
+{
+ if (lua_type(L,1) == LUA_TUSERDATA)
+ {
+ int top = lua_gettop(L);
+ void* u = *((void**)lua_touserdata(L,1));
+ lua_pushstring(L,"tolua_gc");
+ lua_rawget(L,LUA_REGISTRYINDEX); /* gc */
+ lua_pushlightuserdata(L,u); /* gc u */
+ lua_rawget(L,-2); /* gc func */
+ if (!lua_isnil(L, -1))
+ {
+ /* remove entry from table */
+ lua_pushlightuserdata(L,u);
+ lua_pushnil(L);
+ lua_rawset(L,-4);
+ if (lua_isfunction(L,-1)) {
+ /* call collect function */
+ lua_pushvalue(L,1); /* tolua_gc tolua_gc.u(func) u */
+ lua_call(L,1,0); /* tolua_gc */
+ }
+ else if (lua_isuserdata(L,-1) && *((void**)lua_touserdata(L,-1))==NULL) {
+ /* free object */
+ free(u);
+ tolua_release(L,u); /* unmap from tolua tables */
+ }
+ }
+ lua_settop(L,top);
+ }
+ return 0;
+}
+
+
+
+
+/* Register module events
+ * It expects the metatable on the top of the stack
+*/
+TOLUA_API void tolua_moduleevents (lua_State* L)
+{
+ lua_pushstring(L,"__index");
+ lua_pushcfunction(L,module_index_event);
+ lua_rawset(L,-3);
+ lua_pushstring(L,"__newindex");
+ lua_pushcfunction(L,module_newindex_event);
+ lua_rawset(L,-3);
+}
+
+/* Check if the object on the top has a module metatable
+*/
+TOLUA_API int tolua_ismodulemetatable (lua_State* L)
+{
+ int r = 0;
+ if (lua_getmetatable(L,-1))
+ {
+ lua_pushstring(L,"__index");
+ lua_rawget(L,-2);
+ r = (lua_tocfunction(L,-1) == module_index_event);
+ lua_pop(L,2);
+ }
+ return r;
+}
+
+/* Register class events
+ * It expects the metatable on the top of the stack
+*/
+TOLUA_API void tolua_classevents (lua_State* L)
+{
+ lua_pushstring(L,"__index");
+ lua_pushcfunction(L,class_index_event);
+ lua_rawset(L,-3);
+ lua_pushstring(L,"__newindex");
+ lua_pushcfunction(L,class_newindex_event);
+ lua_rawset(L,-3);
+
+ lua_pushstring(L,"__add");
+ lua_pushcfunction(L,class_add_event);
+ lua_rawset(L,-3);
+ lua_pushstring(L,"__sub");
+ lua_pushcfunction(L,class_sub_event);
+ lua_rawset(L,-3);
+ lua_pushstring(L,"__mul");
+ lua_pushcfunction(L,class_mul_event);
+ lua_rawset(L,-3);
+ lua_pushstring(L,"__div");
+ lua_pushcfunction(L,class_div_event);
+ lua_rawset(L,-3);
+
+ lua_pushstring(L,"__lt");
+ lua_pushcfunction(L,class_lt_event);
+ lua_rawset(L,-3);
+ lua_pushstring(L,"__le");
+ lua_pushcfunction(L,class_le_event);
+ lua_rawset(L,-3);
+ lua_pushstring(L,"__eq");
+ lua_pushcfunction(L,class_eq_event);
+ lua_rawset(L,-3);
+
+ lua_pushstring(L,"__gc");
+ lua_pushcfunction(L,class_gc_event);
+ lua_rawset(L,-3);
+}
+
diff --git a/tolua/src/lib/tolua_event.h b/tolua/src/lib/tolua_event.h
new file mode 100644
index 0000000..a393979
--- /dev/null
+++ b/tolua/src/lib/tolua_event.h
@@ -0,0 +1,24 @@
+/* tolua: event functions
+** Support code for Lua bindings.
+** Written by Waldemar Celes
+** TeCGraf/PUC-Rio
+** Apr 2003
+** $Id: tolua_event.h,v 1.3 2009/11/24 16:45:15 fabraham Exp $
+*/
+
+/* This code is free software; you can redistribute it and/or modify it.
+** The software provided hereunder is on an "as is" basis, and
+** the author has no obligation to provide maintenance, support, updates,
+** enhancements, or modifications.
+*/
+
+#ifndef TOLUA_EVENT_H
+#define TOLUA_EVENT_H
+
+#include "tolua.h"
+
+TOLUA_API void tolua_moduleevents (lua_State* L);
+TOLUA_API int tolua_ismodulemetatable (lua_State* L);
+TOLUA_API void tolua_classevents (lua_State* L);
+
+#endif
diff --git a/tolua/src/lib/tolua_is.c b/tolua/src/lib/tolua_is.c
new file mode 100755
index 0000000..c28e01a
--- /dev/null
+++ b/tolua/src/lib/tolua_is.c
@@ -0,0 +1,567 @@
+/* tolua: functions to check types.
+ ** Support code for Lua bindings.
+ ** Written by Waldemar Celes
+ ** TeCGraf/PUC-Rio
+ ** Apr 2003
+ ** $Id: tolua_is.c,v 1.5 2009/11/24 16:45:15 fabraham Exp $
+ */
+
+/* This code is free software; you can redistribute it and/or modify it.
+ ** The software provided hereunder is on an "as is" basis, and
+ ** the author has no obligation to provide maintenance, support, updates,
+ ** enhancements, or modifications.
+ */
+
+#include "tolua.h"
+#include "lauxlib.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+/* Push and returns the corresponding object typename */
+TOLUA_API const char* tolua_typename (lua_State* L, int lo)
+{
+ int tag = lua_type(L,lo);
+ if (tag == LUA_TNONE)
+ lua_pushstring(L,"[no object]");
+ else if (tag != LUA_TUSERDATA && tag != LUA_TTABLE)
+ lua_pushstring(L,lua_typename(L,tag));
+ else if (tag == LUA_TUSERDATA)
+ {
+ if (!lua_getmetatable(L,lo)) {
+ lua_pushstring(L,lua_typename(L,tag));
+ }
+ else
+ {
+ lua_rawget(L,LUA_REGISTRYINDEX);
+ if (!lua_isstring(L,-1))
+ {
+ lua_pop(L,1);
+ lua_pushstring(L,"[undefined]");
+ }
+ }
+ }
+ else /* is table */
+ {
+ lua_pushvalue(L,lo);
+ lua_rawget(L,LUA_REGISTRYINDEX);
+ if (!lua_isstring(L,-1))
+ {
+ lua_pop(L,1);
+ lua_pushstring(L,"table");
+ }
+ else
+ {
+ lua_pushstring(L,"class ");
+ lua_insert(L,-2);
+ lua_concat(L,2);
+ }
+ }
+ return lua_tostring(L,-1);
+}
+
+TOLUA_API void tolua_error (lua_State* L, const char* msg, tolua_Error* err)
+{
+ if (msg[0] == '#')
+ {
+ const char* expected = err->type;
+ const char* provided = tolua_typename(L,err->index);
+ if (msg[1]=='f')
+ {
+ int narg = err->index;
+ if (err->array)
+ luaL_error(L,"%s\n argument #%d is array of '%s'; array of '%s' expected.\n",
+ msg+2,narg,provided,expected);
+ else
+ luaL_error(L,"%s\n argument #%d is '%s'; '%s' expected.\n",
+ msg+2,narg,provided,expected);
+ }
+ else if (msg[1]=='v')
+ {
+ if (err->array)
+ luaL_error(L,"%s\n value is array of '%s'; array of '%s' expected.\n",
+ msg+2,provided,expected);
+ else
+ luaL_error(L,"%s\n value is '%s'; '%s' expected.\n",
+ msg+2,provided,expected);
+ }
+ }
+ else
+ luaL_error(L,msg);
+}
+
+/* the equivalent of lua_is* for usertable */
+static int lua_isusertable (lua_State* L, int lo, const char* type)
+{
+ int r = 0;
+ if (lo < 0) lo = lua_gettop(L)+lo+1;
+ lua_pushvalue(L,lo);
+ lua_rawget(L,LUA_REGISTRYINDEX); /* get registry[t] */
+ if (lua_isstring(L,-1))
+ {
+ r = strcmp(lua_tostring(L,-1),type)==0;
+ if (!r)
+ {
+ /* try const */
+ lua_pushstring(L,"const ");
+ lua_insert(L,-2);
+ lua_concat(L,2);
+ r = lua_isstring(L,-1) && strcmp(lua_tostring(L,-1),type)==0;
+ }
+ }
+ lua_pop(L, 1);
+ return r;
+}
+
+/* the equivalent of lua_is* for usertype */
+static int lua_isusertype (lua_State* L, int lo, const char* type)
+{
+ if (lua_isuserdata(L,lo))
+ {
+ /* check if it is of the same type */
+ int r;
+ const char *tn;
+ if (lua_getmetatable(L,lo)) /* if metatable? */
+ {
+ lua_rawget(L,LUA_REGISTRYINDEX); /* get registry[mt] */
+ tn = lua_tostring(L,-1);
+ r = tn && (strcmp(tn,type) == 0);
+ lua_pop(L, 1);
+ if (r)
+ return 1;
+ else
+ {
+ /* check if it is a specialized class */
+ lua_pushstring(L,"tolua_super");
+ lua_rawget(L,LUA_REGISTRYINDEX); /* get super */
+ lua_getmetatable(L,lo);
+ lua_rawget(L,-2); /* get super[mt] */
+ if (lua_istable(L,-1))
+ {
+ int b;
+ lua_pushstring(L,type);
+ lua_rawget(L,-2); /* get super[mt][type] */
+ b = lua_toboolean(L,-1);
+ lua_pop(L,3);
+ if (b)
+ return 1;
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+TOLUA_API int tolua_isnoobj (lua_State* L, int lo, tolua_Error* err)
+{
+ if (lua_gettop(L)<abs(lo))
+ return 1;
+ err->index = lo;
+ err->array = 0;
+ err->type = NULL;
+ return 0;
+}
+TOLUA_API int tolua_isvalue (lua_State* L, int lo, int def, tolua_Error* err)
+{
+ if (def || abs(lo)<=lua_gettop(L)) /* any valid index */
+ return 1;
+ err->index = lo;
+ err->array = 0;
+ err->type = "value";
+ return 0;
+}
+
+TOLUA_API int tolua_isboolean (lua_State* L, int lo, int def, tolua_Error* err)
+{
+ if (def && lua_gettop(L)<abs(lo))
+ return 1;
+ if (lua_isnil(L,lo) || lua_isboolean(L,lo))
+ return 1;
+ err->index = lo;
+ err->array = 0;
+ err->type = "boolean";
+ return 0;
+}
+
+TOLUA_API int tolua_isnumber (lua_State* L, int lo, int def, tolua_Error* err)
+{
+ if (def && lua_gettop(L)<abs(lo))
+ return 1;
+ if (lua_isnumber(L,lo))
+ return 1;
+ err->index = lo;
+ err->array = 0;
+ err->type = "number";
+ return 0;
+}
+
+TOLUA_API int tolua_isstring (lua_State* L, int lo, int def, tolua_Error* err)
+{
+ if (def && lua_gettop(L)<abs(lo))
+ return 1;
+ if (lua_isnil(L,lo) || lua_isstring(L,lo))
+ return 1;
+ err->index = lo;
+ err->array = 0;
+ err->type = "string";
+ return 0;
+}
+
+TOLUA_API int tolua_istable (lua_State* L, int lo, int def, tolua_Error* err)
+{
+ if (def && lua_gettop(L)<abs(lo))
+ return 1;
+ if (lua_istable(L,lo))
+ return 1;
+ err->index = lo;
+ err->array = 0;
+ err->type = "table";
+ return 0;
+}
+
+TOLUA_API int tolua_isusertable (lua_State* L, int lo, const char* type, int def, tolua_Error* err)
+{
+ if (def && lua_gettop(L)<abs(lo))
+ return 1;
+ if (lua_isusertable(L,lo,type))
+ return 1;
+ err->index = lo;
+ err->array = 0;
+ err->type = type;
+ return 0;
+}
+
+TOLUA_API int tolua_isfunction (lua_State* L, int lo, int def, tolua_Error* err)
+{
+ if (def && lua_gettop(L)<abs(lo))
+ return 1;
+ if (lua_isfunction(L,lo))
+ return 1;
+ err->index = lo;
+ err->array = 0;
+ err->type = "function";
+ return 0;
+}
+
+TOLUA_API int tolua_isuserdata (lua_State* L, int lo, int def, tolua_Error* err)
+{
+ if (def && lua_gettop(L)<abs(lo))
+ return 1;
+ if (lua_isnil(L,lo) || lua_isuserdata(L,lo))
+ return 1;
+ err->index = lo;
+ err->array = 0;
+ err->type = "userdata";
+ return 0;
+}
+
+TOLUA_API int tolua_isusertype (lua_State* L, int lo, const char* type, int def, tolua_Error* err)
+{
+ if (def && lua_gettop(L)<abs(lo))
+ return 1;
+ if (lua_isnil(L,lo) || lua_isusertype(L,lo,type))
+ return 1;
+ err->index = lo;
+ err->array = 0;
+ err->type = type;
+ return 0;
+}
+
+TOLUA_API int tolua_isvaluearray
+(lua_State* L, int lo, int dim, int def, tolua_Error* err)
+{
+ if (!tolua_istable(L,lo,def,err))
+ return 0;
+ else
+ return 1;
+}
+
+TOLUA_API int tolua_isbooleanarray
+(lua_State* L, int lo, int dim, int def, tolua_Error* err)
+{
+ if (!tolua_istable(L,lo,def,err))
+ return 0;
+ else
+ {
+ int i;
+ for (i=1; i<=dim; ++i)
+ {
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (!(lua_isnil(L,-1) || lua_isboolean(L,-1)) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->array = 1;
+ err->type = "boolean";
+ return 0;
+ }
+ lua_pop(L,1);
+ }
+ }
+ return 1;
+}
+
+TOLUA_API int tolua_isnumberarray
+(lua_State* L, int lo, int dim, int def, tolua_Error* err)
+{
+ if (!tolua_istable(L,lo,def,err))
+ return 0;
+ else
+ {
+ int i;
+ for (i=1; i<=dim; ++i)
+ {
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (!lua_isnumber(L,-1) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->array = 1;
+ err->type = "number";
+ return 0;
+ }
+ lua_pop(L,1);
+ }
+ }
+ return 1;
+}
+
+TOLUA_API int tolua_isstringarray
+(lua_State* L, int lo, int dim, int def, tolua_Error* err)
+{
+ if (!tolua_istable(L,lo,def,err))
+ return 0;
+ else
+ {
+ int i;
+ for (i=1; i<=dim; ++i)
+ {
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (!(lua_isnil(L,-1) || lua_isstring(L,-1)) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->array = 1;
+ err->type = "string";
+ return 0;
+ }
+ lua_pop(L,1);
+ }
+ }
+ return 1;
+}
+
+TOLUA_API int tolua_istablearray
+(lua_State* L, int lo, int dim, int def, tolua_Error* err)
+{
+ if (!tolua_istable(L,lo,def,err))
+ return 0;
+ else
+ {
+ int i;
+ for (i=1; i<=dim; ++i)
+ {
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (! lua_istable(L,-1) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->array = 1;
+ err->type = "table";
+ return 0;
+ }
+ lua_pop(L,1);
+ }
+ }
+ return 1;
+}
+
+TOLUA_API int tolua_isuserdataarray
+(lua_State* L, int lo, int dim, int def, tolua_Error* err)
+{
+ if (!tolua_istable(L,lo,def,err))
+ return 0;
+ else
+ {
+ int i;
+ for (i=1; i<=dim; ++i)
+ {
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->array = 1;
+ err->type = "userdata";
+ return 0;
+ }
+ lua_pop(L,1);
+ }
+ }
+ return 1;
+}
+
+TOLUA_API int tolua_isusertypearray
+(lua_State* L, int lo, const char* type, int dim, int def, tolua_Error* err)
+{
+ if (!tolua_istable(L,lo,def,err))
+ return 0;
+ else
+ {
+ int i;
+ for (i=1; i<=dim; ++i)
+ {
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->type = type;
+ err->array = 1;
+ return 0;
+ }
+ lua_pop(L,1);
+ }
+ }
+ return 1;
+}
+
+#if 0
+int tolua_isbooleanfield
+(lua_State* L, int lo, int i, int def, tolua_Error* err)
+{
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (!(lua_isnil(L,-1) || lua_isboolean(L,-1)) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->array = 1;
+ err->type = "boolean";
+ return 0;
+ }
+ lua_pop(L,1);
+ return 1;
+}
+
+int tolua_isnumberfield
+(lua_State* L, int lo, int i, int def, tolua_Error* err)
+{
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (!lua_isnumber(L,-1) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->array = 1;
+ err->type = "number";
+ return 0;
+ }
+ lua_pop(L,1);
+ return 1;
+}
+
+int tolua_isstringfield
+(lua_State* L, int lo, int i, int def, tolua_Error* err)
+{
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (!(lua_isnil(L,-1) || lua_isstring(L,-1)) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->array = 1;
+ err->type = "string";
+ return 0;
+ }
+ lua_pop(L,1);
+ return 1;
+}
+
+int tolua_istablefield
+(lua_State* L, int lo, int i, int def, tolua_Error* err)
+{
+ lua_pushnumber(L,i+1);
+ lua_gettable(L,lo);
+ if (! lua_istable(L,-1) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->array = 1;
+ err->type = "table";
+ return 0;
+ }
+ lua_pop(L,1);
+}
+
+int tolua_isusertablefield
+(lua_State* L, int lo, const char* type, int i, int def, tolua_Error* err)
+{
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (! lua_isusertable(L,-1,type) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->array = 1;
+ err->type = type;
+ return 0;
+ }
+ lua_pop(L,1);
+ return 1;
+}
+
+int tolua_isuserdatafield
+(lua_State* L, int lo, int i, int def, tolua_Error* err)
+{
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->array = 1;
+ err->type = "userdata";
+ return 0;
+ }
+ lua_pop(L,1);
+ return 1;
+}
+
+int tolua_isusertypefield
+(lua_State* L, int lo, const char* type, int i, int def, tolua_Error* err)
+{
+ lua_pushnumber(L,i);
+ lua_gettable(L,lo);
+ if (!(lua_isnil(L,-1) || lua_isusertype(L,-1,type)) &&
+ !(def && lua_isnil(L,-1))
+ )
+ {
+ err->index = lo;
+ err->type = type;
+ err->array = 1;
+ return 0;
+ }
+ lua_pop(L,1);
+ return 1;
+}
+
+#endif
diff --git a/tolua/src/lib/tolua_map.c b/tolua/src/lib/tolua_map.c
new file mode 100644
index 0000000..c732406
--- /dev/null
+++ b/tolua/src/lib/tolua_map.c
@@ -0,0 +1,529 @@
+/* tolua: functions to map features
+ ** Support code for Lua bindings.
+ ** Written by Waldemar Celes
+ ** TeCGraf/PUC-Rio
+ ** Apr 2003
+ ** $Id: tolua_map.c,v 1.10 2011/01/13 13:43:46 fabraham Exp $
+ */
+
+/* This code is free software; you can redistribute it and/or modify it.
+ ** The software provided hereunder is on an "as is" basis, and
+ ** the author has no obligation to provide maintenance, support, updates,
+ ** enhancements, or modifications.
+ */
+
+#include "tolua.h"
+#include "tolua_event.h"
+#include "lauxlib.h"
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+
+static char toluaname[128] = "tolua.";
+static const char* TOLUANAME (const char* n)
+{
+ sprintf(&toluaname[6],"%.120s",n);
+ return toluaname;
+}
+
+/* Create metatable
+ * Create and register new metatable
+ */
+void tolua_newmetatable (lua_State* L, const char* name)
+{
+ if (luaL_newmetatable(L,TOLUANAME(name)))
+ {
+ lua_pushvalue(L,-1);
+ lua_pushstring(L,name);
+ lua_rawset(L,LUA_REGISTRYINDEX);
+ }
+
+ /* set meta events */
+ tolua_classevents(L);
+ lua_pop(L,1);
+}
+
+/* Get metatable
+ * Access already created metatable
+ */
+void tolua_getmetatable (lua_State* L, const char* name)
+{
+ luaL_getmetatable(L,TOLUANAME(name));
+}
+
+/* Map super classes
+ * It sets 'name' as being also a 'base', mapping all super classes of 'base' in 'name'
+ */
+static void mapsuper (lua_State* L, const char* name, const char* base)
+{
+ /* push registry.super */
+ lua_pushstring(L,"tolua_super");
+ lua_rawget(L,LUA_REGISTRYINDEX); /* stack: super */
+ tolua_getmetatable(L,name); /* stack: super mt */
+ lua_rawget(L,-2); /* stack: super table */
+ if (lua_isnil(L,-1))
+ {
+ /* create table */
+ lua_pop(L,1);
+ lua_newtable(L); /* stack: super table */
+ tolua_getmetatable(L,name); /* stack: super table mt */
+ lua_pushvalue(L,-2); /* stack: super table mt table */
+ lua_rawset(L,-4); /* stack: super table */
+ }
+
+ /* set base as super class */
+ lua_pushstring(L,base);
+ lua_pushboolean(L,1);
+ lua_rawset(L,-3); /* stack: super table */
+
+ /* set all super class of base as super class of name */
+ tolua_getmetatable(L,base); /* stack: super table base_mt */
+ lua_rawget(L,-3); /* stack: super table base_table */
+ if (lua_istable(L,-1))
+ {
+ /* traverse base table */
+ lua_pushnil(L); /* first key */
+ while (lua_next(L,-2) != 0)
+ {
+ /* stack: ... base_table key value */
+ lua_pushvalue(L,-2); /* stack: ... base_table key value key */
+ lua_insert(L,-2); /* stack: ... base_table key key value */
+ lua_rawset(L,-5); /* stack: ... base_table key */
+ }
+ }
+ lua_pop(L,3); /* stack: <empty> */
+}
+
+
+/* Map inheritance
+ * It sets 'name' as derived from 'base' by setting 'base' as metatable of 'name'
+ */
+static void mapinheritance (lua_State* L, const char* name, const char* base)
+{
+ /* set metatable inheritance */
+ tolua_getmetatable(L,name);
+ if (base && *base)
+ tolua_getmetatable(L,base);
+ else
+ tolua_getmetatable(L,"tolua_commonclass");
+ lua_setmetatable(L,-2);
+ lua_pop(L,1);
+}
+
+/* Object type
+ */
+static int tolua_bnd_type (lua_State* L)
+{
+ tolua_typename(L,lua_gettop(L));
+ return 1;
+}
+
+/* Take ownership
+ */
+int tolua_bnd_takeownership (lua_State* L)
+{
+ lua_CFunction func = 0;
+ if (lua_isuserdata(L,1))
+ {
+ if (lua_getmetatable(L,1)) /* if metatable? */
+ {
+ void* u;
+ lua_pushstring(L,".collector");
+ lua_rawget(L,-2);
+ func = lua_iscfunction(L,-1) ? lua_tocfunction(L,-1) : NULL;
+ lua_pop(L,2);
+ u = *((void**)lua_touserdata(L,1));
+ tolua_clone(L,u,func);
+ }
+ }
+ lua_pushboolean(L,func!=0);
+ return 1;
+}
+
+/* Release ownership
+ */
+static int tolua_bnd_releaseownership (lua_State* L)
+{
+ int done = 0;
+ if (lua_isuserdata(L,1))
+ {
+ void* u = *((void**)lua_touserdata(L,1));
+ lua_pushstring(L,"tolua_gc");
+ lua_rawget(L,LUA_REGISTRYINDEX);
+ lua_pushlightuserdata(L,u);
+ lua_rawget(L,-2);
+ lua_pushlightuserdata(L,u);
+ lua_pushnil(L);
+ lua_rawset(L,-4);
+ done = 1;
+ }
+ lua_pushboolean(L,done!=0);
+ return 1;
+}
+
+/* Type casting
+ */
+static int tolua_bnd_cast (lua_State* L)
+{
+ void* v = tolua_tousertype(L,1,NULL);
+ const char* s = tolua_tostring(L,2,NULL);
+ if (!v)
+ lua_pushnil(L);
+ else if (v && s) {
+ tolua_getmetatable(L,s); /* stack: ubox[u] super super[mt] flag mt */
+ if (lua_isnil(L,-1)) {
+ tolua_error(L,"Unknown 'type' for 'tolua.cast' function",NULL);
+ }
+ tolua_pushusertype(L,v,s);
+ }
+ else {
+ tolua_error(L,"Invalid arguments for 'tolua.cast' function",NULL);
+ }
+ return 1;
+}
+
+/* Release
+** Function to be called by a Lua code that uses a function to explicitly
+** release a mapped object. This function is automatically called by all
+** destructors bound by tolua and by all collected objects.
+*/
+static int tolua_bnd_release (lua_State* L)
+{
+ void* value = tolua_tousertype(L,1,NULL);
+ if (value)
+ tolua_release(L,value);
+ return 1;
+}
+
+static void tolua_push_globals_table (lua_State* L)
+{
+ lua_pushvalue(L,LUA_REGISTRYINDEX); /* registry */
+ lua_pushnumber(L,LUA_RIDX_GLOBALS); /* registry globalsindex */
+ lua_rawget(L, -2); /* registry registry[globalsindex] */
+ lua_remove(L, -2); /* registry[globalsindex] */
+}
+
+TOLUA_API void tolua_open (lua_State* L)
+{
+ int top = lua_gettop(L);
+ lua_pushstring(L,"tolua_opened");
+ lua_rawget(L,LUA_REGISTRYINDEX);
+ if (!lua_isboolean(L,-1))
+ {
+ lua_pushstring(L,"tolua_opened"); lua_pushboolean(L,1); lua_rawset(L,LUA_REGISTRYINDEX);
+ lua_pushstring(L,"tolua_ubox");
+ lua_newtable(L);
+ lua_pushvalue(L, -1); /* metatable: for weak table */
+ lua_pushstring(L, "__mode");
+ lua_pushstring(L, "v");
+ lua_rawset(L, -3);
+ lua_setmetatable(L, -2);
+ lua_rawset(L,LUA_REGISTRYINDEX);
+ lua_pushstring(L,"tolua_peer");
+ lua_newtable(L);
+ lua_pushvalue(L, -1); /* metatable: for weak table */
+ lua_pushstring(L, "__mode");
+ lua_pushstring(L, "k");
+ lua_rawset(L, -3);
+ lua_setmetatable(L, -2);
+ lua_rawset(L,LUA_REGISTRYINDEX);
+ lua_pushstring(L,"tolua_super"); lua_newtable(L); lua_rawset(L,LUA_REGISTRYINDEX);
+ lua_pushstring(L,"tolua_gc"); lua_newtable(L); lua_rawset(L,LUA_REGISTRYINDEX);
+
+ tolua_newmetatable(L,"tolua_commonclass");
+
+ tolua_module(L,NULL,0);
+ tolua_beginmodule(L,NULL);
+ tolua_module(L,"tolua",0);
+ tolua_beginmodule(L,"tolua");
+ tolua_function(L,"type",tolua_bnd_type);
+ tolua_function(L,"takeownership",tolua_bnd_takeownership);
+ tolua_function(L,"releaseownership",tolua_bnd_releaseownership);
+ tolua_function(L,"cast",tolua_bnd_cast);
+ tolua_function(L,"release",tolua_bnd_release);
+ tolua_endmodule(L);
+ tolua_endmodule(L);
+ }
+ lua_settop(L,top);
+}
+
+/* Copy a C object
+ */
+TOLUA_API void* tolua_copy (lua_State* L, void* value, unsigned int size)
+{
+ void* clone = (void*)malloc(size);
+ if (clone)
+ memcpy(clone,value,size);
+ else
+ tolua_error(L,"insuficient memory",NULL);
+ return clone;
+}
+
+
+/* Release userdata from tolua
+ */
+TOLUA_API void tolua_release (lua_State* L, void* value)
+{
+ void** p;
+ lua_pushstring(L,"tolua_ubox");
+ lua_rawget(L,LUA_REGISTRYINDEX); /* stack: ubox */
+ lua_pushlightuserdata(L,value); /* stack: ubox u */
+ /* set userdata pointer to NULL: this pointer might be reused by C/C++ */
+ lua_rawget(L,-2); /* stack: ubox ud */
+ p = (void**)lua_touserdata(L,-1);
+ if (p) *p = NULL;
+ /* fixed bug: thanks to Ulrik Sverdrup -- it was 1 instead of -1 */
+ lua_pop(L,1); /* stack: ubox */
+ /* remove value from ubox */
+ lua_pushlightuserdata(L,value); /* stack: ubox u */
+ lua_pushnil(L);
+ lua_rawset(L,-3);
+ lua_pop(L,1);
+}
+
+/* Do clone
+ */
+TOLUA_API void* tolua_clone (lua_State* L, void* value, lua_CFunction func)
+{
+ lua_pushstring(L,"tolua_gc");
+ lua_rawget(L,LUA_REGISTRYINDEX);
+ lua_pushlightuserdata(L,value);
+ lua_pushcfunction(L,func);
+ lua_rawset(L,-3);
+ lua_pop(L,1);
+ return value;
+}
+
+/* Register a usertype
+ * It creates the correspoding metatable in the registry, for both 'type' and 'const type'.
+ * It maps 'const type' as being also a 'type'
+ */
+TOLUA_API void tolua_usertype (lua_State* L, const char* type)
+{
+ char ctype[128] = "const ";
+ strncat(ctype,type,120);
+
+ tolua_newmetatable(L,ctype); /* create both metatables */
+ tolua_newmetatable(L,type);
+
+ mapsuper(L,type,ctype); /* 'type' is also a 'const type' */
+}
+
+
+/* Begin module
+ * It pushes the module (or class) table on the stack
+ */
+TOLUA_API void tolua_beginmodule (lua_State* L, const char* name)
+{
+ if (name)
+ {
+ lua_pushstring(L,name);
+ lua_rawget(L,-2);
+ }
+ else
+ tolua_push_globals_table(L);
+}
+
+/* End module
+ * It pops the module (or class) from the stack
+ */
+TOLUA_API void tolua_endmodule (lua_State* L)
+{
+ lua_pop(L,1);
+}
+
+/* Map module
+ * It creates a new module
+ */
+#if 1
+TOLUA_API void tolua_module (lua_State* L, const char* name, int hasvar)
+{
+ if (name)
+ {
+ /* tolua module */
+ lua_pushstring(L,name);
+ lua_rawget(L,-2);
+ if (!lua_istable(L,-1)) /* check if module already exists */
+ {
+ lua_pop(L,1);
+ lua_newtable(L);
+ lua_pushstring(L,name);
+ lua_pushvalue(L,-2);
+ lua_rawset(L,-4); /* assing module into module */
+ }
+ }
+ else
+ tolua_push_globals_table(L);
+ if (hasvar)
+ {
+ if (!tolua_ismodulemetatable(L)) /* check if it already has a module metatable */
+ {
+ /* create metatable to get/set C/C++ variable */
+ lua_newtable(L);
+ tolua_moduleevents(L);
+ if (lua_getmetatable(L,-2))
+ lua_setmetatable(L,-2); /* set old metatable as metatable of metatable */
+ lua_setmetatable(L,-2);
+ }
+ }
+ lua_pop(L,1); /* pop module */
+}
+#else
+TOLUA_API void tolua_module (lua_State* L, const char* name, int hasvar)
+{
+ if (name)
+ {
+ /* tolua module */
+ lua_pushstring(L,name);
+ lua_newtable(L);
+ }
+ else
+ tolua_push_globals_table(L);
+ if (hasvar)
+ {
+ /* create metatable to get/set C/C++ variable */
+ lua_newtable(L);
+ tolua_moduleevents(L);
+ if (lua_getmetatable(L,-2))
+ lua_setmetatable(L,-2); /* set old metatable as metatable of metatable */
+ lua_setmetatable(L,-2);
+ }
+ if (name)
+ lua_rawset(L,-3); /* assing module into module */
+ else
+ lua_pop(L,1); /* pop global table */
+}
+#endif
+
+/* Map C class
+ * It maps a C class, setting the appropriate inheritance and super classes.
+ */
+TOLUA_API void tolua_cclass (lua_State* L, const char* lname, const char* name, const char* base, lua_CFunction col)
+{
+ char cname[128] = "const ";
+ char cbase[128] = "const ";
+ strncat(cname,name,120);
+ strncat(cbase,base,120);
+
+ mapinheritance(L,name,base);
+ mapinheritance(L,cname,name);
+
+ mapsuper(L,cname,cbase);
+ mapsuper(L,name,base);
+
+ lua_pushstring(L,lname);
+ tolua_getmetatable(L,name);
+ lua_pushstring(L,".collector");
+ lua_pushcfunction(L,col);
+ lua_rawset(L,-3); /* store collector function into metatable */
+ lua_rawset(L,-3); /* assign class metatable to module */
+}
+
+/* Map function
+ * It assigns a function into the current module (or class)
+ */
+TOLUA_API void tolua_function (lua_State* L, const char* name, lua_CFunction func)
+{
+ lua_pushstring(L,name);
+ lua_pushcfunction(L,func);
+ lua_rawset(L,-3);
+}
+
+/* Map constant number
+ * It assigns a constant number into the current module (or class)
+ */
+TOLUA_API void tolua_constant (lua_State* L, const char* name, double value)
+{
+ lua_pushstring(L,name);
+ tolua_pushnumber(L,value);
+ lua_rawset(L,-3);
+}
+
+
+/* Map variable
+ * It assigns a variable into the current module (or class)
+ */
+TOLUA_API void tolua_variable (lua_State* L, const char* name, lua_CFunction get, lua_CFunction set)
+{
+ /* get func */
+ lua_pushstring(L,".get");
+ lua_rawget(L,-2);
+ if (!lua_istable(L,-1))
+ {
+ /* create .get table, leaving it at the top */
+ lua_pop(L,1);
+ lua_newtable(L);
+ lua_pushstring(L,".get");
+ lua_pushvalue(L,-2);
+ lua_rawset(L,-4);
+ }
+ lua_pushstring(L,name);
+ lua_pushcfunction(L,get);
+ lua_rawset(L,-3); /* store variable */
+ lua_pop(L,1); /* pop .get table */
+
+ /* set func */
+ if (set)
+ {
+ lua_pushstring(L,".set");
+ lua_rawget(L,-2);
+ if (!lua_istable(L,-1))
+ {
+ /* create .set table, leaving it at the top */
+ lua_pop(L,1);
+ lua_newtable(L);
+ lua_pushstring(L,".set");
+ lua_pushvalue(L,-2);
+ lua_rawset(L,-4);
+ }
+ lua_pushstring(L,name);
+ lua_pushcfunction(L,set);
+ lua_rawset(L,-3); /* store variable */
+ lua_pop(L,1); /* pop .set table */
+ }
+}
+
+/* Access const array
+ * It reports an error when trying to write into a const array
+ */
+static int const_array (lua_State* L)
+{
+ luaL_error(L,"value of const array cannot be changed");
+ return 0;
+}
+
+/* Map an array
+ * It assigns an array into the current module (or class)
+ */
+TOLUA_API void tolua_array (lua_State* L, const char* name, lua_CFunction get, lua_CFunction set)
+{
+ lua_pushstring(L,".get");
+ lua_rawget(L,-2);
+ if (!lua_istable(L,-1))
+ {
+ /* create .get table, leaving it at the top */
+ lua_pop(L,1);
+ lua_newtable(L);
+ lua_pushstring(L,".get");
+ lua_pushvalue(L,-2);
+ lua_rawset(L,-4);
+ }
+ lua_pushstring(L,name);
+
+ lua_newtable(L); /* create array metatable */
+ lua_pushvalue(L,-1);
+ lua_setmetatable(L,-2); /* set the own table as metatable (for modules) */
+ lua_pushstring(L,"__index");
+ lua_pushcfunction(L,get);
+ lua_rawset(L,-3);
+ lua_pushstring(L,"__newindex");
+ lua_pushcfunction(L,set?set:const_array);
+ lua_rawset(L,-3);
+
+ lua_rawset(L,-3); /* store variable */
+ lua_pop(L,1); /* pop .get table */
+}
+
diff --git a/tolua/src/lib/tolua_push.c b/tolua/src/lib/tolua_push.c
new file mode 100755
index 0000000..4413548
--- /dev/null
+++ b/tolua/src/lib/tolua_push.c
@@ -0,0 +1,144 @@
+/* tolua: functions to push C values.
+ ** Support code for Lua bindings.
+ ** Written by Waldemar Celes
+ ** TeCGraf/PUC-Rio
+ ** Apr 2003
+ ** $Id: tolua_push.c,v 1.7 2010/01/22 15:39:29 fabraham Exp $
+ */
+
+/* This code is free software; you can redistribute it and/or modify it.
+ ** The software provided hereunder is on an "as is" basis, and
+ ** the author has no obligation to provide maintenance, support, updates,
+ ** enhancements, or modifications.
+ */
+
+#include "tolua.h"
+#include "lauxlib.h"
+
+#include <stdlib.h>
+
+TOLUA_API void tolua_pushvalue (lua_State* L, int lo)
+{
+ lua_pushvalue(L,lo);
+}
+
+TOLUA_API void tolua_pushboolean (lua_State* L, int value)
+{
+ lua_pushboolean(L,value);
+}
+
+TOLUA_API void tolua_pushnumber (lua_State* L, double value)
+{
+ lua_pushnumber(L,value);
+}
+
+TOLUA_API void tolua_pushstring (lua_State* L, const char* value)
+{
+ if (value == NULL)
+ lua_pushnil(L);
+ else
+ lua_pushstring(L,value);
+}
+
+TOLUA_API void tolua_pushuserdata (lua_State* L, void* value)
+{
+ if (value == NULL)
+ lua_pushnil(L);
+ else
+ lua_pushlightuserdata(L,value);
+}
+
+TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type)
+{
+ if (value == NULL)
+ lua_pushnil(L);
+ else
+ {
+ lua_pushstring(L,"tolua_ubox");
+ lua_rawget(L,LUA_REGISTRYINDEX); /* stack: ubox */
+ lua_pushlightuserdata(L,value);
+ lua_rawget(L,-2); /* stack: ubox ubox[u] */
+ if (lua_isnil(L,-1))
+ {
+ /* creating ubox of value */
+ lua_pop(L,1); /* stack: ubox */
+ lua_pushlightuserdata(L,value);
+ *(void**)lua_newuserdata(L,sizeof(void *)) = value; /* stack: ubox u newud */
+ lua_pushvalue(L,-1); /* stack: ubox u newud newud */
+ lua_insert(L,-4); /* stack: newud ubox u newud */
+ lua_rawset(L,-3); /* stack: newud ubox */
+ lua_pop(L,1); /* stack: newud */
+ tolua_getmetatable(L,type); /* stack: newud mt */
+ lua_setmetatable(L,-2); /* stack: newud */
+ }
+ else
+ {
+ /* reusing ubox of value */
+ /* check the need of updating the metatable to a more specialized class */
+ lua_insert(L,-2); /* stack: ubox[u] ubox */
+ lua_pop(L,1); /* stack: ubox[u] */
+ lua_pushstring(L,"tolua_super");
+ lua_rawget(L,LUA_REGISTRYINDEX); /* stack: ubox[u] super */
+ lua_getmetatable(L,-2); /* stack: ubox[u] super mt */
+ lua_rawget(L,-2); /* stack: ubox[u] super super[mt] */
+ if (lua_istable(L,-1))
+ {
+ lua_pushstring(L,type); /* stack: ubox[u] super super[mt] type */
+ lua_rawget(L,-2); /* stack: ubox[u] super super[mt] flag */
+ if (lua_toboolean(L,-1) == 1) /* if true */
+ {
+ lua_pop(L,3); /* stack: ubox[u] */
+ return;
+ }
+ }
+ /* type represents a more specilized type */
+ tolua_getmetatable(L,type); /* stack: ubox[u] super super[mt] flag mt */
+ lua_setmetatable(L,-5); /* stack: ubox[u] super super[mt] flag */
+ lua_pop(L,3); /* stack: ubox[u] */
+ }
+ }
+}
+
+TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v)
+{
+ lua_pushnumber(L,index);
+ lua_pushvalue(L,v);
+ lua_settable(L,lo);
+}
+
+TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v)
+{
+ lua_pushnumber(L,index);
+ lua_pushboolean(L,v);
+ lua_settable(L,lo);
+}
+
+
+TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, double v)
+{
+ lua_pushnumber(L,index);
+ tolua_pushnumber(L,v);
+ lua_settable(L,lo);
+}
+
+TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v)
+{
+ lua_pushnumber(L,index);
+ tolua_pushstring(L,v);
+ lua_settable(L,lo);
+}
+
+TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v)
+{
+ lua_pushnumber(L,index);
+ tolua_pushuserdata(L,v);
+ lua_settable(L,lo);
+}
+
+TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type)
+{
+ lua_pushnumber(L,index);
+ tolua_pushusertype(L,v,type);
+ lua_settable(L,lo);
+}
+
diff --git a/tolua/src/lib/tolua_to.c b/tolua/src/lib/tolua_to.c
new file mode 100755
index 0000000..e036037
--- /dev/null
+++ b/tolua/src/lib/tolua_to.c
@@ -0,0 +1,125 @@
+/* tolua: funcitons to convert to C types
+** Support code for Lua bindings.
+** Written by Waldemar Celes
+** TeCGraf/PUC-Rio
+** Apr 2003
+** $Id: tolua_to.c,v 1.5 2009/11/24 16:45:15 fabraham Exp $
+*/
+
+/* This code is free software; you can redistribute it and/or modify it.
+** The software provided hereunder is on an "as is" basis, and
+** the author has no obligation to provide maintenance, support, updates,
+** enhancements, or modifications.
+*/
+
+#include "tolua.h"
+
+#include <string.h>
+#include <stdlib.h>
+
+TOLUA_API double tolua_tonumber (lua_State* L, int narg, double def)
+{
+ return lua_gettop(L)<abs(narg) ? def : lua_tonumber(L,narg);
+}
+
+TOLUA_API const char* tolua_tostring (lua_State* L, int narg, const char* def)
+{
+ return lua_gettop(L)<abs(narg) ? def : lua_tostring(L,narg);
+}
+
+TOLUA_API void* tolua_touserdata (lua_State* L, int narg, void* def)
+{
+ return lua_gettop(L)<abs(narg) ? def : lua_touserdata(L,narg);
+}
+
+TOLUA_API void* tolua_tousertype (lua_State* L, int narg, void* def)
+{
+ if (lua_gettop(L)<abs(narg))
+ return def;
+ else
+ {
+ void* u = lua_touserdata(L,narg);
+ return (u==NULL) ? NULL : *((void**)u); /* nil represents NULL */
+ }
+}
+
+TOLUA_API int tolua_tovalue (lua_State* L, int narg, int def)
+{
+ return lua_gettop(L)<abs(narg) ? def : narg;
+}
+
+TOLUA_API int tolua_toboolean (lua_State* L, int narg, int def)
+{
+ return lua_gettop(L)<abs(narg) ? def : lua_toboolean(L,narg);
+}
+
+TOLUA_API double tolua_tofieldnumber (lua_State* L, int lo, int index, double def)
+{
+ double v;
+ lua_pushnumber(L,index);
+ lua_gettable(L,lo);
+ v = lua_isnil(L,-1) ? def : lua_tonumber(L,-1);
+ lua_pop(L,1);
+ return v;
+}
+
+TOLUA_API int tolua_tofieldboolean (lua_State* L, int lo, int index, int def)
+{
+ int v;
+ lua_pushnumber(L,index);
+ lua_gettable(L,lo);
+ v = lua_isnil(L,-1) ? def : lua_toboolean(L,-1);
+ lua_pop(L,1);
+ return v;
+}
+
+TOLUA_API const char* tolua_tofieldstring
+(lua_State* L, int lo, int index, const char* def)
+{
+ const char* v;
+ lua_pushnumber(L,index);
+ lua_gettable(L,lo);
+ v = lua_isnil(L,-1) ? def : lua_tostring(L,-1);
+ lua_pop(L,1);
+ return v;
+}
+
+TOLUA_API void* tolua_tofielduserdata (lua_State* L, int lo, int index, void* def)
+{
+ void* v;
+ lua_pushnumber(L,index);
+ lua_gettable(L,lo);
+ v = lua_isnil(L,-1) ? def : lua_touserdata(L,-1);
+ lua_pop(L,1);
+ return v;
+}
+
+TOLUA_API void* tolua_tofieldusertype (lua_State* L, int lo, int index, void* def)
+{
+ void* v;
+ lua_pushnumber(L,index);
+ lua_gettable(L,lo);
+ v = lua_isnil(L,-1) ? def : (*(void **)(lua_touserdata(L, -1)));
+ lua_pop(L,1);
+ return v;
+}
+
+TOLUA_API int tolua_tofieldvalue (lua_State* L, int lo, int index, int def)
+{
+ int v;
+ lua_pushnumber(L,index);
+ lua_gettable(L,lo);
+ v = lua_isnil(L,-1) ? def : lo;
+ lua_pop(L,1);
+ return v;
+}
+
+TOLUA_API int tolua_getfieldboolean (lua_State* L, int lo, int index, int def)
+{
+ int v;
+ lua_pushnumber(L,index);
+ lua_gettable(L,lo);
+ v = lua_isnil(L,-1) ? 0 : lua_toboolean(L,-1);
+ lua_pop(L,1);
+ return v;
+}