From 19c30b65cde412795933a460a3002b3daf89554a Mon Sep 17 00:00:00 2001 From: Janos Kovacs Date: Mon, 12 Nov 2012 23:02:47 +0200 Subject: [PATCH] lua-decision: start to use MRP_LUA_[ENTER|LEAVE] and replace printf's mrp_debug --- src/core/lua-decision/element.c | 78 ++++++++++++------- src/core/lua-decision/mdb.c | 166 +++++++++++++++++++++++++++++----------- 2 files changed, 173 insertions(+), 71 deletions(-) diff --git a/src/core/lua-decision/element.c b/src/core/lua-decision/element.c index dd08818..c0b8660 100644 --- a/src/core/lua-decision/element.c +++ b/src/core/lua-decision/element.c @@ -36,6 +36,7 @@ #include #include +#include #include #include @@ -92,9 +93,6 @@ struct mrp_lua_element_s { }; - - - static int element_create_from_lua(lua_State *); static int element_getfield(lua_State *); static int element_setfield(lua_State *); @@ -162,6 +160,8 @@ static int element_create_from_lua(lua_State *L) size_t fldnamlen; const char *fldnam; + MRP_LUA_ENTER; + el = (mrp_lua_element_t *)mrp_lua_create_object(L, ELEMENT_CLASS, NULL); table = lua_gettop(L); @@ -210,17 +210,22 @@ static int element_create_from_lua(lua_State *L) mrp_lua_set_object_name(L, ELEMENT_CLASS, el->name); + mrp_debug("element '%s' created", el->name); + if (el->inpmask == INPUT_MASK(el->ninput)) element_install(L, el); - return 1; + MRP_LUA_LEAVE(1); } static int element_getfield(lua_State *L) { - mrp_lua_element_t *el = element_check(L, 1); + mrp_lua_element_t *el; field_t fld; + MRP_LUA_ENTER; + + el = element_check(L, 1); fld = field_check(L, 2, NULL); lua_pop(L, 1); @@ -232,37 +237,46 @@ static int element_getfield(lua_State *L) default: lua_pushnil(L); break; } - return 1; + MRP_LUA_LEAVE(1); } static int element_setfield(lua_State *L) { - mrp_lua_element_t *el = element_check(L, 1); + mrp_lua_element_t *el; + + MRP_LUA_ENTER; + el = element_check(L, 1); luaL_error(L, "'%s' is read-only", el->name); - return 0; + MRP_LUA_LEAVE(0); } static int element_tostring(lua_State *L) { - mrp_lua_element_t *el = element_check(L, 1); + mrp_lua_element_t *el; + + MRP_LUA_ENTER; - if (el && el->name) + if ((el = element_check(L, 1)) && el->name) lua_pushstring(L, el->name); else lua_pushstring(L, ""); - return 1; + MRP_LUA_LEAVE(1); } static void element_destroy_from_lua(void *data) { mrp_lua_element_t *el = (mrp_lua_element_t *)data; + MRP_LUA_ENTER; + if (el) { mrp_free((void *)el->name); } + + MRP_LUA_LEAVE_NOARG; } static mrp_lua_element_t *element_check(lua_State *L, int idx) @@ -276,24 +290,24 @@ static int element_update_cb(mrp_scriptlet_t *script, mrp_context_tbl_t *ctbl) MRP_UNUSED(ctbl); - printf("*** should update element '%s'\n", el->name); + mrp_debug("'%s'", el->name); return TRUE; } -static mrp_interpreter_t element_updater = { - { NULL, NULL }, - "element_updater", - NULL, - NULL, - NULL, - element_update_cb, - NULL -}; - static void element_install(lua_State *L, mrp_lua_element_t *el) { + static mrp_interpreter_t element_updater = { + { NULL, NULL }, + "element_updater", + NULL, + NULL, + NULL, + element_update_cb, + NULL + }; + mrp_lua_element_input_t *inp; mrp_context_t *ctx; size_t i; @@ -304,6 +318,8 @@ static void element_install(lua_State *L, mrp_lua_element_t *el) MRP_UNUSED(L); + MRP_LUA_ENTER; + ctx = mrp_lua_get_murphy_context(); if (ctx == NULL || ctx->r == NULL) { @@ -339,6 +355,8 @@ static void element_install(lua_State *L, mrp_lua_element_t *el) el->name); } } + + MRP_LUA_LEAVE_NOARG; } static void element_input_class_create(lua_State *L) @@ -353,6 +371,8 @@ static void element_input_class_create(lua_State *L) static int element_input_create_luatbl(lua_State *L, int el) { + MRP_LUA_ENTER; + el = (el < 0) ? lua_gettop(L) + el + 1 : el; luaL_checktype(L, el, LUA_TTABLE); @@ -366,7 +386,7 @@ static int element_input_create_luatbl(lua_State *L, int el) lua_pushvalue(L, el); lua_rawset(L, -3); - return 0; + MRP_LUA_LEAVE(0); } static int element_input_getfield(lua_State *L) @@ -376,13 +396,15 @@ static int element_input_getfield(lua_State *L) mrp_lua_element_input_t *inp; size_t i; + MRP_LUA_ENTER; + lua_rawgeti(L, 1, INPUT_IDX); el = element_check(L, -1); lua_pop(L, 1); inpnam = luaL_checklstring(L, 2, NULL); - printf("*** reading %s.inputs.%s \n", el->name, inpnam); + mrp_debug("reading %s.inputs.%s", el->name, inpnam); for (i = 0; i < el->ninput; i++) { inp = el->inputs + i; @@ -399,7 +421,7 @@ static int element_input_getfield(lua_State *L) lua_pushnil(L); - return 1; + MRP_LUA_LEAVE(1); } static int element_input_setfield(lua_State *L) @@ -409,13 +431,15 @@ static int element_input_setfield(lua_State *L) mrp_lua_element_input_t *inp; size_t i; + MRP_LUA_ENTER; + lua_rawgeti(L, 1, INPUT_IDX); el = element_check(L, -1); lua_pop(L, 1); inpnam = luaL_checklstring(L, 2, NULL); - printf("*** writing %s.inputs.%s \n", el->name, inpnam); + mrp_debug("writing %s.inputs.%s", el->name, inpnam); for (i = 0; i < el->ninput; i++) { inp = el->inputs + i; @@ -451,7 +475,7 @@ static int element_input_setfield(lua_State *L) } } /* for inp */ - return 0; + MRP_LUA_LEAVE(0); } static mrp_lua_element_input_t *element_input_create_userdata(lua_State *L, diff --git a/src/core/lua-decision/mdb.c b/src/core/lua-decision/mdb.c index 915db6c..9b0a9c6 100644 --- a/src/core/lua-decision/mdb.c +++ b/src/core/lua-decision/mdb.c @@ -36,9 +36,9 @@ #include #include +#include #include -#include #include #include #include @@ -112,7 +112,7 @@ static int table_tostring(lua_State *); static void table_destroy_from_lua(void *); static void table_row_class_create(lua_State *); -static int table_row_create(lua_State *, int, void *, int); +/* static int table_row_create(lua_State *, int, void *, int); */ static int table_row_getfield(lua_State *); static int table_row_setfield(lua_State *); static int table_row_getlength(lua_State *); @@ -128,7 +128,7 @@ static int select_update_from_resolver(mrp_scriptlet_t *,mrp_context_tbl_t *); static void select_install(lua_State *, mrp_lua_mdb_select_t *); static void select_row_class_create(lua_State *); -static int select_row_create(lua_State *, int, void *, int); +/* static int select_row_create(lua_State *, int, void *, int); */ static int select_row_getfield(lua_State *); static int select_row_setfield(lua_State *); static int select_row_getlength(lua_State *); @@ -155,9 +155,11 @@ MRP_LUA_METHOD_LIST_TABLE ( MRP_LUA_METHOD_CONSTRUCTOR (table_create_from_lua) ); +#if 0 MRP_LUA_METHOD_LIST_TABLE ( table_row_methods, /* methodlist name */ ); +#endif MRP_LUA_METHOD_LIST_TABLE ( select_methods, /* methodlist name */ @@ -231,8 +233,10 @@ mrp_lua_mdb_table_t *mrp_lua_create_builtin_table(lua_State *L, { mrp_lua_mdb_table_t *tbl = NULL; + MRP_UNUSED(L); + MRP_UNUSED(handle); - return NULL; + return tbl; } mrp_lua_mdb_table_t *mrp_lua_table_check(lua_State *L, int idx) @@ -310,6 +314,8 @@ static int table_create_from_lua(lua_State *L) size_t fldnamlen; const char *fldnam; + MRP_LUA_ENTER; + tbl = (mrp_lua_mdb_table_t *)mrp_lua_create_object(L, TABLE_CLASS, NULL); tbl->builtin = false; @@ -344,23 +350,27 @@ static int table_create_from_lua(lua_State *L) mrp_lua_set_object_name(L, TABLE_CLASS, tbl->name); - return 1; + MRP_LUA_LEAVE(1); } static int table_getfield(lua_State *L) { mrp_lua_mdb_table_t *tbl = mrp_lua_table_check(L, 1); + const char *fldnam; field_t fld; + MRP_LUA_ENTER; + if (lua_type(L, 2) == LUA_TNUMBER) { + mrp_debug("reading row %d in '%s'", lua_tointeger(L,-1), tbl->name); lua_rawget(L, 1); - - printf("*** reading row in table '%s'\n", tbl->name); } else { - fld = field_check(L, 2, NULL); + fld = field_check(L, 2, &fldnam); lua_pop(L, 1); + mrp_debug("reading '%s' property of '%s'", fldnam, tbl->name); + if (!tbl) lua_pushnil(L); else { @@ -373,14 +383,18 @@ static int table_getfield(lua_State *L) } } - return 1; + MRP_LUA_LEAVE(1); } static int table_setfield(lua_State *L) { - mrp_lua_mdb_table_t *tbl = mrp_lua_table_check(L, 1); + mrp_lua_mdb_table_t *tbl; size_t rowidx; + MRP_LUA_ENTER; + + tbl = mrp_lua_table_check(L, 1); + if (lua_type(L, 2) != LUA_TNUMBER) luaL_error(L, "'%s' is read-only", tbl->name); else { @@ -402,34 +416,41 @@ static int table_setfield(lua_State *L) luaL_checktype(L, -1, LUA_TTABLE); } - printf("*** setting row %u in table '%s'\n", rowidx+1, tbl->name); - + mrp_debug("setting row %u in table '%s'\n", rowidx+1, tbl->name); } - return 0; + MRP_LUA_LEAVE(1); } static int table_tostring(lua_State *L) { - mrp_lua_mdb_table_t *tbl = mrp_lua_table_check(L, 1); + mrp_lua_mdb_table_t *tbl; + + MRP_LUA_ENTER; + + tbl = mrp_lua_table_check(L, 1); if (tbl && tbl->name) lua_pushstring(L, tbl->name); else lua_pushstring(L, ""); - return 1; + MRP_LUA_LEAVE(1); } static void table_destroy_from_lua(void *data) { mrp_lua_mdb_table_t *tbl = (mrp_lua_mdb_table_t *)data; + MRP_LUA_ENTER; + if (tbl) { mrp_free((void *)tbl->name); mrp_lua_free_strarray(tbl->index); free_coldefs(tbl->columns); } + + MRP_LUA_LEAVE_NOARG; } static void table_row_class_create(lua_State *L) @@ -442,23 +463,33 @@ static void table_row_class_create(lua_State *L) luaL_openlib(L, NULL, table_row_overrides, 0); } +#if 0 static int table_row_create(lua_State *L, int tbl, void *data, int rowidx) { - return row_create(L, tbl, data, rowidx, TABLE_ROW_CLASSID); + int n; + + MRP_LUA_ENTER; + + n = row_create(L, tbl, data, rowidx, TABLE_ROW_CLASSID); + + MRP_LUA_LEAVE(n); } +#endif static int table_row_getfield(lua_State *L) { mrp_lua_mdb_table_t *tbl; int rowidx; + MRP_LUA_ENTER; + tbl = table_row_check(L, 1, &rowidx); - printf("*** reading field in row %d of '%s' table\n", rowidx+1, tbl->name); + mrp_debug("reading field in row %d of '%s' table\n", rowidx+1, tbl->name); lua_pushnil(L); - return 1; + MRP_LUA_LEAVE(1); } static int table_row_setfield(lua_State *L) @@ -466,20 +497,25 @@ static int table_row_setfield(lua_State *L) mrp_lua_mdb_table_t *tbl; int rowidx; + MRP_LUA_ENTER; + tbl = table_row_check(L, 1, &rowidx); - printf("*** writing field in row %d of '%s' table\n", rowidx+1, tbl->name); + mrp_debug("writing field in row %d of '%s' table\n", rowidx+1, tbl->name); - return 0; + MRP_LUA_LEAVE(0); } static int table_row_getlength(lua_State *L) { - mrp_lua_mdb_table_t *tbl = table_row_check(L, 1, NULL); - + mrp_lua_mdb_table_t *tbl; + + MRP_LUA_ENTER; + + tbl = table_row_check(L, 1, NULL); lua_pushinteger(L, tbl->ncolumn); - return 1; + MRP_LUA_LEAVE(1); } @@ -505,6 +541,8 @@ static int select_create_from_lua(lua_State *L) char cols[1024]; char qry[2048]; + MRP_LUA_ENTER; + sel = (mrp_lua_mdb_select_t *)mrp_lua_create_object(L, SELECT_CLASS, NULL); MRP_LUA_FOREACH_FIELD(L, 2, fldnam, fldnamlen) { @@ -560,11 +598,13 @@ static int select_create_from_lua(lua_State *L) mrp_lua_set_object_name(L, SELECT_CLASS, sel->name); + mrp_debug("select '%s' created", sel->name); + select_install(L, sel); select_update(L, -1, sel); - return 1; + MRP_LUA_LEAVE(1); } static int select_getfield(lua_State *L) @@ -573,18 +613,21 @@ static int select_getfield(lua_State *L) field_t fld; const char *fldnam; + MRP_LUA_ENTER; + if (!sel) lua_pushnil(L); else { if (lua_type(L, 2) == LUA_TNUMBER) { + mrp_debug("reading row %d in '%s'", lua_tointeger(L,-1),sel->name); lua_rawget(L, 1); - - printf("*** reading row in selection '%s'\n", sel->name); } else { fld = field_check(L, 2, &fldnam); lua_pop(L, 1); + mrp_debug("reading property %s in '%s'", fldnam, sel->name); + if (fld) { switch (fld) { case NAME: lua_pushstring(L, sel->name); break; @@ -603,16 +646,20 @@ static int select_getfield(lua_State *L) } } - return 1; + MRP_LUA_LEAVE(1); } static int select_setfield(lua_State *L) { - mrp_lua_mdb_select_t *sel = mrp_lua_select_check(L, 1); + mrp_lua_mdb_select_t *sel; + + MRP_LUA_ENTER; + + sel = mrp_lua_select_check(L, 1); luaL_error(L, "'%s' is read-only", sel->name); - return 0; + MRP_LUA_LEAVE(0); } @@ -620,6 +667,8 @@ static void select_destroy_from_lua(void *data) { mrp_lua_mdb_select_t *sel = (mrp_lua_mdb_select_t *)data; + MRP_LUA_ENTER; + if (sel) { mrp_lua_free_strarray(sel->columns); mrp_free((void *)sel->name); @@ -627,6 +676,8 @@ static void select_destroy_from_lua(void *data) mrp_free((void *)sel->condition); mrp_free((void *)sel->statement.string); } + + MRP_LUA_LEAVE_NOARG; } static int select_update(lua_State *L, int tbl, mrp_lua_mdb_select_t *sel) @@ -635,6 +686,8 @@ static int select_update(lua_State *L, int tbl, mrp_lua_mdb_select_t *sel) mql_result_t *result; int nrow; + MRP_LUA_ENTER; + if (!sel->statement.precomp) sel->statement.precomp = mql_precompile(sel->statement.string); @@ -654,28 +707,32 @@ static int select_update(lua_State *L, int tbl, mrp_lua_mdb_select_t *sel) } } - printf("*** query resulted %d rows\n", nrow); + mrp_debug("\"%s\" resulted %d rows", sel->statement.string, nrow); if (nrow >= 0) { adjust_lua_table_size(L, tbl,sel, sel->nrow, nrow, SELECT_ROW_CLASSID); sel->nrow = nrow; } - return nrow; + MRP_LUA_LEAVE(nrow); } static int select_update_from_lua(lua_State *L) { - mrp_lua_mdb_select_t *sel = mrp_lua_select_check(L, 1); + mrp_lua_mdb_select_t *sel; int nrow; - printf("*** update request for select '%s' (from lua)\n", sel->name); + MRP_LUA_ENTER; + + sel = mrp_lua_select_check(L, 1); + + mrp_debug("update request for select '%s'\n", sel->name); nrow = select_update(L, 1, sel); lua_pushinteger(L, nrow < 0 ? 0 : nrow); - return 1; + MRP_LUA_LEAVE(1); } static int select_update_from_resolver(mrp_scriptlet_t *script, @@ -685,12 +742,14 @@ static int select_update_from_resolver(mrp_scriptlet_t *script, lua_State *L = mrp_lua_get_lua_state(); int nrow; + MRP_LUA_ENTER; + MRP_UNUSED(ctbl); if (!sel || !L) return -EINVAL; - printf("*** update request for select '%s' (from resolver)\n", sel->name); + mrp_debug("update request for select '%s'", sel->name); mrp_lua_push_object(L, sel); @@ -698,7 +757,7 @@ static int select_update_from_resolver(mrp_scriptlet_t *script, lua_pop(L, 1); - return nrow; + MRP_LUA_LEAVE(nrow); } @@ -718,6 +777,8 @@ static void select_install(lua_State *L, mrp_lua_mdb_select_t *sel) char target[1024], table[1024]; const char *depends; + MRP_LUA_ENTER; + MRP_UNUSED(L); ctx = mrp_lua_get_murphy_context(); @@ -740,6 +801,8 @@ static void select_install(lua_State *L, mrp_lua_mdb_select_t *sel) printf("Failed to install resolver target for element '%s'.\n", sel->name); } + + MRP_LUA_LEAVE_NOARG; } static void select_row_class_create(lua_State *L) @@ -752,10 +815,18 @@ static void select_row_class_create(lua_State *L) luaL_openlib(L, NULL, select_row_overrides, 0); } +#if 0 static int select_row_create(lua_State *L, int tbl, void *data, int rowidx) { - return row_create(L, tbl, data, rowidx, SELECT_ROW_CLASSID); + int n; + + MRP_LUA_ENTER; + + n = row_create(L, tbl, data, rowidx, SELECT_ROW_CLASSID); + + MRP_LUA_LEAVE(n); } +#endif static int select_row_getfield(lua_State *L) { @@ -769,12 +840,14 @@ static int select_row_getfield(lua_State *L) lua_Number number; char buf[1024]; + MRP_LUA_ENTER; + sel = select_row_check(L, 1, &rowidx); cols = sel->columns; rslt = sel->result; - printf("*** reading field in row %d of '%s' selection\n", - rowidx+1, sel ? sel->name : ""); + mrp_debug("reading field in row %d of '%s' selection\n", + rowidx+1, sel ? sel->name : ""); if (!sel || !rslt || (size_t)rowidx >= sel->nrow) lua_pushnil(L); /* we should never get here actually */ @@ -820,7 +893,7 @@ static int select_row_getfield(lua_State *L) break; } - return 1; + MRP_LUA_LEAVE(1); } static int select_row_setfield(lua_State *L) @@ -828,21 +901,26 @@ static int select_row_setfield(lua_State *L) mrp_lua_mdb_select_t *sel; int rowidx; + MRP_LUA_ENTER; + sel = select_row_check(L, 1, &rowidx); luaL_error(L, "attempt to write row %u of read-only selection '%s'", rowidx+1, sel->name); - return 0; + MRP_LUA_LEAVE(0); } static int select_row_getlength(lua_State *L) { - mrp_lua_mdb_select_t *sel = select_row_check(L, 1, NULL); - + mrp_lua_mdb_select_t *sel; + + MRP_LUA_ENTER; + + sel = select_row_check(L, 1, NULL); lua_pushinteger(L, sel->columns->nstring); - return 1; + MRP_LUA_LEAVE(1); } -- 2.7.4