summaryrefslogtreecommitdiff
path: root/lcrypt
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2013-08-09 20:02:51 +0200
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2013-08-09 20:02:51 +0200
commite5001bd5fc52a039a340b214e736a23fed07c6b2 (patch)
treece2553821aec305f30346c4732071aa3e5c5e3bc /lcrypt
parent7bedd837c040bcbc4fac4c96fa01af6d120c520e (diff)
Optimizing that glue code a bit; the built-in lua code no longer depends on any global nor the object's methods, and isbigint properly returns true or false.
Diffstat (limited to 'lcrypt')
-rw-r--r--lcrypt/lcrypt_math.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/lcrypt/lcrypt_math.c b/lcrypt/lcrypt_math.c
index 258c8e9..6563129 100644
--- a/lcrypt/lcrypt_math.c
+++ b/lcrypt/lcrypt_math.c
@@ -271,22 +271,35 @@ static const struct luaL_reg lcrypt_bigint_flib[] =
#define METAMETHOD_WRAPPER(L, f) \
lua_pushstring(L, "__" #f); \
luaL_dostring(L, "" \
-"return function(a, b) \n" \
-" local aisb = pcall(lcrypt.isbigint, a) \n" \
-" local bisb = pcall(lcrypt.isbigint, b) \n" \
-" if not aisb then \n" \
-" a = lcrypt.bigint(a) \n" \
+"return function(isbigint, bigint, op) return function(a, b) \n" \
+" if not pcall(isbigint, a) then \n" \
+" a = bigint(a) \n" \
" end \n" \
-" if not bisb then \n" \
-" b = lcrypt.bigint(b) \n" \
+" if not pcall(isbigint, b) then \n" \
+" b = bigint(b) \n" \
" end \n" \
-" return a:" #f "(b) \n" \
-"end \n" \
+" return op(a, b) \n" \
+"end end \n" \
""); \
+ lua_pushcfunction(L, lcrypt_bigint_isbigint); \
+ lua_pushcfunction(L, lcrypt_bigint_create); \
+ lua_pushcfunction(L, lcrypt_bigint_##f); \
+ lua_pcall(L, 3, LUA_MULTRET, 0); \
lua_settable(L, -3)
static void lcrypt_start_math(lua_State *L)
{
+ ltc_mp = ltm_desc;
+
+ lua_pushstring(L, "bigint");
+ lua_pushcfunction(L, lcrypt_bigint_create);
+ lua_settable(L, -3);
+ lua_pushstring(L, "isbigint");
+ luaL_dostring(L, "return function (cisbigint) return function(a) return pcall(cisbigint, a) end end");
+ lua_pushcfunction(L, lcrypt_bigint_isbigint);
+ lua_pcall(L, 1, LUA_MULTRET, 0);
+ lua_settable(L, -3);
+
luaL_newmetatable(L, "LCRYPT_BIGINT");
luaL_register(L, NULL, lcrypt_bigint_flib);
METAMETHOD_WRAPPER(L, add);
@@ -299,12 +312,4 @@ static void lcrypt_start_math(lua_State *L)
METAMETHOD_WRAPPER(L, le);
lua_pop(L, 1);
- ltc_mp = ltm_desc;
-
- lua_pushstring(L, "bigint");
- lua_pushcfunction(L, lcrypt_bigint_create);
- lua_settable(L, -3);
- lua_pushstring(L, "isbigint");
- lua_pushcfunction(L, lcrypt_bigint_isbigint);
- lua_settable(L, -3);
}