summaryrefslogtreecommitdiff
path: root/src/plugin-luagmp.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugin-luagmp.cc')
-rw-r--r--src/plugin-luagmp.cc42
1 files changed, 23 insertions, 19 deletions
diff --git a/src/plugin-luagmp.cc b/src/plugin-luagmp.cc
index 354c48c..d584603 100644
--- a/src/plugin-luagmp.cc
+++ b/src/plugin-luagmp.cc
@@ -31,9 +31,9 @@
#define WEAK
#endif
-static const char lgmp_metarand[] = "randmeta";
-static const char lgmp_metaz[] = "zmeta";
-static const char lgmp_metaf[] = "fmeta";
+static const char lgmp_metarand[] = "lgmp_randmeta";
+static const char lgmp_metaz[] = "lgmp_zmeta";
+static const char lgmp_metaf[] = "lgmp_fmeta";
static void lgmp_free_str(char *data)
{
@@ -50,7 +50,7 @@ static void lgmp_free_str(char *data)
static gmp_randstate_t *lgmp_rawrand(lua_State *L)
{
gmp_randstate_t *pr = (gmp_randstate_t *)lua_newuserdata(L, sizeof(gmp_randstate_t));
- lua_getfield(L, LUA_ENVIRONINDEX, lgmp_metarand);
+ luaL_getmetatable(L, lgmp_metarand);
lua_setmetatable(L, -2);
return pr;
@@ -69,7 +69,7 @@ static mpz_t *lgmp_toz(lua_State *L, int idx)
static mpz_t *lgmp_rawz(lua_State *L)
{
mpz_t *pz = (mpz_t *)lua_newuserdata(L, sizeof(mpz_t));
- lua_getfield(L, LUA_ENVIRONINDEX, lgmp_metaz);
+ luaL_getmetatable(L, lgmp_metaz);
lua_setmetatable(L, -2);
return pz;
@@ -102,7 +102,7 @@ static mpf_t *lgmp_tof(lua_State *L, int idx)
static mpf_t *lgmp_rawf(lua_State *L)
{
mpf_t *pf = (mpf_t *)lua_newuserdata(L, sizeof(mpf_t));
- lua_getfield(L, LUA_ENVIRONINDEX, lgmp_metaf);
+ luaL_getmetatable(L, lgmp_metaf);
lua_setmetatable(L, -2);
return pf;
@@ -2476,21 +2476,18 @@ static const luaL_Reg lgmp_prv[] =
static int lgmp_initialize(lua_State *L)
{
lua_pushnumber(L, ULONG_MAX);
- lua_setfield(L, 1, "ULONG_MAX");
+ lua_setfield(L, -2, "ULONG_MAX");
lua_pushnumber(L, LONG_MIN);
- lua_setfield(L, 1, "LONG_MIN");
+ lua_setfield(L, -2, "LONG_MIN");
lua_pushnumber(L, LONG_MAX);
- lua_setfield(L, 1, "LONG_MAX");
+ lua_setfield(L, -2, "LONG_MAX");
lua_pushstring(L, gmp_version);
- lua_setfield(L, 1, "version");
+ lua_setfield(L, -2, "version");
- lua_pushvalue(L, 2);
- lua_replace(L, LUA_ENVIRONINDEX);
-
- lua_pushvalue(L, 1);
+ lua_pushvalue(L, -1);
luaL_register(L, NULL, lgmp_prv);
return 0;
@@ -2503,16 +2500,24 @@ extern unsigned char lgmp[];
static int _init_plugin(Lua *L)
{
- while (L->gettop()) L->remove(1);
- L->newtable(); // [1] = prv
- L->newtable(); // [2] = aux
+ L->newtable(); // prv
L->wrap_open(lgmp_initialize);
Buffer lgmp_buff;
lgmp_buff.write(lgmp, size_lgmp);
L->load(&lgmp_buff);
- L->call("lgmp_lua_init", LUA_GLOBALSINDEX, 2, 0);
+ L->push("lgmp_lua_init");
+ L->gettable(LUA_GLOBALSINDEX);
+ L->insert(-2);
+ L->call(1, 1);
+
+ L->push();
+ while (L->next() != 0) {
+ L->push("lgmp_" + L->tostring(-2));
+ L->insert(-2);
+ L->settable(LUA_REGISTRYINDEX);
+ }
return 0;
}
@@ -2530,4 +2535,3 @@ void luagmp_init(Lua * L) {
}
}
-