webmcp

diff libraries/multirand/multirand.c @ 64:3d43a5cf17c1

Compatibility with Lua 5.2
author jbe
date Sun Apr 15 16:04:33 2012 +0200 (2012-04-15)
parents 9fdfb27f8e67
children 593413f317f4
line diff
     1.1 --- a/libraries/multirand/multirand.c	Tue Apr 03 00:56:02 2012 +0200
     1.2 +++ b/libraries/multirand/multirand.c	Sun Apr 15 16:04:33 2012 +0200
     1.3 @@ -1,6 +1,5 @@
     1.4  #include <lua.h>
     1.5  #include <lauxlib.h>
     1.6 -#include <dirent.h>
     1.7  
     1.8  static FILE *multirand_dev;
     1.9  
    1.10 @@ -43,7 +42,7 @@
    1.11    lua_settop(L, 2);
    1.12    arg1 = luaL_checkinteger(L, 1);
    1.13    if (lua_toboolean(L, 2)) {
    1.14 -    arg2 = luaL_optinteger(L, 2, 0);
    1.15 +    arg2 = luaL_checkinteger(L, 2);
    1.16      if (arg1 > arg2) {
    1.17        return luaL_error(L,
    1.18          "Upper boundary is smaller than lower boundary."
    1.19 @@ -109,16 +108,13 @@
    1.20  };
    1.21  
    1.22  int luaopen_multirand(lua_State *L) {
    1.23 -  const char *module_name;
    1.24 -  lua_settop(L, 1);
    1.25 -  module_name = lua_tostring(L, 1);
    1.26 -  if (module_name) {
    1.27 -    luaL_register(L, module_name, multirand_module_functions);
    1.28 -  } else {
    1.29 -    luaL_register(L, "multirand", multirand_module_functions);
    1.30 -  }
    1.31 -  lua_replace(L, 1);
    1.32    multirand_dev = fopen("/dev/urandom", "r");
    1.33    if (!multirand_dev) return luaL_error(L, "Could not open /dev/urandom.");
    1.34 +#if LUA_VERSION_NUM >= 502
    1.35 +  lua_newtable(L);
    1.36 +  luaL_setfuncs(L, multirand_module_functions, 0);
    1.37 +#else
    1.38 +  luaL_register(L, lua_tostring(L, 1) || "multirand", multirand_module_functions);
    1.39 +#endif
    1.40    return 1;
    1.41  }

Impressum / About Us