diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-08-25 13:02:02 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-08-25 15:42:12 +0100 |
commit | bdb91fe0c6d7cba2fcf4e3a022c60c29b0082061 (patch) | |
tree | 67fde253be81c13151da3555f3d325c273847e8b /src/lua | |
parent | 38a2af4b9ffab4e18364bbe87c238df0a9320a58 (diff) | |
download | rspamd-bdb91fe0c6d7cba2fcf4e3a022c60c29b0082061.tar.gz rspamd-bdb91fe0c6d7cba2fcf4e3a022c60c29b0082061.zip |
[Minor] Lua_tensor: Add __len method
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/lua_tensor.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/lua/lua_tensor.c b/src/lua/lua_tensor.c index 1506d4548..4fcc7e201 100644 --- a/src/lua/lua_tensor.c +++ b/src/lua/lua_tensor.c @@ -34,6 +34,7 @@ LUA_FUNCTION_DEF (tensor, mul); LUA_FUNCTION_DEF (tensor, tostring); LUA_FUNCTION_DEF (tensor, index); LUA_FUNCTION_DEF (tensor, newindex); +LUA_FUNCTION_DEF (tensor, len); static luaL_reg rspamd_tensor_f[] = { LUA_INTERFACE_DEF (tensor, load), @@ -51,6 +52,7 @@ static luaL_reg rspamd_tensor_m[] = { {"__tostring", lua_tensor_tostring}, {"__index", lua_tensor_index}, {"__newindex", lua_tensor_newindex}, + {"__len", lua_tensor_len}, {NULL, NULL}, }; @@ -561,6 +563,30 @@ lua_tensor_load (lua_State *L) } static gint +lua_tensor_len (lua_State *L) +{ + struct rspamd_lua_tensor *t = lua_check_tensor (L, 1); + gint nret = 1; + + if (t) { + /* Return the main dimension first */ + if (t->ndims == 1) { + lua_pushinteger (L, t->dim[0]); + } + else { + lua_pushinteger (L, t->dim[0]); + lua_pushinteger (L, t->dim[1]); + nret = 2; + } + } + else { + return luaL_error (L, "invalid arguments"); + } + + return nret; +} + +static gint lua_load_tensor (lua_State * L) { lua_newtable (L); |