1.1 --- a/sourcepawn/jit/sp_vm_function.cpp Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,355 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourcePawn
1.8 - * Copyright (C) 2004-2008 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 -#include "BaseRuntime.h"
1.39 -
1.40 -/********************
1.41 -* FUNCTION CALLING *
1.42 -********************/
1.43 -
1.44 -void CFunction::Set(uint32_t code_addr, BaseRuntime *runtime, funcid_t fnid, uint32_t pub_id)
1.45 -{
1.46 - m_codeaddr = code_addr;
1.47 - m_pRuntime = runtime;
1.48 - m_curparam = 0;
1.49 - m_errorstate = SP_ERROR_NONE;
1.50 - m_Invalid = false;
1.51 - m_FnId = fnid;
1.52 -}
1.53 -
1.54 -bool CFunction::IsRunnable()
1.55 -{
1.56 - return !m_pRuntime->IsPaused();
1.57 -}
1.58 -
1.59 -int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
1.60 -{
1.61 - return CallFunction2(m_pRuntime->GetDefaultContext(), params, num_params, result);
1.62 -}
1.63 -
1.64 -int CFunction::CallFunction2(IPluginContext *pContext, const cell_t *params, unsigned int num_params, cell_t *result)
1.65 -{
1.66 - return pContext->Execute2(this, params, num_params, result);
1.67 -}
1.68 -
1.69 -IPluginContext *CFunction::GetParentContext()
1.70 -{
1.71 - return m_pRuntime->GetDefaultContext();
1.72 -}
1.73 -
1.74 -CFunction::CFunction(uint32_t code_addr, BaseRuntime *runtime, funcid_t id, uint32_t pub_id) :
1.75 - m_codeaddr(code_addr), m_curparam(0), m_errorstate(SP_ERROR_NONE), m_FnId(id)
1.76 -{
1.77 - m_Invalid = false;
1.78 - m_pRuntime = runtime;
1.79 -}
1.80 -
1.81 -int CFunction::PushCell(cell_t cell)
1.82 -{
1.83 - if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.84 - {
1.85 - return SetError(SP_ERROR_PARAMS_MAX);
1.86 - }
1.87 -
1.88 - m_info[m_curparam].marked = false;
1.89 - m_params[m_curparam] = cell;
1.90 - m_curparam++;
1.91 -
1.92 - return SP_ERROR_NONE;
1.93 -}
1.94 -
1.95 -int CFunction::PushCellByRef(cell_t *cell, int flags)
1.96 -{
1.97 - return PushArray(cell, 1, flags);
1.98 -}
1.99 -
1.100 -int CFunction::PushFloat(float number)
1.101 -{
1.102 - cell_t val = *(cell_t *)&number;
1.103 -
1.104 - return PushCell(val);
1.105 -}
1.106 -
1.107 -int CFunction::PushFloatByRef(float *number, int flags)
1.108 -{
1.109 - return PushCellByRef((cell_t *)number, flags);
1.110 -}
1.111 -
1.112 -int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
1.113 -{
1.114 - if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.115 - {
1.116 - return SetError(SP_ERROR_PARAMS_MAX);
1.117 - }
1.118 -
1.119 - ParamInfo *info = &m_info[m_curparam];
1.120 -
1.121 - info->flags = inarray ? copyback : 0;
1.122 - info->marked = true;
1.123 - info->size = cells;
1.124 - info->str.is_sz = false;
1.125 - info->orig_addr = inarray;
1.126 -
1.127 - m_curparam++;
1.128 -
1.129 - return SP_ERROR_NONE;
1.130 -}
1.131 -
1.132 -int CFunction::PushString(const char *string)
1.133 -{
1.134 - return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
1.135 -}
1.136 -
1.137 -int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
1.138 -{
1.139 - return _PushString(buffer, sz_flags, cp_flags, length);
1.140 -}
1.141 -
1.142 -int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
1.143 -{
1.144 - if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.145 - {
1.146 - return SetError(SP_ERROR_PARAMS_MAX);
1.147 - }
1.148 -
1.149 - ParamInfo *info = &m_info[m_curparam];
1.150 -
1.151 - info->marked = true;
1.152 - info->orig_addr = (cell_t *)string;
1.153 - info->flags = cp_flags;
1.154 - info->size = len;
1.155 - info->str.sz_flags = sz_flags;
1.156 - info->str.is_sz = true;
1.157 -
1.158 - m_curparam++;
1.159 -
1.160 - return SP_ERROR_NONE;
1.161 -}
1.162 -
1.163 -void CFunction::Cancel()
1.164 -{
1.165 - if (!m_curparam)
1.166 - {
1.167 - return;
1.168 - }
1.169 -
1.170 - m_errorstate = SP_ERROR_NONE;
1.171 - m_curparam = 0;
1.172 -}
1.173 -
1.174 -int CFunction::Execute(cell_t *result)
1.175 -{
1.176 - return Execute2(m_pRuntime->GetDefaultContext(), result);
1.177 -}
1.178 -
1.179 -int CFunction::Execute2(IPluginContext *ctx, cell_t *result)
1.180 -{
1.181 - int err = SP_ERROR_NONE;
1.182 -
1.183 - if (!IsRunnable())
1.184 - {
1.185 - m_errorstate = SP_ERROR_NOT_RUNNABLE;
1.186 - }
1.187 -
1.188 - if (m_errorstate != SP_ERROR_NONE)
1.189 - {
1.190 - err = m_errorstate;
1.191 - Cancel();
1.192 - return err;
1.193 - }
1.194 -
1.195 - //This is for re-entrancy!
1.196 - cell_t temp_params[SP_MAX_EXEC_PARAMS];
1.197 - ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
1.198 - unsigned int numparams = m_curparam;
1.199 - unsigned int i;
1.200 - bool docopies = true;
1.201 -
1.202 - if (numparams)
1.203 - {
1.204 - //Save the info locally, then reset it for re-entrant calls.
1.205 - memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
1.206 - }
1.207 - m_curparam = 0;
1.208 -
1.209 - /* Browse the parameters and build arrays */
1.210 - for (i=0; i<numparams; i++)
1.211 - {
1.212 - /* Is this marked as an array? */
1.213 - if (temp_info[i].marked)
1.214 - {
1.215 - if (!temp_info[i].str.is_sz)
1.216 - {
1.217 - /* Allocate a normal/generic array */
1.218 - if ((err=ctx->HeapAlloc(temp_info[i].size,
1.219 - &(temp_info[i].local_addr),
1.220 - &(temp_info[i].phys_addr)))
1.221 - != SP_ERROR_NONE)
1.222 - {
1.223 - break;
1.224 - }
1.225 - if (temp_info[i].orig_addr)
1.226 - {
1.227 - memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
1.228 - }
1.229 - }
1.230 - else
1.231 - {
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=ctx->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 - /* Cut off UTF-8 properly */
1.247 - if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
1.248 - {
1.249 - if ((err=ctx->StringToLocalUTF8(temp_info[i].local_addr,
1.250 - temp_info[i].size,
1.251 - (const char *)temp_info[i].orig_addr,
1.252 - NULL))
1.253 - != SP_ERROR_NONE)
1.254 - {
1.255 - break;
1.256 - }
1.257 - }
1.258 - /* Copy a binary blob */
1.259 - else if (temp_info[i].str.sz_flags & SM_PARAM_STRING_BINARY)
1.260 - {
1.261 - memmove(temp_info[i].phys_addr, temp_info[i].orig_addr, temp_info[i].size);
1.262 - }
1.263 - /* Copy ASCII characters */
1.264 - else
1.265 - {
1.266 - if ((err=ctx->StringToLocal(temp_info[i].local_addr,
1.267 - temp_info[i].size,
1.268 - (const char *)temp_info[i].orig_addr))
1.269 - != SP_ERROR_NONE)
1.270 - {
1.271 - break;
1.272 - }
1.273 - }
1.274 - }
1.275 - } /* End array/string calculation */
1.276 - /* Update the pushed parameter with the byref local address */
1.277 - temp_params[i] = temp_info[i].local_addr;
1.278 - }
1.279 - else
1.280 - {
1.281 - /* Just copy the value normally */
1.282 - temp_params[i] = m_params[i];
1.283 - }
1.284 - }
1.285 -
1.286 - /* Make the call if we can */
1.287 - if (err == SP_ERROR_NONE)
1.288 - {
1.289 - if ((err = CallFunction2(ctx, temp_params, numparams, result)) != SP_ERROR_NONE)
1.290 - {
1.291 - docopies = false;
1.292 - }
1.293 - }
1.294 - else
1.295 - {
1.296 - docopies = false;
1.297 - }
1.298 -
1.299 - /* i should be equal to the last valid parameter + 1 */
1.300 - while (i--)
1.301 - {
1.302 - if (!temp_info[i].marked)
1.303 - {
1.304 - continue;
1.305 - }
1.306 -
1.307 - if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
1.308 - {
1.309 - if (temp_info[i].orig_addr)
1.310 - {
1.311 - if (temp_info[i].str.is_sz)
1.312 - {
1.313 - memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
1.314 -
1.315 - }
1.316 - else
1.317 - {
1.318 - if (temp_info[i].size == 1)
1.319 - {
1.320 - *temp_info[i].orig_addr = *(temp_info[i].phys_addr);
1.321 - }
1.322 - else
1.323 - {
1.324 - memcpy(temp_info[i].orig_addr,
1.325 - temp_info[i].phys_addr,
1.326 - temp_info[i].size * sizeof(cell_t));
1.327 - }
1.328 - }
1.329 - }
1.330 - }
1.331 -
1.332 - if ((err=ctx->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
1.333 - {
1.334 - return err;
1.335 - }
1.336 - }
1.337 -
1.338 - return err;
1.339 -}
1.340 -
1.341 -funcid_t CFunction::GetFunctionID()
1.342 -{
1.343 - return m_FnId;
1.344 -}
1.345 -
1.346 -int CFunction::SetError(int err)
1.347 -{
1.348 - m_errorstate = err;
1.349 -
1.350 - return err;
1.351 -}
1.352 -
1.353 -bool CFunction::IsInvalidated()
1.354 -{
1.355 - return m_Invalid;
1.356 -}
1.357 -
1.358 -