Skip to content

Commit

Permalink
[Minor] Allow rspamd_lua_require_function to work without function
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Aug 18, 2023
1 parent dbaf8c6 commit eb6f412
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
9 changes: 5 additions & 4 deletions src/libutil/cxx/hash_util.hxx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*-
* Copyright 2021 Vsevolod Stakhov
/*
* Copyright 2023 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -59,6 +59,7 @@ struct smart_ptr_equal {
template<typename T>
struct smart_ptr_hash {
using is_transparent = void; /* We want to find values in a set of shared_ptr by reference */
using is_avalanching = void;
auto operator()(const std::shared_ptr<T> &a) const
{
return std::hash<T>()(*a);
Expand Down Expand Up @@ -92,7 +93,7 @@ struct smart_str_equal {

struct smart_str_hash {
using is_transparent = void;
using is_avalanching = typename ankerl::unordered_dense::hash<std::string_view>::is_avalanching;
using is_avalanching = void;
auto operator()(const std::string &a) const
{
return ankerl::unordered_dense::hash<std::string>()(a);
Expand Down
48 changes: 30 additions & 18 deletions src/lua/lua_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2251,33 +2251,45 @@ rspamd_lua_require_function(lua_State *L, const gchar *modname,
lua_remove(L, err_pos);

/* Now we should have a table with results */
if (!lua_istable(L, -1)) {
msg_warn("require of %s.%s failed: not a table but %s", modname,
funcname, lua_typename(L, lua_type(L, -1)));
if (funcname) {
if (!lua_istable(L, -1)) {
msg_warn("require of %s.%s failed: not a table but %s", modname,
funcname, lua_typename(L, lua_type(L, -1)));

lua_pop(L, 1);
lua_pop(L, 1);

return FALSE;
}
return FALSE;
}

table_pos = lua_gettop(L);
lua_pushstring(L, funcname);
lua_gettable(L, -2);
table_pos = lua_gettop(L);
lua_pushstring(L, funcname);
lua_gettable(L, -2);

if (lua_type(L, -1) == LUA_TFUNCTION) {
/* Remove table, preserve just a function */
lua_remove(L, table_pos);

return TRUE;
}
else {
msg_warn("require of %s.%s failed: not a function but %s", modname,
funcname, lua_typename(L, lua_type(L, -1)));
}

if (lua_type(L, -1) == LUA_TFUNCTION) {
/* Remove table, preserve just a function */
lua_remove(L, table_pos);
lua_pop(L, 2);

return FALSE;
}
else if (lua_isfunction(L, -1)) {
return TRUE;
}
else {
msg_warn("require of %s.%s failed: not a function but %s", modname,
funcname, lua_typename(L, lua_type(L, -1)));
}

lua_pop(L, 2);
msg_warn("require of %s failed: not a function but %s", modname,
lua_typename(L, lua_type(L, -1)));
lua_pop(L, 1);

return FALSE;
return FALSE;
}
}

gint rspamd_lua_function_ref_from_str(lua_State *L, const gchar *str, gsize slen,
Expand Down

0 comments on commit eb6f412

Please sign in to comment.