Skip to content

Function as argument #2942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9e6a490
read function as argument
MobinYengejehi Apr 3, 2023
e240103
function as argument
MobinYengejehi Apr 3, 2023
963cf19
function as argument
MobinYengejehi Apr 3, 2023
7d3a6c2
function as argument
MobinYengejehi Apr 3, 2023
8a237c6
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 3, 2023
eb33c32
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 8, 2023
795466e
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 8, 2023
6ec3d62
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 11, 2023
082ebda
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 11, 2023
b2636fb
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 13, 2023
46ad7bf
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 15, 2023
d8f3e87
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 17, 2023
1518edc
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 18, 2023
2caf120
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 19, 2023
034eed4
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 22, 2023
e2a3c26
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 29, 2023
08753ad
Merge branch 'master' into function_as_argument
MobinYengejehi May 2, 2023
a6e7d52
Merge branch 'master' into function_as_argument
MobinYengejehi May 16, 2023
9a96fbf
Merge branch 'master' into function_as_argument
MobinYengejehi May 23, 2023
d39d300
Merge branch 'master' into function_as_argument
MobinYengejehi Jun 1, 2023
ab93444
Merge branch 'master' into function_as_argument
MobinYengejehi Jun 13, 2023
efa4227
Merge branch 'master' into function_as_argument
MobinYengejehi Jun 21, 2023
8a247d8
Merge branch 'master' into function_as_argument
MobinYengejehi Jun 25, 2023
6eb0feb
Merge branch 'master' into function_as_argument
MobinYengejehi Jul 22, 2023
a2fcc3b
Merge branch 'master' into function_as_argument
MobinYengejehi Aug 26, 2023
6e8f867
Merge branch 'master' into function_as_argument
MobinYengejehi Apr 23, 2024
80fb851
Merge branch 'master' into function_as_argument
MobinYengejehi Jun 18, 2024
f6b4a9d
Merge branch 'master' into function_as_argument
MobinYengejehi Mar 1, 2025
e91627b
Merge branch 'master' into function_as_argument
tederis May 31, 2025
c6ac9d8
Merge branch 'master' into function_as_argument
MobinYengejehi Jul 15, 2025
a79cf27
Merge branch 'master' into function_as_argument
MobinYengejehi Jul 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 236 additions & 7 deletions Client/mods/deathmatch/logic/lua/CLuaArgument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "StdInc.h"
#include "net/SyncStructures.h"
#include "CScriptArgReader.h"

#define ARGUMENT_TYPE_INT 9
#define ARGUMENT_TYPE_FLOAT 10
Expand All @@ -24,6 +25,11 @@

extern CClientGame* g_pClientGame;

#ifndef lua_toresource
#define lua_toresource(luaVM, index) \
(g_pClientGame->GetResourceManager()->GetResourceFromScriptID(reinterpret_cast<unsigned long>((CResource*)lua_touserdata(luaVM, index))))
#endif

using namespace std;

// Prevent the warning issued when doing unsigned short -> void*
Expand All @@ -35,25 +41,33 @@ CLuaArgument::CLuaArgument()
m_iIndex = -1;
m_pTableData = NULL;
m_pUserData = NULL;
m_pResource = nullptr;
m_iFunctionRef = -1;
}

CLuaArgument::CLuaArgument(const CLuaArgument& Argument, CFastHashMap<CLuaArguments*, CLuaArguments*>* pKnownTables)
{
// Initialize and call our = on the argument
m_pTableData = NULL;
m_pResource = nullptr;
m_iFunctionRef = -1;
CopyRecursive(Argument, pKnownTables);
}

CLuaArgument::CLuaArgument(NetBitStreamInterface& bitStream, std::vector<CLuaArguments*>* pKnownTables)
{
m_pTableData = NULL;
m_pResource = nullptr;
m_iFunctionRef = -1;
ReadFromBitStream(bitStream, pKnownTables);
}

CLuaArgument::CLuaArgument(lua_State* luaVM, int iArgument, CFastHashMap<const void*, CLuaArguments*>* pKnownTables)
{
// Read the argument out of the lua VM
m_pTableData = NULL;
m_pResource = nullptr;
m_iFunctionRef = -1;
m_iIndex = iArgument;
Read(luaVM, iArgument, pKnownTables);
}
Expand Down Expand Up @@ -121,6 +135,13 @@ void CLuaArgument::CopyRecursive(const CLuaArgument& Argument, CFastHashMap<CLua
break;
}

case LUA_TFUNCTION:
{
m_iFunctionRef = Argument.m_iFunctionRef;
m_pResource = Argument.m_pResource;
break;
}

default:
break;
}
Expand Down Expand Up @@ -240,15 +261,62 @@ void CLuaArgument::Read(lua_State* luaVM, int iArgument, CFastHashMap<const void

case LUA_TTABLE:
{
if (pKnownTables && (m_pTableData = MapFindRef(*pKnownTables, lua_topointer(luaVM, iArgument))))
bool isFunctionReference = false;

if (lua_getmetatable(luaVM, iArgument))
{
m_bWeakTableRef = true;
if (lua_type(luaVM, -1) == LUA_TTABLE)
{
lua_getfield(luaVM, -1, "__call");
if (lua_type(luaVM, -1) == LUA_TFUNCTION)
{
lua_CFunction caller = lua_tocfunction(luaVM, -1);
if (caller == CallFunction)
{
isFunctionReference = true;
}
}
lua_pop(luaVM, 1);
}
lua_pop(luaVM, 1);
}

if (isFunctionReference)
{
bool isNil = true;

lua_getfield(luaVM, iArgument, "resource");
lua_getfield(luaVM, iArgument, "reference");
if (lua_type(luaVM, -2) == LUA_TLIGHTUSERDATA)
{
if (lua_type(luaVM, -1) == LUA_TNUMBER)
{
m_pResource = lua_toresource(luaVM, -2);
m_iFunctionRef = lua_tonumber(luaVM, -1);

isNil = false;
m_iType = LUA_TFUNCTION;
}
}
lua_pop(luaVM, 2);

if (isNil)
{
m_iType = LUA_TNIL;
}
}
else
{
m_pTableData = new CLuaArguments();
m_pTableData->ReadTable(luaVM, iArgument, pKnownTables);
m_bWeakTableRef = false;
if (pKnownTables && (m_pTableData = MapFindRef(*pKnownTables, lua_topointer(luaVM, iArgument))))
{
m_bWeakTableRef = true;
}
else
{
m_pTableData = new CLuaArguments();
m_pTableData->ReadTable(luaVM, iArgument, pKnownTables);
m_bWeakTableRef = false;
}
}
break;
}
Expand Down Expand Up @@ -284,8 +352,11 @@ void CLuaArgument::Read(lua_State* luaVM, int iArgument, CFastHashMap<const void

case LUA_TFUNCTION:
{
// TODO: add function reading (has to work inside tables too)
m_iType = LUA_TNIL;
lua_pushvalue(luaVM, iArgument);
m_iFunctionRef = luaL_ref(luaVM, LUA_REGISTRYINDEX);

CLuaMain& luaMain = lua_getownercluamain(luaVM);
m_pResource = luaMain.GetResource();
break;
}

Expand Down Expand Up @@ -446,6 +517,29 @@ void CLuaArgument::Push(lua_State* luaVM, CFastHashMap<CLuaArguments*, int>* pKn
lua_pushlstring(luaVM, m_strString.c_str(), m_strString.length());
break;
}

case LUA_TFUNCTION:
{
if (m_pResource)
{
lua_newtable(luaVM); // create callable

lua_pushresource(luaVM, m_pResource); // push function inside callable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a referenced resource become stopped this pointer will turn into the dangling one. From what I was able to see there is no an invalidation mechanism for functions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have some cleaner and better version in my mind for this
i will work on it and update its PR to use that instead

lua_setfield(luaVM, -2, "resource");

lua_pushnumber(luaVM, m_iFunctionRef); // push function ref inside callable
lua_setfield(luaVM, -2, "reference");

lua_pushcfunction(luaVM, CleanupFunction); // push cleanup function
lua_setfield(luaVM, -2, "free");

lua_newtable(luaVM); // create metatable
lua_pushcfunction(luaVM, CallFunction);
lua_setfield(luaVM, -2, "__call");
lua_setmetatable(luaVM, -2);
}
break;
}
}
}
}
Expand Down Expand Up @@ -1092,3 +1186,138 @@ bool CLuaArgument::ReadFromJSONObject(json_object* object, std::vector<CLuaArgum
}
return true;
}

int CLuaArgument::CallFunction(lua_State* luaVM)
{
if (lua_type(luaVM, 1) == LUA_TTABLE)
{
lua_getfield(luaVM, 1, "resource");
CResource* resource = nullptr;
if (lua_type(luaVM, -1) == LUA_TLIGHTUSERDATA)
{
resource = lua_toresource(luaVM, -1);
}
lua_pop(luaVM, 1);
if (resource)
{
const char* resourceName = resource->GetName();
if (resource->IsActive() || resource->GetState() == "starting")
{
lua_getfield(luaVM, 1, "reference");
int reference = 0;
if (lua_type(luaVM, -1) == LUA_TNUMBER)
{
reference = (int)lua_tonumber(luaVM, -1);
}
lua_pop(luaVM, 1);
if (reference != 0)
{
lua_State* resourceVM = resource->GetVM()->GetVirtualMachine();
if (resourceVM)
{
lua_getref(resourceVM, reference);
if (lua_type(resourceVM, -1) == LUA_TFUNCTION)
{
lua_pop(resourceVM, 1); // pop function after type checked

CLuaArguments arguments;

CScriptArgReader argReader(luaVM);
argReader.Skip(1);
argReader.ReadLuaArguments(arguments);

int top = lua_gettop(resourceVM);
argReader.m_luaVM = resourceVM;
argReader.m_iIndex = top + 1;

lua_getref(resourceVM, reference);
arguments.PushArguments(resourceVM);
lua_call(resourceVM, arguments.Count(), LUA_MULTRET);

arguments.DeleteArguments();

int returnCount = lua_gettop(resourceVM) - top;
if (returnCount > 0)
{
argReader.ReadLuaArguments(arguments);
arguments.PushArguments(luaVM);
}

return returnCount;
}
else
{
g_pClientGame->GetScriptDebugging()->LogError(NULL, "calling to a none function value on resource[%s]", resourceName);
}
lua_pop(resourceVM, 1); // pop function
}
else
{
g_pClientGame->GetScriptDebugging()->LogError(NULL, "couldn't find resource virutal machine");
}
}
else
{
g_pClientGame->GetScriptDebugging()->LogError(NULL, "invalid reference id while calling to resource[%s]", resourceName);
}
}
else
{
g_pClientGame->GetScriptDebugging()->LogError(NULL, "calling to a none running resource[%s]", resourceName);
}
}
else
{
g_pClientGame->GetScriptDebugging()->LogError(NULL, "couldn't find the resource of function");
}
return 0;
}
}

int CLuaArgument::CleanupFunction(lua_State* luaVM)
{
if (lua_type(luaVM, 1) == LUA_TTABLE)
{
lua_getfield(luaVM, 1, "resource");
CResource* resource = nullptr;
if (lua_type(luaVM, -1) == LUA_TLIGHTUSERDATA)
{
resource = lua_toresource(luaVM, -1);
}
lua_pop(luaVM, 1);
if (resource)
{
const char* resourceName = resource->GetName();
if (resource->IsActive() || resource->GetState() == "starting")
{
lua_getfield(luaVM, 1, "reference");
int reference = 0;
if (lua_type(luaVM, -1) == LUA_TNUMBER)
{
reference = (int)lua_tonumber(luaVM, -1);
}
lua_pop(luaVM, 1);
if (reference != 0)
{
lua_State* resourceVM = resource->GetVM()->GetVirtualMachine();
if (resourceVM)
{
lua_getref(resourceVM, reference);
if (lua_type(resourceVM, -1) != LUA_TNIL)
{
lua_pop(resourceVM, 1);

lua_unref(resourceVM, reference);

lua_pushboolean(luaVM, true);
return 1;
}
lua_pop(resourceVM, 1);
}
}
}
}
}
lua_pushboolean(luaVM, false);
return 1;
}
7 changes: 7 additions & 0 deletions Client/mods/deathmatch/logic/lua/CLuaArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class CLuaArgument
lua_Number GetNumber() const { return m_Number; };
const SString& GetString() { return m_strString; };
void* GetUserData() const { return m_pUserData; };
CResource* GetFunctionResource() const { return m_pResource; };
int GetFunctionReference() const { return m_iFunctionRef; };
CLuaArguments* GetTable() const { return m_pTableData; }
CClientEntity* GetElement() const;

Expand All @@ -66,6 +68,9 @@ class CLuaArgument
bool ReadFromJSONObject(json_object* object, std::vector<CLuaArguments*>* pKnownTables = NULL);
char* WriteToString(char* szBuffer, int length);

static int CallFunction(lua_State* luaVM);
static int CleanupFunction(lua_State* luaVM);

[[nodiscard]] bool IsString() const noexcept { return m_iType == LUA_TSTRING; }

[[nodiscard]] bool TryGetString(std::string_view& string) const noexcept
Expand Down Expand Up @@ -118,6 +123,8 @@ class CLuaArgument
SString m_strString;
void* m_pUserData;
CLuaArguments* m_pTableData;
CResource* m_pResource;
int m_iFunctionRef;
bool m_bWeakTableRef;

#ifdef MTA_DEBUG
Expand Down
Loading