webmcp

diff libraries/mondelefant/mondelefant_native.c @ 64:3d43a5cf17c1

Compatibility with Lua 5.2
author jbe
date Sun Apr 15 16:04:33 2012 +0200 (2012-04-15)
parents ed00b972f40e
children 8d7665e0d490
line diff
     1.1 --- a/libraries/mondelefant/mondelefant_native.c	Tue Apr 03 00:56:02 2012 +0200
     1.2 +++ b/libraries/mondelefant/mondelefant_native.c	Sun Apr 15 16:04:33 2012 +0200
     1.3 @@ -632,7 +632,11 @@
     1.4          }
     1.5          luaL_pushresult(&keybuf);
     1.6          // check, if any characters matched:
     1.7 +#if LUA_VERSION_NUM >= 502
     1.8 +        if (lua_rawlen(L, -1)) {
     1.9 +#else
    1.10          if (lua_objlen(L, -1)) {
    1.11 +#endif
    1.12            // if any alpha numeric chars or underscores were found,
    1.13            // push them on stack as a Lua string and use them to lookup
    1.14            // value from second argument:
    1.15 @@ -683,7 +687,11 @@
    1.16              );
    1.17            }
    1.18            // iterate over items of sub-structure:
    1.19 +#if LUA_VERSION_NUM >= 502
    1.20 +          count = lua_rawlen(L, 5);
    1.21 +#else
    1.22            count = lua_objlen(L, 5);
    1.23 +#endif
    1.24            for (i = 0; i < count; i++) {
    1.25              // add seperator, unless this is the first run:
    1.26              if (i) {
    1.27 @@ -1137,9 +1145,17 @@
    1.28      lua_rawgeti(L, 3, command_idx+1);  // raw result at stack position 5
    1.29      if (lua_toboolean(L, 5)) {
    1.30        lua_getfield(L, 5, "_column_info");  // column_info list at position 6
    1.31 +#if LUA_VERSION_NUM >= 502
    1.32 +      cols = lua_rawlen(L, 6);
    1.33 +#else
    1.34        cols = lua_objlen(L, 6);
    1.35 +#endif
    1.36        if (mode == MONDELEFANT_QUERY_MODE_LIST) {
    1.37 +#if LUA_VERSION_NUM >= 502
    1.38 +        rows = lua_rawlen(L, 5);
    1.39 +#else
    1.40          rows = lua_objlen(L, 5);
    1.41 +#endif
    1.42          for (row = 0; row < rows; row++) {
    1.43            lua_rawgeti(L, 5, row+1);  // row at stack position 7
    1.44            lua_getfield(L, 7, "_data");  // _data table at stack position 8
    1.45 @@ -1260,12 +1276,20 @@
    1.46    lua_settop(L, 2);
    1.47    lua_getmetatable(L, 1);  // 3
    1.48    lua_getfield(L, LUA_REGISTRYINDEX, MONDELEFANT_RESULT_MT_REGKEY);  // 4
    1.49 +#if LUA_VERSION_NUM >= 502
    1.50 +  luaL_argcheck(L, lua_compare(L, 3, 4, LUA_OPEQ), 1, "not a database result");
    1.51 +#else
    1.52    luaL_argcheck(L, lua_equal(L, 3, 4), 1, "not a database result");
    1.53 +#endif
    1.54    // ensure that second argument is a database class (model):
    1.55    lua_settop(L, 2);
    1.56    lua_getmetatable(L, 2);  // 3
    1.57    lua_getfield(L, LUA_REGISTRYINDEX, MONDELEFANT_CLASS_MT_REGKEY);  // 4
    1.58 +#if LUA_VERSION_NUM >= 502
    1.59 +  luaL_argcheck(L, lua_compare(L, 3, 4, LUA_OPEQ), 2, "not a database class");
    1.60 +#else
    1.61    luaL_argcheck(L, lua_equal(L, 3, 4), 2, "not a database class");
    1.62 +#endif
    1.63    // set attribute "_class" of result list/object to given class:
    1.64    lua_settop(L, 2);
    1.65    lua_pushvalue(L, 2);  // 3
    1.66 @@ -1276,7 +1300,11 @@
    1.67    if (lua_rawequal(L, 3, 4)) {
    1.68      int i;
    1.69      // set attribute "_class" of all elements to given class:
    1.70 +#if LUA_VERSION_NUM >= 502
    1.71 +    for (i=0; i < lua_rawlen(L, 1); i++) {
    1.72 +#else
    1.73      for (i=0; i < lua_objlen(L, 1); i++) {
    1.74 +#endif
    1.75        lua_settop(L, 2);
    1.76        lua_rawgeti(L, 1, i+1);  // 3
    1.77        lua_pushvalue(L, 2);  // 4
    1.78 @@ -1740,38 +1768,66 @@
    1.79  int luaopen_mondelefant_native(lua_State *L) {
    1.80    lua_settop(L, 0);
    1.81    lua_newtable(L);  // module at stack position 1
    1.82 +#if LUA_VERSION_NUM >= 502
    1.83 +  luaL_setfuncs(L, mondelefant_module_functions, 0);
    1.84 +#else
    1.85    luaL_register(L, NULL, mondelefant_module_functions);
    1.86 +#endif
    1.87  
    1.88    lua_pushvalue(L, 1);  // 2
    1.89    lua_setfield(L, LUA_REGISTRYINDEX, MONDELEFANT_MODULE_REGKEY);
    1.90  
    1.91    lua_newtable(L);  // 2
    1.92    // NOTE: only PostgreSQL is supported yet:
    1.93 +#if LUA_VERSION_NUM >= 502
    1.94 +  luaL_setfuncs(L, mondelefant_conn_methods, 0);
    1.95 +#else
    1.96    luaL_register(L, NULL, mondelefant_conn_methods);
    1.97 +#endif
    1.98    lua_setfield(L, 1, "postgresql_connection_prototype");
    1.99    lua_newtable(L);  // 2
   1.100    lua_setfield(L, 1, "connection_prototype");
   1.101  
   1.102    luaL_newmetatable(L, MONDELEFANT_CONN_MT_REGKEY);  // 2
   1.103 +#if LUA_VERSION_NUM >= 502
   1.104 +  luaL_setfuncs(L, mondelefant_conn_mt_functions, 0);
   1.105 +#else
   1.106    luaL_register(L, NULL, mondelefant_conn_mt_functions);
   1.107 +#endif
   1.108    lua_settop(L, 1);
   1.109    luaL_newmetatable(L, MONDELEFANT_RESULT_MT_REGKEY);  // 2
   1.110 +#if LUA_VERSION_NUM >= 502
   1.111 +  luaL_setfuncs(L, mondelefant_result_mt_functions, 0);
   1.112 +#else
   1.113    luaL_register(L, NULL, mondelefant_result_mt_functions);
   1.114 +#endif
   1.115    lua_setfield(L, 1, "result_metatable");
   1.116    luaL_newmetatable(L, MONDELEFANT_CLASS_MT_REGKEY);  // 2
   1.117 +#if LUA_VERSION_NUM >= 502
   1.118 +  luaL_setfuncs(L, mondelefant_class_mt_functions, 0);
   1.119 +#else
   1.120    luaL_register(L, NULL, mondelefant_class_mt_functions);
   1.121 +#endif
   1.122    lua_setfield(L, 1, "class_metatable");
   1.123  
   1.124    lua_newtable(L);  // 2
   1.125    lua_newtable(L);  // 3
   1.126 +#if LUA_VERSION_NUM >= 502
   1.127 +  luaL_setfuncs(L, mondelefant_object_methods, 0);
   1.128 +#else
   1.129    luaL_register(L, NULL, mondelefant_object_methods);
   1.130 +#endif
   1.131    lua_setfield(L, 2, "object");
   1.132    lua_newtable(L);  // 3
   1.133    lua_setfield(L, 2, "object_get");
   1.134    lua_newtable(L);  // 3
   1.135    lua_setfield(L, 2, "object_set");
   1.136    lua_newtable(L);  // 3
   1.137 +#if LUA_VERSION_NUM >= 502
   1.138 +  luaL_setfuncs(L, mondelefant_list_methods, 0);
   1.139 +#else
   1.140    luaL_register(L, NULL, mondelefant_list_methods);
   1.141 +#endif
   1.142    lua_setfield(L, 2, "list");
   1.143    lua_newtable(L);  // 3
   1.144    lua_setfield(L, 2, "references");
   1.145 @@ -1791,9 +1847,17 @@
   1.146    lua_settop(L, 1);
   1.147  
   1.148    luaL_newmetatable(L, MONDELEFANT_ERROROBJECT_MT_REGKEY);  // 2
   1.149 +#if LUA_VERSION_NUM >= 502
   1.150 +  luaL_setfuncs(L, mondelefant_errorobject_mt_functions, 0);
   1.151 +#else
   1.152    luaL_register(L, NULL, mondelefant_errorobject_mt_functions);
   1.153 +#endif
   1.154    lua_newtable(L);  // 3
   1.155 +#if LUA_VERSION_NUM >= 502
   1.156 +  luaL_setfuncs(L, mondelefant_errorobject_methods, 0);
   1.157 +#else
   1.158    luaL_register(L, NULL, mondelefant_errorobject_methods);
   1.159 +#endif
   1.160    lua_setfield(L, 2, "__index");
   1.161    lua_setfield(L, 1, "errorobject_metatable");
   1.162  

Impressum / About Us