summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2010-06-08 02:19:00 +0200
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2010-06-08 02:19:00 +0200
commit4ca45dec37b8e7a946d1945d8bb27e5a2a81b394 (patch)
tree645ef49f5b0562ea6dc2470150df49852001b2b9
parent69f364d7fe93e317cf7e315ef0eea400a3452c0b (diff)
More tweaks and fixes to luagmp.
-rw-r--r--src/lgmp.lua48
-rw-r--r--src/plugin-luagmp.cc42
2 files changed, 48 insertions, 42 deletions
diff --git a/src/lgmp.lua b/src/lgmp.lua
index 98bc6af..38da3ee 100644
--- a/src/lgmp.lua
+++ b/src/lgmp.lua
@@ -20,15 +20,21 @@ local error = error
local match = string.match
local prv
-local aux
+local aux = {}
local randmeta = {}
local zmeta = {}
local fmeta = {}
+aux.randmeta = randmeta
+aux.zmeta = zmeta
+aux.fmeta = fmeta
+
randmeta.__index = randmeta
zmeta.__index = zmeta
fmeta.__index = fmeta
+gmp = {}
+
local function isrand(obj)
return type(obj) == "userdata" and getmetatable(obj) == randmeta
end
@@ -100,7 +106,7 @@ end
local dtoz
local dtof
-function z(value, base)
+function gmp.z(value, base)
if value == nil then
return prv.mpz_init()
elseif type(value) == "number" then
@@ -125,7 +131,7 @@ function z(value, base)
end
end
-function f(value, base)
+function gmp.f(value, base)
if value == nil then
return prv.mpf_init()
elseif type(value) == "number" then
@@ -303,7 +309,7 @@ function zmeta:bin(a, res)
return prv.mpz_bin_ui(self, a, res)
end
-function bin(a1, a2, res)
+function gmp.bin(a1, a2, res)
checku(a2)
checkzopt(res)
local t = ntype(a1)
@@ -521,7 +527,7 @@ function zmeta:divisible_2exp(a)
return prv.mpz_divisible_2exp_p(self, a) ~= 0
end
-function fac(a, res)
+function gmp.fac(a, res)
checku(a)
checkzopt(res)
return prv.mpz_fac_ui(a, res)
@@ -609,13 +615,13 @@ function zmeta:fdiv(a)
end
end
-function fib(a, res)
+function gmp.fib(a, res)
checku(a)
checkzopt(res)
return prv.mpz_fib_ui(a, res)
end
-function fib2(a, r1, r2)
+function gmp.fib2(a, r1, r2)
checku(a)
checkzopt(r1)
checkzopt(r2)
@@ -782,13 +788,13 @@ function zmeta:lcm(a, res)
end
end
-function lucnum(a, res)
+function gmp.lucnum(a, res)
checku(a)
checkzopt(res)
return prv.mpz_lucnum_ui(a, res)
end
-function lucnum2(a, r1, r2)
+function gmp.lucnum2(a, r1, r2)
checku(a)
checkzopt(r1)
checkzopt(r2)
@@ -1134,7 +1140,7 @@ function zmeta:tstbit(a)
return prv.mpz_tstbit(self, a)
end
-function pow(a1, a2, res)
+function gmp.pow(a1, a2, res)
checku(a2)
checkzopt(res)
local t = ntype(a1)
@@ -1343,7 +1349,7 @@ function fmeta:reldiff(a, res)
return prv.mpf_reldiff(self, a, res)
end
-function set_default_prec(a)
+function gmp.set_default_prec(a)
checku(a)
prv.mpf_set_default_prec(a)
end
@@ -1389,7 +1395,7 @@ function fmeta:sqrt(res)
return prv.mpf_sqrt(self, res)
end
-function sqrt(a, res)
+function gmp.sqrt(a, res)
checku(a)
checkfopt(res)
return prv.mpf_sqrt_ui(a, res)
@@ -1532,7 +1538,7 @@ function fmeta:__gc()
return prv.mpf_clear(self)
end
-function rand(a)
+function gmp.rand(a)
if a == nil then
return prv.gmp_randinit_default()
else
@@ -1622,21 +1628,17 @@ function fmeta:format(fmt, p)
return prv.mpf_asprintf("%" .. fw .. prec .. "F" .. conv, self, p)
end
-function lgmp_lua_init(l_prv, l_aux)
+function lgmp_lua_init(l_prv)
prv = l_prv
- aux = l_aux
-
- aux.randmeta = randmeta
- aux.zmeta = zmeta
- aux.fmeta = fmeta
-
- lgmpversion = prv.version
dtoz = prv.mpz_init_set_d
dtof = prv.mpf_init_set_d
- get_default_prec = prv.mpf_get_default_prec
+ gmp.get_default_prec = prv.mpf_get_default_prec
+ gmp.version = prv.version
+
+ lgmp_lua_init = nil
- lgmp_init = nil
+ return aux
end
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) {
}
}
-