IPluginFunction implementation is re-entrant across native calls, as heap allocations are delayed until execution
removed ICallable::GetAddressOfPushedParam
removed phys_addr from ICallable::PushArray
fixed a bug where sp_context_t::n_idx was overwritten upon re-entrant calls
2 * ================================================================
3 * SourcePawn (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ================================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
16 #include "PluginSys.h"
22 void CFunction::Set(uint32_t code_addr, IPluginContext *plugin)
24 m_codeaddr = code_addr;
27 m_errorstate = SP_ERROR_NONE;
29 m_pCtx = plugin ? plugin->GetContext() : NULL;
32 bool CFunction::IsRunnable()
34 return ((m_pCtx->flags & SPFLAG_PLUGIN_PAUSED) != SPFLAG_PLUGIN_PAUSED);
37 int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
41 return SP_ERROR_NOT_RUNNABLE;
46 m_pContext->PushCell(params[num_params]);
49 return m_pContext->Execute(m_codeaddr, result);
52 IPluginContext *CFunction::GetParentContext()
57 CFunction::CFunction(uint32_t code_addr, IPluginContext *plugin) :
58 m_codeaddr(code_addr), m_pContext(plugin), m_curparam(0),
59 m_errorstate(SP_ERROR_NONE)
64 m_pCtx = plugin->GetContext();
68 int CFunction::PushCell(cell_t cell)
70 if (m_curparam >= SP_MAX_EXEC_PARAMS)
72 return SetError(SP_ERROR_PARAMS_MAX);
75 m_info[m_curparam].marked = false;
76 m_params[m_curparam] = cell;
82 int CFunction::PushCellByRef(cell_t *cell, int flags)
84 if (m_curparam >= SP_MAX_EXEC_PARAMS)
86 return SetError(SP_ERROR_PARAMS_MAX);
89 return PushArray(cell, 1, flags);
92 int CFunction::PushFloat(float number)
94 cell_t val = *(cell_t *)&number;
99 int CFunction::PushFloatByRef(float *number, int flags)
101 return PushCellByRef((cell_t *)number, flags);
104 int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
106 if (m_curparam >= SP_MAX_EXEC_PARAMS)
108 return SetError(SP_ERROR_PARAMS_MAX);
111 ParamInfo *info = &m_info[m_curparam];
113 info->flags = inarray ? copyback : 0;
116 info->str.is_sz = false;
117 info->orig_addr = inarray;
121 return SP_ERROR_NONE;
124 int CFunction::PushString(const char *string)
126 return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
129 int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
131 return _PushString(buffer, sz_flags, cp_flags, length);
134 int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
136 if (m_curparam >= SP_MAX_EXEC_PARAMS)
138 return SetError(SP_ERROR_PARAMS_MAX);
141 ParamInfo *info = &m_info[m_curparam];
142 size_t cells = (len + sizeof(cell_t) - 1) / sizeof(cell_t);
145 info->orig_addr = (cell_t *)string;
146 info->flags = cp_flags;
148 info->str.sz_flags = sz_flags;
149 info->str.is_sz = true;
153 return SP_ERROR_NONE;
156 void CFunction::Cancel()
163 m_errorstate = SP_ERROR_NONE;
167 int CFunction::Execute(cell_t *result)
169 int err = SP_ERROR_NONE;
172 m_errorstate = SP_ERROR_NOT_RUNNABLE;
174 if (m_errorstate != SP_ERROR_NONE)
181 //This is for re-entrancy!
182 cell_t temp_params[SP_MAX_EXEC_PARAMS];
183 ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
184 unsigned int numparams = m_curparam;
186 bool docopies = true;
190 //Save the info locally, then reset it for re-entrant calls.
191 memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
195 /* Browse the parameters and build arrays */
196 for (i=0; i<numparams; i++)
198 /* Is this marked as an array? */
199 if (temp_info[i].marked)
201 if (!temp_info[i].str.is_sz)
203 /* Allocate a normal/generic array */
204 if ((err=m_pContext->HeapAlloc(temp_info[i].size,
205 &(temp_info[i].local_addr),
206 &(temp_info[i].phys_addr)))
211 if (temp_info[i].orig_addr)
213 memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
216 /* Calculate cells required for the string */
217 size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
219 /* Allocate the buffer */
220 if ((err=m_pContext->HeapAlloc(cells,
221 &(temp_info[i].local_addr),
222 &(temp_info[i].phys_addr)))
227 /* Copy original string if necessary */
228 if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
230 if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
232 if ((err=m_pContext->StringToLocalUTF8(temp_info[i].local_addr,
234 (const char *)temp_info[i].orig_addr,
241 if ((err=m_pContext->StringToLocal(temp_info[i].local_addr,
243 (const char *)temp_info[i].orig_addr))
250 } /* End array/string calculation */
251 /* Update the pushed parameter with the byref local address */
252 temp_params[i] = temp_info[i].local_addr;
254 /* Just copy the value normally */
255 temp_params[i] = m_params[i];
259 /* Make the call if we can */
260 if (err == SP_ERROR_NONE)
262 if ((err = CallFunction(temp_params, numparams, result)) != SP_ERROR_NONE)
270 /* i should be equal to the last valid parameter + 1 */
273 if (!temp_info[i].marked)
277 if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
279 if (temp_info[i].orig_addr)
281 if (temp_info[i].str.is_sz)
283 memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
285 if (temp_info[i].size == 1)
287 *temp_info[i].orig_addr = *(temp_info[i].phys_addr);
289 memcpy(temp_info[i].orig_addr,
290 temp_info[i].phys_addr,
291 temp_info[i].size * sizeof(cell_t));
296 if ((err=m_pContext->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)