src/lxplib.c

changeset 10
e981a82571cf
parent 6
51b85d631863
child 17
7f0e6f8bb8e0
--- a/src/lxplib.c	Fri Jul 27 22:03:24 2012 +0100
+++ b/src/lxplib.c	Sat Jul 28 00:15:08 2012 +0100
@@ -13,14 +13,16 @@
 
 #include "lua.h"
 #include "lauxlib.h"
-#if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
-#include "compat-5.1.h"
-#endif
 
 
 #include "lxplib.h"
 
 
+#if !defined(lua_pushliteral)
+#define lua_pushliteral(L, s)	\
+	lua_pushstring(L, "" s, (sizeof(s)/sizeof(char))-1)
+#endif
+
 
 enum XPState {
   XPSpre,  /* parser just initialized */
@@ -66,7 +68,7 @@
 
 
 static void lxpclose (lua_State *L, lxp_userdata *xpu) {
-  lua_unref(L, xpu->tableref);
+  luaL_unref(L, LUA_REGISTRYINDEX, xpu->tableref);
   xpu->tableref = LUA_REFNIL;
   if (xpu->parser)
     XML_ParserFree(xpu->parser);
@@ -225,7 +227,7 @@
   child->parser = XML_ExternalEntityParserCreate(p, context, NULL);
   if (!child->parser)
     luaL_error(L, "XML_ParserCreate failed");
-  lua_getref(L, xpu->tableref);  /* child uses the same table of its father */
+  lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->tableref); /*lua_getref(L, xpu->tableref); */ /* child uses the same table of its father */
   child->tableref = luaL_ref(L, LUA_REGISTRYINDEX);
   lua_pushstring(L, base);
   lua_pushstring(L, systemId);
@@ -449,7 +451,7 @@
   xpu->state = XPSok;
   xpu->b = &b;
   lua_settop(L, 2);
-  lua_getref(L, xpu->tableref);  /* to be used by handlers */
+  lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->tableref); /*lua_getref(L, xpu->tableref);*/  /* to be used by handlers */
   status = XML_Parse(xpu->parser, s, (int)len, s == NULL);
   if (xpu->state == XPSstring) dischargestring(xpu);
   if (xpu->state == XPSerror) {  /* callback error? */
@@ -517,7 +519,12 @@
   return 1;
 }
 
-static const struct luaL_reg lxp_meths[] = {
+#if !defined LUA_VERSION_NUM
+/* Lua 5.0 */
+#define luaL_Reg luaL_reg
+#endif
+
+static const struct luaL_Reg lxp_meths[] = {
   {"parse", lxp_parse},
   {"close", lxp_close},
   {"__gc", parser_gc},
@@ -530,7 +537,7 @@
   {NULL, NULL}
 };
 
-static const struct luaL_reg lxp_funcs[] = {
+static const struct luaL_Reg lxp_funcs[] = {
   {"new", lxp_make_parser},
   {NULL, NULL}
 };
@@ -541,25 +548,48 @@
 */
 static void set_info (lua_State *L) {
 	lua_pushliteral (L, "_COPYRIGHT");
-	lua_pushliteral (L, "Copyright (C) 2003-2007 Kepler Project");
+	lua_pushliteral (L, "Copyright (C) 2003-2012 Kepler Project");
 	lua_settable (L, -3);
 	lua_pushliteral (L, "_DESCRIPTION");
 	lua_pushliteral (L, "LuaExpat is a SAX XML parser based on the Expat library");
 	lua_settable (L, -3);
 	lua_pushliteral (L, "_VERSION");
-	lua_pushliteral (L, "LuaExpat 1.2.0");
+	lua_pushliteral (L, "LuaExpat 1.3.0");
 	lua_settable (L, -3);
 }
 
 
+#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
+/*
+** Adapted from Lua 5.2.0
+*/
+static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
+  luaL_checkstack(L, nup, "too many upvalues");
+  for (; l->name != NULL; l++) {  /* fill the table with given functions */
+    int i;
+    for (i = 0; i < nup; i++)  /* copy upvalues to the top */
+      lua_pushvalue(L, -nup);
+    lua_pushstring(L, l->name);
+    lua_pushcclosure(L, l->func, nup);  /* closure with those upvalues */
+    lua_settable(L, -(nup + 3));
+  }
+  lua_pop(L, nup);  /* remove upvalues */
+}
+#endif
+
+
 int luaopen_lxp (lua_State *L) {
-  luaL_newmetatable(L, ParserType);
-  lua_pushliteral(L, "__index");
-  lua_pushvalue(L, -2);
-  lua_rawset(L, -3);
-  luaL_openlib (L, NULL, lxp_meths, 0);
-  luaL_openlib (L, "lxp", lxp_funcs, 0);
-  set_info (L);
+	luaL_newmetatable(L, ParserType);
+
+	lua_pushliteral(L, "__index");
+	lua_pushvalue(L, -2);
+	lua_rawset(L, -3);
 
-  return 1;
+	luaL_setfuncs (L, lxp_meths, 0);
+	lua_pop (L, 1); /* remove metatable */
+
+	lua_newtable (L);
+	luaL_setfuncs (L, lxp_funcs, 0);
+	set_info (L);
+	return 1;
 }

mercurial