From e5001bd5fc52a039a340b214e736a23fed07c6b2 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Fri, 9 Aug 2013 20:02:51 +0200 Subject: 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. --- lcrypt/lcrypt_math.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'lcrypt/lcrypt_math.c') 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); } -- cgit v1.2.3