From 79cf4e07eb61b916be15c21bc9e60c08e37a5fcf Mon Sep 17 00:00:00 2001
From: David Anderson <dvander@alliedmods.net>
Date: Fri, 11 Jul 2008 05:14:32 +0000
Subject: [PATCH] GetContext() now returns 'this' as a hack (shouldn't+can't be
 accessed anyway)

--HG--
branch : refac-jit
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/branches/refac-jit%402397
---
 public/sourcepawn/sp_vm_api.h        |  4 +++-
 sourcepawn/jit/BaseRuntime.cpp       |  8 +++++++-
 sourcepawn/jit/BaseRuntime.h         |  2 ++
 sourcepawn/jit/sp_vm_basecontext.cpp | 19 +++++++++++++------
 sourcepawn/jit/sp_vm_basecontext.h   |  3 +--
 sourcepawn/jit/sp_vm_engine.cpp      |  4 ++--
 sourcepawn/jit/x86/jit_x86.cpp       |  9 ++++-----
 7 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/public/sourcepawn/sp_vm_api.h b/public/sourcepawn/sp_vm_api.h
index a86575be..809dc0ed 100644
--- a/public/sourcepawn/sp_vm_api.h
+++ b/public/sourcepawn/sp_vm_api.h
@@ -494,7 +494,9 @@ namespace SourcePawn
 		/**
 		 * @brief Deprecated, do not use.
 		 *
-		 * @return				Opaque pointer.
+		 * Returns the pointer of this object, casted to an opaque structure.
+		 *
+		 * @return				Returns this.
 		 */
 		virtual sp_context_t *GetContext() =0;
 
diff --git a/sourcepawn/jit/BaseRuntime.cpp b/sourcepawn/jit/BaseRuntime.cpp
index 3c3227df..3d7cc402 100644
--- a/sourcepawn/jit/BaseRuntime.cpp
+++ b/sourcepawn/jit/BaseRuntime.cpp
@@ -52,7 +52,7 @@ BaseRuntime::~BaseRuntime()
 
 void BaseRuntime::ClearCompile()
 {
-	g_Jit1.FreeContextVars(m_pCtx->GetContext());
+	g_Jit1.FreeContextVars(m_pCtx->GetCtx());
 	g_Jit1.FreePluginVars(m_pPlugin);
 }
 
@@ -349,3 +349,9 @@ size_t BaseRuntime::GetMemUsage()
 
 	return mem;
 }
+
+BaseContext *BaseRuntime::GetBaseContext()
+{
+	return m_pCtx;
+}
+
diff --git a/sourcepawn/jit/BaseRuntime.h b/sourcepawn/jit/BaseRuntime.h
index fc90b4eb..5ff75601 100644
--- a/sourcepawn/jit/BaseRuntime.h
+++ b/sourcepawn/jit/BaseRuntime.h
@@ -45,6 +45,8 @@ public:
 	virtual void SetPauseState(bool paused);
 	virtual bool IsPaused();
 	virtual size_t GetMemUsage();
+public:
+	BaseContext *GetBaseContext();
 private:
 	void ClearCompile();
 	void RefreshFunctionCache();
diff --git a/sourcepawn/jit/sp_vm_basecontext.cpp b/sourcepawn/jit/sp_vm_basecontext.cpp
index e6988046..32b3c3ae 100644
--- a/sourcepawn/jit/sp_vm_basecontext.cpp
+++ b/sourcepawn/jit/sp_vm_basecontext.cpp
@@ -65,6 +65,11 @@ IVirtualMachine *BaseContext::GetVirtualMachine()
 }
 
 sp_context_t *BaseContext::GetContext()
+{
+	return reinterpret_cast<sp_context_t *>((IPluginContext * )this);
+}
+
+sp_context_t *BaseContext::GetCtx()
 {
 	return &m_ctx;
 }
@@ -481,12 +486,14 @@ int BaseContext::LocalToStringNULL(cell_t local_addr, char **addr)
 
 SourceMod::IdentityToken_t *BaseContext::GetIdentity()
 {
-	return m_pToken;
-}
+	SourceMod::IdentityToken_t *tok;
 
-void BaseContext::SetIdentity(SourceMod::IdentityToken_t *token)
-{
-	m_pToken = token;
+	if (GetKey(1, (void **)&tok))
+	{
+		return tok;
+	}
+
+	return NULL;
 }
 
 cell_t *BaseContext::GetNullRef(SP_NULL_TYPE type)
@@ -536,7 +543,7 @@ int BaseContext::Execute(IPluginFunction *function, const cell_t *params, unsign
 		return SP_ERROR_NOT_RUNNABLE;
 	}
 
-	if (m_ctx.hp + 16*sizeof(cell_t) > (cell_t)(m_ctx.sp - (sizeof(cell_t) * (num_params + 1))))
+	if ((cell_t)(m_ctx.hp + 16*sizeof(cell_t)) > (cell_t)(m_ctx.sp - (sizeof(cell_t) * (num_params + 1))))
 	{
 		return SP_ERROR_STACKLOW;
 	}
diff --git a/sourcepawn/jit/sp_vm_basecontext.h b/sourcepawn/jit/sp_vm_basecontext.h
index d883e31a..38a3e67e 100644
--- a/sourcepawn/jit/sp_vm_basecontext.h
+++ b/sourcepawn/jit/sp_vm_basecontext.h
@@ -49,6 +49,7 @@ public:
 public: //IPluginContext
 	IVirtualMachine *GetVirtualMachine();
 	sp_context_t *GetContext();
+	sp_context_t *GetCtx();
 	bool IsDebugging();
 	int SetDebugBreak(void *newpfn, void *oldpfn);
 	IPluginDebugInfo *GetDebugInfo();
@@ -82,7 +83,6 @@ public: //IPluginContext
 	IPluginFunction *GetFunctionByName(const char *public_name);
 	IPluginFunction *GetFunctionById(funcid_t func_id);
 	SourceMod::IdentityToken_t *GetIdentity();
-	void SetIdentity(SourceMod::IdentityToken_t *token);
 	cell_t *GetNullRef(SP_NULL_TYPE type);
 	int LocalToStringNULL(cell_t local_addr, char **addr);
 	int BindNativeToIndex(uint32_t index, SPVM_NATIVE_FUNC native);
@@ -100,7 +100,6 @@ private:
 	void _SetErrorMessage(const char *msg, ...);
 private:
 	sp_plugin_t *m_pPlugin;
-	SourceMod::IdentityToken_t *m_pToken;
 	cell_t *m_pNullVec;
 	cell_t *m_pNullString;
 	char m_MsgCache[1024];
diff --git a/sourcepawn/jit/sp_vm_engine.cpp b/sourcepawn/jit/sp_vm_engine.cpp
index 9191d9fb..944170b5 100644
--- a/sourcepawn/jit/sp_vm_engine.cpp
+++ b/sourcepawn/jit/sp_vm_engine.cpp
@@ -329,9 +329,9 @@ void SourcePawnEngine::PopTracer(int error, const char *msg)
 	{
 		uint32_t native = INVALID_CIP;
 
-		if (m_CallStack->ctx->GetContext()->n_err)
+		if (m_CallStack->ctx->GetCtx()->n_err)
 		{
-			native = m_CallStack->ctx->GetContext()->n_idx;
+			native = m_CallStack->ctx->GetCtx()->n_idx;
 		}
 
 		CContextTrace trace(m_CallStack, error, msg, native);
diff --git a/sourcepawn/jit/x86/jit_x86.cpp b/sourcepawn/jit/x86/jit_x86.cpp
index fd3ed9db..0c283cf1 100644
--- a/sourcepawn/jit/x86/jit_x86.cpp
+++ b/sourcepawn/jit/x86/jit_x86.cpp
@@ -38,6 +38,8 @@
 #include "../jit_version.h"
 #include "../sp_vm_engine.h"
 #include "../engine2.h"
+#include "BaseRuntime.h"
+#include "../sp_vm_basecontext.h"
 
 #if defined USE_UNGEN_OPCODES
 #include "ungen_opcodes.h"
@@ -2416,10 +2418,7 @@ cell_t NativeCallback_Debug_Profile(sp_context_t *ctx, ucell_t native_idx, cell_
 
 static cell_t InvalidNative(IPluginContext *pCtx, const cell_t *params)
 {
-	sp_context_t *ctx = pCtx->GetContext();
-	ctx->n_err = SP_ERROR_INVALID_NATIVE;
-
-	return 0;
+	return pCtx->ThrowNativeErrorEx(SP_ERROR_INVALID_NATIVE, "Invalid native");
 }
 
 jitoffs_t RelocLookup(JitWriter *jit, cell_t pcode_offs, bool relative)
@@ -2664,7 +2663,7 @@ jit_rewind:
 	 *************/
 
 	/* setup  basics */
-	sp_context_t *ctx = data->runtime->GetDefaultContext()->GetContext();
+	sp_context_t *ctx = data->runtime->GetBaseContext()->GetCtx();
 
 	/* Clear out any old cruft */
 	if (plugin->codebase != NULL)