src/lxplib.c

changeset 10
e981a82571cf
parent 6
51b85d631863
child 17
7f0e6f8bb8e0
equal deleted inserted replaced
9:5dfed844930e 10:e981a82571cf
11 11
12 #include "expat.h" 12 #include "expat.h"
13 13
14 #include "lua.h" 14 #include "lua.h"
15 #include "lauxlib.h" 15 #include "lauxlib.h"
16 #if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 16
17 #include "compat-5.1.h" 17
18 #include "lxplib.h"
19
20
21 #if !defined(lua_pushliteral)
22 #define lua_pushliteral(L, s) \
23 lua_pushstring(L, "" s, (sizeof(s)/sizeof(char))-1)
18 #endif 24 #endif
19
20
21 #include "lxplib.h"
22
23 25
24 26
25 enum XPState { 27 enum XPState {
26 XPSpre, /* parser just initialized */ 28 XPSpre, /* parser just initialized */
27 XPSok, /* state while parsing */ 29 XPSok, /* state while parsing */
64 return xpu; 66 return xpu;
65 } 67 }
66 68
67 69
68 static void lxpclose (lua_State *L, lxp_userdata *xpu) { 70 static void lxpclose (lua_State *L, lxp_userdata *xpu) {
69 lua_unref(L, xpu->tableref); 71 luaL_unref(L, LUA_REGISTRYINDEX, xpu->tableref);
70 xpu->tableref = LUA_REFNIL; 72 xpu->tableref = LUA_REFNIL;
71 if (xpu->parser) 73 if (xpu->parser)
72 XML_ParserFree(xpu->parser); 74 XML_ParserFree(xpu->parser);
73 xpu->parser = NULL; 75 xpu->parser = NULL;
74 } 76 }
223 if (getHandle(xpu, ExternalEntityKey) == 0) return 1; /* no handle */ 225 if (getHandle(xpu, ExternalEntityKey) == 0) return 1; /* no handle */
224 child = createlxp(L); 226 child = createlxp(L);
225 child->parser = XML_ExternalEntityParserCreate(p, context, NULL); 227 child->parser = XML_ExternalEntityParserCreate(p, context, NULL);
226 if (!child->parser) 228 if (!child->parser)
227 luaL_error(L, "XML_ParserCreate failed"); 229 luaL_error(L, "XML_ParserCreate failed");
228 lua_getref(L, xpu->tableref); /* child uses the same table of its father */ 230 lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->tableref); /*lua_getref(L, xpu->tableref); */ /* child uses the same table of its father */
229 child->tableref = luaL_ref(L, LUA_REGISTRYINDEX); 231 child->tableref = luaL_ref(L, LUA_REGISTRYINDEX);
230 lua_pushstring(L, base); 232 lua_pushstring(L, base);
231 lua_pushstring(L, systemId); 233 lua_pushstring(L, systemId);
232 lua_pushstring(L, publicId); 234 lua_pushstring(L, publicId);
233 docall(xpu, 4, 1); 235 docall(xpu, 4, 1);
447 int status; 449 int status;
448 xpu->L = L; 450 xpu->L = L;
449 xpu->state = XPSok; 451 xpu->state = XPSok;
450 xpu->b = &b; 452 xpu->b = &b;
451 lua_settop(L, 2); 453 lua_settop(L, 2);
452 lua_getref(L, xpu->tableref); /* to be used by handlers */ 454 lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->tableref); /*lua_getref(L, xpu->tableref);*/ /* to be used by handlers */
453 status = XML_Parse(xpu->parser, s, (int)len, s == NULL); 455 status = XML_Parse(xpu->parser, s, (int)len, s == NULL);
454 if (xpu->state == XPSstring) dischargestring(xpu); 456 if (xpu->state == XPSstring) dischargestring(xpu);
455 if (xpu->state == XPSerror) { /* callback error? */ 457 if (xpu->state == XPSerror) { /* callback error? */
456 lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->tableref); /* get original msg. */ 458 lua_rawgeti(L, LUA_REGISTRYINDEX, xpu->tableref); /* get original msg. */
457 lua_error(L); 459 lua_error(L);
515 lxp_userdata *xpu = checkparser(L, 1); 517 lxp_userdata *xpu = checkparser(L, 1);
516 lua_pushboolean(L, XML_StopParser(xpu->parser, XML_FALSE) == XML_STATUS_OK); 518 lua_pushboolean(L, XML_StopParser(xpu->parser, XML_FALSE) == XML_STATUS_OK);
517 return 1; 519 return 1;
518 } 520 }
519 521
520 static const struct luaL_reg lxp_meths[] = { 522 #if !defined LUA_VERSION_NUM
523 /* Lua 5.0 */
524 #define luaL_Reg luaL_reg
525 #endif
526
527 static const struct luaL_Reg lxp_meths[] = {
521 {"parse", lxp_parse}, 528 {"parse", lxp_parse},
522 {"close", lxp_close}, 529 {"close", lxp_close},
523 {"__gc", parser_gc}, 530 {"__gc", parser_gc},
524 {"pos", lxp_pos}, 531 {"pos", lxp_pos},
525 {"setencoding", lxp_setencoding}, 532 {"setencoding", lxp_setencoding},
528 {"setbase", setbase}, 535 {"setbase", setbase},
529 {"stop", lxp_stop}, 536 {"stop", lxp_stop},
530 {NULL, NULL} 537 {NULL, NULL}
531 }; 538 };
532 539
533 static const struct luaL_reg lxp_funcs[] = { 540 static const struct luaL_Reg lxp_funcs[] = {
534 {"new", lxp_make_parser}, 541 {"new", lxp_make_parser},
535 {NULL, NULL} 542 {NULL, NULL}
536 }; 543 };
537 544
538 545
539 /* 546 /*
540 ** Assumes the table is on top of the stack. 547 ** Assumes the table is on top of the stack.
541 */ 548 */
542 static void set_info (lua_State *L) { 549 static void set_info (lua_State *L) {
543 lua_pushliteral (L, "_COPYRIGHT"); 550 lua_pushliteral (L, "_COPYRIGHT");
544 lua_pushliteral (L, "Copyright (C) 2003-2007 Kepler Project"); 551 lua_pushliteral (L, "Copyright (C) 2003-2012 Kepler Project");
545 lua_settable (L, -3); 552 lua_settable (L, -3);
546 lua_pushliteral (L, "_DESCRIPTION"); 553 lua_pushliteral (L, "_DESCRIPTION");
547 lua_pushliteral (L, "LuaExpat is a SAX XML parser based on the Expat library"); 554 lua_pushliteral (L, "LuaExpat is a SAX XML parser based on the Expat library");
548 lua_settable (L, -3); 555 lua_settable (L, -3);
549 lua_pushliteral (L, "_VERSION"); 556 lua_pushliteral (L, "_VERSION");
550 lua_pushliteral (L, "LuaExpat 1.2.0"); 557 lua_pushliteral (L, "LuaExpat 1.3.0");
551 lua_settable (L, -3); 558 lua_settable (L, -3);
552 } 559 }
553 560
554 561
562 #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501
563 /*
564 ** Adapted from Lua 5.2.0
565 */
566 static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
567 luaL_checkstack(L, nup, "too many upvalues");
568 for (; l->name != NULL; l++) { /* fill the table with given functions */
569 int i;
570 for (i = 0; i < nup; i++) /* copy upvalues to the top */
571 lua_pushvalue(L, -nup);
572 lua_pushstring(L, l->name);
573 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
574 lua_settable(L, -(nup + 3));
575 }
576 lua_pop(L, nup); /* remove upvalues */
577 }
578 #endif
579
580
555 int luaopen_lxp (lua_State *L) { 581 int luaopen_lxp (lua_State *L) {
556 luaL_newmetatable(L, ParserType); 582 luaL_newmetatable(L, ParserType);
557 lua_pushliteral(L, "__index"); 583
558 lua_pushvalue(L, -2); 584 lua_pushliteral(L, "__index");
559 lua_rawset(L, -3); 585 lua_pushvalue(L, -2);
560 luaL_openlib (L, NULL, lxp_meths, 0); 586 lua_rawset(L, -3);
561 luaL_openlib (L, "lxp", lxp_funcs, 0); 587
562 set_info (L); 588 luaL_setfuncs (L, lxp_meths, 0);
563 589 lua_pop (L, 1); /* remove metatable */
564 return 1; 590
565 } 591 lua_newtable (L);
592 luaL_setfuncs (L, lxp_funcs, 0);
593 set_info (L);
594 return 1;
595 }

mercurial