From 65d0d1380eae9821356df943eb0ae24f8df3ba63 Mon Sep 17 00:00:00 2001 From: F1F88 <0xf1f88@gmail.com> Date: Mon, 7 Sep 2026 00:17:48 +0800 Subject: [PATCH] GetFunctionByName: return the runtime's funcid instead of deriving it from the public index (#2566) * fix: use the runtime's funcid in GetFunctionByName * test: update testsuit --- core/logic/smn_functions.cpp | 13 +++++---- plugins/testsuite/mock/test_function.sp | 36 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 plugins/testsuite/mock/test_function.sp diff --git a/core/logic/smn_functions.cpp b/core/logic/smn_functions.cpp index c8a03eaa..4ca3565d 100644 --- a/core/logic/smn_functions.cpp +++ b/core/logic/smn_functions.cpp @@ -91,12 +91,6 @@ public: } g_ForwardNativeHelpers; -/* Turn a public index into a function ID */ -inline funcid_t PublicIndexToFuncId(uint32_t idx) -{ - return (idx << 1) | (1 << 0); -} - /* Reset global function/forward call variables */ inline void ResetCall() { @@ -135,8 +129,13 @@ static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *param return pContext->GetNullFunctionValue(); } + /* Turn a public index into a function ID */ + sp_public_t *func; + if (pPlugin->GetBaseContext()->GetBaseRuntime()->GetPublicByIndex(idx, &func) != SP_ERROR_NONE) + return pContext->GetNullFunctionValue(); + /* Return function ID */ - return PublicIndexToFuncId(idx); + return func->funcid; } static cell_t sm_CreateGlobalForward(IPluginContext *pContext, const cell_t *params) diff --git a/plugins/testsuite/mock/test_function.sp b/plugins/testsuite/mock/test_function.sp new file mode 100644 index 00000000..7ff41180 --- /dev/null +++ b/plugins/testsuite/mock/test_function.sp @@ -0,0 +1,36 @@ +#pragma semicolon 1 +#pragma newdecls required + +#include +#include + + +public void OnPluginStart() +{ + Test_GetFunctionByName(); +} + + +void Test_GetFunctionByName() +{ + SetTestContext("Test GetFunctionByName"); + + Function func = GetFunctionByName(INVALID_HANDLE, "__Func"); + AssertTrue("Function is valid", func != INVALID_FUNCTION); + AssertTrue("Function is __Func", func == __Func); + + int result = 0; + Call_StartFunction(INVALID_HANDLE, func); + Call_PushCell(1); + Call_PushCell(2); + Call_Finish(result); + AssertEq("Function result", result, 3); +} + + +public int __Func(int a, int b) +{ + AssertEq("__Func param a", a, 1); + AssertEq("__Func param b", b, 2); + return a + b; +}