sourcepawn/jit/sp_vm_function.cpp
branchrefac-jit
changeset 2171 0885b66fcf81
parent 2169 d1e225d141fb
child 2188 24b170ea6ce7
     1.1 --- a/sourcepawn/jit/sp_vm_function.cpp	Sun Jul 06 05:04:38 2008 +0000
     1.2 +++ b/sourcepawn/jit/sp_vm_function.cpp	Mon Jul 07 04:56:29 2008 +0000
     1.3 @@ -32,76 +32,47 @@
     1.4  #include <stdio.h>
     1.5  #include <string.h>
     1.6  #include "sp_vm_function.h"
     1.7 -#include "sm_stringutil.h"
     1.8 +#include "BaseRuntime.h"
     1.9  
    1.10  /********************
    1.11  * FUNCTION CALLING *
    1.12  ********************/
    1.13  
    1.14 -void CFunction::Set(uint32_t code_addr, IPluginContext *plugin, funcid_t id, uint32_t pub_id)
    1.15 +void CFunction::Set(uint32_t code_addr, BaseRuntime *runtime, funcid_t fnid, uint32_t pub_id)
    1.16  {
    1.17  	m_codeaddr = code_addr;
    1.18 -	m_pContext = plugin;
    1.19 +	m_pRuntime = runtime;
    1.20  	m_curparam = 0;
    1.21  	m_errorstate = SP_ERROR_NONE;
    1.22  	m_Invalid = false;
    1.23 -	m_pCtx = plugin ? plugin->GetContext() : NULL;
    1.24 -	m_FnId = id;
    1.25 -	
    1.26 -	m_pContext->GetPublicByIndex(pub_id, &m_pPublic);
    1.27 +	m_FnId = fnid;
    1.28  }
    1.29  
    1.30  bool CFunction::IsRunnable()
    1.31  {
    1.32 -	return ((m_pCtx->flags & SPFLAG_PLUGIN_PAUSED) != SPFLAG_PLUGIN_PAUSED);
    1.33 +	return m_pRuntime->IsPaused();
    1.34  }
    1.35  
    1.36  int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
    1.37  {
    1.38 -	int ir, serial;
    1.39 -
    1.40 -	if (!IsRunnable())
    1.41 -	{
    1.42 -		return SP_ERROR_NOT_RUNNABLE;
    1.43 -	}
    1.44 -
    1.45 -	if ((m_pCtx->prof_flags & SP_PROF_CALLBACKS) == SP_PROF_CALLBACKS
    1.46 -		&& m_pPublic != NULL)
    1.47 -	{
    1.48 -		serial = m_pCtx->profiler->OnCallbackBegin(m_pContext, m_pPublic);
    1.49 -	}
    1.50 +	return CallFunction(m_pRuntime->GetDefaultContext(), params, num_params, result);
    1.51 +}
    1.52  
    1.53 -	while (num_params--)
    1.54 -	{
    1.55 -		m_pContext->PushCell(params[num_params]);
    1.56 -	}
    1.57 -
    1.58 -	ir = m_pContext->Execute(m_codeaddr, result);
    1.59 -
    1.60 -	if ((m_pCtx->prof_flags & SP_PROF_CALLBACKS) == SP_PROF_CALLBACKS
    1.61 -		&& m_pPublic != NULL)
    1.62 -	{
    1.63 -		m_pCtx->profiler->OnCallbackEnd(serial);
    1.64 -	}
    1.65 -
    1.66 -	return ir;
    1.67 +int CFunction::CallFunction(IPluginContext *pContext, const cell_t *params, unsigned int num_params, cell_t *result)
    1.68 +{
    1.69 +	return pContext->Execute(this, params, num_params, result);
    1.70  }
    1.71  
    1.72  IPluginContext *CFunction::GetParentContext()
    1.73  {
    1.74 -	return m_pContext;
    1.75 +	return m_pRuntime->GetDefaultContext();
    1.76  }
    1.77  
    1.78 -CFunction::CFunction(uint32_t code_addr, IPluginContext *plugin, funcid_t id, uint32_t pub_id) : 
    1.79 -	m_codeaddr(code_addr), m_pContext(plugin), m_curparam(0), 
    1.80 -	m_errorstate(SP_ERROR_NONE), m_FnId(id)
    1.81 +CFunction::CFunction(uint32_t code_addr, BaseRuntime *runtime, funcid_t id, uint32_t pub_id) : 
    1.82 +	m_codeaddr(code_addr), m_curparam(0), m_errorstate(SP_ERROR_NONE), m_FnId(id)
    1.83  {
    1.84  	m_Invalid = false;
    1.85 -	if (plugin)
    1.86 -	{
    1.87 -		m_pCtx = plugin->GetContext();
    1.88 -	}
    1.89 -	m_pContext->GetPublicByIndex(pub_id, &m_pPublic);
    1.90 +	m_pRuntime = runtime;
    1.91  }
    1.92  
    1.93  int CFunction::PushCell(cell_t cell)
    1.94 @@ -199,6 +170,11 @@
    1.95  
    1.96  int CFunction::Execute(cell_t *result)
    1.97  {
    1.98 +	return Execute(m_pRuntime->GetDefaultContext(), result);
    1.99 +}
   1.100 +
   1.101 +int CFunction::Execute(IPluginContext *ctx, cell_t *result)
   1.102 +{
   1.103  	int err = SP_ERROR_NONE;
   1.104  
   1.105  	if (!IsRunnable())
   1.106 @@ -236,9 +212,9 @@
   1.107  			if (!temp_info[i].str.is_sz)
   1.108  			{
   1.109  				/* Allocate a normal/generic array */
   1.110 -				if ((err=m_pContext->HeapAlloc(temp_info[i].size, 
   1.111 -											   &(temp_info[i].local_addr),
   1.112 -											   &(temp_info[i].phys_addr)))
   1.113 +				if ((err=ctx->HeapAlloc(temp_info[i].size, 
   1.114 +										   &(temp_info[i].local_addr),
   1.115 +										   &(temp_info[i].phys_addr)))
   1.116  					!= SP_ERROR_NONE)
   1.117  				{
   1.118  					break;
   1.119 @@ -254,9 +230,9 @@
   1.120  				size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
   1.121  
   1.122  				/* Allocate the buffer */
   1.123 -				if ((err=m_pContext->HeapAlloc(cells,
   1.124 -												&(temp_info[i].local_addr),
   1.125 -												&(temp_info[i].phys_addr)))
   1.126 +				if ((err=ctx->HeapAlloc(cells,
   1.127 +										&(temp_info[i].local_addr),
   1.128 +										&(temp_info[i].phys_addr)))
   1.129  					!= SP_ERROR_NONE)
   1.130  				{
   1.131  					break;
   1.132 @@ -267,10 +243,10 @@
   1.133  					/* Cut off UTF-8 properly */
   1.134  					if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
   1.135  					{
   1.136 -						if ((err=m_pContext->StringToLocalUTF8(temp_info[i].local_addr, 
   1.137 -																temp_info[i].size, 
   1.138 -																(const char *)temp_info[i].orig_addr,
   1.139 -																NULL))
   1.140 +						if ((err=ctx->StringToLocalUTF8(temp_info[i].local_addr, 
   1.141 +															temp_info[i].size, 
   1.142 +															(const char *)temp_info[i].orig_addr,
   1.143 +															NULL))
   1.144  							!= SP_ERROR_NONE)
   1.145  						{
   1.146  							break;
   1.147 @@ -284,9 +260,9 @@
   1.148  					/* Copy ASCII characters */
   1.149  					else
   1.150  					{
   1.151 -						if ((err=m_pContext->StringToLocal(temp_info[i].local_addr,
   1.152 -															temp_info[i].size,
   1.153 -															(const char *)temp_info[i].orig_addr))
   1.154 +						if ((err=ctx->StringToLocal(temp_info[i].local_addr,
   1.155 +														temp_info[i].size,
   1.156 +														(const char *)temp_info[i].orig_addr))
   1.157  							!= SP_ERROR_NONE)
   1.158  						{
   1.159  							break;
   1.160 @@ -307,7 +283,7 @@
   1.161  	/* Make the call if we can */
   1.162  	if (err == SP_ERROR_NONE)
   1.163  	{
   1.164 -		if ((err = CallFunction(temp_params, numparams, result)) != SP_ERROR_NONE)
   1.165 +		if ((err = CallFunction(ctx, temp_params, numparams, result)) != SP_ERROR_NONE)
   1.166  		{
   1.167  			docopies = false;
   1.168  		}
   1.169 @@ -350,7 +326,7 @@
   1.170  			}
   1.171  		}
   1.172  
   1.173 -		if ((err=m_pContext->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
   1.174 +		if ((err=ctx->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
   1.175  		{
   1.176  			return err;
   1.177  		}
   1.178 @@ -364,3 +340,16 @@
   1.179  	return m_FnId;
   1.180  }
   1.181  
   1.182 +int CFunction::SetError(int err)
   1.183 +{
   1.184 +	m_errorstate = err;
   1.185 +
   1.186 +	return err;
   1.187 +}
   1.188 +
   1.189 +bool CFunction::IsInvalidated()
   1.190 +{
   1.191 +	return m_Invalid;
   1.192 +}
   1.193 +
   1.194 +