Modernize sp_vm_function style.

This commit is contained in:
dvander@alliedmods.net 2015-02-23 13:01:00 -08:00
parent 10d778e344
commit bcd88b4437
4 changed files with 297 additions and 344 deletions

View File

@ -1,4 +1,4 @@
// vim: set sts=2 ts=8 sw=2 tw=99 noet: // vim: set sts=2 ts=8 sw=2 tw=99 et:
// //
// Copyright (C) 2006-2015 AlliedModders LLC // Copyright (C) 2006-2015 AlliedModders LLC
// //

View File

@ -1,4 +1,4 @@
// vim: set sts=2 ts=8 sw=2 tw=99 noet: // vim: set sts=2 ts=8 sw=2 tw=99 et:
// //
// Copyright (C) 2006-2015 AlliedModders LLC // Copyright (C) 2006-2015 AlliedModders LLC
// //

View File

@ -1,31 +1,15 @@
/** // vim: set sts=2 ts=8 sw=2 tw=99 et:
* vim: set ts=4 sw=4 tw=99 noet : //
* ============================================================================= // Copyright (C) 2006-2015 AlliedModders LLC
* SourcePawn //
* Copyright (C) 2004-2009 AlliedModders LLC. All rights reserved. // This file is part of SourcePawn. SourcePawn is free software: you can
* ============================================================================= // redistribute it and/or modify it under the terms of the GNU General Public
* // License as published by the Free Software Foundation, either version 3 of
* This program is free software; you can redistribute it and/or modify it under // the License, or (at your option) any later version.
* the terms of the GNU General Public License, version 3.0, as published by the //
* Free Software Foundation. // You should have received a copy of the GNU General Public License along with
* // SourcePawn. If not, see http://www.gnu.org/licenses/.
* This program is distributed in the hope that it will be useful, but WITHOUT //
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*/
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -41,28 +25,34 @@ CFunction::~CFunction()
delete [] full_name_; delete [] full_name_;
} }
bool CFunction::IsRunnable() bool
CFunction::IsRunnable()
{ {
return !m_pRuntime->IsPaused(); return !m_pRuntime->IsPaused();
} }
int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result) int
CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
{ {
return CallFunction2(m_pRuntime->GetDefaultContext(), params, num_params, result); return CallFunction2(m_pRuntime->GetDefaultContext(), params, num_params, result);
} }
int CFunction::CallFunction2(IPluginContext *pContext, const cell_t *params, unsigned int num_params, cell_t *result) int
CFunction::CallFunction2(IPluginContext *pContext, const cell_t *params, unsigned int num_params, cell_t *result)
{ {
return pContext->Execute2(this, params, num_params, result); return pContext->Execute2(this, params, num_params, result);
} }
IPluginContext *CFunction::GetParentContext() IPluginContext *
CFunction::GetParentContext()
{ {
return m_pRuntime->GetDefaultContext(); return m_pRuntime->GetDefaultContext();
} }
CFunction::CFunction(BaseRuntime *runtime, funcid_t id, uint32_t pub_id) : CFunction::CFunction(BaseRuntime *runtime, funcid_t id, uint32_t pub_id)
m_curparam(0), m_errorstate(SP_ERROR_NONE), m_FnId(id) : m_curparam(0),
m_errorstate(SP_ERROR_NONE),
m_FnId(id)
{ {
m_pRuntime = runtime; m_pRuntime = runtime;
@ -80,9 +70,7 @@ CFunction::CFunction(BaseRuntime *runtime, funcid_t id, uint32_t pub_id) :
int CFunction::PushCell(cell_t cell) int CFunction::PushCell(cell_t cell)
{ {
if (m_curparam >= SP_MAX_EXEC_PARAMS) if (m_curparam >= SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX); return SetError(SP_ERROR_PARAMS_MAX);
}
m_info[m_curparam].marked = false; m_info[m_curparam].marked = false;
m_params[m_curparam] = cell; m_params[m_curparam] = cell;
@ -91,24 +79,28 @@ int CFunction::PushCell(cell_t cell)
return SP_ERROR_NONE; return SP_ERROR_NONE;
} }
int CFunction::PushCellByRef(cell_t *cell, int flags) int
CFunction::PushCellByRef(cell_t *cell, int flags)
{ {
return PushArray(cell, 1, flags); return PushArray(cell, 1, flags);
} }
int CFunction::PushFloat(float number) int
CFunction::PushFloat(float number)
{ {
cell_t val = *(cell_t *)&number; cell_t val = *(cell_t *)&number;
return PushCell(val); return PushCell(val);
} }
int CFunction::PushFloatByRef(float *number, int flags) int
CFunction::PushFloatByRef(float *number, int flags)
{ {
return PushCellByRef((cell_t *)number, flags); return PushCellByRef((cell_t *)number, flags);
} }
int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback) int
CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
{ {
if (m_curparam >= SP_MAX_EXEC_PARAMS) if (m_curparam >= SP_MAX_EXEC_PARAMS)
{ {
@ -128,22 +120,23 @@ int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
return SP_ERROR_NONE; return SP_ERROR_NONE;
} }
int CFunction::PushString(const char *string) int
CFunction::PushString(const char *string)
{ {
return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1); return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
} }
int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags) int
CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
{ {
return _PushString(buffer, sz_flags, cp_flags, length); return _PushString(buffer, sz_flags, cp_flags, length);
} }
int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len) int
CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
{ {
if (m_curparam >= SP_MAX_EXEC_PARAMS) if (m_curparam >= SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX); return SetError(SP_ERROR_PARAMS_MAX);
}
ParamInfo *info = &m_info[m_curparam]; ParamInfo *info = &m_info[m_curparam];
@ -159,33 +152,31 @@ int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_
return SP_ERROR_NONE; return SP_ERROR_NONE;
} }
void CFunction::Cancel() void
CFunction::Cancel()
{ {
if (!m_curparam) if (!m_curparam)
{
return; return;
}
m_errorstate = SP_ERROR_NONE; m_errorstate = SP_ERROR_NONE;
m_curparam = 0; m_curparam = 0;
} }
int CFunction::Execute(cell_t *result) int
CFunction::Execute(cell_t *result)
{ {
return Execute2(m_pRuntime->GetDefaultContext(), result); return Execute2(m_pRuntime->GetDefaultContext(), result);
} }
int CFunction::Execute2(IPluginContext *ctx, cell_t *result) int
CFunction::Execute2(IPluginContext *ctx, cell_t *result)
{ {
int err = SP_ERROR_NONE; int err = SP_ERROR_NONE;
if (!IsRunnable()) if (!IsRunnable())
{
m_errorstate = SP_ERROR_NOT_RUNNABLE; m_errorstate = SP_ERROR_NOT_RUNNABLE;
}
if (m_errorstate != SP_ERROR_NONE) if (m_errorstate != SP_ERROR_NONE) {
{
err = m_errorstate; err = m_errorstate;
Cancel(); Cancel();
return err; return err;
@ -206,13 +197,10 @@ int CFunction::Execute2(IPluginContext *ctx, cell_t *result)
m_curparam = 0; m_curparam = 0;
/* Browse the parameters and build arrays */ /* Browse the parameters and build arrays */
for (i=0; i<numparams; i++) for (i=0; i<numparams; i++) {
{
/* Is this marked as an array? */ /* Is this marked as an array? */
if (temp_info[i].marked) if (temp_info[i].marked) {
{ if (!temp_info[i].str.is_sz) {
if (!temp_info[i].str.is_sz)
{
/* Allocate a normal/generic array */ /* Allocate a normal/generic array */
if ((err=ctx->HeapAlloc(temp_info[i].size, if ((err=ctx->HeapAlloc(temp_info[i].size,
&(temp_info[i].local_addr), &(temp_info[i].local_addr),
@ -225,9 +213,7 @@ int CFunction::Execute2(IPluginContext *ctx, cell_t *result)
{ {
memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size); memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
} }
} } else {
else
{
/* Calculate cells required for the string */ /* Calculate cells required for the string */
size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t); size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
@ -243,8 +229,7 @@ int CFunction::Execute2(IPluginContext *ctx, cell_t *result)
if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL)) if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
{ {
/* Cut off UTF-8 properly */ /* Cut off UTF-8 properly */
if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8) if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8) {
{
if ((err=ctx->StringToLocalUTF8(temp_info[i].local_addr, if ((err=ctx->StringToLocalUTF8(temp_info[i].local_addr,
temp_info[i].size, temp_info[i].size,
(const char *)temp_info[i].orig_addr, (const char *)temp_info[i].orig_addr,
@ -274,52 +259,34 @@ int CFunction::Execute2(IPluginContext *ctx, cell_t *result)
} /* End array/string calculation */ } /* End array/string calculation */
/* Update the pushed parameter with the byref local address */ /* Update the pushed parameter with the byref local address */
temp_params[i] = temp_info[i].local_addr; temp_params[i] = temp_info[i].local_addr;
} } else {
else
{
/* Just copy the value normally */ /* Just copy the value normally */
temp_params[i] = m_params[i]; temp_params[i] = m_params[i];
} }
} }
/* Make the call if we can */ /* Make the call if we can */
if (err == SP_ERROR_NONE) if (err == SP_ERROR_NONE) {
{
if ((err = CallFunction2(ctx, temp_params, numparams, result)) != SP_ERROR_NONE) if ((err = CallFunction2(ctx, temp_params, numparams, result)) != SP_ERROR_NONE)
{
docopies = false; docopies = false;
} } else {
}
else
{
docopies = false; docopies = false;
} }
/* i should be equal to the last valid parameter + 1 */ /* i should be equal to the last valid parameter + 1 */
while (i--) while (i--) {
{
if (!temp_info[i].marked) if (!temp_info[i].marked)
{
continue; continue;
}
if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK)) if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK)) {
{ if (temp_info[i].orig_addr) {
if (temp_info[i].orig_addr) if (temp_info[i].str.is_sz) {
{
if (temp_info[i].str.is_sz)
{
memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size); memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
} } else {
else if (temp_info[i].size == 1) {
{
if (temp_info[i].size == 1)
{
*temp_info[i].orig_addr = *(temp_info[i].phys_addr); *temp_info[i].orig_addr = *(temp_info[i].phys_addr);
} } else {
else
{
memcpy(temp_info[i].orig_addr, memcpy(temp_info[i].orig_addr,
temp_info[i].phys_addr, temp_info[i].phys_addr,
temp_info[i].size * sizeof(cell_t)); temp_info[i].size * sizeof(cell_t));
@ -329,25 +296,26 @@ int CFunction::Execute2(IPluginContext *ctx, cell_t *result)
} }
if ((err=ctx->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE) if ((err=ctx->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
{
return err; return err;
} }
}
return err; return err;
} }
IPluginRuntime *CFunction::GetParentRuntime() IPluginRuntime *
CFunction::GetParentRuntime()
{ {
return m_pRuntime; return m_pRuntime;
} }
funcid_t CFunction::GetFunctionID() funcid_t
CFunction::GetFunctionID()
{ {
return m_FnId; return m_FnId;
} }
int CFunction::SetError(int err) int
CFunction::SetError(int err)
{ {
m_errorstate = err; m_errorstate = err;

View File

@ -1,32 +1,15 @@
/** // vim: set sts=2 ts=8 sw=2 tw=99 et:
* vim: set ts=4 sw=4 tw=99 noet : //
* ============================================================================= // Copyright (C) 2006-2015 AlliedModders LLC
* SourcePawn //
* Copyright (C) 2004-2009 AlliedModders LLC. All rights reserved. // This file is part of SourcePawn. SourcePawn is free software: you can
* ============================================================================= // redistribute it and/or modify it under the terms of the GNU General Public
* // License as published by the Free Software Foundation, either version 3 of
* This program is free software; you can redistribute it and/or modify it under // the License, or (at your option) any later version.
* the terms of the GNU General Public License, version 3.0, as published by the //
* Free Software Foundation. // You should have received a copy of the GNU General Public License along with
* // SourcePawn. If not, see http://www.gnu.org/licenses/.
* This program is distributed in the hope that it will be useful, but WITHOUT //
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*/
#ifndef _INCLUDE_SOURCEMOD_BASEFUNCTION_H_ #ifndef _INCLUDE_SOURCEMOD_BASEFUNCTION_H_
#define _INCLUDE_SOURCEMOD_BASEFUNCTION_H_ #define _INCLUDE_SOURCEMOD_BASEFUNCTION_H_
@ -44,8 +27,7 @@ struct ParamInfo
cell_t *phys_addr; /* Physical address of our copy */ cell_t *phys_addr; /* Physical address of our copy */
cell_t *orig_addr; /* Original address to copy back to */ cell_t *orig_addr; /* Original address to copy back to */
ucell_t size; /* Size of array in bytes */ ucell_t size; /* Size of array in bytes */
struct struct {
{
bool is_sz; /* is a string */ bool is_sz; /* is a string */
int sz_flags; /* has sz flags */ int sz_flags; /* has sz flags */
} str; } str;
@ -57,11 +39,11 @@ class JitFunction;
class CFunction : public IPluginFunction class CFunction : public IPluginFunction
{ {
friend class SourcePawnEngine; friend class SourcePawnEngine;
public: public:
CFunction(BaseRuntime *pRuntime, CFunction(BaseRuntime *pRuntime, funcid_t fnid, uint32_t pub_id);
funcid_t fnid,
uint32_t pub_id);
~CFunction(); ~CFunction();
public: public:
virtual int PushCell(cell_t cell); virtual int PushCell(cell_t cell);
virtual int PushCellByRef(cell_t *cell, int flags); virtual int PushCellByRef(cell_t *cell, int flags);
@ -82,6 +64,7 @@ public:
unsigned int num_params, unsigned int num_params,
cell_t *result); cell_t *result);
IPluginRuntime *GetParentRuntime(); IPluginRuntime *GetParentRuntime();
public: public:
const char *FullName() const { const char *FullName() const {
return full_name_; return full_name_;
@ -89,9 +72,11 @@ public:
sp_public_t *Public() const { sp_public_t *Public() const {
return public_; return public_;
} }
private: private:
int _PushString(const char *string, int sz_flags, int cp_flags, size_t len); int _PushString(const char *string, int sz_flags, int cp_flags, size_t len);
int SetError(int err); int SetError(int err);
private: private:
BaseRuntime *m_pRuntime; BaseRuntime *m_pRuntime;
cell_t m_params[SP_MAX_EXEC_PARAMS]; cell_t m_params[SP_MAX_EXEC_PARAMS];