C++/AngelScript/コードメモ
をテンプレートにして作成
ホーム
検索
最終更新
ヘルプ
Wiki書式ヘルプ(整形ルール)
開始行:
* ビルド時間短縮化 [#v1a93e9a]
:as_builder.cpp|
#code(c,){{
{
asCString str(name);
asCModule::SFMapNode* node = 0;
if ( module->scriptFunctionsMap.MoveTo( &node , a...
{// 移動成功
do
{
// 追加
if ( node->value->objectType == 0 )
{
funcs.PushLast(node->value->id);
}
// 次へ
module->scriptFunctionsMap.MoveNext( &nod...
} while( node != 0 );
}
}
}}
:as_module.h|
#code(c,){{
/*
AngelCode Scripting Library
Copyright (c) 2003-2010 Andreas Jonsson
This software is provided 'as-is', without any express...
warranty. In no event will the authors be held liable ...
damages arising from the use of this software.
Permission is granted to anyone to use this software f...
purpose, including commercial applications, and to alt...
redistribute it freely, subject to the following restr...
1. The origin of this software must not be misrepresen...
must not claim that you wrote the original software...
this software in a product, an acknowledgment in th...
documentation would be appreciated but is not requi...
2. Altered source versions must be plainly marked as s...
must not be misrepresented as being the original so...
3. This notice may not be removed or altered from any ...
distribution.
The original version of this library can be located at:
http://www.angelcode.com/angelscript/
Andreas Jonsson
andreas@angelcode.com
*/
//
// as_module.h
//
// A class that holds a script module
//
#ifndef AS_MODULE_H
#define AS_MODULE_H
#include "as_config.h"
#include "as_atomic.h"
#include "as_string.h"
#include "as_array.h"
#include "as_datatype.h"
#include "as_scriptfunction.h"
#include "as_property.h"
#include "as_map.h"
BEGIN_AS_NAMESPACE
// TODO: import: Remove this when the imported functions ...
const int FUNC_IMPORTED = 0x40000000;
class asCScriptEngine;
class asCCompiler;
class asCBuilder;
class asCContext;
class asCConfigGroup;
struct sBindInfo
{
asCScriptFunction *importedFunctionSignature;
asCString importFromModule;
int boundFunctionId;
};
struct sObjectTypePair
{
asCObjectType *a;
asCObjectType *b;
};
// TODO: import: Remove function imports. When I have imp...
// pointers the function imports should be ...
// TODO: Need a separate interface for compiling scripts....
// will have a target module, and will allow the co...
// script or just individual functions within the s...
//
// With this separation it will be possible to comp...
// the compiler, thus giving a much smaller binary ...
// TODO: There should be an special compile option that w...
// recompile an already compiled script. The compil...
// destructive changes have been made (changing fun...
// then it should simply replace the bytecode withi...
// changing the values of existing global propertie...
class asCModule : public asIScriptModule
{
//-------------------------------------------
// Public interface
//--------------------------------------------
public:
class SFCompare
{
public:
explicit SFCompare(asCString* str = 0) : mStrPtr(...
bool operator < (const SFCompare& aRHS)const
{
assert(mStrPtr != 0);
assert(aRHS.mStrPtr != 0);
return *mStrPtr < *aRHS.mStrPtr;
}
bool operator == (const SFCompare& aRHS)const
{
assert(mStrPtr != 0);
assert(aRHS.mStrPtr != 0);
return *mStrPtr == *aRHS.mStrPtr;
}
private:
asCString* mStrPtr;
};
typedef asCMap<SFCompare, asCScriptFunction*> SFMap;
typedef asSMapNode<SFCompare, asCScriptFunction*> SFM...
virtual asIScriptEngine *GetEngine();
virtual void SetName(const char *name);
virtual const char *GetName();
// Compilation
virtual int AddScriptSection(const char *name, const ch...
virtual int Build();
virtual int CompileFunction(const char *sectionName, co...
virtual int CompileGlobalVar(const char *sectionName, c...
// Script functions
virtual int GetFunctionCount();
virtual int GetFunctionIdByIndex(int inde...
virtual int GetFunctionIdByName(const cha...
virtual int GetFunctionIdByDecl(const cha...
virtual asIScriptFunction *GetFunctionDescriptorByIndex(...
virtual asIScriptFunction *GetFunctionDescriptorById(int...
virtual int RemoveFunction(int funcId);
// Script global variables
virtual int ResetGlobalVars();
virtual int GetGlobalVarCount();
virtual int GetGlobalVarIndexByName(const char *...
virtual int GetGlobalVarIndexByDecl(const char *...
virtual const char *GetGlobalVarDeclaration(int index);
virtual const char *GetGlobalVarName(int index);
virtual int GetGlobalVarTypeId(int index, bool *...
virtual void *GetAddressOfGlobalVar(int index);
virtual int RemoveGlobalVar(int index);
// Type identification
virtual int GetObjectTypeCount();
virtual asIObjectType *GetObjectTypeByIndex(asUINT index);
virtual int GetTypeIdByDecl(const char *decl);
// Enums
virtual int GetEnumCount();
virtual const char *GetEnumByIndex(asUINT index, int *en...
virtual int GetEnumValueCount(int enumTypeId);
virtual const char *GetEnumValueByIndex(int enumTypeId, ...
// Typedefs
virtual int GetTypedefCount();
virtual const char *GetTypedefByIndex(asUINT index, int ...
// Dynamic binding between modules
virtual int GetImportedFunctionCount();
virtual int GetImportedFunctionIndexByDecl(const...
virtual const char *GetImportedFunctionDeclaration(int i...
virtual const char *GetImportedFunctionSourceModule(int ...
virtual int BindImportedFunction(int index, int ...
virtual int UnbindImportedFunction(int importInd...
virtual int BindAllImportedFunctions();
virtual int UnbindAllImportedFunctions();
// Bytecode Saving/Loading
virtual int SaveByteCode(asIBinaryStream *out);
virtual int LoadByteCode(asIBinaryStream *in);
//-----------------------------------------------
// Internal
//-----------------------------------------------
asCModule(const char *name, asCScriptEngine *engine);
~asCModule();
//protected:
friend class asCScriptEngine;
friend class asCBuilder;
friend class asCCompiler;
friend class asCContext;
friend class asCRestore;
void InternalReset();
int CallInit();
void CallExit();
void JITCompile();
int AddScriptFunction(int sectionIdx, int id, const cha...
int AddScriptFunction(asCScriptFunction *func);
int AddImportedFunction(int id, const char *name, const...
int AddFuncDef(const char *name);
int GetNextImportedFunctionId();
void ResolveInterfaceIds();
bool AreInterfacesEqual(asCObjectType *a, asCObjectType ...
bool AreTypesEqual(const asCDataType &a, const asCDataTy...
asCScriptFunction *GetImportedFunction(int funcId);
asCObjectType *GetObjectType(const char *type);
asCGlobalProperty *AllocateGlobalProperty(const char *na...
asCString name;
asCScriptEngine *engine;
asCBuilder *builder;
// This array holds all functions, class members, factor...
asCArray<asCScriptFunction *> scriptFunctions;
SFMap scriptFunctionsMap;
// This array holds global functions declared in the mod...
asCArray<asCScriptFunction *> globalFunctions;
// This array holds imported functions in the module
asCArray<sBindInfo *> bindInformations;
// This array holds the global variables declared in the...
asCArray<asCGlobalProperty *> scriptGlobals;
bool isGlobalVarInitialized;
// This array holds class and interface types
asCArray<asCObjectType*> classTypes;
// This array holds enum types
asCArray<asCObjectType*> enumTypes;
// This array holds typedefs
asCArray<asCObjectType*> typeDefs;
// This array holds the funcdefs declared in the module
asCArray<asCScriptFunction*> funcDefs;
};
END_AS_NAMESPACE
#endif
}}
:as_module.cpp|
#code(c,){{
/*
AngelCode Scripting Library
Copyright (c) 2003-2010 Andreas Jonsson
This software is provided 'as-is', without any express...
warranty. In no event will the authors be held liable ...
damages arising from the use of this software.
Permission is granted to anyone to use this software f...
purpose, including commercial applications, and to alt...
redistribute it freely, subject to the following restr...
1. The origin of this software must not be misrepresen...
must not claim that you wrote the original software...
this software in a product, an acknowledgment in th...
documentation would be appreciated but is not requi...
2. Altered source versions must be plainly marked as s...
must not be misrepresented as being the original so...
3. This notice may not be removed or altered from any ...
distribution.
The original version of this library can be located at:
http://www.angelcode.com/angelscript/
Andreas Jonsson
andreas@angelcode.com
*/
//
// as_module.cpp
//
// A class that holds a script module
//
#include "as_config.h"
#include "as_module.h"
#include "as_builder.h"
#include "as_context.h"
#include "as_texts.h"
BEGIN_AS_NAMESPACE
// internal
asCModule::asCModule(const char *name, asCScriptEngine *e...
{
this->name = name;
this->engine = engine;
builder = 0;
isGlobalVarInitialized = false;
}
// internal
asCModule::~asCModule()
{
InternalReset();
if( builder )
{
asDELETE(builder,asCBuilder);
builder = 0;
}
// Remove the module from the engine
if( engine )
{
if( engine->lastModule == this )
engine->lastModule = 0;
engine->scriptModules.RemoveValue(this);
}
}
// interface
asIScriptEngine *asCModule::GetEngine()
{
return engine;
}
// interface
void asCModule::SetName(const char *name)
{
this->name = name;
}
// interface
const char *asCModule::GetName()
{
return name.AddressOf();
}
// interface
int asCModule::AddScriptSection(const char *name, const c...
{
if( !builder )
builder = asNEW(asCBuilder)(engine, this);
builder->AddCode(name, code, (int)codeLength, lineOffset...
return asSUCCESS;
}
// internal
void asCModule::JITCompile()
{
for (unsigned int i = 0; i < scriptFunctions.GetLengt...
{
scriptFunctions[i]->JITCompile();
}
}
// interface
int asCModule::Build()
{
// Only one thread may build at one time
// TODO: It should be possible to have multiple threads ...
int r = engine->RequestBuild();
if( r < 0 )
return r;
engine->PrepareEngine();
if( engine->configFailed )
{
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INV...
engine->BuildCompleted();
return asINVALID_CONFIGURATION;
}
InternalReset();
if( !builder )
{
engine->BuildCompleted();
return asSUCCESS;
}
// Compile the script
r = builder->Build();
asDELETE(builder,asCBuilder);
builder = 0;
if( r < 0 )
{
// Reset module again
InternalReset();
engine->BuildCompleted();
return r;
}
JITCompile();
engine->PrepareEngine();
engine->BuildCompleted();
// Initialize global variables
if( r >= 0 && engine->ep.initGlobalVarsAfterBuild )
r = ResetGlobalVars();
return r;
}
// interface
int asCModule::ResetGlobalVars()
{
if( isGlobalVarInitialized )
CallExit();
// TODO: The application really should do this manually ...
// otherwise it cannot properly handle script exce...
// thrown by object initializations.
return CallInit();
}
// interface
int asCModule::GetFunctionIdByIndex(int index)
{
if( index < 0 || index >= (int)scriptFunctions.GetLength...
return asNO_FUNCTION;
return scriptFunctions[index]->id;
}
// internal
int asCModule::CallInit()
{
if( isGlobalVarInitialized )
return asERROR;
// Each global variable needs to be cleared individually
asUINT n;
for( n = 0; n < scriptGlobals.GetLength(); n++ )
{
if( scriptGlobals[n] )
{
memset(scriptGlobals[n]->GetAddressOfValue(), 0, sizeo...
}
}
// Call the init function for each of the global variables
asIScriptContext *ctx = 0;
int r = 0;
for( n = 0; n < scriptGlobals.GetLength() && r == 0; n++ )
{
if( scriptGlobals[n]->GetInitFunc() )
{
if( ctx == 0 )
{
r = engine->CreateContext(&ctx, true);
if( r < 0 )
break;
}
r = ctx->Prepare(scriptGlobals[n]->GetInitFunc()->id);
if( r >= 0 )
r = ctx->Execute();
}
}
if( ctx )
{
ctx->Release();
ctx = 0;
}
// Even if the initialization failed we need to set the
// flag that the variables have been initialized, otherw...
// the module won't free those variables that really were
// initialized.
isGlobalVarInitialized = true;
if( r != asEXECUTION_FINISHED )
return asINIT_GLOBAL_VARS_FAILED;
return asSUCCESS;
}
// internal
void asCModule::CallExit()
{
if( !isGlobalVarInitialized ) return;
for( size_t n = 0; n < scriptGlobals.GetLength(); n++ )
{
if( scriptGlobals[n]->type.IsObject() )
{
void *obj = *(void**)scriptGlobals[n]->GetAddressOfVal...
if( obj )
{
asCObjectType *ot = scriptGlobals[n]->type.GetObjectT...
if( ot->beh.release )
engine->CallObjectMethod(obj, ot->beh.release);
else
{
if( ot->beh.destruct )
engine->CallObjectMethod(obj, ot->beh.destruct);
engine->CallFree(obj);
}
}
}
}
isGlobalVarInitialized = false;
}
// internal
void asCModule::InternalReset()
{
CallExit();
size_t n;
// Release all global functions
for( n = 0; n < globalFunctions.GetLength(); n++ )
{
if( globalFunctions[n] )
globalFunctions[n]->Release();
}
globalFunctions.SetLength(0);
// First release all compiled functions
for( n = 0; n < scriptFunctions.GetLength(); n++ )
{
if( scriptFunctions[n] )
{
// Remove the module reference in the functions
scriptFunctions[n]->module = 0;
scriptFunctions[n]->Release();
}
}
scriptFunctions.SetLength(0);
scriptFunctionsMap.EraseAll();
// Release the global properties declared in the module
for( n = 0; n < scriptGlobals.GetLength(); n++ )
scriptGlobals[n]->Release();
scriptGlobals.SetLength(0);
UnbindAllImportedFunctions();
// Free bind information
for( n = 0; n < bindInformations.GetLength(); n++ )
{
engine->importedFunctions[bindInformations[n]->imported...
asDELETE(bindInformations[n]->importedFunctionSignature...
asDELETE(bindInformations[n], sBindInfo);
}
bindInformations.SetLength(0);
// Free declared types, including classes, typedefs, and...
for( n = 0; n < classTypes.GetLength(); n++ )
classTypes[n]->Release();
classTypes.SetLength(0);
for( n = 0; n < enumTypes.GetLength(); n++ )
enumTypes[n]->Release();
enumTypes.SetLength(0);
for( n = 0; n < typeDefs.GetLength(); n++ )
typeDefs[n]->Release();
typeDefs.SetLength(0);
// Free funcdefs
for( n = 0; n < funcDefs.GetLength(); n++ )
{
// TODO: funcdefs: These may be shared between modules,...
engine->funcDefs.RemoveValue(funcDefs[n]);
funcDefs[n]->Release();
}
funcDefs.SetLength(0);
}
// interface
int asCModule::GetFunctionIdByName(const char *name)
{
// TODO: optimize: Improve linear search
// Find the function id
int id = -1;
for( size_t n = 0; n < globalFunctions.GetLength(); n++ )
{
if( globalFunctions[n]->name == name )
{
if( id == -1 )
id = globalFunctions[n]->id;
else
return asMULTIPLE_FUNCTIONS;
}
}
if( id == -1 ) return asNO_FUNCTION;
return id;
}
// interface
int asCModule::GetImportedFunctionCount()
{
return (int)bindInformations.GetLength();
}
// interface
int asCModule::GetImportedFunctionIndexByDecl(const char ...
{
asCBuilder bld(engine, this);
asCScriptFunction func(engine, this, -1);
bld.ParseFunctionDeclaration(0, decl, &func, false);
// TODO: optimize: Improve linear search
// Search script functions for matching interface
int id = -1;
for( asUINT n = 0; n < bindInformations.GetLength(); ++n )
{
if( func.name == bindInformations[n]->importedFunctionS...
func.returnType == bindInformations[n]->importedFuncti...
func.parameterTypes.GetLength() == bindInformations[n]...
{
bool match = true;
for( asUINT p = 0; p < func.parameterTypes.GetLength()...
{
if( func.parameterTypes[p] != bindInformations[n]->im...
{
match = false;
break;
}
}
if( match )
{
if( id == -1 )
id = n;
else
return asMULTIPLE_FUNCTIONS;
}
}
}
if( id == -1 ) return asNO_FUNCTION;
return id;
}
// interface
int asCModule::GetFunctionCount()
{
return (int)globalFunctions.GetLength();
}
// interface
int asCModule::GetFunctionIdByDecl(const char *decl)
{
asCBuilder bld(engine, this);
asCScriptFunction func(engine, this, -1);
int r = bld.ParseFunctionDeclaration(0, decl, &func, fal...
if( r < 0 )
return asINVALID_DECLARATION;
// TODO: optimize: Improve linear search
// Search script functions for matching interface
int id = -1;
for( size_t n = 0; n < globalFunctions.GetLength(); ++n )
{
if( globalFunctions[n]->objectType == 0 &&
func.name == globalFunctions[n]->name &&
func.returnType == globalFunctions[n]->returnType &&
func.parameterTypes.GetLength() == globalFunctions[n]-...
{
bool match = true;
for( size_t p = 0; p < func.parameterTypes.GetLength()...
{
if( func.parameterTypes[p] != globalFunctions[n]->par...
{
match = false;
break;
}
}
if( match )
{
if( id == -1 )
id = globalFunctions[n]->id;
else
return asMULTIPLE_FUNCTIONS;
}
}
}
if( id == -1 ) return asNO_FUNCTION;
return id;
}
// interface
int asCModule::GetGlobalVarCount()
{
return (int)scriptGlobals.GetLength();
}
// interface
int asCModule::GetGlobalVarIndexByName(const char *name)
{
// Find the global var id
int id = -1;
for( size_t n = 0; n < scriptGlobals.GetLength(); n++ )
{
if( scriptGlobals[n]->name == name )
{
id = (int)n;
break;
}
}
if( id == -1 ) return asNO_GLOBAL_VAR;
return id;
}
// interface
int asCModule::RemoveGlobalVar(int index)
{
if( index < 0 || index >= (int)scriptGlobals.GetLength() )
return asINVALID_ARG;
scriptGlobals[index]->Release();
scriptGlobals.RemoveIndex(index);
return 0;
}
// interface
asIScriptFunction *asCModule::GetFunctionDescriptorByInde...
{
if( index < 0 || index >= (int)globalFunctions.GetLength...
return 0;
return globalFunctions[index];
}
// interface
asIScriptFunction *asCModule::GetFunctionDescriptorById(i...
{
return engine->GetFunctionDescriptorById(funcId);
}
// interface
int asCModule::GetGlobalVarIndexByDecl(const char *decl)
{
asCBuilder bld(engine, this);
asCObjectProperty gvar;
bld.ParseVariableDeclaration(decl, &gvar);
// TODO: optimize: Improve linear search
// Search script functions for matching interface
int id = -1;
for( size_t n = 0; n < scriptGlobals.GetLength(); ++n )
{
if( gvar.name == scriptGlobals[n]->name &&
gvar.type == scriptGlobals[n]->type )
{
id = (int)n;
break;
}
}
if( id == -1 ) return asNO_GLOBAL_VAR;
return id;
}
// interface
void *asCModule::GetAddressOfGlobalVar(int index)
{
if( index < 0 || index >= (int)scriptGlobals.GetLength() )
return 0;
// TODO: value types shouldn't need dereferencing
// For object variables it's necessary to dereference th...
if( scriptGlobals[index]->type.IsObject() && !scriptGlob...
return *(void**)(scriptGlobals[index]->GetAddressOfValu...
return (void*)(scriptGlobals[index]->GetAddressOfValue());
}
// interface
const char *asCModule::GetGlobalVarDeclaration(int index)
{
if( index < 0 || index >= (int)scriptGlobals.GetLength() )
return 0;
asCGlobalProperty *prop = scriptGlobals[index];
asASSERT(threadManager);
asCString *tempString = &threadManager->GetLocalData()->...
*tempString = prop->type.Format();
*tempString += " " + prop->name;
return tempString->AddressOf();
}
// interface
const char *asCModule::GetGlobalVarName(int index)
{
if( index < 0 || index >= (int)scriptGlobals.GetLength() )
return 0;
return scriptGlobals[index]->name.AddressOf();
}
// interface
// TODO: If the typeId ever encodes the const flag, then ...
int asCModule::GetGlobalVarTypeId(int index, bool *isConst)
{
if( index < 0 || index >= (int)scriptGlobals.GetLength() )
return asINVALID_ARG;
if( isConst )
*isConst = scriptGlobals[index]->type.IsReadOnly();
return engine->GetTypeIdFromDataType(scriptGlobals[index...
}
// interface
int asCModule::GetObjectTypeCount()
{
return (int)classTypes.GetLength();
}
// interface
asIObjectType *asCModule::GetObjectTypeByIndex(asUINT ind...
{
if( index >= classTypes.GetLength() )
return 0;
return classTypes[index];
}
// interface
int asCModule::GetTypeIdByDecl(const char *decl)
{
asCDataType dt;
asCBuilder bld(engine, this);
int r = bld.ParseDataType(decl, &dt);
if( r < 0 )
return asINVALID_TYPE;
return engine->GetTypeIdFromDataType(dt);
}
// interface
int asCModule::GetEnumCount()
{
return (int)enumTypes.GetLength();
}
// interface
const char *asCModule::GetEnumByIndex(asUINT index, int *...
{
if( index >= enumTypes.GetLength() )
return 0;
if( enumTypeId )
*enumTypeId = GetTypeIdByDecl(enumTypes[index]->name.Ad...
return enumTypes[index]->name.AddressOf();
}
// interface
int asCModule::GetEnumValueCount(int enumTypeId)
{
const asCDataType *dt = engine->GetDataTypeFromTypeId(en...
asCObjectType *t = dt->GetObjectType();
if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
return asINVALID_TYPE;
return (int)t->enumValues.GetLength();
}
// interface
const char *asCModule::GetEnumValueByIndex(int enumTypeId...
{
const asCDataType *dt = engine->GetDataTypeFromTypeId(en...
asCObjectType *t = dt->GetObjectType();
if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
return 0;
if( index >= t->enumValues.GetLength() )
return 0;
if( outValue )
*outValue = t->enumValues[index]->value;
return t->enumValues[index]->name.AddressOf();
}
// interface
int asCModule::GetTypedefCount()
{
return (int)typeDefs.GetLength();
}
// interface
const char *asCModule::GetTypedefByIndex(asUINT index, in...
{
if( index >= typeDefs.GetLength() )
return 0;
if( typeId )
*typeId = GetTypeIdByDecl(typeDefs[index]->name.Address...
return typeDefs[index]->name.AddressOf();
}
// internal
int asCModule::GetNextImportedFunctionId()
{
return FUNC_IMPORTED | (asUINT)engine->importedFunctions...
}
// internal
int asCModule::AddScriptFunction(int sectionIdx, int id, ...
{
asASSERT(id >= 0);
// Store the function information
asCScriptFunction *func = asNEW(asCScriptFunction)(engin...
func->name = name;
func->id = id;
func->returnType = returnType;
func->scriptSectionIdx = sectionIdx;
for( int n = 0; n < paramCount; n++ )
{
func->parameterTypes.PushLast(params[n]);
func->inOutFlags.PushLast(inOutFlags[n]);
}
func->objectType = objType;
func->isReadOnly = isConstMethod;
// The script function's refCount was initialized to 1
scriptFunctions.PushLast(func);
if ( objType == 0 )
{
scriptFunctionsMap.Insert(SFCompare(&func->name),...
}
engine->SetScriptFunction(func);
// Compute the signature id
if( objType )
func->ComputeSignatureId();
// Add reference
if( isGlobalFunction )
{
globalFunctions.PushLast(func);
func->AddRef();
}
return 0;
}
// internal
int asCModule::AddScriptFunction(asCScriptFunction *func)
{
scriptFunctions.PushLast(func);
if ( func->objectType == 0 )
{
scriptFunctionsMap.Insert(SFCompare(&func->name),...
}
func->AddRef();
engine->SetScriptFunction(func);
return 0;
}
// internal
int asCModule::AddImportedFunction(int id, const char *na...
{
asASSERT(id >= 0);
// Store the function information
asCScriptFunction *func = asNEW(asCScriptFunction)(engin...
func->name = name;
func->id = id;
func->returnType = returnType;
for( int n = 0; n < paramCount; n++ )
{
func->parameterTypes.PushLast(params[n]);
func->inOutFlags.PushLast(inOutFlags[n]);
}
func->objectType = 0;
sBindInfo *info = asNEW(sBindInfo);
info->importedFunctionSignature = func;
info->boundFunctionId = -1;
info->importFromModule = moduleName;
bindInformations.PushLast(info);
// Add the info to the array in the engine
engine->importedFunctions.PushLast(info);
return 0;
}
// internal
asCScriptFunction *asCModule::GetImportedFunction(int ind...
{
return bindInformations[index]->importedFunctionSignature;
}
// interface
int asCModule::BindImportedFunction(int index, int source...
{
// First unbind the old function
int r = UnbindImportedFunction(index);
if( r < 0 ) return r;
// Must verify that the interfaces are equal
asCScriptFunction *dst = GetImportedFunction(index);
if( dst == 0 ) return asNO_FUNCTION;
asCScriptFunction *src = engine->GetScriptFunction(sourc...
if( src == 0 )
return asNO_FUNCTION;
// Verify return type
if( dst->returnType != src->returnType )
return asINVALID_INTERFACE;
if( dst->parameterTypes.GetLength() != src->parameterTyp...
return asINVALID_INTERFACE;
for( size_t n = 0; n < dst->parameterTypes.GetLength(); ...
{
if( dst->parameterTypes[n] != src->parameterTypes[n] )
return asINVALID_INTERFACE;
}
bindInformations[index]->boundFunctionId = sourceId;
engine->scriptFunctions[sourceId]->AddRef();
return asSUCCESS;
}
// interface
int asCModule::UnbindImportedFunction(int index)
{
if( index < 0 || index > (int)bindInformations.GetLength...
return asINVALID_ARG;
// Remove reference to old module
int oldFuncID = bindInformations[index]->boundFunctionId;
if( oldFuncID != -1 )
{
bindInformations[index]->boundFunctionId = -1;
engine->scriptFunctions[oldFuncID]->Release();
}
return asSUCCESS;
}
// interface
const char *asCModule::GetImportedFunctionDeclaration(int...
{
asCScriptFunction *func = GetImportedFunction(index);
if( func == 0 ) return 0;
asASSERT(threadManager);
asCString *tempString = &threadManager->GetLocalData()->...
*tempString = func->GetDeclarationStr();
return tempString->AddressOf();
}
// interface
const char *asCModule::GetImportedFunctionSourceModule(in...
{
if( index >= (int)bindInformations.GetLength() )
return 0;
return bindInformations[index]->importFromModule.Address...
}
// inteface
int asCModule::BindAllImportedFunctions()
{
bool notAllFunctionsWereBound = false;
// Bind imported functions
int c = GetImportedFunctionCount();
for( int n = 0; n < c; ++n )
{
asCScriptFunction *func = GetImportedFunction(n);
if( func == 0 ) return asERROR;
asCString str = func->GetDeclarationStr();
// Get module name from where the function should be im...
const char *moduleName = GetImportedFunctionSourceModul...
if( moduleName == 0 ) return asERROR;
asCModule *srcMod = engine->GetModule(moduleName, false);
int funcId = -1;
if( srcMod )
funcId = srcMod->GetFunctionIdByDecl(str.AddressOf());
if( funcId < 0 )
notAllFunctionsWereBound = true;
else
{
if( BindImportedFunction(n, funcId) < 0 )
notAllFunctionsWereBound = true;
}
}
if( notAllFunctionsWereBound )
return asCANT_BIND_ALL_FUNCTIONS;
return asSUCCESS;
}
// interface
int asCModule::UnbindAllImportedFunctions()
{
int c = GetImportedFunctionCount();
for( int n = 0; n < c; ++n )
UnbindImportedFunction(n);
return asSUCCESS;
}
// internal
asCObjectType *asCModule::GetObjectType(const char *type)
{
size_t n;
// TODO: optimize: Improve linear search
for( n = 0; n < classTypes.GetLength(); n++ )
if( classTypes[n]->name == type )
return classTypes[n];
for( n = 0; n < enumTypes.GetLength(); n++ )
if( enumTypes[n]->name == type )
return enumTypes[n];
for( n = 0; n < typeDefs.GetLength(); n++ )
if( typeDefs[n]->name == type )
return typeDefs[n];
return 0;
}
// internal
asCGlobalProperty *asCModule::AllocateGlobalProperty(cons...
{
asCGlobalProperty *prop = engine->AllocateGlobalProperty...
prop->name = name;
// Allocate the memory for this property based on its type
prop->type = dt;
prop->AllocateMemory();
// Store the variable in the module scope (the reference...
scriptGlobals.PushLast(prop);
return prop;
}
// internal
void asCModule::ResolveInterfaceIds()
{
// For each of the interfaces declared in the script fin...
// If an identical interface was found then substitute t...
// then remove this interface declaration. If an interfa...
// retry the detection of identical interface for it sin...
// For an interface to be equal to another the name and ...
// references another interface, then that must be check...
// Example:
//
// interface A { void f(B@); }
// interface B { void f(A@); void f(C@); }
// interface C { void f(A@); }
//
// A1 equals A2 if and only if B1 equals B2
// B1 equals B2 if and only if A1 equals A2 and C1 equal...
// C1 equals C2 if and only if A1 equals A2
unsigned int i;
// The interface can only be equal to interfaces declare...
// Interfaces registered by the application will conflic...
// This means that we only need to look for the interfac...
asCArray<sObjectTypePair> equals;
for( i = 0; i < classTypes.GetLength(); i++ )
{
asCObjectType *intf1 = classTypes[i];
if( !intf1->IsInterface() )
continue;
// The interface may have been determined to be equal t...
bool found = false;
for( unsigned int e = 0; e < equals.GetLength(); e++ )
{
if( equals[e].a == intf1 )
{
found = true;
break;
}
}
if( found )
break;
for( unsigned int n = 0; n < engine->classTypes.GetLeng...
{
// Don't compare against self
if( engine->classTypes[n] == intf1 )
continue;
asCObjectType *intf2 = engine->classTypes[n];
// Assume the interface are equal, then validate this
sObjectTypePair pair = {intf1,intf2};
equals.PushLast(pair);
if( AreInterfacesEqual(intf1, intf2, equals) )
break;
// Since they are not equal, remove them from the list...
equals.PopLast();
}
}
// For each of the interfaces that have been found to be...
// remove the new declaration and instead have the modul...
for( i = 0; i < equals.GetLength(); i++ )
{
// Substitute the old object type from the module's cla...
unsigned int c;
for( c = 0; c < classTypes.GetLength(); c++ )
{
if( classTypes[c] == equals[i].a )
{
classTypes[c] = equals[i].b;
equals[i].b->AddRef();
break;
}
}
// Remove the old object type from the engine's class t...
for( c = 0; c < engine->classTypes.GetLength(); c++ )
{
if( engine->classTypes[c] == equals[i].a )
{
engine->classTypes[c] = engine->classTypes[engine->cl...
engine->classTypes.PopLast();
break;
}
}
// Substitute all uses of this object type
// Only interfaces in the module is using the type so far
for( c = 0; c < classTypes.GetLength(); c++ )
{
if( classTypes[c]->IsInterface() )
{
asCObjectType *intf = classTypes[c];
for( int m = 0; m < intf->GetMethodCount(); m++ )
{
asCScriptFunction *func = engine->GetScriptFunction(...
if( func )
{
if( func->returnType.GetObjectType() == equals[i].a )
func->returnType.SetObjectType(equals[i].b);
for( int p = 0; p < func->GetParamCount(); p++ )
{
if( func->parameterTypes[p].GetObjectType() == equ...
func->parameterTypes[p].SetObjectType(equals[i].b);
}
}
}
}
}
// Substitute all interface methods in the module. Dele...
for( unsigned int m = 0; m < equals[i].a->methods.GetLe...
{
for( c = 0; c < scriptFunctions.GetLength(); c++ )
{
if( scriptFunctions[c]->id == equals[i].a->methods[m] )
{
scriptFunctions[c]->Release();
assert(false); // todo: tree
scriptFunctions[c] = engine->GetScriptFunction(equal...
scriptFunctions[c]->AddRef();
}
}
}
// Deallocate the object type
asDELETE(equals[i].a, asCObjectType);
}
}
// internal
bool asCModule::AreInterfacesEqual(asCObjectType *a, asCO...
{
// An interface is considered equal to another if the fo...
//
// - The interface names are equal
// - The number of methods is equal
// - All the methods are equal
// - The order of the methods is equal
// - If a method returns or takes an interface by handle...
// ------------
// TODO: Study the possiblity of allowing interfaces whe...
// be considered equal. The compiler and VM can handle t...
// where multiple methods take different interfaces as p...
//
// interface A
// {
// void f(B, C);
// void f(B);
// void f(C);
// }
//
// If 'void f(B)' in module A is compared against 'void ...
// interface B in module A equals interface C in module ...
// 'void f(C, B)' in module B.
// ------------
// Are both interfaces?
if( !a->IsInterface() || !b->IsInterface() )
return false;
// Are the names equal?
if( a->name != b->name )
return false;
// Are the number of methods equal?
if( a->methods.GetLength() != b->methods.GetLength() )
return false;
// Keep the number of equals in the list so we can resto...
int prevEquals = (int)equals.GetLength();
// Are the methods equal to each other?
bool match = true;
for( unsigned int n = 0; n < a->methods.GetLength(); n++ )
{
match = false;
asCScriptFunction *funcA = (asCScriptFunction*)engine->...
asCScriptFunction *funcB = (asCScriptFunction*)engine->...
// funcB can be null if the module that created the int...
// discarded but the type has not yet been released by ...
if( funcB == 0 )
break;
// The methods must have the same name and the same num...
if( funcA->name != funcB->name ||
funcA->parameterTypes.GetLength() != funcB->parameterT...
break;
// The return types must be equal. If the return type i...
if( !AreTypesEqual(funcA->returnType, funcB->returnType...
break;
match = true;
for( unsigned int p = 0; p < funcA->parameterTypes.GetL...
{
if( !AreTypesEqual(funcA->parameterTypes[p], funcB->pa...
funcA->inOutFlags[p] != funcB->inOutFlags[p] )
{
match = false;
break;
}
}
if( !match )
break;
}
// For each of the new interfaces that we're assuming to...
if( match )
{
for( unsigned int n = prevEquals; n < equals.GetLength(...
{
if( !AreInterfacesEqual(equals[n].a, equals[n].b, equa...
{
match = false;
break;
}
}
}
if( !match )
{
// The interfaces doesn't match.
// Restore the list of previous equals before we go on,...
// the caller can continue comparing with another inter...
equals.SetLength(prevEquals);
}
return match;
}
// internal
bool asCModule::AreTypesEqual(const asCDataType &a, const...
{
if( !a.IsEqualExceptInterfaceType(b) )
return false;
asCObjectType *ai = a.GetObjectType();
asCObjectType *bi = b.GetObjectType();
if( ai && ai->IsInterface() )
{
// If the interface is in the equals list, then the pai...
bool found = false;
unsigned int e;
for( e = 0; e < equals.GetLength(); e++ )
{
if( equals[e].a == ai )
{
found = true;
break;
}
}
if( found )
{
// Do the pairs match?
if( equals[e].b != bi )
return false;
}
else
{
// Assume they are equal from now on
sObjectTypePair pair = {ai, bi};
equals.PushLast(pair);
}
}
return true;
}
// interface
int asCModule::SaveByteCode(asIBinaryStream *out)
{
if( out == 0 ) return asINVALID_ARG;
asCRestore rest(this, out, engine);
return rest.Save();
}
// interface
int asCModule::LoadByteCode(asIBinaryStream *in)
{
if( in == 0 ) return asINVALID_ARG;
// Only permit loading bytecode if no other thread is cu...
// TODO: It should be possible to have multiple threads ...
int r = engine->RequestBuild();
if( r < 0 )
return r;
asCRestore rest(this, in, engine);
r = rest.Restore();
JITCompile();
engine->BuildCompleted();
return r;
}
// interface
int asCModule::CompileGlobalVar(const char *sectionName, ...
{
// Validate arguments
if( code == 0 )
return asINVALID_ARG;
// Only one thread may build at one time
// TODO: It should be possible to have multiple threads ...
int r = engine->RequestBuild();
if( r < 0 )
return r;
// Prepare the engine
engine->PrepareEngine();
if( engine->configFailed )
{
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INV...
engine->BuildCompleted();
return asINVALID_CONFIGURATION;
}
// Compile the global variable and add it to the module ...
asCBuilder builder(engine, this);
asCString str = code;
r = builder.CompileGlobalVar(sectionName, str.AddressOf(...
engine->BuildCompleted();
// Initialize the variable
if( r >= 0 && engine->ep.initGlobalVarsAfterBuild )
{
// Clear the memory
asCGlobalProperty *prop = scriptGlobals[scriptGlobals.G...
memset(prop->GetAddressOfValue(), 0, sizeof(asDWORD)*pr...
if( prop->GetInitFunc() )
{
// Call the init function for the global variable
asIScriptContext *ctx = 0;
int r = engine->CreateContext(&ctx, true);
if( r < 0 )
return r;
r = ctx->Prepare(prop->GetInitFunc()->id);
if( r >= 0 )
r = ctx->Execute();
ctx->Release();
}
}
return r;
}
// interface
int asCModule::CompileFunction(const char *sectionName, c...
{
asASSERT(outFunc == 0 || *outFunc == 0);
// Validate arguments
if( code == 0 ||
(compileFlags != 0 && compileFlags != asCOMP_ADD_TO_MOD...
return asINVALID_ARG;
// Only one thread may build at one time
// TODO: It should be possible to have multiple threads ...
int r = engine->RequestBuild();
if( r < 0 )
return r;
// Prepare the engine
engine->PrepareEngine();
if( engine->configFailed )
{
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INV...
engine->BuildCompleted();
return asINVALID_CONFIGURATION;
}
// Compile the single function
asCBuilder builder(engine, this);
asCString str = code;
asCScriptFunction *func = 0;
r = builder.CompileFunction(sectionName, str.AddressOf()...
engine->BuildCompleted();
if( r >= 0 && outFunc )
{
// Return the function to the caller
*outFunc = func;
func->AddRef();
}
// Release our reference to the function
if( func )
func->Release();
return r;
}
// interface
int asCModule::RemoveFunction(int funcId)
{
// Find the global function
for( asUINT n = 0; n < globalFunctions.GetLength(); n++ )
{
if( globalFunctions[n] && globalFunctions[n]->id == fun...
{
asCScriptFunction *func = globalFunctions[n];
globalFunctions.RemoveIndex(n);
func->Release();
scriptFunctions.RemoveValue(func);
func->Release();
return 0;
}
}
return asNO_FUNCTION;
}
// internal
int asCModule::AddFuncDef(const char *name)
{
asCScriptFunction *func = asNEW(asCScriptFunction)(engin...
func->name = name;
funcDefs.PushLast(func);
engine->funcDefs.PushLast(func);
func->id = engine->GetNextScriptFunctionId();
engine->SetScriptFunction(func);
return (int)funcDefs.GetLength()-1;
}
END_AS_NAMESPACE
}}
終了行:
* ビルド時間短縮化 [#v1a93e9a]
:as_builder.cpp|
#code(c,){{
{
asCString str(name);
asCModule::SFMapNode* node = 0;
if ( module->scriptFunctionsMap.MoveTo( &node , a...
{// 移動成功
do
{
// 追加
if ( node->value->objectType == 0 )
{
funcs.PushLast(node->value->id);
}
// 次へ
module->scriptFunctionsMap.MoveNext( &nod...
} while( node != 0 );
}
}
}}
:as_module.h|
#code(c,){{
/*
AngelCode Scripting Library
Copyright (c) 2003-2010 Andreas Jonsson
This software is provided 'as-is', without any express...
warranty. In no event will the authors be held liable ...
damages arising from the use of this software.
Permission is granted to anyone to use this software f...
purpose, including commercial applications, and to alt...
redistribute it freely, subject to the following restr...
1. The origin of this software must not be misrepresen...
must not claim that you wrote the original software...
this software in a product, an acknowledgment in th...
documentation would be appreciated but is not requi...
2. Altered source versions must be plainly marked as s...
must not be misrepresented as being the original so...
3. This notice may not be removed or altered from any ...
distribution.
The original version of this library can be located at:
http://www.angelcode.com/angelscript/
Andreas Jonsson
andreas@angelcode.com
*/
//
// as_module.h
//
// A class that holds a script module
//
#ifndef AS_MODULE_H
#define AS_MODULE_H
#include "as_config.h"
#include "as_atomic.h"
#include "as_string.h"
#include "as_array.h"
#include "as_datatype.h"
#include "as_scriptfunction.h"
#include "as_property.h"
#include "as_map.h"
BEGIN_AS_NAMESPACE
// TODO: import: Remove this when the imported functions ...
const int FUNC_IMPORTED = 0x40000000;
class asCScriptEngine;
class asCCompiler;
class asCBuilder;
class asCContext;
class asCConfigGroup;
struct sBindInfo
{
asCScriptFunction *importedFunctionSignature;
asCString importFromModule;
int boundFunctionId;
};
struct sObjectTypePair
{
asCObjectType *a;
asCObjectType *b;
};
// TODO: import: Remove function imports. When I have imp...
// pointers the function imports should be ...
// TODO: Need a separate interface for compiling scripts....
// will have a target module, and will allow the co...
// script or just individual functions within the s...
//
// With this separation it will be possible to comp...
// the compiler, thus giving a much smaller binary ...
// TODO: There should be an special compile option that w...
// recompile an already compiled script. The compil...
// destructive changes have been made (changing fun...
// then it should simply replace the bytecode withi...
// changing the values of existing global propertie...
class asCModule : public asIScriptModule
{
//-------------------------------------------
// Public interface
//--------------------------------------------
public:
class SFCompare
{
public:
explicit SFCompare(asCString* str = 0) : mStrPtr(...
bool operator < (const SFCompare& aRHS)const
{
assert(mStrPtr != 0);
assert(aRHS.mStrPtr != 0);
return *mStrPtr < *aRHS.mStrPtr;
}
bool operator == (const SFCompare& aRHS)const
{
assert(mStrPtr != 0);
assert(aRHS.mStrPtr != 0);
return *mStrPtr == *aRHS.mStrPtr;
}
private:
asCString* mStrPtr;
};
typedef asCMap<SFCompare, asCScriptFunction*> SFMap;
typedef asSMapNode<SFCompare, asCScriptFunction*> SFM...
virtual asIScriptEngine *GetEngine();
virtual void SetName(const char *name);
virtual const char *GetName();
// Compilation
virtual int AddScriptSection(const char *name, const ch...
virtual int Build();
virtual int CompileFunction(const char *sectionName, co...
virtual int CompileGlobalVar(const char *sectionName, c...
// Script functions
virtual int GetFunctionCount();
virtual int GetFunctionIdByIndex(int inde...
virtual int GetFunctionIdByName(const cha...
virtual int GetFunctionIdByDecl(const cha...
virtual asIScriptFunction *GetFunctionDescriptorByIndex(...
virtual asIScriptFunction *GetFunctionDescriptorById(int...
virtual int RemoveFunction(int funcId);
// Script global variables
virtual int ResetGlobalVars();
virtual int GetGlobalVarCount();
virtual int GetGlobalVarIndexByName(const char *...
virtual int GetGlobalVarIndexByDecl(const char *...
virtual const char *GetGlobalVarDeclaration(int index);
virtual const char *GetGlobalVarName(int index);
virtual int GetGlobalVarTypeId(int index, bool *...
virtual void *GetAddressOfGlobalVar(int index);
virtual int RemoveGlobalVar(int index);
// Type identification
virtual int GetObjectTypeCount();
virtual asIObjectType *GetObjectTypeByIndex(asUINT index);
virtual int GetTypeIdByDecl(const char *decl);
// Enums
virtual int GetEnumCount();
virtual const char *GetEnumByIndex(asUINT index, int *en...
virtual int GetEnumValueCount(int enumTypeId);
virtual const char *GetEnumValueByIndex(int enumTypeId, ...
// Typedefs
virtual int GetTypedefCount();
virtual const char *GetTypedefByIndex(asUINT index, int ...
// Dynamic binding between modules
virtual int GetImportedFunctionCount();
virtual int GetImportedFunctionIndexByDecl(const...
virtual const char *GetImportedFunctionDeclaration(int i...
virtual const char *GetImportedFunctionSourceModule(int ...
virtual int BindImportedFunction(int index, int ...
virtual int UnbindImportedFunction(int importInd...
virtual int BindAllImportedFunctions();
virtual int UnbindAllImportedFunctions();
// Bytecode Saving/Loading
virtual int SaveByteCode(asIBinaryStream *out);
virtual int LoadByteCode(asIBinaryStream *in);
//-----------------------------------------------
// Internal
//-----------------------------------------------
asCModule(const char *name, asCScriptEngine *engine);
~asCModule();
//protected:
friend class asCScriptEngine;
friend class asCBuilder;
friend class asCCompiler;
friend class asCContext;
friend class asCRestore;
void InternalReset();
int CallInit();
void CallExit();
void JITCompile();
int AddScriptFunction(int sectionIdx, int id, const cha...
int AddScriptFunction(asCScriptFunction *func);
int AddImportedFunction(int id, const char *name, const...
int AddFuncDef(const char *name);
int GetNextImportedFunctionId();
void ResolveInterfaceIds();
bool AreInterfacesEqual(asCObjectType *a, asCObjectType ...
bool AreTypesEqual(const asCDataType &a, const asCDataTy...
asCScriptFunction *GetImportedFunction(int funcId);
asCObjectType *GetObjectType(const char *type);
asCGlobalProperty *AllocateGlobalProperty(const char *na...
asCString name;
asCScriptEngine *engine;
asCBuilder *builder;
// This array holds all functions, class members, factor...
asCArray<asCScriptFunction *> scriptFunctions;
SFMap scriptFunctionsMap;
// This array holds global functions declared in the mod...
asCArray<asCScriptFunction *> globalFunctions;
// This array holds imported functions in the module
asCArray<sBindInfo *> bindInformations;
// This array holds the global variables declared in the...
asCArray<asCGlobalProperty *> scriptGlobals;
bool isGlobalVarInitialized;
// This array holds class and interface types
asCArray<asCObjectType*> classTypes;
// This array holds enum types
asCArray<asCObjectType*> enumTypes;
// This array holds typedefs
asCArray<asCObjectType*> typeDefs;
// This array holds the funcdefs declared in the module
asCArray<asCScriptFunction*> funcDefs;
};
END_AS_NAMESPACE
#endif
}}
:as_module.cpp|
#code(c,){{
/*
AngelCode Scripting Library
Copyright (c) 2003-2010 Andreas Jonsson
This software is provided 'as-is', without any express...
warranty. In no event will the authors be held liable ...
damages arising from the use of this software.
Permission is granted to anyone to use this software f...
purpose, including commercial applications, and to alt...
redistribute it freely, subject to the following restr...
1. The origin of this software must not be misrepresen...
must not claim that you wrote the original software...
this software in a product, an acknowledgment in th...
documentation would be appreciated but is not requi...
2. Altered source versions must be plainly marked as s...
must not be misrepresented as being the original so...
3. This notice may not be removed or altered from any ...
distribution.
The original version of this library can be located at:
http://www.angelcode.com/angelscript/
Andreas Jonsson
andreas@angelcode.com
*/
//
// as_module.cpp
//
// A class that holds a script module
//
#include "as_config.h"
#include "as_module.h"
#include "as_builder.h"
#include "as_context.h"
#include "as_texts.h"
BEGIN_AS_NAMESPACE
// internal
asCModule::asCModule(const char *name, asCScriptEngine *e...
{
this->name = name;
this->engine = engine;
builder = 0;
isGlobalVarInitialized = false;
}
// internal
asCModule::~asCModule()
{
InternalReset();
if( builder )
{
asDELETE(builder,asCBuilder);
builder = 0;
}
// Remove the module from the engine
if( engine )
{
if( engine->lastModule == this )
engine->lastModule = 0;
engine->scriptModules.RemoveValue(this);
}
}
// interface
asIScriptEngine *asCModule::GetEngine()
{
return engine;
}
// interface
void asCModule::SetName(const char *name)
{
this->name = name;
}
// interface
const char *asCModule::GetName()
{
return name.AddressOf();
}
// interface
int asCModule::AddScriptSection(const char *name, const c...
{
if( !builder )
builder = asNEW(asCBuilder)(engine, this);
builder->AddCode(name, code, (int)codeLength, lineOffset...
return asSUCCESS;
}
// internal
void asCModule::JITCompile()
{
for (unsigned int i = 0; i < scriptFunctions.GetLengt...
{
scriptFunctions[i]->JITCompile();
}
}
// interface
int asCModule::Build()
{
// Only one thread may build at one time
// TODO: It should be possible to have multiple threads ...
int r = engine->RequestBuild();
if( r < 0 )
return r;
engine->PrepareEngine();
if( engine->configFailed )
{
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INV...
engine->BuildCompleted();
return asINVALID_CONFIGURATION;
}
InternalReset();
if( !builder )
{
engine->BuildCompleted();
return asSUCCESS;
}
// Compile the script
r = builder->Build();
asDELETE(builder,asCBuilder);
builder = 0;
if( r < 0 )
{
// Reset module again
InternalReset();
engine->BuildCompleted();
return r;
}
JITCompile();
engine->PrepareEngine();
engine->BuildCompleted();
// Initialize global variables
if( r >= 0 && engine->ep.initGlobalVarsAfterBuild )
r = ResetGlobalVars();
return r;
}
// interface
int asCModule::ResetGlobalVars()
{
if( isGlobalVarInitialized )
CallExit();
// TODO: The application really should do this manually ...
// otherwise it cannot properly handle script exce...
// thrown by object initializations.
return CallInit();
}
// interface
int asCModule::GetFunctionIdByIndex(int index)
{
if( index < 0 || index >= (int)scriptFunctions.GetLength...
return asNO_FUNCTION;
return scriptFunctions[index]->id;
}
// internal
int asCModule::CallInit()
{
if( isGlobalVarInitialized )
return asERROR;
// Each global variable needs to be cleared individually
asUINT n;
for( n = 0; n < scriptGlobals.GetLength(); n++ )
{
if( scriptGlobals[n] )
{
memset(scriptGlobals[n]->GetAddressOfValue(), 0, sizeo...
}
}
// Call the init function for each of the global variables
asIScriptContext *ctx = 0;
int r = 0;
for( n = 0; n < scriptGlobals.GetLength() && r == 0; n++ )
{
if( scriptGlobals[n]->GetInitFunc() )
{
if( ctx == 0 )
{
r = engine->CreateContext(&ctx, true);
if( r < 0 )
break;
}
r = ctx->Prepare(scriptGlobals[n]->GetInitFunc()->id);
if( r >= 0 )
r = ctx->Execute();
}
}
if( ctx )
{
ctx->Release();
ctx = 0;
}
// Even if the initialization failed we need to set the
// flag that the variables have been initialized, otherw...
// the module won't free those variables that really were
// initialized.
isGlobalVarInitialized = true;
if( r != asEXECUTION_FINISHED )
return asINIT_GLOBAL_VARS_FAILED;
return asSUCCESS;
}
// internal
void asCModule::CallExit()
{
if( !isGlobalVarInitialized ) return;
for( size_t n = 0; n < scriptGlobals.GetLength(); n++ )
{
if( scriptGlobals[n]->type.IsObject() )
{
void *obj = *(void**)scriptGlobals[n]->GetAddressOfVal...
if( obj )
{
asCObjectType *ot = scriptGlobals[n]->type.GetObjectT...
if( ot->beh.release )
engine->CallObjectMethod(obj, ot->beh.release);
else
{
if( ot->beh.destruct )
engine->CallObjectMethod(obj, ot->beh.destruct);
engine->CallFree(obj);
}
}
}
}
isGlobalVarInitialized = false;
}
// internal
void asCModule::InternalReset()
{
CallExit();
size_t n;
// Release all global functions
for( n = 0; n < globalFunctions.GetLength(); n++ )
{
if( globalFunctions[n] )
globalFunctions[n]->Release();
}
globalFunctions.SetLength(0);
// First release all compiled functions
for( n = 0; n < scriptFunctions.GetLength(); n++ )
{
if( scriptFunctions[n] )
{
// Remove the module reference in the functions
scriptFunctions[n]->module = 0;
scriptFunctions[n]->Release();
}
}
scriptFunctions.SetLength(0);
scriptFunctionsMap.EraseAll();
// Release the global properties declared in the module
for( n = 0; n < scriptGlobals.GetLength(); n++ )
scriptGlobals[n]->Release();
scriptGlobals.SetLength(0);
UnbindAllImportedFunctions();
// Free bind information
for( n = 0; n < bindInformations.GetLength(); n++ )
{
engine->importedFunctions[bindInformations[n]->imported...
asDELETE(bindInformations[n]->importedFunctionSignature...
asDELETE(bindInformations[n], sBindInfo);
}
bindInformations.SetLength(0);
// Free declared types, including classes, typedefs, and...
for( n = 0; n < classTypes.GetLength(); n++ )
classTypes[n]->Release();
classTypes.SetLength(0);
for( n = 0; n < enumTypes.GetLength(); n++ )
enumTypes[n]->Release();
enumTypes.SetLength(0);
for( n = 0; n < typeDefs.GetLength(); n++ )
typeDefs[n]->Release();
typeDefs.SetLength(0);
// Free funcdefs
for( n = 0; n < funcDefs.GetLength(); n++ )
{
// TODO: funcdefs: These may be shared between modules,...
engine->funcDefs.RemoveValue(funcDefs[n]);
funcDefs[n]->Release();
}
funcDefs.SetLength(0);
}
// interface
int asCModule::GetFunctionIdByName(const char *name)
{
// TODO: optimize: Improve linear search
// Find the function id
int id = -1;
for( size_t n = 0; n < globalFunctions.GetLength(); n++ )
{
if( globalFunctions[n]->name == name )
{
if( id == -1 )
id = globalFunctions[n]->id;
else
return asMULTIPLE_FUNCTIONS;
}
}
if( id == -1 ) return asNO_FUNCTION;
return id;
}
// interface
int asCModule::GetImportedFunctionCount()
{
return (int)bindInformations.GetLength();
}
// interface
int asCModule::GetImportedFunctionIndexByDecl(const char ...
{
asCBuilder bld(engine, this);
asCScriptFunction func(engine, this, -1);
bld.ParseFunctionDeclaration(0, decl, &func, false);
// TODO: optimize: Improve linear search
// Search script functions for matching interface
int id = -1;
for( asUINT n = 0; n < bindInformations.GetLength(); ++n )
{
if( func.name == bindInformations[n]->importedFunctionS...
func.returnType == bindInformations[n]->importedFuncti...
func.parameterTypes.GetLength() == bindInformations[n]...
{
bool match = true;
for( asUINT p = 0; p < func.parameterTypes.GetLength()...
{
if( func.parameterTypes[p] != bindInformations[n]->im...
{
match = false;
break;
}
}
if( match )
{
if( id == -1 )
id = n;
else
return asMULTIPLE_FUNCTIONS;
}
}
}
if( id == -1 ) return asNO_FUNCTION;
return id;
}
// interface
int asCModule::GetFunctionCount()
{
return (int)globalFunctions.GetLength();
}
// interface
int asCModule::GetFunctionIdByDecl(const char *decl)
{
asCBuilder bld(engine, this);
asCScriptFunction func(engine, this, -1);
int r = bld.ParseFunctionDeclaration(0, decl, &func, fal...
if( r < 0 )
return asINVALID_DECLARATION;
// TODO: optimize: Improve linear search
// Search script functions for matching interface
int id = -1;
for( size_t n = 0; n < globalFunctions.GetLength(); ++n )
{
if( globalFunctions[n]->objectType == 0 &&
func.name == globalFunctions[n]->name &&
func.returnType == globalFunctions[n]->returnType &&
func.parameterTypes.GetLength() == globalFunctions[n]-...
{
bool match = true;
for( size_t p = 0; p < func.parameterTypes.GetLength()...
{
if( func.parameterTypes[p] != globalFunctions[n]->par...
{
match = false;
break;
}
}
if( match )
{
if( id == -1 )
id = globalFunctions[n]->id;
else
return asMULTIPLE_FUNCTIONS;
}
}
}
if( id == -1 ) return asNO_FUNCTION;
return id;
}
// interface
int asCModule::GetGlobalVarCount()
{
return (int)scriptGlobals.GetLength();
}
// interface
int asCModule::GetGlobalVarIndexByName(const char *name)
{
// Find the global var id
int id = -1;
for( size_t n = 0; n < scriptGlobals.GetLength(); n++ )
{
if( scriptGlobals[n]->name == name )
{
id = (int)n;
break;
}
}
if( id == -1 ) return asNO_GLOBAL_VAR;
return id;
}
// interface
int asCModule::RemoveGlobalVar(int index)
{
if( index < 0 || index >= (int)scriptGlobals.GetLength() )
return asINVALID_ARG;
scriptGlobals[index]->Release();
scriptGlobals.RemoveIndex(index);
return 0;
}
// interface
asIScriptFunction *asCModule::GetFunctionDescriptorByInde...
{
if( index < 0 || index >= (int)globalFunctions.GetLength...
return 0;
return globalFunctions[index];
}
// interface
asIScriptFunction *asCModule::GetFunctionDescriptorById(i...
{
return engine->GetFunctionDescriptorById(funcId);
}
// interface
int asCModule::GetGlobalVarIndexByDecl(const char *decl)
{
asCBuilder bld(engine, this);
asCObjectProperty gvar;
bld.ParseVariableDeclaration(decl, &gvar);
// TODO: optimize: Improve linear search
// Search script functions for matching interface
int id = -1;
for( size_t n = 0; n < scriptGlobals.GetLength(); ++n )
{
if( gvar.name == scriptGlobals[n]->name &&
gvar.type == scriptGlobals[n]->type )
{
id = (int)n;
break;
}
}
if( id == -1 ) return asNO_GLOBAL_VAR;
return id;
}
// interface
void *asCModule::GetAddressOfGlobalVar(int index)
{
if( index < 0 || index >= (int)scriptGlobals.GetLength() )
return 0;
// TODO: value types shouldn't need dereferencing
// For object variables it's necessary to dereference th...
if( scriptGlobals[index]->type.IsObject() && !scriptGlob...
return *(void**)(scriptGlobals[index]->GetAddressOfValu...
return (void*)(scriptGlobals[index]->GetAddressOfValue());
}
// interface
const char *asCModule::GetGlobalVarDeclaration(int index)
{
if( index < 0 || index >= (int)scriptGlobals.GetLength() )
return 0;
asCGlobalProperty *prop = scriptGlobals[index];
asASSERT(threadManager);
asCString *tempString = &threadManager->GetLocalData()->...
*tempString = prop->type.Format();
*tempString += " " + prop->name;
return tempString->AddressOf();
}
// interface
const char *asCModule::GetGlobalVarName(int index)
{
if( index < 0 || index >= (int)scriptGlobals.GetLength() )
return 0;
return scriptGlobals[index]->name.AddressOf();
}
// interface
// TODO: If the typeId ever encodes the const flag, then ...
int asCModule::GetGlobalVarTypeId(int index, bool *isConst)
{
if( index < 0 || index >= (int)scriptGlobals.GetLength() )
return asINVALID_ARG;
if( isConst )
*isConst = scriptGlobals[index]->type.IsReadOnly();
return engine->GetTypeIdFromDataType(scriptGlobals[index...
}
// interface
int asCModule::GetObjectTypeCount()
{
return (int)classTypes.GetLength();
}
// interface
asIObjectType *asCModule::GetObjectTypeByIndex(asUINT ind...
{
if( index >= classTypes.GetLength() )
return 0;
return classTypes[index];
}
// interface
int asCModule::GetTypeIdByDecl(const char *decl)
{
asCDataType dt;
asCBuilder bld(engine, this);
int r = bld.ParseDataType(decl, &dt);
if( r < 0 )
return asINVALID_TYPE;
return engine->GetTypeIdFromDataType(dt);
}
// interface
int asCModule::GetEnumCount()
{
return (int)enumTypes.GetLength();
}
// interface
const char *asCModule::GetEnumByIndex(asUINT index, int *...
{
if( index >= enumTypes.GetLength() )
return 0;
if( enumTypeId )
*enumTypeId = GetTypeIdByDecl(enumTypes[index]->name.Ad...
return enumTypes[index]->name.AddressOf();
}
// interface
int asCModule::GetEnumValueCount(int enumTypeId)
{
const asCDataType *dt = engine->GetDataTypeFromTypeId(en...
asCObjectType *t = dt->GetObjectType();
if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
return asINVALID_TYPE;
return (int)t->enumValues.GetLength();
}
// interface
const char *asCModule::GetEnumValueByIndex(int enumTypeId...
{
const asCDataType *dt = engine->GetDataTypeFromTypeId(en...
asCObjectType *t = dt->GetObjectType();
if( t == 0 || !(t->GetFlags() & asOBJ_ENUM) )
return 0;
if( index >= t->enumValues.GetLength() )
return 0;
if( outValue )
*outValue = t->enumValues[index]->value;
return t->enumValues[index]->name.AddressOf();
}
// interface
int asCModule::GetTypedefCount()
{
return (int)typeDefs.GetLength();
}
// interface
const char *asCModule::GetTypedefByIndex(asUINT index, in...
{
if( index >= typeDefs.GetLength() )
return 0;
if( typeId )
*typeId = GetTypeIdByDecl(typeDefs[index]->name.Address...
return typeDefs[index]->name.AddressOf();
}
// internal
int asCModule::GetNextImportedFunctionId()
{
return FUNC_IMPORTED | (asUINT)engine->importedFunctions...
}
// internal
int asCModule::AddScriptFunction(int sectionIdx, int id, ...
{
asASSERT(id >= 0);
// Store the function information
asCScriptFunction *func = asNEW(asCScriptFunction)(engin...
func->name = name;
func->id = id;
func->returnType = returnType;
func->scriptSectionIdx = sectionIdx;
for( int n = 0; n < paramCount; n++ )
{
func->parameterTypes.PushLast(params[n]);
func->inOutFlags.PushLast(inOutFlags[n]);
}
func->objectType = objType;
func->isReadOnly = isConstMethod;
// The script function's refCount was initialized to 1
scriptFunctions.PushLast(func);
if ( objType == 0 )
{
scriptFunctionsMap.Insert(SFCompare(&func->name),...
}
engine->SetScriptFunction(func);
// Compute the signature id
if( objType )
func->ComputeSignatureId();
// Add reference
if( isGlobalFunction )
{
globalFunctions.PushLast(func);
func->AddRef();
}
return 0;
}
// internal
int asCModule::AddScriptFunction(asCScriptFunction *func)
{
scriptFunctions.PushLast(func);
if ( func->objectType == 0 )
{
scriptFunctionsMap.Insert(SFCompare(&func->name),...
}
func->AddRef();
engine->SetScriptFunction(func);
return 0;
}
// internal
int asCModule::AddImportedFunction(int id, const char *na...
{
asASSERT(id >= 0);
// Store the function information
asCScriptFunction *func = asNEW(asCScriptFunction)(engin...
func->name = name;
func->id = id;
func->returnType = returnType;
for( int n = 0; n < paramCount; n++ )
{
func->parameterTypes.PushLast(params[n]);
func->inOutFlags.PushLast(inOutFlags[n]);
}
func->objectType = 0;
sBindInfo *info = asNEW(sBindInfo);
info->importedFunctionSignature = func;
info->boundFunctionId = -1;
info->importFromModule = moduleName;
bindInformations.PushLast(info);
// Add the info to the array in the engine
engine->importedFunctions.PushLast(info);
return 0;
}
// internal
asCScriptFunction *asCModule::GetImportedFunction(int ind...
{
return bindInformations[index]->importedFunctionSignature;
}
// interface
int asCModule::BindImportedFunction(int index, int source...
{
// First unbind the old function
int r = UnbindImportedFunction(index);
if( r < 0 ) return r;
// Must verify that the interfaces are equal
asCScriptFunction *dst = GetImportedFunction(index);
if( dst == 0 ) return asNO_FUNCTION;
asCScriptFunction *src = engine->GetScriptFunction(sourc...
if( src == 0 )
return asNO_FUNCTION;
// Verify return type
if( dst->returnType != src->returnType )
return asINVALID_INTERFACE;
if( dst->parameterTypes.GetLength() != src->parameterTyp...
return asINVALID_INTERFACE;
for( size_t n = 0; n < dst->parameterTypes.GetLength(); ...
{
if( dst->parameterTypes[n] != src->parameterTypes[n] )
return asINVALID_INTERFACE;
}
bindInformations[index]->boundFunctionId = sourceId;
engine->scriptFunctions[sourceId]->AddRef();
return asSUCCESS;
}
// interface
int asCModule::UnbindImportedFunction(int index)
{
if( index < 0 || index > (int)bindInformations.GetLength...
return asINVALID_ARG;
// Remove reference to old module
int oldFuncID = bindInformations[index]->boundFunctionId;
if( oldFuncID != -1 )
{
bindInformations[index]->boundFunctionId = -1;
engine->scriptFunctions[oldFuncID]->Release();
}
return asSUCCESS;
}
// interface
const char *asCModule::GetImportedFunctionDeclaration(int...
{
asCScriptFunction *func = GetImportedFunction(index);
if( func == 0 ) return 0;
asASSERT(threadManager);
asCString *tempString = &threadManager->GetLocalData()->...
*tempString = func->GetDeclarationStr();
return tempString->AddressOf();
}
// interface
const char *asCModule::GetImportedFunctionSourceModule(in...
{
if( index >= (int)bindInformations.GetLength() )
return 0;
return bindInformations[index]->importFromModule.Address...
}
// inteface
int asCModule::BindAllImportedFunctions()
{
bool notAllFunctionsWereBound = false;
// Bind imported functions
int c = GetImportedFunctionCount();
for( int n = 0; n < c; ++n )
{
asCScriptFunction *func = GetImportedFunction(n);
if( func == 0 ) return asERROR;
asCString str = func->GetDeclarationStr();
// Get module name from where the function should be im...
const char *moduleName = GetImportedFunctionSourceModul...
if( moduleName == 0 ) return asERROR;
asCModule *srcMod = engine->GetModule(moduleName, false);
int funcId = -1;
if( srcMod )
funcId = srcMod->GetFunctionIdByDecl(str.AddressOf());
if( funcId < 0 )
notAllFunctionsWereBound = true;
else
{
if( BindImportedFunction(n, funcId) < 0 )
notAllFunctionsWereBound = true;
}
}
if( notAllFunctionsWereBound )
return asCANT_BIND_ALL_FUNCTIONS;
return asSUCCESS;
}
// interface
int asCModule::UnbindAllImportedFunctions()
{
int c = GetImportedFunctionCount();
for( int n = 0; n < c; ++n )
UnbindImportedFunction(n);
return asSUCCESS;
}
// internal
asCObjectType *asCModule::GetObjectType(const char *type)
{
size_t n;
// TODO: optimize: Improve linear search
for( n = 0; n < classTypes.GetLength(); n++ )
if( classTypes[n]->name == type )
return classTypes[n];
for( n = 0; n < enumTypes.GetLength(); n++ )
if( enumTypes[n]->name == type )
return enumTypes[n];
for( n = 0; n < typeDefs.GetLength(); n++ )
if( typeDefs[n]->name == type )
return typeDefs[n];
return 0;
}
// internal
asCGlobalProperty *asCModule::AllocateGlobalProperty(cons...
{
asCGlobalProperty *prop = engine->AllocateGlobalProperty...
prop->name = name;
// Allocate the memory for this property based on its type
prop->type = dt;
prop->AllocateMemory();
// Store the variable in the module scope (the reference...
scriptGlobals.PushLast(prop);
return prop;
}
// internal
void asCModule::ResolveInterfaceIds()
{
// For each of the interfaces declared in the script fin...
// If an identical interface was found then substitute t...
// then remove this interface declaration. If an interfa...
// retry the detection of identical interface for it sin...
// For an interface to be equal to another the name and ...
// references another interface, then that must be check...
// Example:
//
// interface A { void f(B@); }
// interface B { void f(A@); void f(C@); }
// interface C { void f(A@); }
//
// A1 equals A2 if and only if B1 equals B2
// B1 equals B2 if and only if A1 equals A2 and C1 equal...
// C1 equals C2 if and only if A1 equals A2
unsigned int i;
// The interface can only be equal to interfaces declare...
// Interfaces registered by the application will conflic...
// This means that we only need to look for the interfac...
asCArray<sObjectTypePair> equals;
for( i = 0; i < classTypes.GetLength(); i++ )
{
asCObjectType *intf1 = classTypes[i];
if( !intf1->IsInterface() )
continue;
// The interface may have been determined to be equal t...
bool found = false;
for( unsigned int e = 0; e < equals.GetLength(); e++ )
{
if( equals[e].a == intf1 )
{
found = true;
break;
}
}
if( found )
break;
for( unsigned int n = 0; n < engine->classTypes.GetLeng...
{
// Don't compare against self
if( engine->classTypes[n] == intf1 )
continue;
asCObjectType *intf2 = engine->classTypes[n];
// Assume the interface are equal, then validate this
sObjectTypePair pair = {intf1,intf2};
equals.PushLast(pair);
if( AreInterfacesEqual(intf1, intf2, equals) )
break;
// Since they are not equal, remove them from the list...
equals.PopLast();
}
}
// For each of the interfaces that have been found to be...
// remove the new declaration and instead have the modul...
for( i = 0; i < equals.GetLength(); i++ )
{
// Substitute the old object type from the module's cla...
unsigned int c;
for( c = 0; c < classTypes.GetLength(); c++ )
{
if( classTypes[c] == equals[i].a )
{
classTypes[c] = equals[i].b;
equals[i].b->AddRef();
break;
}
}
// Remove the old object type from the engine's class t...
for( c = 0; c < engine->classTypes.GetLength(); c++ )
{
if( engine->classTypes[c] == equals[i].a )
{
engine->classTypes[c] = engine->classTypes[engine->cl...
engine->classTypes.PopLast();
break;
}
}
// Substitute all uses of this object type
// Only interfaces in the module is using the type so far
for( c = 0; c < classTypes.GetLength(); c++ )
{
if( classTypes[c]->IsInterface() )
{
asCObjectType *intf = classTypes[c];
for( int m = 0; m < intf->GetMethodCount(); m++ )
{
asCScriptFunction *func = engine->GetScriptFunction(...
if( func )
{
if( func->returnType.GetObjectType() == equals[i].a )
func->returnType.SetObjectType(equals[i].b);
for( int p = 0; p < func->GetParamCount(); p++ )
{
if( func->parameterTypes[p].GetObjectType() == equ...
func->parameterTypes[p].SetObjectType(equals[i].b);
}
}
}
}
}
// Substitute all interface methods in the module. Dele...
for( unsigned int m = 0; m < equals[i].a->methods.GetLe...
{
for( c = 0; c < scriptFunctions.GetLength(); c++ )
{
if( scriptFunctions[c]->id == equals[i].a->methods[m] )
{
scriptFunctions[c]->Release();
assert(false); // todo: tree
scriptFunctions[c] = engine->GetScriptFunction(equal...
scriptFunctions[c]->AddRef();
}
}
}
// Deallocate the object type
asDELETE(equals[i].a, asCObjectType);
}
}
// internal
bool asCModule::AreInterfacesEqual(asCObjectType *a, asCO...
{
// An interface is considered equal to another if the fo...
//
// - The interface names are equal
// - The number of methods is equal
// - All the methods are equal
// - The order of the methods is equal
// - If a method returns or takes an interface by handle...
// ------------
// TODO: Study the possiblity of allowing interfaces whe...
// be considered equal. The compiler and VM can handle t...
// where multiple methods take different interfaces as p...
//
// interface A
// {
// void f(B, C);
// void f(B);
// void f(C);
// }
//
// If 'void f(B)' in module A is compared against 'void ...
// interface B in module A equals interface C in module ...
// 'void f(C, B)' in module B.
// ------------
// Are both interfaces?
if( !a->IsInterface() || !b->IsInterface() )
return false;
// Are the names equal?
if( a->name != b->name )
return false;
// Are the number of methods equal?
if( a->methods.GetLength() != b->methods.GetLength() )
return false;
// Keep the number of equals in the list so we can resto...
int prevEquals = (int)equals.GetLength();
// Are the methods equal to each other?
bool match = true;
for( unsigned int n = 0; n < a->methods.GetLength(); n++ )
{
match = false;
asCScriptFunction *funcA = (asCScriptFunction*)engine->...
asCScriptFunction *funcB = (asCScriptFunction*)engine->...
// funcB can be null if the module that created the int...
// discarded but the type has not yet been released by ...
if( funcB == 0 )
break;
// The methods must have the same name and the same num...
if( funcA->name != funcB->name ||
funcA->parameterTypes.GetLength() != funcB->parameterT...
break;
// The return types must be equal. If the return type i...
if( !AreTypesEqual(funcA->returnType, funcB->returnType...
break;
match = true;
for( unsigned int p = 0; p < funcA->parameterTypes.GetL...
{
if( !AreTypesEqual(funcA->parameterTypes[p], funcB->pa...
funcA->inOutFlags[p] != funcB->inOutFlags[p] )
{
match = false;
break;
}
}
if( !match )
break;
}
// For each of the new interfaces that we're assuming to...
if( match )
{
for( unsigned int n = prevEquals; n < equals.GetLength(...
{
if( !AreInterfacesEqual(equals[n].a, equals[n].b, equa...
{
match = false;
break;
}
}
}
if( !match )
{
// The interfaces doesn't match.
// Restore the list of previous equals before we go on,...
// the caller can continue comparing with another inter...
equals.SetLength(prevEquals);
}
return match;
}
// internal
bool asCModule::AreTypesEqual(const asCDataType &a, const...
{
if( !a.IsEqualExceptInterfaceType(b) )
return false;
asCObjectType *ai = a.GetObjectType();
asCObjectType *bi = b.GetObjectType();
if( ai && ai->IsInterface() )
{
// If the interface is in the equals list, then the pai...
bool found = false;
unsigned int e;
for( e = 0; e < equals.GetLength(); e++ )
{
if( equals[e].a == ai )
{
found = true;
break;
}
}
if( found )
{
// Do the pairs match?
if( equals[e].b != bi )
return false;
}
else
{
// Assume they are equal from now on
sObjectTypePair pair = {ai, bi};
equals.PushLast(pair);
}
}
return true;
}
// interface
int asCModule::SaveByteCode(asIBinaryStream *out)
{
if( out == 0 ) return asINVALID_ARG;
asCRestore rest(this, out, engine);
return rest.Save();
}
// interface
int asCModule::LoadByteCode(asIBinaryStream *in)
{
if( in == 0 ) return asINVALID_ARG;
// Only permit loading bytecode if no other thread is cu...
// TODO: It should be possible to have multiple threads ...
int r = engine->RequestBuild();
if( r < 0 )
return r;
asCRestore rest(this, in, engine);
r = rest.Restore();
JITCompile();
engine->BuildCompleted();
return r;
}
// interface
int asCModule::CompileGlobalVar(const char *sectionName, ...
{
// Validate arguments
if( code == 0 )
return asINVALID_ARG;
// Only one thread may build at one time
// TODO: It should be possible to have multiple threads ...
int r = engine->RequestBuild();
if( r < 0 )
return r;
// Prepare the engine
engine->PrepareEngine();
if( engine->configFailed )
{
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INV...
engine->BuildCompleted();
return asINVALID_CONFIGURATION;
}
// Compile the global variable and add it to the module ...
asCBuilder builder(engine, this);
asCString str = code;
r = builder.CompileGlobalVar(sectionName, str.AddressOf(...
engine->BuildCompleted();
// Initialize the variable
if( r >= 0 && engine->ep.initGlobalVarsAfterBuild )
{
// Clear the memory
asCGlobalProperty *prop = scriptGlobals[scriptGlobals.G...
memset(prop->GetAddressOfValue(), 0, sizeof(asDWORD)*pr...
if( prop->GetInitFunc() )
{
// Call the init function for the global variable
asIScriptContext *ctx = 0;
int r = engine->CreateContext(&ctx, true);
if( r < 0 )
return r;
r = ctx->Prepare(prop->GetInitFunc()->id);
if( r >= 0 )
r = ctx->Execute();
ctx->Release();
}
}
return r;
}
// interface
int asCModule::CompileFunction(const char *sectionName, c...
{
asASSERT(outFunc == 0 || *outFunc == 0);
// Validate arguments
if( code == 0 ||
(compileFlags != 0 && compileFlags != asCOMP_ADD_TO_MOD...
return asINVALID_ARG;
// Only one thread may build at one time
// TODO: It should be possible to have multiple threads ...
int r = engine->RequestBuild();
if( r < 0 )
return r;
// Prepare the engine
engine->PrepareEngine();
if( engine->configFailed )
{
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INV...
engine->BuildCompleted();
return asINVALID_CONFIGURATION;
}
// Compile the single function
asCBuilder builder(engine, this);
asCString str = code;
asCScriptFunction *func = 0;
r = builder.CompileFunction(sectionName, str.AddressOf()...
engine->BuildCompleted();
if( r >= 0 && outFunc )
{
// Return the function to the caller
*outFunc = func;
func->AddRef();
}
// Release our reference to the function
if( func )
func->Release();
return r;
}
// interface
int asCModule::RemoveFunction(int funcId)
{
// Find the global function
for( asUINT n = 0; n < globalFunctions.GetLength(); n++ )
{
if( globalFunctions[n] && globalFunctions[n]->id == fun...
{
asCScriptFunction *func = globalFunctions[n];
globalFunctions.RemoveIndex(n);
func->Release();
scriptFunctions.RemoveValue(func);
func->Release();
return 0;
}
}
return asNO_FUNCTION;
}
// internal
int asCModule::AddFuncDef(const char *name)
{
asCScriptFunction *func = asNEW(asCScriptFunction)(engin...
func->name = name;
funcDefs.PushLast(func);
engine->funcDefs.PushLast(func);
func->id = engine->GetNextScriptFunctionId();
engine->SetScriptFunction(func);
return (int)funcDefs.GetLength()-1;
}
END_AS_NAMESPACE
}}
ページ名: