initial
This commit is contained in:
715
vscript/languages/gm/vgm/vgm.cpp
Normal file
715
vscript/languages/gm/vgm/vgm.cpp
Normal file
@@ -0,0 +1,715 @@
|
||||
//========== Copyright <20> 2008, Valve Corporation, All rights reserved. ========
|
||||
//
|
||||
// Purpose:
|
||||
//
|
||||
//=============================================================================
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#pragma warning( disable:4201)
|
||||
#pragma comment(lib, "winmm.lib" )
|
||||
#include <windows.h>
|
||||
#include <mmsystem.h> // multimedia timer (may need winmm.lib)
|
||||
#include <stdio.h>
|
||||
#include <io.h>
|
||||
#include <conio.h>
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
#include "datamap.h"
|
||||
#include "tier1/functors.h"
|
||||
#include "tier1/utlvector.h"
|
||||
#include "tier1/utlhash.h"
|
||||
#include "tier1/fmtstr.h"
|
||||
#include "vscript//ivscript.h"
|
||||
|
||||
#include "gmThread.h" // game monkey script
|
||||
#include "gmArrayLib.h"
|
||||
#include "gmCall.h"
|
||||
#include "gmGCRoot.h"
|
||||
#include "gmGCRootUtil.h"
|
||||
#include "gmHelpers.h"
|
||||
#include "gmMathLib.h"
|
||||
#include "gmStringLib.h"
|
||||
#include "gmVector3Lib.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
class CGCDisabler
|
||||
{
|
||||
public:
|
||||
CGCDisabler( gmMachine *pMachine )
|
||||
{
|
||||
m_pMachine = pMachine;
|
||||
m_bWasEnabled = m_pMachine->IsGCEnabled();
|
||||
m_pMachine->EnableGC( false );
|
||||
}
|
||||
~CGCDisabler()
|
||||
{
|
||||
m_pMachine->EnableGC( m_bWasEnabled );
|
||||
}
|
||||
|
||||
gmMachine *m_pMachine;
|
||||
bool m_bWasEnabled;
|
||||
};
|
||||
|
||||
#define DISABLE_GC() CGCDisabler gcDisabler( this )
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
class CGameMonkeyVM : public IScriptVM,
|
||||
public gmMachine
|
||||
{
|
||||
public:
|
||||
CGameMonkeyVM()
|
||||
{
|
||||
}
|
||||
|
||||
bool Init()
|
||||
{
|
||||
if ( !m_global )
|
||||
{
|
||||
// Must be a re-init
|
||||
gmMachine::Init();
|
||||
}
|
||||
s_printCallback = ScriptPrintCallback;
|
||||
SetDebugMode( true );
|
||||
AddCPPOwnedGMObject( GetGlobals() );
|
||||
|
||||
gmBindArrayLib( this );
|
||||
gmBindMathLib( this );
|
||||
gmBindStringLib( this );
|
||||
gmBindVector3Lib( this );
|
||||
|
||||
m_TypeMap.Init( 256 );
|
||||
return true;
|
||||
}
|
||||
|
||||
void Shutdown()
|
||||
{
|
||||
// Dump run time errors to output
|
||||
if ( GetDebugMode() )
|
||||
{
|
||||
FlushErrorLog();
|
||||
}
|
||||
RemoveAllCPPOwnedGMObjects();
|
||||
ResetAndFreeMemory();
|
||||
m_TypeMap.Purge();
|
||||
}
|
||||
|
||||
ScriptLanguage_t GetLanguage()
|
||||
{
|
||||
return SL_GAMEMONKEY;
|
||||
}
|
||||
|
||||
virtual const char *GetLanguageName()
|
||||
{
|
||||
return "GameMonkey";
|
||||
}
|
||||
|
||||
virtual void AddSearchPath( const char *pszSearchPath )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool ConnectDebugger() { return true; }
|
||||
void DisconnectDebugger() {}
|
||||
|
||||
bool Frame( float simTime )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
HSCRIPT CompileScript( const char *pszScript, const char *pszId )
|
||||
{
|
||||
DISABLE_GC();
|
||||
gmFunctionObject *pFunction = CompileStringToFunction( pszScript );
|
||||
if ( !pFunction )
|
||||
{
|
||||
FlushErrorLog();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ObjectHandle_t *pObjectHandle = new ObjectHandle_t;
|
||||
pObjectHandle->pObject = pFunction;
|
||||
AddCPPOwnedGMObject( pFunction );
|
||||
if ( pszId )
|
||||
{
|
||||
pObjectHandle->pString = AllocStringObject( pszId );
|
||||
AddCPPOwnedGMObject( pObjectHandle->pString );
|
||||
gmTableObject *pGlobals = GetGlobals();
|
||||
pGlobals->Set( this, gmVariable(pObjectHandle->pString), gmVariable(pFunction) );
|
||||
}
|
||||
else
|
||||
{
|
||||
pObjectHandle->pString = NULL;
|
||||
}
|
||||
return (HSCRIPT)pObjectHandle;
|
||||
}
|
||||
|
||||
void ReleaseScript( HSCRIPT hScript )
|
||||
{
|
||||
ReleaseHandle( hScript );
|
||||
}
|
||||
|
||||
ScriptStatus_t Run( HSCRIPT hScript, HSCRIPT hScope = NULL, bool bWait = true )
|
||||
{
|
||||
return CGameMonkeyVM::ExecuteFunction( hScript, NULL, 0, NULL, hScope, bWait );
|
||||
}
|
||||
|
||||
ScriptStatus_t Run( const char *pszScript, bool bWait = true )
|
||||
{
|
||||
Assert( bWait );
|
||||
int errors = ExecuteString( pszScript, NULL, bWait );
|
||||
// Dump compile time errors to output
|
||||
if(errors)
|
||||
{
|
||||
bool first = true;
|
||||
const char * message;
|
||||
|
||||
while((message = GetLog().GetEntry(first)))
|
||||
{
|
||||
Msg( "%s\n", message );
|
||||
}
|
||||
GetLog().Reset();
|
||||
return SCRIPT_ERROR;
|
||||
}
|
||||
return SCRIPT_DONE;
|
||||
}
|
||||
|
||||
ScriptStatus_t Run( HSCRIPT hScript, bool bWait )
|
||||
{
|
||||
Assert( bWait );
|
||||
return CGameMonkeyVM::Run( hScript, (HSCRIPT)NULL, bWait );
|
||||
}
|
||||
|
||||
HSCRIPT CreateScope( const char *pszScope, HSCRIPT hParent = NULL )
|
||||
{
|
||||
Assert( pszScope );
|
||||
Assert( !hParent );
|
||||
|
||||
DISABLE_GC();
|
||||
|
||||
gmTableObject *pGlobals = GetGlobals();
|
||||
gmTableObject *pNewTable = AllocTableObject();
|
||||
ObjectHandle_t *pObjectHandle = new ObjectHandle_t;
|
||||
|
||||
AddCPPOwnedGMObject( pNewTable );
|
||||
pObjectHandle->pObject = pNewTable;
|
||||
pObjectHandle->pString = AllocStringObject( pszScope );
|
||||
AddCPPOwnedGMObject( pObjectHandle->pString );
|
||||
|
||||
pGlobals->Set( this, gmVariable(pObjectHandle->pString), gmVariable(pNewTable) );
|
||||
|
||||
return (HSCRIPT)pObjectHandle;
|
||||
}
|
||||
|
||||
void ReleaseScope( HSCRIPT hScript )
|
||||
{
|
||||
ReleaseHandle( hScript );
|
||||
}
|
||||
|
||||
HSCRIPT LookupFunction( const char *pszFunction, HSCRIPT hScope = NULL )
|
||||
{
|
||||
gmTableObject *pScope;
|
||||
|
||||
if ( !hScope )
|
||||
{
|
||||
pScope = GetGlobals();
|
||||
}
|
||||
else
|
||||
{
|
||||
ObjectHandle_t *pScopeHandle = (ObjectHandle_t *)hScope;
|
||||
pScope = assert_cast<gmTableObject *>(pScopeHandle->pObject);
|
||||
}
|
||||
gmVariable varFunction = pScope->Get( this, pszFunction );
|
||||
gmFunctionObject *pFunction = varFunction.GetFunctionObjectSafe();
|
||||
if ( pFunction )
|
||||
{
|
||||
ObjectHandle_t *pFunctionHandle = new ObjectHandle_t;
|
||||
pFunctionHandle->pObject = pFunction;
|
||||
pFunctionHandle->pString = NULL;
|
||||
AddCPPOwnedGMObject( pFunction );
|
||||
return (HSCRIPT)pFunctionHandle;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void ReleaseFunction( HSCRIPT hScript )
|
||||
{
|
||||
ReleaseHandle( hScript );
|
||||
}
|
||||
|
||||
ScriptStatus_t ExecuteFunction( HSCRIPT hFunction, ScriptVariant_t *pArgs, int nArgs, ScriptVariant_t *pReturn, HSCRIPT hScope, bool bWait )
|
||||
{
|
||||
Assert( hFunction );
|
||||
Assert( bWait );
|
||||
ScriptStatus_t result = SCRIPT_ERROR;
|
||||
if ( pReturn )
|
||||
{
|
||||
pReturn->m_type = FIELD_VOID;
|
||||
}
|
||||
|
||||
if ( hFunction )
|
||||
{
|
||||
ObjectHandle_t *pObjectHandle= (ObjectHandle_t *)hFunction;
|
||||
gmTableObject *pGlobals = NULL;
|
||||
gmTableObject *pScope;
|
||||
if ( hScope )
|
||||
{
|
||||
ObjectHandle_t *pScopeHandle= (ObjectHandle_t *)hScope;
|
||||
pScope = assert_cast<gmTableObject *>(pScopeHandle->pObject);
|
||||
pGlobals = GetGlobals();
|
||||
SetGlobals( pScope );
|
||||
}
|
||||
|
||||
gmCall functionCaller;
|
||||
gmFunctionObject *pFunction = assert_cast<gmFunctionObject *>(pObjectHandle->pObject);
|
||||
if ( functionCaller.BeginFunction( this, pFunction, gmVariable::s_null, !bWait ) )
|
||||
{
|
||||
for ( int i = 0; i < nArgs; i++ )
|
||||
{
|
||||
switch ( pArgs[i].m_type )
|
||||
{
|
||||
case FIELD_FLOAT: functionCaller.AddParamFloat( pArgs[i] ); break;
|
||||
case FIELD_CSTRING: functionCaller.AddParamString( pArgs[i], strlen( pArgs[i].m_pszString ) ); break;
|
||||
case FIELD_VECTOR: Assert( 0 ); functionCaller.AddParamNull(); break;
|
||||
case FIELD_INTEGER: functionCaller.AddParamInt( pArgs[i] ); break;
|
||||
case FIELD_BOOLEAN: functionCaller.AddParamInt( pArgs[i].m_bool ); break;
|
||||
case FIELD_CHARACTER: { char sz[2]; sz[0] = pArgs[i].m_char; sz[1] = 0; functionCaller.AddParamString( sz, 1 ); break; }
|
||||
}
|
||||
}
|
||||
|
||||
functionCaller.End();
|
||||
|
||||
if ( pReturn && ( bWait || !functionCaller.GetThread() ) && functionCaller.DidReturnVariable() )
|
||||
{
|
||||
const gmVariable &ret = functionCaller.GetReturnedVariable();
|
||||
switch ( ret.m_type )
|
||||
{
|
||||
case GM_NULL: break;
|
||||
case GM_INT: *pReturn = ret.m_value.m_int; break;
|
||||
case GM_FLOAT: *pReturn = ret.m_value.m_float; break;
|
||||
case GM_STRING:
|
||||
{
|
||||
gmStringObject *pString = (gmStringObject *)ret.m_value.m_ref;
|
||||
int size = pString->GetLength() + 1;
|
||||
pReturn->m_type = FIELD_CSTRING;
|
||||
pReturn->m_pszString = new char[size];
|
||||
memcpy( (void *)pReturn->m_pszString, pString->GetString(), size );
|
||||
}
|
||||
break;
|
||||
default:
|
||||
DevMsg( "Script function returned unsupported type\n" );
|
||||
pReturn->m_type = FIELD_VOID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( !FlushErrorLog() )
|
||||
{
|
||||
result = SCRIPT_DONE;
|
||||
}
|
||||
|
||||
if ( hScope )
|
||||
{
|
||||
SetGlobals( pGlobals );
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
gmMachine *GetMachine()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
void RegisterFunction( ScriptFunctionBinding_t *pScriptFunction )
|
||||
{
|
||||
RegisterLibraryFunction( pScriptFunction->m_desc.m_pszScriptName, &TranslateCall, NULL, pScriptFunction );
|
||||
}
|
||||
|
||||
bool RegisterClass( ScriptClassDesc_t *pClassDesc )
|
||||
{
|
||||
COMPILE_TIME_ASSERT( sizeof(pClassDesc) == sizeof(int) );
|
||||
if ( m_TypeMap.Find( (int)pClassDesc ) != m_TypeMap.InvalidHandle() )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
gmType type = CreateUserType( pClassDesc->m_pszScriptName );
|
||||
m_TypeMap.Insert( (int)pClassDesc, type );
|
||||
|
||||
ScriptClassDesc_t *pCurDesc = pClassDesc;
|
||||
CUtlVectorFixed<gmFunctionEntry, 512> functionEntries;
|
||||
while ( pCurDesc )
|
||||
{
|
||||
int nFunctions = pClassDesc->m_FunctionBindings.Count();
|
||||
functionEntries.SetSize( nFunctions );
|
||||
int i;
|
||||
for ( i = 0; i < nFunctions; i++)
|
||||
{
|
||||
functionEntries[i].m_function = &TranslateCall;
|
||||
functionEntries[i].m_name = pClassDesc->m_FunctionBindings[i].m_desc.m_pszFunction;
|
||||
functionEntries[i].m_userData = &pClassDesc->m_FunctionBindings[i];
|
||||
}
|
||||
RegisterTypeLibrary( type, functionEntries.Base(), nFunctions );
|
||||
pCurDesc = pCurDesc->m_pBaseDesc;
|
||||
}
|
||||
|
||||
if ( pClassDesc->m_pfnConstruct )
|
||||
{
|
||||
RegisterLibraryFunction( pClassDesc->m_pszScriptName, &Construct, NULL, pClassDesc );
|
||||
RegisterUserCallbacks( type, NULL, &Destruct );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RegisterInstance( ScriptClassDesc_t *pDesc, void *pInstance, const char *pszInstance, HSCRIPT hScope = NULL )
|
||||
{
|
||||
if ( !RegisterClass( pDesc ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
DISABLE_GC();
|
||||
|
||||
bool bResult = true;
|
||||
|
||||
InstanceContext_t *pInstanceContext = new InstanceContext_t;
|
||||
pInstanceContext->pInstance = pInstance;
|
||||
pInstanceContext->pClassDesc = NULL; // i.e., no destruct
|
||||
|
||||
// @TODO: This extra hash lookup could be eliminated (as it is also done in RegisterClass above) [2/13/2008 tom]
|
||||
gmUserObject *pObject = AllocUserObject( pInstanceContext, m_TypeMap[m_TypeMap.Find((int)pDesc)] );
|
||||
AddCPPOwnedGMObject( pObject );
|
||||
|
||||
gmTableObject *pScope;
|
||||
|
||||
if ( hScope )
|
||||
{
|
||||
ObjectHandle_t *pScopeHandle= (ObjectHandle_t *)hScope;
|
||||
pScope = (gmTableObject *)pScopeHandle->pObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
pScope = GetGlobals();
|
||||
}
|
||||
|
||||
if ( pScope )
|
||||
{
|
||||
pScope->Set( this, pszInstance, gmVariable( pObject ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
DevMsg( "Undefined script scope!\n" );
|
||||
bResult = false;
|
||||
}
|
||||
|
||||
return bResult;
|
||||
}
|
||||
|
||||
void RemoveInstance( ScriptClassDesc_t *pDesc, void *pInstance, const char *pszInstance, HSCRIPT hScope = NULL )
|
||||
{
|
||||
DISABLE_GC();
|
||||
gmTableObject *pScope;
|
||||
|
||||
if ( hScope )
|
||||
{
|
||||
ObjectHandle_t *pScopeHandle= (ObjectHandle_t *)hScope;
|
||||
pScope = (gmTableObject *)pScopeHandle->pObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
pScope = GetGlobals();
|
||||
}
|
||||
|
||||
if ( pScope )
|
||||
{
|
||||
gmVariable scriptVar = pScope->Get( this, pszInstance );
|
||||
gmUserObject *pObject = ( !scriptVar.IsNull() ) ? scriptVar.GetUserObjectSafe( m_TypeMap[m_TypeMap.Find((int)pDesc)] ) : NULL;
|
||||
if ( pObject )
|
||||
{
|
||||
delete pObject->m_user;
|
||||
pObject->m_user = NULL;
|
||||
pScope->Set( this, pszInstance, gmVariable::s_null );
|
||||
RemoveCPPOwnedGMObject( pObject );
|
||||
}
|
||||
else
|
||||
{
|
||||
DevMsg( "Unknown instance\n" );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DevMsg( "Undefined script scope!\n" );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
struct InstanceContext_t
|
||||
{
|
||||
void *pInstance;
|
||||
ScriptClassDesc_t *pClassDesc;
|
||||
};
|
||||
|
||||
struct ObjectHandle_t
|
||||
{
|
||||
gmStringObject *pString;
|
||||
gmObject *pObject;
|
||||
};
|
||||
|
||||
void ReleaseHandle( HSCRIPT hScript )
|
||||
{
|
||||
if ( hScript )
|
||||
{
|
||||
ObjectHandle_t *pObjectHandle = (ObjectHandle_t *)hScript;
|
||||
RemoveCPPOwnedGMObject( pObjectHandle->pObject );
|
||||
|
||||
if ( pObjectHandle->pString )
|
||||
{
|
||||
gmTableObject *pGlobals = GetGlobals();
|
||||
pGlobals->Set( this, gmVariable(pObjectHandle->pString), gmVariable::s_null );
|
||||
RemoveCPPOwnedGMObject( pObjectHandle->pString );
|
||||
}
|
||||
delete pObjectHandle;
|
||||
}
|
||||
}
|
||||
|
||||
static void ScriptPrintCallback(gmMachine *pMachine, const char *pString)
|
||||
{
|
||||
Msg( "%s\n", pString );
|
||||
}
|
||||
|
||||
static int GM_CDECL TranslateCall( gmThread *a_thread )
|
||||
{
|
||||
const gmFunctionObject *fn = a_thread->GetFunctionObject();
|
||||
ScriptFunctionBinding_t *pVMScriptFunction = (ScriptFunctionBinding_t *)fn->m_cUserData;
|
||||
|
||||
int nActualParams = a_thread->GetNumParams();
|
||||
int nFormalParams = pVMScriptFunction->m_desc.m_Parameters.Count();
|
||||
|
||||
GM_CHECK_NUM_PARAMS( nFormalParams );
|
||||
|
||||
CUtlVectorFixed<ScriptVariant_t, 14> params;
|
||||
ScriptVariant_t returnValue;
|
||||
|
||||
params.SetSize( nFormalParams );
|
||||
|
||||
int i = 0;
|
||||
|
||||
if ( nActualParams )
|
||||
{
|
||||
int iLimit = min( nActualParams, nFormalParams );
|
||||
ScriptDataType_t *pCurParamType = pVMScriptFunction->m_desc.m_Parameters.Base();
|
||||
for ( i = 0; i < iLimit; i++, pCurParamType++ )
|
||||
{
|
||||
switch ( *pCurParamType )
|
||||
{
|
||||
case FIELD_FLOAT: params[i] = a_thread->ParamFloatOrInt( i ); break;
|
||||
case FIELD_CSTRING: params[i] = a_thread->ParamString( i ); break;
|
||||
case FIELD_VECTOR: Assert( 0 ); params[i] = (Vector *)NULL; break;
|
||||
case FIELD_INTEGER: params[i] = (int)a_thread->ParamFloatOrInt( i ); break;
|
||||
case FIELD_BOOLEAN:
|
||||
{
|
||||
int type = a_thread->ParamType(i);
|
||||
if ( type == GM_INT )
|
||||
params[i] = (bool)(a_thread->Param(i).m_value.m_int != 0 );
|
||||
else if ( type == GM_FLOAT )
|
||||
params[i] = (bool)(a_thread->Param(i).m_value.m_float != 0.0 );
|
||||
else
|
||||
params[i] = true;
|
||||
break;
|
||||
}
|
||||
case FIELD_CHARACTER: params[i] = a_thread->ParamString( i )[0]; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for ( ; i < nFormalParams; i++ )
|
||||
{
|
||||
COMPILE_TIME_ASSERT( sizeof(Vector *) >= sizeof(int) );
|
||||
params[i] = (Vector *)NULL;
|
||||
}
|
||||
|
||||
InstanceContext_t *pContext;
|
||||
void *pObject;
|
||||
|
||||
if ( pVMScriptFunction->m_flags & SF_MEMBER_FUNC )
|
||||
{
|
||||
pContext = (InstanceContext_t *)a_thread->ThisUser();
|
||||
|
||||
if ( !pContext )
|
||||
{
|
||||
return GM_EXCEPTION;
|
||||
}
|
||||
|
||||
pObject = pContext->pInstance;
|
||||
|
||||
if ( !pObject )
|
||||
{
|
||||
return GM_EXCEPTION;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pObject = NULL;
|
||||
}
|
||||
|
||||
(*pVMScriptFunction->m_pfnBinding)( pVMScriptFunction->m_pFunction, pObject, params.Base(), params.Count(), ( pVMScriptFunction->m_desc.m_ReturnType != FIELD_VOID ) ? &returnValue : NULL );
|
||||
|
||||
if ( pVMScriptFunction->m_desc.m_ReturnType != FIELD_VOID )
|
||||
{
|
||||
switch ( pVMScriptFunction->m_desc.m_ReturnType )
|
||||
{
|
||||
case FIELD_FLOAT: a_thread->PushFloat( returnValue ); break;
|
||||
case FIELD_CSTRING: Assert( 0 ); a_thread->PushNull(); break;
|
||||
case FIELD_VECTOR: Assert( 0 ); a_thread->PushNull(); break;
|
||||
case FIELD_INTEGER: a_thread->PushInt( returnValue ); break;
|
||||
case FIELD_BOOLEAN: a_thread->PushInt( (bool)returnValue ); break;
|
||||
case FIELD_CHARACTER: Assert( 0 ); a_thread->PushNull(); break;
|
||||
}
|
||||
}
|
||||
|
||||
return GM_OK;
|
||||
}
|
||||
|
||||
static int GM_CDECL Construct( gmThread *a_thread )
|
||||
{
|
||||
CGameMonkeyVM *pThis = assert_cast<CGameMonkeyVM *>(a_thread->GetMachine());
|
||||
const gmFunctionObject *fn = a_thread->GetFunctionObject();
|
||||
ScriptClassDesc_t *pClassDesc = (ScriptClassDesc_t *)fn->m_cUserData;
|
||||
|
||||
GM_CHECK_NUM_PARAMS( 0 );
|
||||
|
||||
InstanceContext_t *pInstanceContext = new InstanceContext_t;
|
||||
pInstanceContext->pInstance = pClassDesc->m_pfnConstruct();
|
||||
pInstanceContext->pClassDesc = pClassDesc;
|
||||
|
||||
// @TODO: put the type in the userdata? [2/12/2008 tom]
|
||||
a_thread->PushNewUser( pInstanceContext, pThis->m_TypeMap[pThis->m_TypeMap.Find((int)pClassDesc)] );
|
||||
|
||||
return GM_OK;
|
||||
}
|
||||
|
||||
static void Destruct( gmMachine *pMachine, gmUserObject* pObject )
|
||||
{
|
||||
InstanceContext_t *pInstanceContext = ( InstanceContext_t *)pObject->m_user;
|
||||
if ( pInstanceContext && pInstanceContext->pClassDesc )
|
||||
{
|
||||
pInstanceContext->pClassDesc->m_pfnDestruct( pInstanceContext->pInstance );
|
||||
delete pInstanceContext;
|
||||
}
|
||||
}
|
||||
|
||||
bool FlushErrorLog()
|
||||
{
|
||||
bool first = true;
|
||||
bool errors = false;
|
||||
const char * message;
|
||||
|
||||
while((message = GetLog().GetEntry(first)))
|
||||
{
|
||||
Msg( "%s\n", message );
|
||||
errors = true;
|
||||
}
|
||||
GetLog().Reset();
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
CUtlHashFast<gmType, CUtlHashFastGenericHash> m_TypeMap;
|
||||
};
|
||||
|
||||
IScriptVM *ScriptCreateGameMonkeyVM()
|
||||
{
|
||||
return new CGameMonkeyVM;
|
||||
}
|
||||
|
||||
void ScriptDestroyGameMonkeyVM( IScriptVM *pVM )
|
||||
{
|
||||
CGameMonkeyVM *pGameMonkeyVM = assert_cast<CGameMonkeyVM *>(pVM);
|
||||
delete pGameMonkeyVM;
|
||||
}
|
||||
|
||||
|
||||
#ifdef VGM_TEST
|
||||
CGameMonkeyVM g_GMScriptVM;
|
||||
IScriptVM *pScriptVM = &g_GMScriptVM;
|
||||
|
||||
struct Test
|
||||
{
|
||||
void Foo() { Msg( "Foo!\n");}
|
||||
};
|
||||
|
||||
BEGIN_SCRIPTDESC_ROOT( Test )
|
||||
DEFINE_SCRIPTFUNC( Foo )
|
||||
END_SCRIPTDESC();
|
||||
|
||||
Test test;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
int main( int argc, const char **argv)
|
||||
{
|
||||
if ( argc < 2 )
|
||||
{
|
||||
printf( "No script specified" );
|
||||
return 1;
|
||||
}
|
||||
|
||||
int key;
|
||||
do
|
||||
{
|
||||
pScriptVM->Init();
|
||||
|
||||
const char *pszScript = argv[1];
|
||||
FILE *hFile = fopen( pszScript, "rb" );
|
||||
if ( !hFile )
|
||||
{
|
||||
printf( "\"%s\" not found.\n", pszScript );
|
||||
return 1;
|
||||
}
|
||||
|
||||
int nFileLen = _filelength( _fileno( hFile ) );
|
||||
char *pBuf = new char[nFileLen + 1];
|
||||
fread( pBuf, 1, nFileLen, hFile );
|
||||
pBuf[nFileLen] = 0;
|
||||
fclose( hFile );
|
||||
|
||||
printf( "Executing script \"%s\"\n----------------------------------------\n", pszScript );
|
||||
|
||||
HSCRIPT hScript = pScriptVM->CompileScript( pBuf, "test" );
|
||||
if ( hScript )
|
||||
{
|
||||
HSCRIPT hScope = pScriptVM->CreateScope( "testScope" );
|
||||
pScriptVM->Run( hScript, hScope );
|
||||
HSCRIPT hFunction = pScriptVM->LookupFunction( "DoIt" );
|
||||
Assert( !hFunction );
|
||||
hFunction = pScriptVM->LookupFunction( "DoIt", hScope );
|
||||
Assert( hFunction );
|
||||
ScriptVariant_t ret;
|
||||
pScriptVM->RegisterInstance( &test, "test" );
|
||||
pScriptVM->Call( hFunction, hScope, true, &ret, "Har", 6.0, 99 );
|
||||
|
||||
ret.Free();
|
||||
|
||||
pScriptVM->ReleaseFunction( hFunction );
|
||||
pScriptVM->ReleaseScript( hScript );
|
||||
pScriptVM->ReleaseScope( hScope );
|
||||
}
|
||||
|
||||
printf("Script complete. Press q to exit, enter to run again.\n");
|
||||
key = _getch(); // Keypress before exit
|
||||
pScriptVM->Shutdown();
|
||||
} while ( key != 'q' );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
17
vscript/languages/gm/vgm/vgm.h
Normal file
17
vscript/languages/gm/vgm/vgm.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//========== Copyright <20> 2008, Valve Corporation, All rights reserved. ========
|
||||
//
|
||||
// Purpose:
|
||||
//
|
||||
//=============================================================================
|
||||
|
||||
#ifndef VGM_H
|
||||
#define VGM_H
|
||||
|
||||
#if defined( _WIN32 )
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
IScriptVM *ScriptCreateGameMonkeyVM();
|
||||
void ScriptDestroyGameMonkeyVM( IScriptVM *pVM );
|
||||
|
||||
#endif // VGM_H
|
||||
135
vscript/languages/gm/vgm/vgm.vpc
Normal file
135
vscript/languages/gm/vgm/vgm.vpc
Normal file
@@ -0,0 +1,135 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Project Script
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
$Macro SRCDIR "..\..\..\.."
|
||||
$Macro OUTBINDIR "."
|
||||
|
||||
$Include "$SRCDIR\vpc_scripts\source_exe_con_win32_base.vpc"
|
||||
|
||||
$Configuration
|
||||
{
|
||||
$Compiler
|
||||
{
|
||||
$AdditionalIncludeDirectories "$BASE,..\src\gm;..\src\binds;..\src\platform\win32msvc"
|
||||
$PreprocessorDefinitions "$BASE;PROTECTED_THINGS_DISABLE;VGM_TEST"
|
||||
}
|
||||
}
|
||||
|
||||
$Project "vgm"
|
||||
{
|
||||
$Folder "Source Files"
|
||||
{
|
||||
$File "..\..\..\..\public\vscript\ivscript.h"
|
||||
$File "..\..\..\..\public\vscript\vscript_templates.h"
|
||||
$File "vgm.cpp"
|
||||
}
|
||||
|
||||
$Folder "GameMonkey"
|
||||
{
|
||||
$Folder "gm"
|
||||
{
|
||||
$Folder "Source Files"
|
||||
{
|
||||
$File "..\src\gm\gmArraySimple.cpp" \
|
||||
"..\src\gm\gmByteCode.cpp" \
|
||||
"..\src\gm\gmByteCodeGen.cpp" \
|
||||
"..\src\gm\gmCodeGen.cpp" \
|
||||
"..\src\gm\gmCodeGenHooks.cpp" \
|
||||
"..\src\gm\gmCodeTree.cpp" \
|
||||
"..\src\gm\gmCrc.cpp" \
|
||||
"..\src\gm\gmDebug.cpp" \
|
||||
"..\src\gm\gmFunctionObject.cpp" \
|
||||
"..\src\gm\gmHash.cpp" \
|
||||
"..\src\gm\gmIncGC.cpp" \
|
||||
"..\src\gm\gmLibHooks.cpp" \
|
||||
"..\src\gm\gmListDouble.cpp" \
|
||||
"..\src\gm\gmLog.cpp" \
|
||||
"..\src\gm\gmMachine.cpp" \
|
||||
"..\src\gm\gmMachineLib.cpp" \
|
||||
"..\src\gm\gmMem.cpp" \
|
||||
"..\src\gm\gmMemChain.cpp" \
|
||||
"..\src\gm\gmMemFixed.cpp" \
|
||||
"..\src\gm\gmMemFixedSet.cpp" \
|
||||
"..\src\gm\gmOperators.cpp" \
|
||||
"..\src\gm\gmParser.cpp" \
|
||||
"..\src\gm\gmScanner.cpp" \
|
||||
"..\src\gm\gmStream.cpp" \
|
||||
"..\src\gm\gmStreamBuffer.cpp" \
|
||||
"..\src\gm\gmStringObject.cpp" \
|
||||
"..\src\gm\gmTableObject.cpp" \
|
||||
"..\src\gm\gmThread.cpp" \
|
||||
"..\src\gm\gmUserObject.cpp" \
|
||||
"..\src\gm\gmUtil.cpp" \
|
||||
"..\src\gm\gmVariable.cpp"
|
||||
}
|
||||
|
||||
$Folder "Header Files"
|
||||
{
|
||||
$File "..\src\platform\win32msvc\gmConfig_p.h" \
|
||||
"..\src\gm\gmArraySimple.h" \
|
||||
"..\src\gm\gmByteCode.h" \
|
||||
"..\src\gm\gmByteCodeGen.h" \
|
||||
"..\src\gm\gmCodeGen.h" \
|
||||
"..\src\gm\gmCodeGenHooks.h" \
|
||||
"..\src\gm\gmCodeTree.h" \
|
||||
"..\src\gm\gmConfig.h" \
|
||||
"..\src\gm\gmCrc.h" \
|
||||
"..\src\gm\gmDebug.h" \
|
||||
"..\src\gm\gmFunctionObject.h" \
|
||||
"..\src\gm\gmHash.h" \
|
||||
"..\src\gm\gmIncGC.h" \
|
||||
"..\src\gm\gmIterator.h" \
|
||||
"..\src\gm\gmLibHooks.h" \
|
||||
"..\src\gm\gmListDouble.h" \
|
||||
"..\src\gm\gmLog.h" \
|
||||
"..\src\gm\gmMachine.h" \
|
||||
"..\src\gm\gmMachineLib.h" \
|
||||
"..\src\gm\gmMem.h" \
|
||||
"..\src\gm\gmMemChain.h" \
|
||||
"..\src\gm\gmMemFixed.h" \
|
||||
"..\src\gm\gmMemFixedSet.h" \
|
||||
"..\src\gm\gmOperators.h" \
|
||||
"..\src\gm\gmParser.cpp.h" \
|
||||
"..\src\gm\gmScanner.h" \
|
||||
"..\src\gm\gmStream.h" \
|
||||
"..\src\gm\gmStreamBuffer.h" \
|
||||
"..\src\gm\gmStringObject.h" \
|
||||
"..\src\gm\gmTableObject.h" \
|
||||
"..\src\gm\gmThread.h" \
|
||||
"..\src\gm\gmUserObject.h" \
|
||||
"..\src\gm\gmUtil.h" \
|
||||
"..\src\gm\gmVariable.h"
|
||||
}
|
||||
}
|
||||
|
||||
$Folder "binds"
|
||||
{
|
||||
$Folder "Source Files"
|
||||
{
|
||||
$File "..\src\binds\gmArrayLib.cpp" \
|
||||
"..\src\binds\gmCall.cpp" \
|
||||
"..\src\binds\gmGCRoot.cpp" \
|
||||
"..\src\binds\gmGCRootUtil.cpp" \
|
||||
"..\src\binds\gmHelpers.cpp" \
|
||||
"..\src\binds\gmMathLib.cpp" \
|
||||
"..\src\binds\gmStringLib.cpp" \
|
||||
"..\src\binds\gmSystemLib.cpp" \
|
||||
"..\src\binds\gmVector3Lib.cpp"
|
||||
}
|
||||
$Folder "Header Files"
|
||||
{
|
||||
$File "..\src\binds\gmArrayLib.h" \
|
||||
"..\src\binds\gmCall.h" \
|
||||
"..\src\binds\gmGCRoot.h" \
|
||||
"..\src\binds\gmGCRootUtil.h" \
|
||||
"..\src\binds\gmHelpers.h" \
|
||||
"..\src\binds\gmMathLib.h" \
|
||||
"..\src\binds\gmStringLib.h" \
|
||||
"..\src\binds\gmSystemLib.h" \
|
||||
"..\src\binds\gmVector3Lib.h"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
246
vscript/languages/gm/vgm/vscript_templates.h
Normal file
246
vscript/languages/gm/vgm/vscript_templates.h
Normal file
@@ -0,0 +1,246 @@
|
||||
//========== Copyright <20> 2008, Valve Corporation, All rights reserved. ========
|
||||
//
|
||||
// Purpose:
|
||||
//
|
||||
//=============================================================================
|
||||
|
||||
#ifndef VSCRIPT_TEMPLATES_H
|
||||
#define VSCRIPT_TEMPLATES_H
|
||||
|
||||
#if defined( _WIN32 )
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#define FUNC_APPEND_PARAMS_0
|
||||
#define FUNC_APPEND_PARAMS_1 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 1 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_2 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 2 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_3 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 3 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_4 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 4 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_4 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_5 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 5 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_4 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_5 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_6 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 6 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_4 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_5 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_6 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_7 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 7 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_4 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_5 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_6 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_7 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_8 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 8 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_4 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_5 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_6 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_7 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_8 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_9 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 9 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_4 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_5 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_6 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_7 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_8 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_9 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_10 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 10 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_4 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_5 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_6 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_7 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_8 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_9 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_10 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_11 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 11 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_4 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_5 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_6 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_7 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_8 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_9 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_10 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_11 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_12 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 12 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_4 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_5 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_6 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_7 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_8 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_9 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_10 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_11 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_12 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_13 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 13 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_4 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_5 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_6 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_7 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_8 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_9 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_10 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_11 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_12 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_13 *)(0) ) );
|
||||
#define FUNC_APPEND_PARAMS_14 pDesc->m_Parameters.SetGrowSize( 1 ); pDesc->m_Parameters.EnsureCapacity( 14 ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_1 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_2 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_3 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_4 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_5 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_6 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_7 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_8 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_9 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_10 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_11 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_12 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_13 *)(0) ) ); pDesc->m_Parameters.AddToTail( ScriptDeduceType( (FUNC_ARG_TYPE_14 *)(0) ) );
|
||||
|
||||
#define DEFINE_NONMEMBER_FUNC_TYPE_DEDUCER(N) \
|
||||
template <typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N> \
|
||||
inline void ScriptDeduceFunctionSignature(ScriptFuncDescriptor_t *pDesc, FUNCTION_RETTYPE (*pfnProxied)( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) ) \
|
||||
{ \
|
||||
pDesc->m_ReturnType = ScriptDeduceType((FUNCTION_RETTYPE *)(0)); \
|
||||
FUNC_APPEND_PARAMS_##N \
|
||||
}
|
||||
|
||||
FUNC_GENERATE_ALL( DEFINE_NONMEMBER_FUNC_TYPE_DEDUCER );
|
||||
|
||||
#define DEFINE_MEMBER_FUNC_TYPE_DEDUCER(N) \
|
||||
template <typename OBJECT_TYPE_PTR, typename FUNCTION_CLASS, typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N> \
|
||||
inline void ScriptDeduceFunctionSignature(ScriptFuncDescriptor_t *pDesc, OBJECT_TYPE_PTR pObject, FUNCTION_RETTYPE ( FUNCTION_CLASS::*pfnProxied )( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) ) \
|
||||
{ \
|
||||
pDesc->m_ReturnType = ScriptDeduceType((FUNCTION_RETTYPE *)(0)); \
|
||||
FUNC_APPEND_PARAMS_##N \
|
||||
}
|
||||
|
||||
FUNC_GENERATE_ALL( DEFINE_MEMBER_FUNC_TYPE_DEDUCER );
|
||||
|
||||
//-------------------------------------
|
||||
|
||||
#define DEFINE_CONST_MEMBER_FUNC_TYPE_DEDUCER(N) \
|
||||
template <typename OBJECT_TYPE_PTR, typename FUNCTION_CLASS, typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N> \
|
||||
inline void ScriptDeduceFunctionSignature(ScriptFuncDescriptor_t *pDesc, OBJECT_TYPE_PTR pObject, FUNCTION_RETTYPE ( FUNCTION_CLASS::*pfnProxied )( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) const ) \
|
||||
{ \
|
||||
pDesc->m_ReturnType = ScriptDeduceType((FUNCTION_RETTYPE *)(0)); \
|
||||
FUNC_APPEND_PARAMS_##N \
|
||||
}
|
||||
|
||||
FUNC_GENERATE_ALL( DEFINE_CONST_MEMBER_FUNC_TYPE_DEDUCER );
|
||||
|
||||
#define ScriptInitMemberFuncDescriptor_( pDesc, class, func, scriptName ) if ( 0 ) {} else { (pDesc)->m_pszScriptName = scriptName; (pDesc)->m_pszFunction = #func; ScriptDeduceFunctionSignature( pDesc, (class *)(0), &class::func ); }
|
||||
|
||||
#define ScriptInitFuncDescriptorNamed( pDesc, func, scriptName ) if ( 0 ) {} else { (pDesc)->m_pszScriptName = scriptName; (pDesc)->m_pszFunction = #func; ScriptDeduceFunctionSignature( pDesc, &func ); }
|
||||
#define ScriptInitFuncDescriptor( pDesc, func ) ScriptInitFuncDescriptorNamed( pDesc, func, #func )
|
||||
#define ScriptInitMemberFuncDescriptorNamed( pDesc, class, func, scriptName ) ScriptInitMemberFuncDescriptor_( pDesc, class, func, scriptName )
|
||||
#define ScriptInitMemberFuncDescriptor( pDesc, class, func ) ScriptInitMemberFuncDescriptorNamed( pDesc, class, func, #func )
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template <typename FUNCPTR_TYPE>
|
||||
inline void *ScriptConvertFuncPtrToVoid( FUNCPTR_TYPE pFunc )
|
||||
{
|
||||
union FuncPtrConvert
|
||||
{
|
||||
void *p;
|
||||
FUNCPTR_TYPE pFunc;
|
||||
};
|
||||
|
||||
Assert( sizeof( pFunc ) == sizeof( void * ) );
|
||||
|
||||
FuncPtrConvert convert;
|
||||
convert.pFunc = pFunc;
|
||||
return convert.p;
|
||||
}
|
||||
|
||||
template <typename FUNCPTR_TYPE>
|
||||
inline FUNCPTR_TYPE ScriptConvertFuncPtrFromVoid( void *p )
|
||||
{
|
||||
union FuncPtrConvert
|
||||
{
|
||||
void *p;
|
||||
FUNCPTR_TYPE pFunc;
|
||||
};
|
||||
|
||||
Assert( sizeof( FUNCPTR_TYPE ) == sizeof( void * ) );
|
||||
|
||||
FuncPtrConvert convert;
|
||||
convert.p = p;
|
||||
return convert.pFunc;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_0
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_1 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_1
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_2 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_2
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_3 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_3
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_4 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_4
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_5 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_5
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_6 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_6
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_7 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_7
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_8 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_8
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_9 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_9
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_10 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_10
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_11 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_11
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_12 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_12
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_13 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_13
|
||||
#define FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_14 , FUNC_BASE_TEMPLATE_FUNC_PARAMS_14
|
||||
|
||||
#define SCRIPT_BINDING_ARGS_0
|
||||
#define SCRIPT_BINDING_ARGS_1 pArguments[0]
|
||||
#define SCRIPT_BINDING_ARGS_2 pArguments[0], pArguments[1]
|
||||
#define SCRIPT_BINDING_ARGS_3 pArguments[0], pArguments[1], pArguments[2]
|
||||
#define SCRIPT_BINDING_ARGS_4 pArguments[0], pArguments[1], pArguments[2], pArguments[3]
|
||||
#define SCRIPT_BINDING_ARGS_5 pArguments[0], pArguments[1], pArguments[2], pArguments[3], pArguments[4]
|
||||
#define SCRIPT_BINDING_ARGS_6 pArguments[0], pArguments[1], pArguments[2], pArguments[3], pArguments[4], pArguments[5]
|
||||
#define SCRIPT_BINDING_ARGS_7 pArguments[0], pArguments[1], pArguments[2], pArguments[3], pArguments[4], pArguments[5], pArguments[6]
|
||||
#define SCRIPT_BINDING_ARGS_8 pArguments[0], pArguments[1], pArguments[2], pArguments[3], pArguments[4], pArguments[5], pArguments[6], pArguments[7]
|
||||
#define SCRIPT_BINDING_ARGS_9 pArguments[0], pArguments[1], pArguments[2], pArguments[3], pArguments[4], pArguments[5], pArguments[6], pArguments[7], pArguments[8]
|
||||
#define SCRIPT_BINDING_ARGS_10 pArguments[0], pArguments[1], pArguments[2], pArguments[3], pArguments[4], pArguments[5], pArguments[6], pArguments[7], pArguments[8], pArguments[9]
|
||||
#define SCRIPT_BINDING_ARGS_11 pArguments[0], pArguments[1], pArguments[2], pArguments[3], pArguments[4], pArguments[5], pArguments[6], pArguments[7], pArguments[8], pArguments[9], pArguments[10]
|
||||
#define SCRIPT_BINDING_ARGS_12 pArguments[0], pArguments[1], pArguments[2], pArguments[3], pArguments[4], pArguments[5], pArguments[6], pArguments[7], pArguments[8], pArguments[9], pArguments[10], pArguments[11]
|
||||
#define SCRIPT_BINDING_ARGS_13 pArguments[0], pArguments[1], pArguments[2], pArguments[3], pArguments[4], pArguments[5], pArguments[6], pArguments[7], pArguments[8], pArguments[9], pArguments[10], pArguments[11], pArguments[12]
|
||||
#define SCRIPT_BINDING_ARGS_14 pArguments[0], pArguments[1], pArguments[2], pArguments[3], pArguments[4], pArguments[5], pArguments[6], pArguments[7], pArguments[8], pArguments[9], pArguments[10], pArguments[11], pArguments[12], pArguments[13]
|
||||
|
||||
|
||||
#define DEFINE_SCRIPT_BINDINGS(N) \
|
||||
template <typename FUNC_TYPE, typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N> \
|
||||
class CNonMemberScriptBinding##N \
|
||||
{ \
|
||||
public: \
|
||||
static bool Call( void *pFunction, void *pContext, ScriptVariant_t *pArguments, int nArguments, ScriptVariant_t *pReturn ) \
|
||||
{ \
|
||||
Assert( nArguments == N ); \
|
||||
Assert( pReturn ); \
|
||||
Assert( !pContext ); \
|
||||
\
|
||||
if ( nArguments != N || !pReturn || pContext ) \
|
||||
{ \
|
||||
return false; \
|
||||
} \
|
||||
*pReturn = ((FUNC_TYPE)pFunction)( SCRIPT_BINDING_ARGS_##N ); \
|
||||
return true; \
|
||||
} \
|
||||
}; \
|
||||
\
|
||||
template <typename FUNC_TYPE FUNC_TEMPLATE_FUNC_PARAMS_##N> \
|
||||
class CNonMemberScriptBinding##N<FUNC_TYPE, void FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_##N> \
|
||||
{ \
|
||||
public: \
|
||||
static bool Call( void *pFunction, void *pContext, ScriptVariant_t *pArguments, int nArguments, ScriptVariant_t *pReturn ) \
|
||||
{ \
|
||||
Assert( nArguments == N ); \
|
||||
Assert( !pReturn ); \
|
||||
Assert( !pContext ); \
|
||||
\
|
||||
if ( nArguments != N || pReturn || pContext ) \
|
||||
{ \
|
||||
return false; \
|
||||
} \
|
||||
((FUNC_TYPE)pFunction)( SCRIPT_BINDING_ARGS_##N ); \
|
||||
return true; \
|
||||
} \
|
||||
}; \
|
||||
\
|
||||
template <class OBJECT_TYPE_PTR, typename FUNC_TYPE, typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N> \
|
||||
class CMemberScriptBinding##N \
|
||||
{ \
|
||||
public: \
|
||||
static bool Call( void *pFunction, void *pContext, ScriptVariant_t *pArguments, int nArguments, ScriptVariant_t *pReturn ) \
|
||||
{ \
|
||||
Assert( nArguments == N ); \
|
||||
Assert( pReturn ); \
|
||||
Assert( pContext ); \
|
||||
\
|
||||
if ( nArguments != N || !pReturn || !pContext ) \
|
||||
{ \
|
||||
return false; \
|
||||
} \
|
||||
*pReturn = (((OBJECT_TYPE_PTR)(pContext))->*ScriptConvertFuncPtrFromVoid<FUNC_TYPE>(pFunction))( SCRIPT_BINDING_ARGS_##N ); \
|
||||
return true; \
|
||||
} \
|
||||
}; \
|
||||
\
|
||||
template <class OBJECT_TYPE_PTR, typename FUNC_TYPE FUNC_TEMPLATE_FUNC_PARAMS_##N> \
|
||||
class CMemberScriptBinding##N<OBJECT_TYPE_PTR, FUNC_TYPE, void FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_##N> \
|
||||
{ \
|
||||
public: \
|
||||
static bool Call( void *pFunction, void *pContext, ScriptVariant_t *pArguments, int nArguments, ScriptVariant_t *pReturn ) \
|
||||
{ \
|
||||
Assert( nArguments == N ); \
|
||||
Assert( !pReturn ); \
|
||||
Assert( pContext ); \
|
||||
\
|
||||
if ( nArguments != N || pReturn || !pContext ) \
|
||||
{ \
|
||||
return false; \
|
||||
} \
|
||||
(((OBJECT_TYPE_PTR)(pContext))->*ScriptConvertFuncPtrFromVoid<FUNC_TYPE>(pFunction))( SCRIPT_BINDING_ARGS_##N ); \
|
||||
return true; \
|
||||
} \
|
||||
}; \
|
||||
\
|
||||
template <typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N> \
|
||||
inline ScriptBindingFunc_t ScriptCreateBinding(FUNCTION_RETTYPE (*pfnProxied)( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) ) \
|
||||
{ \
|
||||
typedef FUNCTION_RETTYPE (*Func_t)(FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N); \
|
||||
return &CNonMemberScriptBinding##N<Func_t, FUNCTION_RETTYPE FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_##N>::Call; \
|
||||
} \
|
||||
\
|
||||
template <typename OBJECT_TYPE_PTR, typename FUNCTION_CLASS, typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N> \
|
||||
inline ScriptBindingFunc_t ScriptCreateBinding(OBJECT_TYPE_PTR pObject, FUNCTION_RETTYPE (FUNCTION_CLASS::*pfnProxied)( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) ) \
|
||||
{ \
|
||||
typedef FUNCTION_RETTYPE (FUNCTION_CLASS::*Func_t)(FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N); \
|
||||
return &CMemberScriptBinding##N<OBJECT_TYPE_PTR, Func_t, FUNCTION_RETTYPE FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_##N>::Call; \
|
||||
} \
|
||||
\
|
||||
template <typename OBJECT_TYPE_PTR, typename FUNCTION_CLASS, typename FUNCTION_RETTYPE FUNC_TEMPLATE_FUNC_PARAMS_##N> \
|
||||
inline ScriptBindingFunc_t ScriptCreateBinding(OBJECT_TYPE_PTR pObject, FUNCTION_RETTYPE (FUNCTION_CLASS::*pfnProxied)( FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N ) const ) \
|
||||
{ \
|
||||
typedef FUNCTION_RETTYPE (FUNCTION_CLASS::*Func_t)(FUNC_BASE_TEMPLATE_FUNC_PARAMS_##N); \
|
||||
return &CMemberScriptBinding##N<OBJECT_TYPE_PTR, Func_t, FUNCTION_RETTYPE FUNC_BASE_TEMPLATE_FUNC_PARAMS_PASSTHRU_##N>::Call; \
|
||||
}
|
||||
|
||||
FUNC_GENERATE_ALL( DEFINE_SCRIPT_BINDINGS );
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif // VSCRIPT_TEMPLATES_H
|
||||
Reference in New Issue
Block a user