Modernize sp_vm_engine style.

This commit is contained in:
dvander@alliedmods.net 2015-02-23 13:08:28 -08:00
parent bcd88b4437
commit 1b47aa10ca
2 changed files with 246 additions and 283 deletions

View File

@ -1,34 +1,15 @@
/** // vim: set sts=2 ts=8 sw=2 tw=99 et:
* vim: set ts=4 sw=4 tw=99 et: //
* ============================================================================= // Copyright (C) 2006-2015 AlliedModders LLC
* SourcePawn //
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. // This file is part of SourcePawn. SourcePawn is free software: you can
* ============================================================================= // redistribute it and/or modify it under the terms of the GNU General Public
* // License as published by the Free Software Foundation, either version 3 of
* This program is free software; you can redistribute it and/or modify it under // the License, or (at your option) any later version.
* the terms of the GNU General Public License, version 3.0, as published by the //
* Free Software Foundation. // You should have received a copy of the GNU General Public License along with
* // SourcePawn. If not, see http://www.gnu.org/licenses/.
* This program is distributed in the hope that it will be useful, but WITHOUT //
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
@ -93,13 +74,11 @@ static const char *g_ErrorMsgTable[] =
"Script execution timed out" "Script execution timed out"
}; };
const char *SourcePawnEngine::GetErrorString(int error) const char *
SourcePawnEngine::GetErrorString(int error)
{ {
if (error < 1 || error > ERROR_MESSAGE_MAX) if (error < 1 || error > ERROR_MESSAGE_MAX)
{
return NULL; return NULL;
}
return g_ErrorMsgTable[error]; return g_ErrorMsgTable[error];
} }
@ -112,7 +91,8 @@ SourcePawnEngine::~SourcePawnEngine()
{ {
} }
void *SourcePawnEngine::ExecAlloc(size_t size) void *
SourcePawnEngine::ExecAlloc(size_t size)
{ {
#if defined WIN32 #if defined WIN32
return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
@ -122,8 +102,7 @@ void *SourcePawnEngine::ExecAlloc(size_t size)
# else # else
void *base = memalign(sysconf(_SC_PAGESIZE), size); void *base = memalign(sysconf(_SC_PAGESIZE), size);
# endif # endif
if (mprotect(base, size, PROT_READ|PROT_WRITE|PROT_EXEC) != 0) if (mprotect(base, size, PROT_READ|PROT_WRITE|PROT_EXEC) != 0) {
{
free(base); free(base);
return NULL; return NULL;
} }
@ -131,27 +110,32 @@ void *SourcePawnEngine::ExecAlloc(size_t size)
#endif #endif
} }
void *SourcePawnEngine::AllocatePageMemory(size_t size) void *
SourcePawnEngine::AllocatePageMemory(size_t size)
{ {
return g_Jit.AllocCode(size); return g_Jit.AllocCode(size);
} }
void SourcePawnEngine::SetReadExecute(void *ptr) void
SourcePawnEngine::SetReadExecute(void *ptr)
{ {
/* already re */ /* already re */
} }
void SourcePawnEngine::SetReadWrite(void *ptr) void
SourcePawnEngine::SetReadWrite(void *ptr)
{ {
/* already rw */ /* already rw */
} }
void SourcePawnEngine::FreePageMemory(void *ptr) void
SourcePawnEngine::FreePageMemory(void *ptr)
{ {
g_Jit.FreeCode(ptr); g_Jit.FreeCode(ptr);
} }
void SourcePawnEngine::ExecFree(void *address) void
SourcePawnEngine::ExecFree(void *address)
{ {
#if defined WIN32 #if defined WIN32
VirtualFree(address, 0, MEM_RELEASE); VirtualFree(address, 0, MEM_RELEASE);
@ -160,48 +144,51 @@ void SourcePawnEngine::ExecFree(void *address)
#endif #endif
} }
void SourcePawnEngine::SetReadWriteExecute(void *ptr) void
SourcePawnEngine::SetReadWriteExecute(void *ptr)
{ {
//:TODO: g_ExeMemory.SetRWE(ptr); //:TODO: g_ExeMemory.SetRWE(ptr);
SetReadExecute(ptr); SetReadExecute(ptr);
} }
void *SourcePawnEngine::BaseAlloc(size_t size) void *
SourcePawnEngine::BaseAlloc(size_t size)
{ {
return malloc(size); return malloc(size);
} }
void SourcePawnEngine::BaseFree(void *memory) void
SourcePawnEngine::BaseFree(void *memory)
{ {
free(memory); free(memory);
} }
sp_plugin_t *SourcePawnEngine::LoadFromFilePointer(FILE *fp, int *err) sp_plugin_t *
SourcePawnEngine::LoadFromFilePointer(FILE *fp, int *err)
{ {
if (err != NULL) if (err != NULL)
{
*err = SP_ERROR_ABORTED; *err = SP_ERROR_ABORTED;
}
return NULL; return NULL;
} }
sp_plugin_t *SourcePawnEngine::LoadFromMemory(void *base, sp_plugin_t *plugin, int *err) sp_plugin_t *
SourcePawnEngine::LoadFromMemory(void *base, sp_plugin_t *plugin, int *err)
{ {
if (err != NULL) if (err != NULL)
{
*err = SP_ERROR_ABORTED; *err = SP_ERROR_ABORTED;
}
return NULL; return NULL;
} }
int SourcePawnEngine::FreeFromMemory(sp_plugin_t *plugin) int
SourcePawnEngine::FreeFromMemory(sp_plugin_t *plugin)
{ {
return SP_ERROR_ABORTED; return SP_ERROR_ABORTED;
} }
IDebugListener *SourcePawnEngine::SetDebugListener(IDebugListener *pListener) IDebugListener *
SourcePawnEngine::SetDebugListener(IDebugListener *pListener)
{ {
IDebugListener *old = m_pDebugHook; IDebugListener *old = m_pDebugHook;
@ -210,22 +197,23 @@ IDebugListener *SourcePawnEngine::SetDebugListener(IDebugListener *pListener)
return old; return old;
} }
unsigned int SourcePawnEngine::GetEngineAPIVersion() unsigned int
SourcePawnEngine::GetEngineAPIVersion()
{ {
return SOURCEPAWN_ENGINE_API_VERSION; return SOURCEPAWN_ENGINE_API_VERSION;
} }
unsigned int SourcePawnEngine::GetContextCallCount() unsigned int
SourcePawnEngine::GetContextCallCount()
{ {
return 0; return 0;
} }
void SourcePawnEngine::ReportError(BaseRuntime *runtime, int err, const char *errstr, cell_t rp_start) void
SourcePawnEngine::ReportError(BaseRuntime *runtime, int err, const char *errstr, cell_t rp_start)
{ {
if (m_pDebugHook == NULL) if (m_pDebugHook == NULL)
{
return; return;
}
CContextTrace trace(runtime, err, errstr, rp_start); CContextTrace trace(runtime, err, errstr, rp_start);
@ -233,54 +221,56 @@ void SourcePawnEngine::ReportError(BaseRuntime *runtime, int err, const char *er
} }
CContextTrace::CContextTrace(BaseRuntime *pRuntime, int err, const char *errstr, cell_t start_rp) CContextTrace::CContextTrace(BaseRuntime *pRuntime, int err, const char *errstr, cell_t start_rp)
: m_pRuntime(pRuntime), m_Error(err), m_pMsg(errstr), m_StartRp(start_rp), m_Level(0) : m_pRuntime(pRuntime),
m_Error(err),
m_pMsg(errstr),
m_StartRp(start_rp),
m_Level(0)
{ {
m_ctx = pRuntime->m_pCtx->GetCtx(); m_ctx = pRuntime->m_pCtx->GetCtx();
m_pDebug = m_pRuntime->GetDebugInfo(); m_pDebug = m_pRuntime->GetDebugInfo();
} }
bool CContextTrace::DebugInfoAvailable() bool
CContextTrace::DebugInfoAvailable()
{ {
return (m_pDebug != NULL); return (m_pDebug != NULL);
} }
const char *CContextTrace::GetCustomErrorString() const char *
CContextTrace::GetCustomErrorString()
{ {
return m_pMsg; return m_pMsg;
} }
int CContextTrace::GetErrorCode() int
CContextTrace::GetErrorCode()
{ {
return m_Error; return m_Error;
} }
const char *CContextTrace::GetErrorString() const char *
CContextTrace::GetErrorString()
{ {
if (m_Error > ERROR_MESSAGE_MAX || m_Error < 1) if (m_Error > ERROR_MESSAGE_MAX || m_Error < 1)
{
return "Invalid error code"; return "Invalid error code";
}
else
{
return g_ErrorMsgTable[m_Error]; return g_ErrorMsgTable[m_Error];
} }
}
void CContextTrace::ResetTrace() void
CContextTrace::ResetTrace()
{ {
m_Level = 0; m_Level = 0;
} }
bool CContextTrace::GetTraceInfo(CallStackInfo *trace) bool
CContextTrace::GetTraceInfo(CallStackInfo *trace)
{ {
cell_t cip; cell_t cip;
if (m_Level == 0) if (m_Level == 0) {
{
cip = m_ctx->cip; cip = m_ctx->cip;
} } else if (m_ctx->rp > 0) {
else if (m_ctx->rp > 0)
{
/* Entries go from ctx.rp - 1 to m_StartRp */ /* Entries go from ctx.rp - 1 to m_StartRp */
cell_t offs, start, end; cell_t offs, start, end;
@ -294,60 +284,47 @@ bool CContextTrace::GetTraceInfo(CallStackInfo *trace)
} }
cip = m_ctx->rstk_cips[start - offs]; cip = m_ctx->rstk_cips[start - offs];
} } else {
else
{
return false; return false;
} }
if (trace == NULL) if (trace == NULL) {
{
m_Level++; m_Level++;
return true; return true;
} }
if (m_pDebug->LookupFile(cip, &(trace->filename)) != SP_ERROR_NONE) if (m_pDebug->LookupFile(cip, &(trace->filename)) != SP_ERROR_NONE)
{
trace->filename = NULL; trace->filename = NULL;
}
if (m_pDebug->LookupFunction(cip, &(trace->function)) != SP_ERROR_NONE) if (m_pDebug->LookupFunction(cip, &(trace->function)) != SP_ERROR_NONE)
{
trace->function = NULL; trace->function = NULL;
}
if (m_pDebug->LookupLine(cip, &(trace->line)) != SP_ERROR_NONE) if (m_pDebug->LookupLine(cip, &(trace->line)) != SP_ERROR_NONE)
{
trace->line = 0; trace->line = 0;
}
m_Level++; m_Level++;
return true; return true;
} }
const char *CContextTrace::GetLastNative(uint32_t *index) const char *
CContextTrace::GetLastNative(uint32_t *index)
{ {
if (m_ctx->n_err == SP_ERROR_NONE) if (m_ctx->n_err == SP_ERROR_NONE)
{
return NULL; return NULL;
}
sp_native_t *native; sp_native_t *native;
if (m_pRuntime->GetNativeByIndex(m_ctx->n_idx, &native) != SP_ERROR_NONE) if (m_pRuntime->GetNativeByIndex(m_ctx->n_idx, &native) != SP_ERROR_NONE)
{
return NULL; return NULL;
}
if (index) if (index)
{
*index = m_ctx->n_idx; *index = m_ctx->n_idx;
}
return native->name; return native->name;
} }
IDebugListener *SourcePawnEngine::GetDebugHook() IDebugListener *
SourcePawnEngine::GetDebugHook()
{ {
return m_pDebugHook; return m_pDebugHook;
} }

View File

@ -1,34 +1,15 @@
/** // vim: set sts=2 ts=8 sw=2 tw=99 et:
* vim: set ts=4 : //
* ============================================================================= // Copyright (C) 2006-2015 AlliedModders LLC
* SourcePawn //
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. // This file is part of SourcePawn. SourcePawn is free software: you can
* ============================================================================= // redistribute it and/or modify it under the terms of the GNU General Public
* // License as published by the Free Software Foundation, either version 3 of
* This program is free software; you can redistribute it and/or modify it under // the License, or (at your option) any later version.
* the terms of the GNU General Public License, version 3.0, as published by the //
* Free Software Foundation. // You should have received a copy of the GNU General Public License along with
* // SourcePawn. If not, see http://www.gnu.org/licenses/.
* This program is distributed in the hope that it will be useful, but WITHOUT //
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#ifndef _INCLUDE_SOURCEPAWN_VM_ENGINE_H_ #ifndef _INCLUDE_SOURCEPAWN_VM_ENGINE_H_
#define _INCLUDE_SOURCEPAWN_VM_ENGINE_H_ #define _INCLUDE_SOURCEPAWN_VM_ENGINE_H_
@ -41,6 +22,7 @@ class CContextTrace : public IContextTrace
{ {
public: public:
CContextTrace(BaseRuntime *pRuntime, int err, const char *errstr, cell_t start_rp); CContextTrace(BaseRuntime *pRuntime, int err, const char *errstr, cell_t start_rp);
public: public:
int GetErrorCode(); int GetErrorCode();
const char *GetErrorString(); const char *GetErrorString();
@ -49,6 +31,7 @@ public:
bool GetTraceInfo(CallStackInfo *trace); bool GetTraceInfo(CallStackInfo *trace);
void ResetTrace(); void ResetTrace();
const char *GetLastNative(uint32_t *index); const char *GetLastNative(uint32_t *index);
private: private:
BaseRuntime *m_pRuntime; BaseRuntime *m_pRuntime;
sp_context_t *m_ctx; sp_context_t *m_ctx;
@ -64,6 +47,7 @@ class SourcePawnEngine : public ISourcePawnEngine
public: public:
SourcePawnEngine(); SourcePawnEngine();
~SourcePawnEngine(); ~SourcePawnEngine();
public: //ISourcePawnEngine public: //ISourcePawnEngine
sp_plugin_t *LoadFromFilePointer(FILE *fp, int *err); sp_plugin_t *LoadFromFilePointer(FILE *fp, int *err);
sp_plugin_t *LoadFromMemory(void *base, sp_plugin_t *plugin, int *err); sp_plugin_t *LoadFromMemory(void *base, sp_plugin_t *plugin, int *err);
@ -82,10 +66,12 @@ public: //ISourcePawnEngine
void FreePageMemory(void *ptr); void FreePageMemory(void *ptr);
const char *GetErrorString(int err); const char *GetErrorString(int err);
void ReportError(BaseRuntime *runtime, int err, const char *errstr, cell_t rp_start); void ReportError(BaseRuntime *runtime, int err, const char *errstr, cell_t rp_start);
public: //Plugin function stuff public: //Plugin function stuff
CFunction *GetFunctionFromPool(funcid_t f, IPluginContext *plugin); CFunction *GetFunctionFromPool(funcid_t f, IPluginContext *plugin);
void ReleaseFunctionToPool(CFunction *func); void ReleaseFunctionToPool(CFunction *func);
IDebugListener *GetDebugHook(); IDebugListener *GetDebugHook();
private: private:
IDebugListener *m_pDebugHook; IDebugListener *m_pDebugHook;
}; };