ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1952: * SourcePawn
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #include
ds@1952: #include
ds@1952: #include "sp_vm_function.h"
ds@1952: #include "sm_stringutil.h"
ds@1952:
ds@1952: /********************
ds@1952: * FUNCTION CALLING *
ds@1952: ********************/
ds@1952:
ds@1952: void CFunction::Set(uint32_t code_addr, IPluginContext *plugin, funcid_t id, uint32_t pub_id)
ds@1952: {
ds@1952: m_codeaddr = code_addr;
ds@1952: m_pContext = plugin;
ds@1952: m_curparam = 0;
ds@1952: m_errorstate = SP_ERROR_NONE;
ds@1952: m_Invalid = false;
ds@1952: m_pCtx = plugin ? plugin->GetContext() : NULL;
ds@1952: m_FnId = id;
ds@1952:
ds@1952: m_pContext->GetPublicByIndex(pub_id, &m_pPublic);
ds@1952: }
ds@1952:
ds@1952: bool CFunction::IsRunnable()
ds@1952: {
ds@1952: return ((m_pCtx->flags & SPFLAG_PLUGIN_PAUSED) != SPFLAG_PLUGIN_PAUSED);
ds@1952: }
ds@1952:
ds@1952: int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
ds@1952: {
ds@1952: int ir, serial;
ds@1952:
ds@1952: if (!IsRunnable())
ds@1952: {
ds@1952: return SP_ERROR_NOT_RUNNABLE;
ds@1952: }
ds@1952:
ds@1952: if ((m_pCtx->prof_flags & SP_PROF_CALLBACKS) == SP_PROF_CALLBACKS
ds@1952: && m_pPublic != NULL)
ds@1952: {
ds@1952: serial = m_pCtx->profiler->OnCallbackBegin(m_pContext, m_pPublic);
ds@1952: }
ds@1952:
ds@1952: while (num_params--)
ds@1952: {
ds@1952: m_pContext->PushCell(params[num_params]);
ds@1952: }
ds@1952:
ds@1952: ir = m_pContext->Execute(m_codeaddr, result);
ds@1952:
ds@1952: if ((m_pCtx->prof_flags & SP_PROF_CALLBACKS) == SP_PROF_CALLBACKS
ds@1952: && m_pPublic != NULL)
ds@1952: {
ds@1952: m_pCtx->profiler->OnCallbackEnd(serial);
ds@1952: }
ds@1952:
ds@1952: return ir;
ds@1952: }
ds@1952:
ds@1952: IPluginContext *CFunction::GetParentContext()
ds@1952: {
ds@1952: return m_pContext;
ds@1952: }
ds@1952:
ds@1952: CFunction::CFunction(uint32_t code_addr, IPluginContext *plugin, funcid_t id, uint32_t pub_id) :
ds@1952: m_codeaddr(code_addr), m_pContext(plugin), m_curparam(0),
ds@1952: m_errorstate(SP_ERROR_NONE), m_FnId(id)
ds@1952: {
ds@1952: m_Invalid = false;
ds@1952: if (plugin)
ds@1952: {
ds@1952: m_pCtx = plugin->GetContext();
ds@1952: }
ds@1952: m_pContext->GetPublicByIndex(pub_id, &m_pPublic);
ds@1952: }
ds@1952:
ds@1952: int CFunction::PushCell(cell_t cell)
ds@1952: {
ds@1952: if (m_curparam >= SP_MAX_EXEC_PARAMS)
ds@1952: {
ds@1952: return SetError(SP_ERROR_PARAMS_MAX);
ds@1952: }
ds@1952:
ds@1952: m_info[m_curparam].marked = false;
ds@1952: m_params[m_curparam] = cell;
ds@1952: m_curparam++;
ds@1952:
ds@1952: return SP_ERROR_NONE;
ds@1952: }
ds@1952:
ds@1952: int CFunction::PushCellByRef(cell_t *cell, int flags)
ds@1952: {
ds@1952: return PushArray(cell, 1, flags);
ds@1952: }
ds@1952:
ds@1952: int CFunction::PushFloat(float number)
ds@1952: {
ds@1952: cell_t val = *(cell_t *)&number;
ds@1952:
ds@1952: return PushCell(val);
ds@1952: }
ds@1952:
ds@1952: int CFunction::PushFloatByRef(float *number, int flags)
ds@1952: {
ds@1952: return PushCellByRef((cell_t *)number, flags);
ds@1952: }
ds@1952:
ds@1952: int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
ds@1952: {
ds@1952: if (m_curparam >= SP_MAX_EXEC_PARAMS)
ds@1952: {
ds@1952: return SetError(SP_ERROR_PARAMS_MAX);
ds@1952: }
ds@1952:
ds@1952: ParamInfo *info = &m_info[m_curparam];
ds@1952:
ds@1952: info->flags = inarray ? copyback : 0;
ds@1952: info->marked = true;
ds@1952: info->size = cells;
ds@1952: info->str.is_sz = false;
ds@1952: info->orig_addr = inarray;
ds@1952:
ds@1952: m_curparam++;
ds@1952:
ds@1952: return SP_ERROR_NONE;
ds@1952: }
ds@1952:
ds@1952: int CFunction::PushString(const char *string)
ds@1952: {
ds@1952: return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
ds@1952: }
ds@1952:
ds@1952: int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
ds@1952: {
ds@1952: return _PushString(buffer, sz_flags, cp_flags, length);
ds@1952: }
ds@1952:
ds@1952: int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
ds@1952: {
ds@1952: if (m_curparam >= SP_MAX_EXEC_PARAMS)
ds@1952: {
ds@1952: return SetError(SP_ERROR_PARAMS_MAX);
ds@1952: }
ds@1952:
ds@1952: ParamInfo *info = &m_info[m_curparam];
ds@1952:
ds@1952: info->marked = true;
ds@1952: info->orig_addr = (cell_t *)string;
ds@1952: info->flags = cp_flags;
ds@1952: info->size = len;
ds@1952: info->str.sz_flags = sz_flags;
ds@1952: info->str.is_sz = true;
ds@1952:
ds@1952: m_curparam++;
ds@1952:
ds@1952: return SP_ERROR_NONE;
ds@1952: }
ds@1952:
ds@1952: void CFunction::Cancel()
ds@1952: {
ds@1952: if (!m_curparam)
ds@1952: {
ds@1952: return;
ds@1952: }
ds@1952:
ds@1952: m_errorstate = SP_ERROR_NONE;
ds@1952: m_curparam = 0;
ds@1952: }
ds@1952:
ds@1952: int CFunction::Execute(cell_t *result)
ds@1952: {
ds@1952: int err = SP_ERROR_NONE;
ds@1952:
ds@1952: if (!IsRunnable())
ds@1952: {
ds@1952: m_errorstate = SP_ERROR_NOT_RUNNABLE;
ds@1952: }
ds@1952:
ds@1952: if (m_errorstate != SP_ERROR_NONE)
ds@1952: {
ds@1952: err = m_errorstate;
ds@1952: Cancel();
ds@1952: return err;
ds@1952: }
ds@1952:
ds@1952: //This is for re-entrancy!
ds@1952: cell_t temp_params[SP_MAX_EXEC_PARAMS];
ds@1952: ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
ds@1952: unsigned int numparams = m_curparam;
ds@1952: unsigned int i;
ds@1952: bool docopies = true;
ds@1952:
ds@1952: if (numparams)
ds@1952: {
ds@1952: //Save the info locally, then reset it for re-entrant calls.
ds@1952: memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
ds@1952: }
ds@1952: m_curparam = 0;
ds@1952:
ds@1952: /* Browse the parameters and build arrays */
ds@1952: for (i=0; iHeapAlloc(temp_info[i].size,
ds@1952: &(temp_info[i].local_addr),
ds@1952: &(temp_info[i].phys_addr)))
ds@1952: != SP_ERROR_NONE)
ds@1952: {
ds@1952: break;
ds@1952: }
ds@1952: if (temp_info[i].orig_addr)
ds@1952: {
ds@1952: memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
ds@1952: }
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: /* Calculate cells required for the string */
ds@1952: size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
ds@1952:
ds@1952: /* Allocate the buffer */
ds@1952: if ((err=m_pContext->HeapAlloc(cells,
ds@1952: &(temp_info[i].local_addr),
ds@1952: &(temp_info[i].phys_addr)))
ds@1952: != SP_ERROR_NONE)
ds@1952: {
ds@1952: break;
ds@1952: }
ds@1952: /* Copy original string if necessary */
ds@1952: if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
ds@1952: {
ds@1952: /* Cut off UTF-8 properly */
ds@1952: if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
ds@1952: {
ds@1952: if ((err=m_pContext->StringToLocalUTF8(temp_info[i].local_addr,
ds@1952: temp_info[i].size,
ds@1952: (const char *)temp_info[i].orig_addr,
ds@1952: NULL))
ds@1952: != SP_ERROR_NONE)
ds@1952: {
ds@1952: break;
ds@1952: }
ds@1952: }
ds@1952: /* Copy a binary blob */
ds@1952: else if (temp_info[i].str.sz_flags & SM_PARAM_STRING_BINARY)
ds@1952: {
ds@1952: memmove(temp_info[i].phys_addr, temp_info[i].orig_addr, temp_info[i].size);
ds@1952: }
ds@1952: /* Copy ASCII characters */
ds@1952: else
ds@1952: {
ds@1952: if ((err=m_pContext->StringToLocal(temp_info[i].local_addr,
ds@1952: temp_info[i].size,
ds@1952: (const char *)temp_info[i].orig_addr))
ds@1952: != SP_ERROR_NONE)
ds@1952: {
ds@1952: break;
ds@1952: }
ds@1952: }
ds@1952: }
ds@1952: } /* End array/string calculation */
ds@1952: /* Update the pushed parameter with the byref local address */
ds@1952: temp_params[i] = temp_info[i].local_addr;
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: /* Just copy the value normally */
ds@1952: temp_params[i] = m_params[i];
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: /* Make the call if we can */
ds@1952: if (err == SP_ERROR_NONE)
ds@1952: {
ds@1952: if ((err = CallFunction(temp_params, numparams, result)) != SP_ERROR_NONE)
ds@1952: {
ds@1952: docopies = false;
ds@1952: }
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: docopies = false;
ds@1952: }
ds@1952:
ds@1952: /* i should be equal to the last valid parameter + 1 */
ds@1952: while (i--)
ds@1952: {
ds@1952: if (!temp_info[i].marked)
ds@1952: {
ds@1952: continue;
ds@1952: }
ds@1952:
ds@1952: if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
ds@1952: {
ds@1952: if (temp_info[i].orig_addr)
ds@1952: {
ds@1952: if (temp_info[i].str.is_sz)
ds@1952: {
ds@1952: memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
ds@1952:
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: if (temp_info[i].size == 1)
ds@1952: {
ds@1952: *temp_info[i].orig_addr = *(temp_info[i].phys_addr);
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: memcpy(temp_info[i].orig_addr,
ds@1952: temp_info[i].phys_addr,
ds@1952: temp_info[i].size * sizeof(cell_t));
ds@1952: }
ds@1952: }
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: if ((err=m_pContext->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
ds@1952: {
ds@1952: return err;
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: return err;
ds@1952: }
ds@1952:
ds@1952: funcid_t CFunction::GetFunctionID()
ds@1952: {
ds@1952: return m_FnId;
ds@1952: }
ds@1952: