1.1 --- a/core/vm/sp_vm_function.cpp Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,321 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourcePawn
1.8 - * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include <stdio.h>
1.36 -#include <string.h>
1.37 -#include "sp_vm_function.h"
1.38 -
1.39 -/********************
1.40 -* FUNCTION CALLING *
1.41 -********************/
1.42 -
1.43 -void CFunction::Set(uint32_t code_addr, IPluginContext *plugin, funcid_t id)
1.44 -{
1.45 - m_codeaddr = code_addr;
1.46 - m_pContext = plugin;
1.47 - m_curparam = 0;
1.48 - m_errorstate = SP_ERROR_NONE;
1.49 - m_Invalid = false;
1.50 - m_pCtx = plugin ? plugin->GetContext() : NULL;
1.51 - m_FnId = id;
1.52 -}
1.53 -
1.54 -bool CFunction::IsRunnable()
1.55 -{
1.56 - return ((m_pCtx->flags & SPFLAG_PLUGIN_PAUSED) != SPFLAG_PLUGIN_PAUSED);
1.57 -}
1.58 -
1.59 -int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
1.60 -{
1.61 - if (!IsRunnable())
1.62 - {
1.63 - return SP_ERROR_NOT_RUNNABLE;
1.64 - }
1.65 -
1.66 - while (num_params--)
1.67 - {
1.68 - m_pContext->PushCell(params[num_params]);
1.69 - }
1.70 -
1.71 - return m_pContext->Execute(m_codeaddr, result);
1.72 -}
1.73 -
1.74 -IPluginContext *CFunction::GetParentContext()
1.75 -{
1.76 - return m_pContext;
1.77 -}
1.78 -
1.79 -CFunction::CFunction(uint32_t code_addr, IPluginContext *plugin, funcid_t id) :
1.80 - m_codeaddr(code_addr), m_pContext(plugin), m_curparam(0),
1.81 - m_errorstate(SP_ERROR_NONE), m_FnId(id)
1.82 -{
1.83 - m_Invalid = false;
1.84 - if (plugin)
1.85 - {
1.86 - m_pCtx = plugin->GetContext();
1.87 - }
1.88 -}
1.89 -
1.90 -int CFunction::PushCell(cell_t cell)
1.91 -{
1.92 - if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.93 - {
1.94 - return SetError(SP_ERROR_PARAMS_MAX);
1.95 - }
1.96 -
1.97 - m_info[m_curparam].marked = false;
1.98 - m_params[m_curparam] = cell;
1.99 - m_curparam++;
1.100 -
1.101 - return SP_ERROR_NONE;
1.102 -}
1.103 -
1.104 -int CFunction::PushCellByRef(cell_t *cell, int flags)
1.105 -{
1.106 - return PushArray(cell, 1, flags);
1.107 -}
1.108 -
1.109 -int CFunction::PushFloat(float number)
1.110 -{
1.111 - cell_t val = *(cell_t *)&number;
1.112 -
1.113 - return PushCell(val);
1.114 -}
1.115 -
1.116 -int CFunction::PushFloatByRef(float *number, int flags)
1.117 -{
1.118 - return PushCellByRef((cell_t *)number, flags);
1.119 -}
1.120 -
1.121 -int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
1.122 -{
1.123 - if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.124 - {
1.125 - return SetError(SP_ERROR_PARAMS_MAX);
1.126 - }
1.127 -
1.128 - ParamInfo *info = &m_info[m_curparam];
1.129 -
1.130 - info->flags = inarray ? copyback : 0;
1.131 - info->marked = true;
1.132 - info->size = cells;
1.133 - info->str.is_sz = false;
1.134 - info->orig_addr = inarray;
1.135 -
1.136 - m_curparam++;
1.137 -
1.138 - return SP_ERROR_NONE;
1.139 -}
1.140 -
1.141 -int CFunction::PushString(const char *string)
1.142 -{
1.143 - return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
1.144 -}
1.145 -
1.146 -int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
1.147 -{
1.148 - return _PushString(buffer, sz_flags, cp_flags, length);
1.149 -}
1.150 -
1.151 -int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
1.152 -{
1.153 - if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.154 - {
1.155 - return SetError(SP_ERROR_PARAMS_MAX);
1.156 - }
1.157 -
1.158 - ParamInfo *info = &m_info[m_curparam];
1.159 -
1.160 - info->marked = true;
1.161 - info->orig_addr = (cell_t *)string;
1.162 - info->flags = cp_flags;
1.163 - info->size = len;
1.164 - info->str.sz_flags = sz_flags;
1.165 - info->str.is_sz = true;
1.166 -
1.167 - m_curparam++;
1.168 -
1.169 - return SP_ERROR_NONE;
1.170 -}
1.171 -
1.172 -void CFunction::Cancel()
1.173 -{
1.174 - if (!m_curparam)
1.175 - {
1.176 - return;
1.177 - }
1.178 -
1.179 - m_errorstate = SP_ERROR_NONE;
1.180 - m_curparam = 0;
1.181 -}
1.182 -
1.183 -int CFunction::Execute(cell_t *result)
1.184 -{
1.185 - int err = SP_ERROR_NONE;
1.186 - if (!IsRunnable())
1.187 - {
1.188 - m_errorstate = SP_ERROR_NOT_RUNNABLE;
1.189 - }
1.190 - if (m_errorstate != SP_ERROR_NONE)
1.191 - {
1.192 - err = m_errorstate;
1.193 - Cancel();
1.194 - return err;
1.195 - }
1.196 -
1.197 - //This is for re-entrancy!
1.198 - cell_t temp_params[SP_MAX_EXEC_PARAMS];
1.199 - ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
1.200 - unsigned int numparams = m_curparam;
1.201 - unsigned int i;
1.202 - bool docopies = true;
1.203 -
1.204 - if (numparams)
1.205 - {
1.206 - //Save the info locally, then reset it for re-entrant calls.
1.207 - memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
1.208 - }
1.209 - m_curparam = 0;
1.210 -
1.211 - /* Browse the parameters and build arrays */
1.212 - for (i=0; i<numparams; i++)
1.213 - {
1.214 - /* Is this marked as an array? */
1.215 - if (temp_info[i].marked)
1.216 - {
1.217 - if (!temp_info[i].str.is_sz)
1.218 - {
1.219 - /* Allocate a normal/generic array */
1.220 - if ((err=m_pContext->HeapAlloc(temp_info[i].size,
1.221 - &(temp_info[i].local_addr),
1.222 - &(temp_info[i].phys_addr)))
1.223 - != SP_ERROR_NONE)
1.224 - {
1.225 - break;
1.226 - }
1.227 - if (temp_info[i].orig_addr)
1.228 - {
1.229 - memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
1.230 - }
1.231 - } else {
1.232 - /* Calculate cells required for the string */
1.233 - size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
1.234 -
1.235 - /* Allocate the buffer */
1.236 - if ((err=m_pContext->HeapAlloc(cells,
1.237 - &(temp_info[i].local_addr),
1.238 - &(temp_info[i].phys_addr)))
1.239 - != SP_ERROR_NONE)
1.240 - {
1.241 - break;
1.242 - }
1.243 - /* Copy original string if necessary */
1.244 - if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
1.245 - {
1.246 - if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
1.247 - {
1.248 - if ((err=m_pContext->StringToLocalUTF8(temp_info[i].local_addr,
1.249 - temp_info[i].size,
1.250 - (const char *)temp_info[i].orig_addr,
1.251 - NULL))
1.252 - != SP_ERROR_NONE)
1.253 - {
1.254 - break;
1.255 - }
1.256 - } else {
1.257 - if ((err=m_pContext->StringToLocal(temp_info[i].local_addr,
1.258 - temp_info[i].size,
1.259 - (const char *)temp_info[i].orig_addr))
1.260 - != SP_ERROR_NONE)
1.261 - {
1.262 - break;
1.263 - }
1.264 - }
1.265 - }
1.266 - } /* End array/string calculation */
1.267 - /* Update the pushed parameter with the byref local address */
1.268 - temp_params[i] = temp_info[i].local_addr;
1.269 - } else {
1.270 - /* Just copy the value normally */
1.271 - temp_params[i] = m_params[i];
1.272 - }
1.273 - }
1.274 -
1.275 - /* Make the call if we can */
1.276 - if (err == SP_ERROR_NONE)
1.277 - {
1.278 - if ((err = CallFunction(temp_params, numparams, result)) != SP_ERROR_NONE)
1.279 - {
1.280 - docopies = false;
1.281 - }
1.282 - } else {
1.283 - docopies = false;
1.284 - }
1.285 -
1.286 - /* i should be equal to the last valid parameter + 1 */
1.287 - while (i--)
1.288 - {
1.289 - if (!temp_info[i].marked)
1.290 - {
1.291 - continue;
1.292 - }
1.293 - if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
1.294 - {
1.295 - if (temp_info[i].orig_addr)
1.296 - {
1.297 - if (temp_info[i].str.is_sz)
1.298 - {
1.299 - memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
1.300 - } else {
1.301 - if (temp_info[i].size == 1)
1.302 - {
1.303 - *temp_info[i].orig_addr = *(temp_info[i].phys_addr);
1.304 - } else {
1.305 - memcpy(temp_info[i].orig_addr,
1.306 - temp_info[i].phys_addr,
1.307 - temp_info[i].size * sizeof(cell_t));
1.308 - }
1.309 - }
1.310 - }
1.311 - }
1.312 - if ((err=m_pContext->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
1.313 - {
1.314 - return err;
1.315 - }
1.316 - }
1.317 -
1.318 - return err;
1.319 -}
1.320 -
1.321 -funcid_t CFunction::GetFunctionID()
1.322 -{
1.323 - return m_FnId;
1.324 -}