webmcp

changeset 396:762ab1e87702

Use uservalues rather than ephemeron tables in mondelefant
author jbe
date Wed Dec 09 21:16:24 2015 +0100 (2015-12-09)
parents ae1a66bb6b7d
children 46ba2168693a
files libraries/mondelefant/mondelefant_native.c
line diff
     1.1 --- a/libraries/mondelefant/mondelefant_native.c	Wed Dec 09 05:23:42 2015 +0100
     1.2 +++ b/libraries/mondelefant/mondelefant_native.c	Wed Dec 09 21:16:24 2015 +0100
     1.3 @@ -15,8 +15,6 @@
     1.4  #define MONDELEFANT_MODULE_REGKEY (MONDELEFANT_REGKEY "module")
     1.5  // registry key of meta-table for database connections:
     1.6  #define MONDELEFANT_CONN_MT_REGKEY (MONDELEFANT_REGKEY "connection")
     1.7 -// registry key of table storing connection specific data:
     1.8 -#define MONDELEFANT_CONN_DATA_REGKEY (MONDELEFANT_REGKEY "connection_data")
     1.9  // registry key of meta-table for database result lists and objects:
    1.10  #define MONDELEFANT_RESULT_MT_REGKEY (MONDELEFANT_REGKEY "result")
    1.11  // registry key of meta-table for database error objects:
    1.12 @@ -306,15 +304,12 @@
    1.13    // set meta-table of userdata:
    1.14    luaL_getmetatable(L, MONDELEFANT_CONN_MT_REGKEY);  // 2
    1.15    lua_setmetatable(L, 1);
    1.16 -  // create entry in table storing connection specific data and associate
    1.17 -  // created userdata with it:
    1.18 -  lua_getfield(L, LUA_REGISTRYINDEX, MONDELEFANT_CONN_DATA_REGKEY);  // 2
    1.19 -  lua_pushvalue(L, 1);  // 3
    1.20 -  lua_newtable(L);  // 4
    1.21 -  lua_pushinteger(L, PQsocket(pgconn));  // determine file descriptor
    1.22 -  lua_setfield(L, 4, "fd");  // set "fd" attribute
    1.23 -  lua_settable(L, 2);
    1.24 -  lua_settop(L, 1);
    1.25 +  // create and associate userdata table:
    1.26 +  lua_newtable(L);
    1.27 +  lua_setuservalue(L, 1);
    1.28 +  // store key "fd" with file descriptor of connection:
    1.29 +  lua_pushinteger(L, PQsocket(pgconn));
    1.30 +  lua_setfield(L, 1, "fd");
    1.31    // store key "engine" with value "postgresql" as connection specific data:
    1.32    lua_pushliteral(L, "postgresql");
    1.33    lua_setfield(L, 1, "engine");
    1.34 @@ -338,10 +333,7 @@
    1.35  static int mondelefant_conn_index(lua_State *L) {
    1.36    // try table for connection specific data:
    1.37    lua_settop(L, 2);
    1.38 -  lua_getfield(L, LUA_REGISTRYINDEX, MONDELEFANT_CONN_DATA_REGKEY);  // 3
    1.39 -  lua_pushvalue(L, 1);  // 4
    1.40 -  lua_gettable(L, 3);  // 4
    1.41 -  lua_remove(L, 3);  // connection specific data-table at stack position 3
    1.42 +  lua_getuservalue(L, 1);  // 3
    1.43    lua_pushvalue(L, 2);  // 4
    1.44    lua_gettable(L, 3);  // 4
    1.45    if (!lua_isnil(L, 4)) return 1;
    1.46 @@ -378,10 +370,7 @@
    1.47  static int mondelefant_conn_newindex(lua_State *L) {
    1.48    // store key-value pair in table for connection specific data:
    1.49    lua_settop(L, 3);
    1.50 -  lua_getfield(L, LUA_REGISTRYINDEX, MONDELEFANT_CONN_DATA_REGKEY);  // 4
    1.51 -  lua_pushvalue(L, 1);  // 5
    1.52 -  lua_gettable(L, 4);  // 5
    1.53 -  lua_remove(L, 4);  // connection specific data-table at stack position 4
    1.54 +  lua_getuservalue(L, 1);  // 4
    1.55    lua_pushvalue(L, 2);
    1.56    lua_pushvalue(L, 3);
    1.57    lua_settable(L, 4);
    1.58 @@ -405,11 +394,8 @@
    1.59    conn = mondelefant_get_conn(L, 1);
    1.60    PQfinish(conn->pgconn);
    1.61    conn->pgconn = NULL;
    1.62 -  lua_getfield(L, LUA_REGISTRYINDEX, MONDELEFANT_CONN_DATA_REGKEY);  // 2
    1.63 -  lua_pushvalue(L, 1);  // 3
    1.64 -  lua_gettable(L, 2);  // 3
    1.65 -  lua_pushnil(L);  // 4
    1.66 -  lua_setfield(L, 3, "fd");  // set "fd" attribute to nil
    1.67 +  lua_pushnil(L);  // 2
    1.68 +  lua_setfield(L, 1, "fd");  // set "fd" attribute to nil
    1.69    return 0;
    1.70  }
    1.71  
    1.72 @@ -1990,15 +1976,6 @@
    1.73    lua_setfield(L, LUA_REGISTRYINDEX, MONDELEFANT_CLASS_PROTO_REGKEY);
    1.74    lua_setfield(L, 1, "class_prototype");
    1.75  
    1.76 -  lua_newtable(L);  // 2
    1.77 -  lua_pushliteral(L, "k");  // 3
    1.78 -  lua_setfield(L, 2, "__mode");
    1.79 -  lua_newtable(L);  // 3
    1.80 -  lua_pushvalue(L, 2);  // 4
    1.81 -  lua_setmetatable(L, 3);
    1.82 -  lua_setfield(L, LUA_REGISTRYINDEX, MONDELEFANT_CONN_DATA_REGKEY);
    1.83 -  lua_settop(L, 1);
    1.84 -
    1.85    luaL_newmetatable(L, MONDELEFANT_ERROROBJECT_MT_REGKEY);  // 2
    1.86  #if LUA_VERSION_NUM >= 502
    1.87    luaL_setfuncs(L, mondelefant_errorobject_mt_functions, 0);

Impressum / About Us