code:
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2025 Andreas Jonsson

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
      must not claim that you wrote the original software. If you use
      this software in a product, an acknowledgment in the product
      documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
      must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
      distribution.

   The original version of this library can be located at:
   http://www.angelcode.com/angelscript/

   Andreas Jonsson
   andreas@angelcode.com
*/

//
// as_scriptengine.h
//
// The implementation of the script engine interface
//

#ifndef AS_SCRIPTENGINE_H
#define AS_SCRIPTENGINE_H

#include "as_config.h"
#include "as_atomic.h"
#include "as_scriptfunction.h"
#include "as_array.h"
#include "as_datatype.h"
#include "as_objecttype.h"
#include "as_module.h"
#include "as_callfunc.h"
#include "as_configgroup.h"
#include "as_memory.h"
#include "as_gc.h"
#include "as_tokenizer.h"
#include "as_map.h"

BEGIN_AS_NAMESPACE

class asCBuilder;
class asCContext;

// TODO: import: Remove this when import is removed
struct sBindInfo;

class asCScriptEngine : public asIScriptEngine
{
//=============================================================
// From asIScriptEngine
//=============================================================
public:
	// Memory management
	virtual int AddRef() const;
	virtual int Release() const;
	virtual int ShutDownAndRelease();

	// Engine properties
	virtual int     SetEngineProperty(asEEngineProp property, asPWORD value);
	virtual asPWORD GetEngineProperty(asEEngineProp property) const;

	// Compiler messages
	virtual int SetMessageCallback(const asSFuncPtr &callback, void *obj, asDWORD callConv);
	virtual int GetMessageCallback(asSFuncPtr* callback, void** obj, asDWORD* callConv);
	virtual int ClearMessageCallback();
	virtual int WriteMessage(const char *section, int row, int col, asEMsgType type, const char *message);

	// JIT Compiler
	virtual int                     SetJITCompiler(asIJITCompilerAbstract *compiler);
	virtual asIJITCompilerAbstract *GetJITCompiler() const;

	// Global functions
	virtual int                RegisterGlobalFunction(const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv, void *auxiliary = 0);
	virtual asUINT             GetGlobalFunctionCount() const;
	virtual asIScriptFunction *GetGlobalFunctionByIndex(asUINT index) const;
	virtual asIScriptFunction *GetGlobalFunctionByDecl(const char *declaration) const;

	// Global properties
	virtual int    RegisterGlobalProperty(const char *declaration, void *pointer);
	virtual asUINT GetGlobalPropertyCount() const;
	virtual int    GetGlobalPropertyByIndex(asUINT index, const char **name, const char **nameSpace = 0, int *typeId = 0, bool *isConst = 0, const char **configGroup = 0, void **pointer = 0, asDWORD *accessMask = 0) const;
	virtual int    GetGlobalPropertyIndexByName(const char *name) const;
	virtual int    GetGlobalPropertyIndexByDecl(const char *decl) const;

	// Type registration
	virtual int            RegisterObjectType(const char *obj, int byteSize, asQWORD flags);
	virtual int            RegisterObjectProperty(const char *obj, const char *declaration, int byteOffset, int compositeOffset = 0, bool isCompositeIndirect = false);
	virtual int            RegisterObjectMethod(const char *obj, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv, void *auxiliary = 0, int compositeOffset = 0, bool isCompositeIndirect = false);
	virtual int            RegisterObjectBehaviour(const char *obj, asEBehaviours behaviour, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv, void *auxiliary = 0, int compositeOffset = 0, bool isCompositeIndirect = false);
	virtual int            RegisterInterface(const char *name);
	virtual int            RegisterInterfaceMethod(const char *intf, const char *declaration);
	virtual asUINT         GetObjectTypeCount() const;
	virtual asITypeInfo   *GetObjectTypeByIndex(asUINT index) const;

	// String factory
	virtual int RegisterStringFactory(const char *datatype, asIStringFactory *factory);
	virtual int GetStringFactory(asDWORD* typeModifiers, asIStringFactory** factory) const;
#ifdef AS_DEPRECATED
	// deprecated since 2024-07-27, 2.38.0
	virtual int GetStringFactoryReturnTypeId(asDWORD *flags) const;
#endif

	// Default array type
	virtual int RegisterDefaultArrayType(const char *type);
	virtual int GetDefaultArrayTypeId() const;

	// Enums
	virtual int          RegisterEnum(const char *type);
	virtual int          RegisterEnumValue(const char *type, const char *name, int value);
	virtual asUINT       GetEnumCount() const;
	virtual asITypeInfo *GetEnumByIndex(asUINT index) const;

	// Funcdefs
	virtual int          RegisterFuncdef(const char *decl);
	virtual asUINT       GetFuncdefCount() const;
	virtual asITypeInfo *GetFuncdefByIndex(asUINT index) const;

	// Typedefs
	// TODO: interface: Should perhaps rename this to Alias, since it doesn't really create a new type
	virtual int          RegisterTypedef(const char *type, const char *decl);
	virtual asUINT       GetTypedefCount() const;
	virtual asITypeInfo *GetTypedefByIndex(asUINT index) const;

	// Configuration groups
	virtual int         BeginConfigGroup(const char *groupName);
	virtual int         EndConfigGroup();
	virtual int         RemoveConfigGroup(const char *groupName);
	virtual asDWORD     SetDefaultAccessMask(asDWORD defaultMask);
	virtual int         SetDefaultNamespace(const char *nameSpace);
	virtual const char *GetDefaultNamespace() const;

	// Script modules
	virtual asIScriptModule *GetModule(const char *module, asEGMFlags flag);
	virtual int              DiscardModule(const char *module);
	virtual asUINT           GetModuleCount() const;
	virtual asIScriptModule *GetModuleByIndex(asUINT index) const;

	// Script functions
	virtual int                GetLastFunctionId() const;
	virtual asIScriptFunction *GetFunctionById(int funcId) const;

	// Type identification
	virtual int            GetTypeIdByDecl(const char *decl) const;
	virtual const char    *GetTypeDeclaration(int typeId, bool includeNamespace = false) const;
	virtual int            GetSizeOfPrimitiveType(int typeId) const;
	virtual asITypeInfo   *GetTypeInfoById(int typeId) const;
	virtual asITypeInfo   *GetTypeInfoByName(const char *name) const;
	virtual asITypeInfo   *GetTypeInfoByDecl(const char *decl) const;

	// Script execution
	virtual asIScriptContext      *CreateContext();
	virtual void                  *CreateScriptObject(const asITypeInfo *type);
	virtual void                  *CreateScriptObjectCopy(void *obj, const asITypeInfo *type);
	virtual void                  *CreateUninitializedScriptObject(const asITypeInfo *type);
	virtual asIScriptFunction     *CreateDelegate(asIScriptFunction *func, void *obj);
	virtual int                    AssignScriptObject(void *dstObj, void *srcObj, const asITypeInfo *type);
	virtual void                   ReleaseScriptObject(void *obj, const asITypeInfo *type);
	virtual void                   AddRefScriptObject(void *obj, const asITypeInfo *type);
	virtual int                    RefCastObject(void *obj, asITypeInfo *fromType, asITypeInfo *toType, void **newPtr, bool useOnlyImplicitCast = false);
	virtual asILockableSharedBool *GetWeakRefFlagOfScriptObject(void *obj, const asITypeInfo *type) const;

	// Context pooling
	virtual asIScriptContext *RequestContext();
	virtual void              ReturnContext(asIScriptContext *ctx);
	virtual int               SetContextCallbacks(asREQUESTCONTEXTFUNC_t requestCtx, asRETURNCONTEXTFUNC_t returnCtx, void *param = 0);

	// String interpretation
	virtual asETokenClass ParseToken(const char *string, size_t stringLength = 0, asUINT *tokenLength = 0) const;

	// Garbage collection
	virtual int  GarbageCollect(asDWORD flags = asGC_FULL_CYCLE, asUINT numIterations = 1);
	virtual void GetGCStatistics(asUINT *currentSize, asUINT *totalDestroyed, asUINT *totalDetected, asUINT *newObjects, asUINT *totalNewDestroyed) const;
	virtual int  NotifyGarbageCollectorOfNewObject(void *obj, asITypeInfo *type);
	virtual int  GetObjectInGC(asUINT idx, asUINT *seqNbr, void **obj = 0, asITypeInfo **type = 0);
	virtual void GCEnumCallback(void *reference);
	virtual void ForwardGCEnumReferences(void *ref, asITypeInfo *type);
	virtual void ForwardGCReleaseReferences(void *ref, asITypeInfo *type);
	virtual void SetCircularRefDetectedCallback(asCIRCULARREFFUNC_t callback, void *param = 0);

	// User data
	virtual void *SetUserData(void *data, asPWORD type);
	virtual void *GetUserData(asPWORD type) const;
	virtual void  SetEngineUserDataCleanupCallback(asCLEANENGINEFUNC_t callback, asPWORD type);
	virtual void  SetModuleUserDataCleanupCallback(asCLEANMODULEFUNC_t callback, asPWORD type);
	virtual void  SetContextUserDataCleanupCallback(asCLEANCONTEXTFUNC_t callback, asPWORD type);
	virtual void  SetFunctionUserDataCleanupCallback(asCLEANFUNCTIONFUNC_t callback, asPWORD type);
	virtual void  SetTypeInfoUserDataCleanupCallback(asCLEANTYPEINFOFUNC_t callback, asPWORD type);
	virtual void  SetScriptObjectUserDataCleanupCallback(asCLEANSCRIPTOBJECTFUNC_t callback, asPWORD type);

	// Exception handling
	virtual int SetTranslateAppExceptionCallback(const asSFuncPtr &callback, void *param, int callConv);

//===========================================================
// internal methods
//===========================================================
public:
	asCScriptEngine();
	virtual ~asCScriptEngine();

//protected:
	friend class asCBuilder;
	friend class asCCompiler;
	friend class asCContext;
	friend class asCDataType;
	friend class asCModule;
	friend class asCRestore;
	friend class asCByteCode;
	friend int PrepareSystemFunction(asCScriptFunction *func, asSSystemFunctionInterface *internal, asCScriptEngine *engine);

	int RegisterMethodToObjectType(asCObjectType *objectType, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv, void *auxiliary = 0, int compositeOffset = 0, bool isCompositeIndirect = false);
	int RegisterBehaviourToObjectType(asCObjectType *objectType, asEBehaviours behaviour, const char *decl, const asSFuncPtr &funcPointer, asDWORD callConv, void *auxiliary = 0, int compositeOffset = 0, bool isCompositeIndirect = false);

	int VerifyVarTypeNotInFunction(asCScriptFunction *func);

	void *CallAlloc(const asCObjectType *objType) const;
	void  CallFree(void *obj) const;

	void *CallGlobalFunctionRetPtr(int func) const;
	void *CallGlobalFunctionRetPtr(int func, void *param1) const;
	void *CallGlobalFunctionRetPtr(asSSystemFunctionInterface *func, asCScriptFunction *desc) const;
	void *CallGlobalFunctionRetPtr(asSSystemFunctionInterface *i, asCScriptFunction *s, void *param1) const;
	void  CallObjectMethod(void *obj, int func) const;
	void  CallObjectMethod(void *obj, void *param, int func) const;
	void  CallObjectMethod(void *obj, asSSystemFunctionInterface *func, asCScriptFunction *desc) const;
	void  CallObjectMethod(void *obj, void *param, asSSystemFunctionInterface *func, asCScriptFunction *desc) const;
	bool  CallObjectMethodRetBool(void *obj, int func) const;
	int   CallObjectMethodRetInt(void *obj, int func) const;
	void *CallObjectMethodRetPtr(void *obj, int func) const;
	void *CallObjectMethodRetPtr(void *obj, int param1, asCScriptFunction *func) const;
	void  CallGlobalFunction(void *param1, void *param2, asSSystemFunctionInterface *func, asCScriptFunction *desc) const;
	bool  CallGlobalFunctionRetBool(void *param1, void *param2, asSSystemFunctionInterface *func, asCScriptFunction *desc) const;
	int   CallScriptObjectMethod(void *obj, int func);

	void ConstructScriptObjectCopy(void *mem, void *obj, asCObjectType *type);

	void DeleteDiscardedModules();

	void RemoveTemplateInstanceType(asCObjectType *t);

	asCConfigGroup *FindConfigGroupForFunction(int funcId) const;
	asCConfigGroup *FindConfigGroupForGlobalVar(int gvarId) const;
	asCConfigGroup *FindConfigGroupForTypeInfo(const asCTypeInfo *type) const;
	asCConfigGroup *FindConfigGroupForFuncDef(const asCFuncdefType *funcDef) const;

	int  RequestBuild();
	void BuildCompleted();

	void PrepareEngine();
	bool isPrepared;

	int CreateContext(asIScriptContext **context, bool isInternal);

	asCTypeInfo *GetRegisteredType(const asCString &name, asSNameSpace *ns) const;

	asCObjectType *GetListPatternType(int listPatternFuncId);
	void DestroyList(asBYTE *buffer, const asCObjectType *listPatternType);
	void DestroySubList(asBYTE *&buffer, asSListPatternNode *&patternNode);

	int AddBehaviourFunction(asCScriptFunction &func, asSSystemFunctionInterface &internal);

	int ParseNamespace(const char* ns, asCArray<asCString>& nsStrings) const;
	asCString GetFunctionDeclaration(int funcId);

	asCScriptFunction *GetScriptFunction(int funcId) const;

	asCModule *GetModule(const char *name, bool create);
	asCModule *GetModuleFromFuncId(int funcId);

	int  GetMethodIdByDecl(const asCObjectType *ot, const char *decl, asCModule *mod);
	int  GetFactoryIdByDecl(const asCObjectType *ot, const char *decl);

	int  GetNextScriptFunctionId();
	void AddScriptFunction(asCScriptFunction *func);
	void RemoveScriptFunction(asCScriptFunction *func);
	void RemoveFuncdef(asCFuncdefType *func);

	int ConfigError(int err, const char *funcName, const char *arg1, const char *arg2);

	int                GetTypeIdFromDataType(const asCDataType &dt) const;
	asCDataType        GetDataTypeFromTypeId(int typeId) const;
	asCObjectType     *GetObjectTypeFromTypeId(int typeId) const;
	void               RemoveFromTypeIdMap(asCTypeInfo *type);

	bool               IsTemplateType(const char *name) const;
	bool               IsTemplateFn(const char *name) const;
	int                GetTemplateFunctionInstance(asCScriptFunction* templateFunction, const asCArray<asCDataType>& subTypes);
	int                SetTemplateRestrictions(asCObjectType *templateType, asCScriptFunction *func, const char *caller, const char *decl);
	asCObjectType     *GetTemplateInstanceType(asCObjectType *templateType, asCArray<asCDataType> &subTypes, asCModule *requestingModule);
	asCScriptFunction *GenerateFactoryStubForTemplateObjectInstance(asCObjectType *templateType, asCObjectType *templateInstanceType, int origFactoryId);
	bool               GenerateFunctionForTemplateObjectInstance(asCObjectType *templateType, asCObjectType *templateInstanceType, asCScriptFunction *templateFunc, asCScriptFunction **newFunc);
	asCFuncdefType    *GenerateFuncdefForTemplateObjectInstance(asCObjectType *templateType, asCObjectType *templateInstanceType, asCFuncdefType *templateFuncdef);
	asCDataType        DetermineTypeForTemplate(const asCDataType &orig, const asCArray<asCDataType>& templateSubTypes, const asCArray<asCDataType>& targetSubTypes, asCModule *mod, asCObjectType* tmpl, asCObjectType* ot);

	asCModule         *FindNewOwnerForSharedType(asCTypeInfo *type, asCModule *mod);
	asCModule         *FindNewOwnerForSharedFunc(asCScriptFunction *func, asCModule *mod);

	asCFuncdefType    *FindMatchingFuncdef(asCScriptFunction *func, asCModule *mod);

	int                DetermineNameAndNamespace(const char *in_name, asSNameSpace *implicitNs, asCString &out_name, asSNameSpace *&out_ns) const;
	asCTypeInfo       *GetTemplateSubTypeByName(const asCString &name);

	// Global property management
	asCGlobalProperty *AllocateGlobalProperty();
	void RemoveGlobalProperty(asCGlobalProperty *prop);

	int GetScriptSectionNameIndex(const char *name);

	// Namespace management
	asSNameSpace *AddNameSpace(const char *name);
	asSNameSpace *FindNameSpace(const char *name) const;
	asSNameSpace *GetParentNameSpace(asSNameSpace *ns) const;

//===========================================================
// internal properties
//===========================================================
	asCMemoryMgr memoryMgr;

	asCObjectType   *defaultArrayObjectType;
	asCObjectType    scriptTypeBehaviours;
	asCObjectType    functionBehaviours;

	// Registered interface
	asCArray<asCObjectType *>         registeredObjTypes;      // doesn't increase ref count
	asCArray<asCTypedefType *>        registeredTypeDefs;      // doesn't increase ref count
	asCArray<asCEnumType *>           registeredEnums;         // doesn't increase ref count
	// TODO: memory savings: Since there can be only one property with the same name a simpler symbol table should be used for global props
	asCSymbolTable<asCGlobalProperty> registeredGlobalProps;   // increases ref count
	asCSymbolTable<asCScriptFunction> registeredGlobalFuncs;
	// The template global function instances will be stored in this array
	asCArray<asCScriptFunction*>      generatedTemplateFunctionInstances;  // increases ref count
	// This array contains a list of registered template global functions
	asCArray<asCScriptFunction*>      registeredTemplateGlobalFuncs;
	asCArray<asCFuncdefType *>        registeredFuncDefs;      // doesn't increase ref count
	asCArray<asCObjectType *>         registeredTemplateTypes; // doesn't increase ref count
	asIStringFactory                 *stringFactory;
	asCDataType                       stringType;
	bool configFailed;

	// Stores all registered types
	asCMap<asSNameSpaceNamePair, asCTypeInfo*> allRegisteredTypes; // increases ref count

	// Dummy types used to name the subtypes in the template objects
	asCArray<asCTypeInfo *>        registeredTemplateSubTypes;

	// Store information about template types
	// This list will contain all instances of templates, both registered specialized
	// types and those automacially instantiated from scripts
	asCArray<asCObjectType *>      templateInstanceTypes; // increases ref count

	// Store information about list patterns
	asCArray<asCObjectType *>      listPatternTypes; // increases ref count

	// Stores all global properties, both those registered by application, and those declared by scripts.
	// The id of a global property is the index in this array.
	asCArray<asCGlobalProperty *> globalProperties; // increases ref count
	asCArray<int>                 freeGlobalPropertyIds;

	// This map is used to quickly find a property by its memory address
	// It is used principally during building, cleanup, and garbage detection for script functions
	asCMap<void*, asCGlobalProperty*> varAddressMap; // doesn't increase ref count

	// Stores all functions, i.e. registered functions, script functions, class methods, behaviours, etc.
	asCArray<asCScriptFunction *> scriptFunctions;       // doesn't increase ref count
	asCArray<int>                 freeScriptFunctionIds;
	asCArray<asCScriptFunction *> signatureIds;

	// An array with all module imported functions
	asCArray<sBindInfo *>  importedFunctions; // doesn't increase ref count
	asCArray<int>          freeImportedFunctionIdxs;

	// Synchronized
	mutable asCAtomic      refCount;
	// Synchronized with engineRWLock
	// This array holds all live script modules
	asCArray<asCModule *>  scriptModules;
	// Synchronized with engineRWLock
	// This is a pointer to the last module that was requested. It is used for performance
	// improvement, since it is common that the same module is accessed many times in a row
	asCModule             *lastModule;
	// Synchronized with engineRWLock
	// This flag is true if a script is currently being compiled. It is used to prevent multiple
	// threads from requesting builds at the same time (without blocking)
	bool                   isBuilding;
	// Synchronized with engineRWLock
	// This array holds modules that have been discard (thus are no longer visible to the application)
	// but cannot yet be deleted due to having external references to some of the entities in them
	asCArray<asCModule *>  discardedModules;
	// This flag is set to true during compilations of scripts (or loading pre-compiled scripts)
	// to delay the validation of template types until the subtypes have been fully declared
	bool                   deferValidationOfTemplateTypes;

	// Tokenizer is instantiated once to share resources
	asCTokenizer tok;

	// Stores shared script declared types (classes, interfaces, enums)
	asCArray<asCTypeInfo *> sharedScriptTypes; // increases ref count
	// This array stores the template instances types that have been automatically generated from template types
	asCArray<asCObjectType *> generatedTemplateTypes;
	// Stores the funcdefs
	// TODO: redesign: Only shared funcdefs should be stored here
	//                 a funcdef becomes shared if all arguments and the return type are shared (or application registered)
	asCArray<asCFuncdefType *> funcDefs; // doesn't increases ref count

	// Stores the names of the script sections for debugging purposes
	asCArray<asCString *> scriptSectionNames;

	// Type identifiers
	mutable int                             typeIdSeqNbr;
	mutable asCMap<int, asCTypeInfo*>       mapTypeIdToTypeInfo;

	// Garbage collector
	asCGarbageCollector gc;

	// Dynamic groups
	asCConfigGroup             defaultGroup;
	asCArray<asCConfigGroup*>  configGroups;
	asCConfigGroup            *currentGroup;
	asDWORD                    defaultAccessMask;
	asSNameSpace              *defaultNamespace;

	// Message callback
	bool                        msgCallback;
	asSSystemFunctionInterface  msgCallbackFunc;
	asSFuncPtr                  msgCallbackOriginalFuncPtr;
	asDWORD                     msgCallbackOriginalCallConv;
	void                       *msgCallbackObj;
	struct preMessage_t
	{
		preMessage_t() { isSet = false; r = c = 0; }
		bool      isSet;
		asCString message;
		asCString scriptname;
		int       r;
		int       c;
	} preMessage;

	// JIt compilation
	asIJITCompilerAbstract *jitCompiler;

	// Namespaces
	// These are shared between all entities and are
	// only deleted once the engine is destroyed
	asCArray<asSNameSpace*> nameSpaces;

	// Callbacks for context pooling
	asREQUESTCONTEXTFUNC_t  requestCtxFunc;
	asRETURNCONTEXTFUNC_t   returnCtxFunc;
	void                   *ctxCallbackParam;

	// User data
	asCArray<asPWORD>       userData;

	struct SEngineClean    { asPWORD type; asCLEANENGINEFUNC_t       cleanFunc; };
	asCArray<SEngineClean>    cleanEngineFuncs;
	struct SModuleClean    { asPWORD type; asCLEANMODULEFUNC_t       cleanFunc; };
	asCArray<SModuleClean>    cleanModuleFuncs;
	struct SContextClean   { asPWORD type; asCLEANCONTEXTFUNC_t      cleanFunc; };
	asCArray<SContextClean>   cleanContextFuncs;
	struct SFunctionClean  { asPWORD type; asCLEANFUNCTIONFUNC_t     cleanFunc; };
	asCArray<SFunctionClean>  cleanFunctionFuncs;
	struct STypeInfoClean  { asPWORD type; asCLEANTYPEINFOFUNC_t     cleanFunc; };
	asCArray<STypeInfoClean>  cleanTypeInfoFuncs;
	struct SScriptObjClean { asPWORD type; asCLEANSCRIPTOBJECTFUNC_t cleanFunc; };
	asCArray<SScriptObjClean> cleanScriptObjectFuncs;

	// Synchronization for threads
	DECLAREREADWRITELOCK(mutable engineRWLock)

	// Engine properties
	struct
	{
		bool   allowUnsafeReferences;
		bool   optimizeByteCode;
		bool   copyScriptSections;
		asUINT maximumContextStackSize;
		asUINT initContextStackSize;
		bool   useCharacterLiterals;
		bool   allowMultilineStrings;
		bool   allowImplicitHandleTypes;
		bool   buildWithoutLineCues;
		bool   initGlobalVarsAfterBuild;
		bool   requireEnumScope;
		int    scanner;
		bool   includeJitInstructions;
		int    stringEncoding;
		int    propertyAccessorMode;
		bool   expandDefaultArrayToTemplate;
		bool   autoGarbageCollect;
		bool   disallowGlobalVars;
		asUINT alwaysImplDefaultConstruct;
		int    compilerWarnings;
		bool   disallowValueAssignForRefType;
		// TODO: 3.0.0: Remove the alterSyntaxNamedArgs
		int    alterSyntaxNamedArgs;
		bool   disableIntegerDivision;
		bool   disallowEmptyListElements;
		// TODO: 3.0.0: Remove the privatePropAsProtected
		bool   privatePropAsProtected;
		bool   allowUnicodeIdentifiers;
		int    heredocTrimMode;
		asUINT maxNestedCalls;
		asUINT genericCallMode;
		asUINT initCallStackSize;
		asUINT maxCallStackSize;
		bool   ignoreDuplicateSharedIntf;
		bool   noDebugOutput;
		bool   disableScriptClassGC;
		asUINT jitInterfaceVersion;
		asUINT alwaysImplDefaultCopy;
		asUINT alwaysImplDefaultCopyConstruct;
		asUINT memberInitMode;
		asUINT boolConversionMode;
		bool   foreachSupport;
	} ep;

	// Callbacks
#ifndef AS_NO_EXCEPTIONS
	bool                       translateExceptionCallback;
	asSSystemFunctionInterface translateExceptionCallbackFunc;
	void *                     translateExceptionCallbackObj;
#endif

	// This flag is to allow a quicker shutdown when releasing the engine
	bool shuttingDown;

	// This flag is set when the engine's destructor is called, this is to
	// avoid recursive calls if an object happens to increment/decrement
	// the ref counter during shutdown
	bool inDestructor;
};

END_AS_NAMESPACE

#endif

repo_name: Antidote/openrpgeditor

license: mit


code:
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2025 Andreas Jonsson

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
      must not claim that you wrote the original software. If you use
      this software in a product, an acknowledgment in the product
      documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
      must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
      distribution.

   The original version of this library can be located at:
   http://www.angelcode.com/angelscript/

   Andreas Jonsson
   andreas@angelcode.com
*/

//
// as_scriptfunction.cpp
//
// A container for a compiled script function
//

#include "as_config.h"
#include "as_scriptfunction.h"
#include "as_tokendef.h"
#include "as_scriptengine.h"
#include "as_callfunc.h"
#include "as_bytecode.h"
#include "as_texts.h"
#include "as_scriptnode.h"
#include "as_builder.h"
#include "as_scriptcode.h"

#include <cstdlib> // qsort

BEGIN_AS_NAMESPACE

#if defined(AS_MAX_PORTABILITY) || defined(AS_NO_CLASS_METHODS)

static void ScriptFunction_AddRef_Generic(asIScriptGeneric *gen)
{
	asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
	self->AddRef();
}

static void ScriptFunction_Release_Generic(asIScriptGeneric *gen)
{
	asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
	self->Release();
}

static void ScriptFunction_GetRefCount_Generic(asIScriptGeneric *gen)
{
	asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
	*(int*)gen->GetAddressOfReturnLocation() = self->GetRefCount();
}

static void ScriptFunction_SetFlag_Generic(asIScriptGeneric *gen)
{
	asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
	self->SetFlag();
}

static void ScriptFunction_GetFlag_Generic(asIScriptGeneric *gen)
{
	asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
	*(bool*)gen->GetAddressOfReturnLocation() = self->GetFlag();
}

static void ScriptFunction_EnumReferences_Generic(asIScriptGeneric *gen)
{
	asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
	asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
	self->EnumReferences(engine);
}

static void ScriptFunction_ReleaseAllHandles_Generic(asIScriptGeneric *gen)
{
	asCScriptFunction *self = (asCScriptFunction*)gen->GetObject();
	asIScriptEngine *engine = *(asIScriptEngine**)gen->GetAddressOfArg(0);
	self->ReleaseAllHandles(engine);
}

#ifdef AS_MAX_PORTABILITY
static void ScriptFunction_CreateDelegate_Generic(asIScriptGeneric *gen)
{
	asCScriptFunction *func = (asCScriptFunction*)gen->GetArgAddress(0);
	void *obj = gen->GetArgAddress(1);
	gen->SetReturnAddress(CreateDelegate(func, obj));
}
#endif

// TODO: operator==
/*static void ScriptFunction_opEquals_Generic(asIScriptGeneric *gen)
{
	asCScriptFunction *funcSelf = (asCScriptFunction*)gen->GetObject();
	asCScriptFunction *funcOther = (asCScriptFunction*)gen->GetArgAddress(0);
	*(bool*)gen->GetAddressOfReturnLocation() = *funcSelf == *funcOther;
}
*/

#endif

void RegisterScriptFunction(asCScriptEngine *engine)
{
	// Register the gc behaviours for the script functions
	int r = 0;
	UNUSED_VAR(r); // It is only used in debug mode
	engine->functionBehaviours.engine = engine;
	engine->functionBehaviours.flags = asOBJ_REF | asOBJ_GC;
	engine->functionBehaviours.name = "$func";
#if !defined(AS_MAX_PORTABILITY) && !defined(AS_NO_CLASS_METHODS)
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCScriptFunction,AddRef), asCALL_THISCALL, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCScriptFunction,Release), asCALL_THISCALL, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCScriptFunction,GetRefCount), asCALL_THISCALL, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCScriptFunction,SetFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCScriptFunction,GetFlag), asCALL_THISCALL, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCScriptFunction,EnumReferences), asCALL_THISCALL, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCScriptFunction,ReleaseAllHandles), asCALL_THISCALL, 0); asASSERT( r >= 0 );
	// TODO: Need some way to allow the arg type to adapt when the funcdefs are instantiated
//	r = engine->RegisterMethodToObjectType(&engine->functionBehaviours, "bool opEquals(const int &in)", asMETHOD(asCScriptFunction,operator==), asCALL_THISCALL); asASSERT( r >= 0 );
#else
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(ScriptFunction_AddRef_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(ScriptFunction_Release_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(ScriptFunction_GetRefCount_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(ScriptFunction_SetFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(ScriptFunction_GetFlag_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(ScriptFunction_EnumReferences_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(ScriptFunction_ReleaseAllHandles_Generic), asCALL_GENERIC, 0); asASSERT( r >= 0 );
//	r = engine->RegisterMethodToObjectType(&engine->functionBehaviours, "bool opEquals(const int &in)", asFUNCTION(ScriptFunction_opEquals_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
#endif

	// Register the builtin function for creating delegates
	// This function returns a handle to the delegate, but since the type is not known at this time it is
	// registered to return a void then the return type is changed manually to the builtin function type
	// The name of the function is an invalid identifier so it cannot be invoked accidentally from the script
#ifndef AS_MAX_PORTABILITY
	r = engine->RegisterGlobalFunction("void f(int &in, int &in)", asFUNCTION(CreateDelegate), asCALL_CDECL); asASSERT( r >= 0 );
#else
	r = engine->RegisterGlobalFunction("void f(int &in, int &in)", asFUNCTION(ScriptFunction_CreateDelegate_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
#endif

	// Rename the function so that it cannot be called manually by the script
	int idx = engine->registeredGlobalFuncs.GetIndex(engine->scriptFunctions[r]);
	engine->registeredGlobalFuncs.Erase(idx);
	engine->scriptFunctions[r]->name = DELEGATE_FACTORY;
	engine->registeredGlobalFuncs.Put(engine->scriptFunctions[r]);

	// Change the return type so the VM will know the function really returns a handle
	engine->scriptFunctions[r]->returnType = asCDataType::CreateType(&engine->functionBehaviours, false);
	engine->scriptFunctions[r]->returnType.MakeHandle(true);
}

asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj)
{
	if( func == 0 || obj == 0 )
	{
		// TODO: delegate: Should set script exception
		return 0;
	}

	// Create an instance of a asCScriptFunction with the type asFUNC_DELEGATE
	// The delegate shouldn't have a function id and is not added to the engine->scriptFunctions
	asCScriptFunction *delegate = asNEW(asCScriptFunction)(static_cast<asCScriptEngine*>(func->GetEngine()), 0, asFUNC_DELEGATE);
	if( delegate )
		delegate->MakeDelegate(func, obj);

	return delegate;
}

// internal
void asCScriptFunction::MakeDelegate(asCScriptFunction *func, void *obj)
{
	// Increase the reference of the function and object
	func->AddRef();
	funcForDelegate = func;

	func->GetEngine()->AddRefScriptObject(obj, func->GetObjectType());
	objForDelegate = obj;

	// The return type and parameters are copied from the delegated method to this object
	// TODO: optimize: Do we really need to copy? Whenever requested the delegate can simply return the delegated methods' info directly
	parameterTypes = func->parameterTypes;
	returnType     = func->returnType;
	inOutFlags     = func->inOutFlags;

	// The delegate doesn't own the parameters as it will only forward them to the real method
	// so the exception handler must not clean up the parameters for the delegate
	dontCleanUpOnException = true;
}

// interface
void *asCScriptFunction::GetAuxiliary() const
{
	if (sysFuncIntf)
		return sysFuncIntf->auxiliary;

	return 0;
}

// interface
void *asCScriptFunction::GetDelegateObject() const
{
	return objForDelegate;
}

// interface
asITypeInfo *asCScriptFunction::GetDelegateObjectType() const
{
	if( objForDelegate == 0 || funcForDelegate == 0 )
		return 0;

	return funcForDelegate->objectType;
}

// interface
asIScriptFunction *asCScriptFunction::GetDelegateFunction() const
{
	return funcForDelegate;
}

// TODO: operator==
/*
// internal
bool asCScriptFunction::operator==(const asCScriptFunction &other) const
{
	if( this == &other ) return true;

	if( this->funcType == asFUNC_DELEGATE && other.funcType == asFUNC_DELEGATE )
	{
		if( this->objForDelegate == other.objForDelegate &&
			this->funcForDelegate == other.funcForDelegate )
			return true;
	}

	return false;
}
*/

// internal
int asCScriptFunction::RegisterListPattern(const char *decl, asCScriptNode *listNodes)
{
	if( listNodes == 0 )
		return asINVALID_ARG;

	// Build the representation of the list pattern from the script nodes
	asSListPatternNode *node;
	listPattern = asNEW(asSListPatternNode)(asLPT_START);
	node = listPattern;

	// Recursively parse the child
	int r = ParseListPattern(node, decl, listNodes);

	node->next = asNEW(asSListPatternNode)(asLPT_END);

	return r;
}

// internal
int asCScriptFunction::ParseListPattern(asSListPatternNode *&target, const char *decl, asCScriptNode *listNodes)
{
	asSListPatternNode *node = target;

	listNodes = listNodes->firstChild;
	while( listNodes )
	{
		if( listNodes->nodeType == snIdentifier )
		{
			asCString token(&decl[listNodes->tokenPos], listNodes->tokenLength);
			if( token == "repeat" )
			{
				node->next = asNEW(asSListPatternNode)(asLPT_REPEAT);
				node = node->next;
			}
			else if( token == "repeat_same" )
			{
				// TODO: list: Should make sure this is a sub-list
				node->next = asNEW(asSListPatternNode)(asLPT_REPEAT_SAME);
				node = node->next;
			}
			else
			{
				// Shouldn't happen as the parser already reported the error
				asASSERT(false);
			}
		}
		else if( listNodes->nodeType == snDataType )
		{
			asCDataType dt;
			asCBuilder builder(engine, 0);
			asCScriptCode code;
			code.SetCode("", decl, 0, false);

			// For list factory we get the object type from the return type, for list constructor we get it from the object type directly
			dt = builder.CreateDataTypeFromNode(listNodes, &code, engine->defaultNamespace, false, objectType ? objectType : CastToObjectType(returnType.GetTypeInfo()));

			node->next = asNEW(asSListPatternDataTypeNode)(dt);
			node = node->next;
		}
		else if( listNodes->nodeType == snListPattern )
		{
			node->next = asNEW(asSListPatternNode)(asLPT_START);
			node = node->next;

			// Recursively parse the child
			int r = ParseListPattern(node, decl, listNodes);
			if( r < 0 )
				return r;

			node->next = asNEW(asSListPatternNode)(asLPT_END);
			node = node->next;
		}
		else
		{
			// Unexpected token in the list, the parser shouldn't have allowed
			asASSERT( false );
			return -1;
		}

		listNodes = listNodes->next;
	}

	target = node;
	return 0;
}

// internal
asCScriptFunction::asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType _funcType)
{
	funcType               = _funcType;
	if( funcType == asFUNC_DELEGATE )
	{
		// Delegates behave like object instances, rather than script code
		externalRefCount.set(1);
		internalRefCount.set(0);
	}
	else
	{
		internalRefCount.set(1);
		externalRefCount.set(0);
	}

	this->engine           = engine;
	this->scriptData       = 0;
	module                 = mod;
	objectType             = 0;
	name                   = "";
	sysFuncIntf            = 0;
	signatureId            = 0;
	dontCleanUpOnException = false;
	vfTableIdx             = -1;
	gcFlag                 = false;
	id                     = 0;
	accessMask             = 0xFFFFFFFF;
	nameSpace              = engine->nameSpaces[0];
	objForDelegate         = 0;
	funcForDelegate        = 0;
	listPattern            = 0;
	funcdefType            = 0;

	if( funcType == asFUNC_SCRIPT )
		AllocateScriptFunctionData();

	// Notify the GC of delegates
	if( funcType == asFUNC_DELEGATE )
		engine->gc.AddScriptObjectToGC(this, &engine->functionBehaviours);
}

void asCScriptFunction::AllocateScriptFunctionData()
{
	if( scriptData ) return;

	scriptData = asNEW(ScriptFunctionData);

	scriptData->stackNeeded      = 0;
	scriptData->variableSpace    = 0;
	scriptData->scriptSectionIdx = -1;
	scriptData->declaredAt       = 0;
	scriptData->jitFunction      = 0;
}

void asCScriptFunction::DeallocateScriptFunctionData()
{
	if( !scriptData ) return;

	for( asUINT n = 0; n < scriptData->variables.GetLength(); n++ )
		asDELETE(scriptData->variables[n],asSScriptVariable);
	scriptData->variables.SetLength(0);

	asDELETE(scriptData, ScriptFunctionData);
	scriptData = 0;
}

// internal
asCScriptFunction::~asCScriptFunction()
{
	// Dummy functions that are allocated on the stack are not reference counted
	asASSERT( funcType == asFUNC_DUMMY    ||
		      (externalRefCount.get() == 0 && internalRefCount.get() == 0) );

	// Remove the script function from the engine's scriptFunctions array here
	// Don't remove it before, because there may still be functions referring to it
	// by index until now. If it was removed in DestroyInternal, those would not
	// be able to release the refcount, thus causing memory leak.
	if( engine && id != 0 && funcType != asFUNC_DUMMY )
		engine->RemoveScriptFunction(this);

	// If the engine pointer is 0, then DestroyInternal has already been called and there is nothing more to do
	if( engine == 0 ) return;

	DestroyInternal();

	// Finally set the engine pointer to 0 because it must not be accessed again
	engine = 0;
}

// internal
void asCScriptFunction::DestroyHalfCreated()
{
	asASSERT( externalRefCount.get() == 0 && internalRefCount.get() == 1 );

	// Set the funcType to dummy so the destructor won't complain
	funcType = asFUNC_DUMMY;

	// If the bytecode exist remove it before destroying, otherwise it
	// will fail when the destructor releases the references as the bytecode
	// is not fully constructed.
	if( scriptData )
		scriptData->byteCode.SetLength(0);

	asDELETE(this, asCScriptFunction);
}

// internal
void asCScriptFunction::DestroyInternal()
{
	// Clean up user data
	for( asUINT n = 0; n < userData.GetLength(); n += 2 )
	{
		if( userData[n+1] )
		{
			for( asUINT c = 0; c < engine->cleanFunctionFuncs.GetLength(); c++ )
				if( engine->cleanFunctionFuncs[c].type == userData[n] )
					engine->cleanFunctionFuncs[c].cleanFunc(this);
		}
	}
	userData.SetLength(0);

	// Release all references the function holds to other objects
	ReleaseReferences();
	parameterTypes.SetLength(0);
	returnType = asCDataType::CreatePrimitive(ttVoid, false);

	for( asUINT p = 0; p < defaultArgs.GetLength(); p++ )
		if( defaultArgs[p] )
			asDELETE(defaultArgs[p], asCString);
	defaultArgs.SetLength(0);

	if( sysFuncIntf )
		asDELETE(sysFuncIntf,asSSystemFunctionInterface);
	sysFuncIntf = 0;

	if( objectType )
	{
		objectType->ReleaseInternal();
		objectType = 0;
	}

	DeallocateScriptFunctionData();

	// Deallocate list pattern data
	while( listPattern )
	{
		asSListPatternNode *n = listPattern->next;
		asDELETE(listPattern, asSListPatternNode);
		listPattern = n;
	}

	// Release template sub types
	for (asUINT n = 0; n < templateSubTypes.GetLength(); n++)
		if(templateSubTypes[n].GetTypeInfo())
			templateSubTypes[n].GetTypeInfo()->ReleaseInternal();
}

// interface
int asCScriptFunction::GetId() const
{
	return id;
}

// interface
int asCScriptFunction::AddRef() const
{
	gcFlag = false;
	return externalRefCount.atomicInc();
}

// interface
int asCScriptFunction::Release() const
{
	gcFlag = false;
	int r = externalRefCount.atomicDec();
	if( r == 0 &&
		funcType != asFUNC_DUMMY )    // Dummy functions are allocated on the stack and cannot be deleted
	{
		// There are no more external references, if there are also no
		// internal references then it is time to delete the function
		if( internalRefCount.get() == 0 )
		{
			// If there are no internal references, then no module is owning the function
			// For example if the function was dynamically compiled without adding it to the scope of the module
			asASSERT( module == 0 );

			asDELETE(const_cast<asCScriptFunction*>(this),asCScriptFunction);
		}
	}

	return r;
}

// internal
int asCScriptFunction::AddRefInternal()
{
	return internalRefCount.atomicInc();
}

// internal
int asCScriptFunction::ReleaseInternal()
{
	int r = internalRefCount.atomicDec();
	if( r == 0 &&
		funcType != asFUNC_DUMMY )
	{
		// There are no more internal references, if there are also no
		// external references then it is time to delete the function
		if( externalRefCount.get() == 0 )
		{
			asDELETE(const_cast<asCScriptFunction*>(this),asCScriptFunction);
		}
	}

	return r;
}

// interface
int asCScriptFunction::GetTypeId() const
{
	// This const cast is ok, the object won't be modified
	asCDataType dt = asCDataType::CreateType(engine->FindMatchingFuncdef(const_cast<asCScriptFunction*>(this), 0), false);
	return engine->GetTypeIdFromDataType(dt);
}

// interface
bool asCScriptFunction::IsCompatibleWithTypeId(int typeId) const
{
	asCDataType dt = engine->GetDataTypeFromTypeId(typeId);

	// Make sure the type is a function
	if (!dt.IsFuncdef())
		return false;

	asCScriptFunction *func = CastToFuncdefType(dt.GetTypeInfo())->funcdef;
	if( !IsSignatureExceptNameEqual(func) )
		return false;

	// If this is a class method, then only return true if the object type is the same
	if( objectType != func->objectType )
		return false;

	return true;
}

// interface
const char *asCScriptFunction::GetModuleName() const
{
	if( module )
		return module->GetName();

	return 0;
}

// interface
asIScriptModule *asCScriptFunction::GetModule() const
{
	return module;
}

// interface
asITypeInfo *asCScriptFunction::GetObjectType() const
{
	return objectType;
}

// interface
const char *asCScriptFunction::GetObjectName() const
{
	if( objectType )
		return objectType->GetName();

	return 0;
}

// interface
const char *asCScriptFunction::GetName() const
{
	return name.AddressOf();
}

// interface
const char *asCScriptFunction::GetNamespace() const
{
	if (nameSpace)
		return nameSpace->name.AddressOf();

	return 0;
}

// interface
bool asCScriptFunction::IsReadOnly() const
{
	return traits.GetTrait(asTRAIT_CONST);
}

// interface
bool asCScriptFunction::IsPrivate() const
{
	return traits.GetTrait(asTRAIT_PRIVATE);
}

// interface
bool asCScriptFunction::IsProtected() const
{
	return traits.GetTrait(asTRAIT_PROTECTED);
}

// internal
int asCScriptFunction::GetSpaceNeededForArguments()
{
	// We need to check the size for each type
	int s = 0;
	for( asUINT n = 0; n < parameterTypes.GetLength(); n++ )
		s += parameterTypes[n].GetSizeOnStackDWords();

	return s;
}

// internal
int asCScriptFunction::GetSpaceNeededForReturnValue()
{
	return returnType.GetSizeOnStackDWords();
}

// internal
bool asCScriptFunction::DoesReturnOnStack() const
{
	if( returnType.GetTypeInfo() &&
		(returnType.GetTypeInfo()->flags & asOBJ_VALUE) &&
		!returnType.IsReference() )
		return true;

	return false;
}

// internal
asCString asCScriptFunction::GetDeclarationStr(bool includeObjectName, bool includeNamespace, bool includeParamNames) const
{
	asCString str;

	// TODO: default arg: Make the declaration with the default args an option

	// Don't add the return type for constructors and destructors
	if( !(returnType.GetTokenType() == ttVoid &&
		  objectType &&
		  (name == objectType->name || (name.GetLength() > 0 && name[0] == '~') ||
		   name == "$beh0" || name == "$beh2")) )
	{
		str = returnType.Format(nameSpace, includeNamespace);
		str += " ";
	}
	if( objectType && includeObjectName )
	{
		asCDataType dt = asCDataType::CreateType(objectType, false);
		str += dt.Format(nameSpace, includeNamespace);
		str += "::";
	}
	else if (funcdefType && funcdefType->parentClass && includeObjectName)
	{
		asCDataType dt = asCDataType::CreateType(funcdefType->parentClass, false);
		str += dt.Format(nameSpace, includeNamespace);
		str += "::";
	}
	else if( includeNamespace && nameSpace->name != "" && !objectType && !(funcdefType && funcdefType->parentClass) )
	{
		str += nameSpace->name + "::";
	}
	if( name == "" )
		str += "_unnamed_function_(";
	else if( name.SubString(0,4) == "$beh" && name.GetLength() == 5 )
	{
		if( name[4] == '0' + asBEHAVE_CONSTRUCT )
			str += objectType->name + "(";
		else if( name[4] == '0' + asBEHAVE_FACTORY )
			str += returnType.GetTypeInfo()->name + "(";
		else if( name[4] == '0' + asBEHAVE_DESTRUCT )
			str += "~" + objectType->name + "(";
		else
			str += name + "(";
	}
	else
	{
		if (funcType == asFUNC_TEMPLATE)
		{
			str += name + "<";
			for (asUINT t = 0; t < templateSubTypes.GetLength()-1; t++)
				str += templateSubTypes[t].GetTypeInfo()->name + ",";
			str += templateSubTypes[templateSubTypes.GetLength() - 1].GetTypeInfo()->name;
			str += ">(";
		}
		else
			str += name + "(";
	}

	if( parameterTypes.GetLength() > 0 )
	{
		asUINT n;
		for( n = 0; n < parameterTypes.GetLength() - 1; n++ )
		{
			str += parameterTypes[n].Format(nameSpace, includeNamespace);
			if( parameterTypes[n].IsReference() && inOutFlags.GetLength() > n )
			{
				if( inOutFlags[n] == asTM_INREF ) str += "in";
				else if( inOutFlags[n] == asTM_OUTREF ) str += "out";
				else if( inOutFlags[n] == asTM_INOUTREF ) str += "inout";
			}

			if( includeParamNames && n < parameterNames.GetLength() && parameterNames[n].GetLength() != 0 )
			{
				str += " ";
				str += parameterNames[n];
			}

			if( defaultArgs.GetLength() > n && defaultArgs[n] )
			{
				asCString tmp;
				tmp.Format(" = %s", defaultArgs[n]->AddressOf());
				str += tmp;
			}

			str += ", ";
		}

		// Add the last parameter
		str += parameterTypes[n].Format(nameSpace, includeNamespace);
		if( parameterTypes[n].IsReference() && inOutFlags.GetLength() > n )
		{
			if( inOutFlags[n] == asTM_INREF ) str += "in";
			else if( inOutFlags[n] == asTM_OUTREF ) str += "out";
			else if( inOutFlags[n] == asTM_INOUTREF ) str += "inout";
		}

		if( includeParamNames && n < parameterNames.GetLength() && parameterNames[n].GetLength() != 0 )
		{
			str += " ";
			str += parameterNames[n];
		}

		if( defaultArgs.GetLength() > n && defaultArgs[n] )
		{
			asCString tmp;
			tmp.Format(" = %s", defaultArgs[n]->AddressOf());
			str += tmp;
		}
	}

	if (IsVariadic())
	{
		str += "...";
	}

	str += ")";

	if( IsReadOnly() )
		str += " const";

	// Add the declaration of the list pattern
	if( listPattern )
	{
		asSListPatternNode *n = listPattern;
		bool first = true;
		while( n )
		{
			if( n->type == asLPT_START )
			{
				str += " {";
				first = true;
			}
			else if( n->type == asLPT_END )
			{
				str += " }";
				first = false;
			}
			else if( n->type == asLPT_REPEAT )
				str += " repeat";
			else if( n->type == asLPT_REPEAT_SAME )
				str += " repeat_same";
			else if( n->type == asLPT_TYPE )
			{
				if( first )
				{
					str += " ";
					first = false;
				}
				else
					str += ", ";
				str += reinterpret_cast<asSListPatternDataTypeNode*>(n)->dataType.Format(nameSpace, includeNamespace);
			}

			n = n->next;
		}
	}

	return str;
}

// interface
int asCScriptFunction::FindNextLineWithCode(int line) const
{
	if( scriptData == 0 ) return -1;
	if( scriptData->lineNumbers.GetLength() == 0 ) return -1;

	// The line numbers for constructors are not in order due to the way
	// class members can be initialized directly in the declaration
	if( objectType && objectType->name == name )
	{
		// Sort all line numbers before looking for the next
		asCArray<int> lineNbrs;
		for( asUINT n = 1; n < scriptData->lineNumbers.GetLength(); n += 2 )
			lineNbrs.PushLast(scriptData->lineNumbers[n]&0xFFFFF);

		struct C
		{
			static int cmp(const void *a, const void *b) { return *(int*)a - *(int*)b; }
		};
		std::qsort(&lineNbrs[0], lineNbrs.GetLength(), sizeof(int), C::cmp);

		if( line < lineNbrs[0] && line < (scriptData->declaredAt&0xFFFFF)) return -1;
		if( line > lineNbrs[lineNbrs.GetLength()-1] ) return -1;

		// Find the line with code on or right after the input line
		// TODO: optimize: Do binary search
		for( asUINT n = 0; n < lineNbrs.GetLength(); n++ )
			if( line <= lineNbrs[n] )
				return lineNbrs[n];
	}
	else
	{
		// Check if given line is outside function
		if( line < (scriptData->declaredAt&0xFFFFF) ) return -1;
		if( line > (scriptData->lineNumbers[scriptData->lineNumbers.GetLength()-1]&0xFFFFF) ) return -1;

		// Find the line with code on or right after the input line
		// TODO: optimize: Do binary search instead
		for( asUINT n = 1; n < scriptData->lineNumbers.GetLength(); n += 2 )
		{
			if( line <= (scriptData->lineNumbers[n]&0xFFFFF) )
				return (scriptData->lineNumbers[n]&0xFFFFF);
		}
	}

	return -1;
}

// interface
int asCScriptFunction::GetDeclaredAt(const char** scriptSection, int* row, int* col) const
{
	if (!scriptData)
	{
		if (scriptSection) *scriptSection = 0;
		if (row) *row = 0;
		if (col) *col = 0;
		return asNOT_SUPPORTED;
	}
	if (scriptSection) *scriptSection = engine->scriptSectionNames[scriptData->scriptSectionIdx]->AddressOf();
	if (row) *row = scriptData->declaredAt & 0xFFFFF;
	if (col) *col = scriptData->declaredAt >> 20;
	return 0;
}

// internal
int asCScriptFunction::GetLineNumber(int programPosition, int *sectionIdx)
{
	asASSERT( scriptData );

	if( sectionIdx ) *sectionIdx = scriptData->scriptSectionIdx;
	if( scriptData->lineNumbers.GetLength() == 0 ) return 0;

	if( sectionIdx )
	{
		// Find the correct section index if the function is compiled from multiple sections
		// This array will be empty most of the time so we don't need a sofisticated algorithm to search it
		for( asUINT n = 0; n < scriptData->sectionIdxs.GetLength(); n += 2 )
		{
			if( scriptData->sectionIdxs[n] <= programPosition )
				*sectionIdx = scriptData->sectionIdxs[n+1];
		}
	}

	// Do a binary search in the buffer
	int max = (int)scriptData->lineNumbers.GetLength()/2 - 1;
	int min = 0;
	int i = max/2;

	for(;;)
	{
		if( scriptData->lineNumbers[i*2] < programPosition )
		{
			// Have we found the largest number < programPosition?
			if( max == i ) return scriptData->lineNumbers[i*2+1];
			if( scriptData->lineNumbers[i*2+2] > programPosition ) return scriptData->lineNumbers[i*2+1];

			min = i + 1;
			i = (max + min)/2;
		}
		else if( scriptData->lineNumbers[i*2] > programPosition )
		{
			// Have we found the smallest number > programPosition?
			if( min == i ) return scriptData->lineNumbers[i*2+1];

			max = i - 1;
			i = (max + min)/2;
		}
		else
		{
			// We found the exact position
			return scriptData->lineNumbers[i*2+1];
		}
	}
}

// interface
asEFuncType asCScriptFunction::GetFuncType() const
{
	return funcType;
}

// interface
asUINT asCScriptFunction::GetVarCount() const
{
	if( scriptData )
		return asUINT(scriptData->variables.GetLength());
	return 0;
}

// interface
int asCScriptFunction::GetVar(asUINT index, const char **out_name, int *out_typeId) const
{
	if( scriptData == 0 )
		return asNOT_SUPPORTED;
	if( index >= scriptData->variables.GetLength() )
		return asINVALID_ARG;

	if( out_name )
		*out_name = scriptData->variables[index]->name.AddressOf();
	if( out_typeId )
		*out_typeId = engine->GetTypeIdFromDataType(scriptData->variables[index]->type);

	return asSUCCESS;
}

// interface
const char *asCScriptFunction::GetVarDecl(asUINT index, bool includeNamespace) const
{
	if( scriptData == 0 || index >= scriptData->variables.GetLength() )
		return 0;

	asCString *tempString = &asCThreadManager::GetLocalData()->string;
	*tempString = scriptData->variables[index]->type.Format(nameSpace, includeNamespace);
	*tempString += " " + scriptData->variables[index]->name;

	return tempString->AddressOf();
}

// internal
void asCScriptFunction::AddVariable(const asCString &in_name, const asCDataType &in_type, int in_stackOffset, bool in_onHeap)
{
	asASSERT( scriptData );
	asSScriptVariable *var = asNEW(asSScriptVariable);
	if( var == 0 )
	{
		// Out of memory
		return;
	}
	var->name                 = in_name;
	var->type                 = in_type;
	var->stackOffset          = in_stackOffset;
	var->onHeap               = in_onHeap;
	var->declaredAtProgramPos = 0;
	scriptData->variables.PushLast(var);
}

// internal
asCTypeInfo *asCScriptFunction::GetTypeInfoOfLocalVar(short varOffset)
{
	asASSERT( scriptData );

	for( asUINT n = 0; n < scriptData->variables.GetLength(); n++ )
	{
		if (scriptData->variables[n]->stackOffset == varOffset)
			return scriptData->variables[n]->type.GetTypeInfo();
	}

	return 0;
}

// internal
void asCScriptFunction::ComputeSignatureId()
{
	// This function will compute the signatureId based on the
	// function name, return type, and parameter types. The object
	// type for methods is not used, so that class methods and
	// interface methods match each other.
	for( asUINT n = 0; n < engine->signatureIds.GetLength(); n++ )
	{
		if( !IsSignatureEqual(engine->signatureIds[n]) ) continue;

		// We don't need to increment the reference counter here, because
		// asCScriptEngine::FreeScriptFunctionId will maintain the signature
		// id as the function is freed.
		signatureId = engine->signatureIds[n]->signatureId;
		return;
	}

	signatureId = id;
	engine->signatureIds.PushLast(this);
}

// internal
bool asCScriptFunction::IsSignatureEqual(const asCScriptFunction *func) const
{
	if( name != func->name || !IsSignatureExceptNameEqual(func) ) return false;

	return true;
}

// internal
bool asCScriptFunction::IsSignatureExceptNameEqual(const asCScriptFunction *func) const
{
	return IsSignatureExceptNameEqual(func->returnType, func->parameterTypes, func->inOutFlags, func->objectType, func->IsReadOnly());
}

// internal
bool asCScriptFunction::IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &paramInOut, const asCObjectType *objType, bool readOnly) const
{
	if( this->returnType != retType ) return false;

	return IsSignatureExceptNameAndReturnTypeEqual(paramTypes, paramInOut, objType, readOnly);
}

// internal
bool asCScriptFunction::IsSignatureExceptNameAndObjectTypeEqual(const asCScriptFunction *func) const
{
	return IsSignatureExceptNameEqual(func->returnType, func->parameterTypes, func->inOutFlags, objectType, IsReadOnly());
}

// internal
bool asCScriptFunction::IsSignatureExceptNameAndReturnTypeEqual(const asCScriptFunction *func) const
{
	return IsSignatureExceptNameAndReturnTypeEqual(func->parameterTypes, func->inOutFlags, func->objectType, func->IsReadOnly());
}

// internal
bool asCScriptFunction::IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &paramInOut, const asCObjectType *objType, bool readOnly) const
{
	if( this->IsReadOnly()      != readOnly       ) return false;
	if( (this->objectType != 0) != (objType != 0) ) return false;
	if( this->inOutFlags        != paramInOut     ) return false;
	if( this->parameterTypes    != paramTypes     ) return false;

	return true;
}

// internal
void asCScriptFunction::AddReferences()
{
	// This array will be used to make sure we only add the reference to the same resource once
	// This is especially important for global variables, as it expects the initialization function
	// to hold only one reference to the variable. However, if the variable is initialized through
	// the default constructor followed by the assignment operator we will have two references to
	// the variable in the function.
	asCArray<void*> ptrs;

	// Only count references if there is any bytecode
	if( scriptData && scriptData->byteCode.GetLength() )
	{
		if( returnType.GetTypeInfo() )
		{
			returnType.GetTypeInfo()->AddRefInternal();

			asCConfigGroup *group = engine->FindConfigGroupForTypeInfo(returnType.GetTypeInfo());
			if( group != 0 ) group->AddRef();
		}

		for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
			if( parameterTypes[p].GetTypeInfo() )
			{
				parameterTypes[p].GetTypeInfo()->AddRefInternal();

				asCConfigGroup *group = engine->FindConfigGroupForTypeInfo(parameterTypes[p].GetTypeInfo());
				if( group != 0 ) group->AddRef();
			}

		for (asUINT v = 0; v < scriptData->variables.GetLength(); v++)
		{
			asCTypeInfo* ti = reinterpret_cast<asCTypeInfo*>(scriptData->variables[v]->type.GetTypeInfo());
			if (ti) // The null handle is also stored, but it doesn't have an object type
			{
				ti->AddRefInternal();

				asCConfigGroup* group = engine->FindConfigGroupForTypeInfo(ti);
				if (group != 0) group->AddRef();
			}
		}

		// Go through the byte code and add references to all resources used by the function
		asCArray<asDWORD> &bc = scriptData->byteCode;
		for( asUINT n = 0; n < bc.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&bc[n]].type] )
		{
			switch( *(asBYTE*)&bc[n] )
			{
			// Object types
			case asBC_OBJTYPE:
			case asBC_FREE:
			case asBC_REFCPY:
			case asBC_RefCpyV:
				{
					asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
					asASSERT( objType );
					if( objType )
						objType->AddRefInternal();
				}
				break;

			// Object type and function
			case asBC_ALLOC:
				{
					asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
					asASSERT( objType );
					if( objType )
						objType->AddRefInternal();

					int funcId = asBC_INTARG(&bc[n]+AS_PTR_SIZE);
					if( funcId )
						engine->scriptFunctions[funcId]->AddRefInternal();
				}
				break;

			// Global variables
			case asBC_PGA:
			case asBC_PshGPtr:
			case asBC_LDG:
			case asBC_PshG4:
			case asBC_LdGRdR4:
			case asBC_CpyGtoV4:
			case asBC_CpyVtoG4:
			case asBC_SetG4:
				// Need to increase the reference for each global variable
				{
					void *gvarPtr = (void*)asBC_PTRARG(&bc[n]);
					if( !gvarPtr ) break;
					asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);

					if (!prop)
					{
						// The pointer is a string constant. In order to make sure the correct resource
						// management is maintained we request a new string constant here, so the compiler
						// or bytecode loader can release its copy afterwards.
						asCString str;
						asUINT length;
						int r = engine->stringFactory->GetRawStringData(gvarPtr, 0, &length);
						if (r >= 0)
						{
							str.SetLength(length);
							engine->stringFactory->GetRawStringData(gvarPtr, str.AddressOf(), &length);

							// Get a new pointer (depending on the string factory implementation it may actually be the same)
							gvarPtr = const_cast<void*>(engine->stringFactory->GetStringConstant(str.AddressOf(), length));
							asBC_PTRARG(&bc[n]) = (asPWORD)gvarPtr;
						}

						// If we get an error from the string factory there is not
						// anything we can do about it, except report a message.
						// TODO: NEWSTRING: Write a message and then exit gracefully
						asASSERT(r >= 0);
						break;
					}

					// Only addref the properties once
					if( !ptrs.Exists(gvarPtr) )
					{
						prop->AddRef();
						ptrs.PushLast(gvarPtr);
					}

					asCConfigGroup *group = engine->FindConfigGroupForGlobalVar(prop->id);
					if( group != 0 ) group->AddRef();
				}
				break;

			// System functions
			case asBC_CALLSYS:
				{
					int funcId = asBC_INTARG(&bc[n]);
					asCConfigGroup *group = engine->FindConfigGroupForFunction(funcId);
					if( group != 0 ) group->AddRef();

					asASSERT( funcId > 0 );
					if( funcId > 0 )
						engine->scriptFunctions[funcId]->AddRefInternal();
				}
				break;

			// Functions
			case asBC_CALL:
			case asBC_CALLINTF:
				{
					int funcId = asBC_INTARG(&bc[n]);
					asASSERT( funcId > 0 );
					if( funcId > 0 )
						engine->scriptFunctions[funcId]->AddRefInternal();
				}
				break;

			// Function pointers
			case asBC_FuncPtr:
				{
					asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&bc[n]);
					asASSERT( func );
					if( func )
						func->AddRefInternal();
				}
				break;
			}
		}
	}
}

// internal
void asCScriptFunction::ReleaseReferences()
{
	asCArray<void*> ptrs;

	// Only count references if there is any bytecode
	if( scriptData && scriptData->byteCode.GetLength() )
	{
		if( returnType.GetTypeInfo() )
		{
			returnType.GetTypeInfo()->ReleaseInternal();

			asCConfigGroup *group = engine->FindConfigGroupForTypeInfo(returnType.GetTypeInfo());
			if( group != 0 ) group->Release();
		}

		for( asUINT p = 0; p < parameterTypes.GetLength(); p++ )
			if( parameterTypes[p].GetTypeInfo() )
			{
				parameterTypes[p].GetTypeInfo()->ReleaseInternal();

				asCConfigGroup *group = engine->FindConfigGroupForTypeInfo(parameterTypes[p].GetTypeInfo());
				if( group != 0 ) group->Release();
			}

		for (asUINT v = 0; v < scriptData->variables.GetLength(); v++)
		{
			asCTypeInfo* ti = reinterpret_cast<asCTypeInfo*>(scriptData->variables[v]->type.GetTypeInfo());
			if (ti) // The null handle is also stored, but it doesn't have an object type
			{
				ti->ReleaseInternal();

				asCConfigGroup* group = engine->FindConfigGroupForTypeInfo(ti);
				if (group != 0) group->Release();
			}
		}

		// Go through the byte code and release references to all resources used by the function
		asCArray<asDWORD> &bc = scriptData->byteCode;
		for( asUINT n = 0; n < bc.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE*)&bc[n]].type] )
		{
			switch( *(asBYTE*)&bc[n] )
			{
			// Object types
			case asBC_OBJTYPE:
			case asBC_FREE:
			case asBC_REFCPY:
			case asBC_RefCpyV:
				{
					asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
					if( objType )
						objType->ReleaseInternal();
				}
				break;

			// Object type and function
			case asBC_ALLOC:
				{
					asCObjectType *objType = (asCObjectType*)asBC_PTRARG(&bc[n]);
					if( objType )
						objType->ReleaseInternal();

					int funcId = asBC_INTARG(&bc[n]+AS_PTR_SIZE);
					if( funcId > 0 )
					{
						asCScriptFunction *fptr = engine->scriptFunctions[funcId];
						if( fptr )
							fptr->ReleaseInternal();

						// The engine may have been forced to destroy the function internals early
						// and this may will make it impossible to find the function by id anymore.
						// This should only happen if the engine is released while the application
						// is still keeping functions alive.
						// TODO: Fix this possible memory leak
					}
				}
				break;

			// Global variables
			case asBC_PGA:
			case asBC_PshGPtr:
			case asBC_LDG:
			case asBC_PshG4:
			case asBC_LdGRdR4:
			case asBC_CpyGtoV4:
			case asBC_CpyVtoG4:
			case asBC_SetG4:
				// Need to increase the reference for each global variable
				{
					void *gvarPtr = (void*)asBC_PTRARG(&bc[n]);
					if( !gvarPtr ) break;
					asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);

					if (!prop)
					{
						// The pointer is a string constant, so it needs to be released by the string factory
						int r = engine->stringFactory->ReleaseStringConstant(gvarPtr);
						UNUSED_VAR(r);

						// If we get an error from the string factory there is not
						// anything we can do about it, except report a message.
						// TODO: Write a message showing that the string couldn't be
						//       released. Include the first 10 characters and the length
						//       to make it easier to identify which string it was
						asASSERT(r >= 0);
						break;
					}

					// Only release the properties once
					if( !ptrs.Exists(gvarPtr) )
					{
						prop->Release();
						ptrs.PushLast(gvarPtr);
					}

					asCConfigGroup *group = engine->FindConfigGroupForGlobalVar(prop->id);
					if( group != 0 ) group->Release();
				}
				break;

			// System functions
			case asBC_CALLSYS:
				{
					int funcId = asBC_INTARG(&bc[n]);
					asCConfigGroup *group = engine->FindConfigGroupForFunction(funcId);
					if( group != 0 ) group->Release();

					if( funcId )
					{
						asCScriptFunction *fptr = engine->scriptFunctions[funcId];
						if( fptr )
							fptr->ReleaseInternal();
					}
				}
				break;

			// Functions
			case asBC_CALL:
			case asBC_CALLINTF:
				{
					int funcId = asBC_INTARG(&bc[n]);
					if( funcId )
					{
						asCScriptFunction *fptr = engine->scriptFunctions[funcId];
						if( fptr )
							fptr->ReleaseInternal();

						// The engine may have been forced to destroy the function internals early
						// and this may will make it impossible to find the function by id anymore.
						// This should only happen if the engine is released while the application
						// is still keeping functions alive.
						// TODO: Fix this possible memory leak
					}
				}
				break;

			// Function pointers
			case asBC_FuncPtr:
				{
					asCScriptFunction *func = (asCScriptFunction*)asBC_PTRARG(&bc[n]);
					if( func )
						func->ReleaseInternal();
				}
				break;
			}
		}

		// Release the jit compiled function
		if (scriptData->jitFunction)
		{
			if (engine->ep.jitInterfaceVersion == 1)
				static_cast<asIJITCompiler*>(engine->jitCompiler)->ReleaseJITFunction(scriptData->jitFunction);
			else if (engine->ep.jitInterfaceVersion == 2)
				static_cast<asIJITCompilerV2*>(engine->jitCompiler)->CleanFunction(this, scriptData->jitFunction);
		}
		scriptData->jitFunction = 0;
	}

	// Delegate
	if( objForDelegate )
		engine->ReleaseScriptObject(objForDelegate, funcForDelegate->GetObjectType());
	objForDelegate = 0;
	if( funcForDelegate )
		funcForDelegate->Release();
	funcForDelegate = 0;
}

// interface
int asCScriptFunction::GetReturnTypeId(asDWORD *flags) const
{
	if( flags )
	{
		if( returnType.IsReference() )
		{
			*flags = asTM_INOUTREF;
			*flags |= returnType.IsReadOnly() ? asTM_CONST : 0;
		}
		else
			*flags = asTM_NONE;
	}

	return engine->GetTypeIdFromDataType(returnType);
}

// interface
asUINT asCScriptFunction::GetParamCount() const
{
	return (asUINT)parameterTypes.GetLength();
}

// interface
int asCScriptFunction::GetParam(asUINT index, int *out_typeId, asDWORD *out_flags, const char **out_name, const char **out_defaultArg) const
{
	if( index >= parameterTypes.GetLength() )
		return asINVALID_ARG;

	if( out_typeId )
		*out_typeId = engine->GetTypeIdFromDataType(parameterTypes[index]);

	if( out_flags )
	{
		*out_flags = inOutFlags[index];
		*out_flags |= parameterTypes[index].IsReadOnly() ? asTM_CONST : 0;
	}

	if( out_name )
	{
		// The parameter names are not stored if loading from bytecode without debug information
		if( index < parameterNames.GetLength() )
			*out_name = parameterNames[index].AddressOf();
		else
			*out_name = 0;
	}

	if( out_defaultArg )
	{
		if( index < defaultArgs.GetLength() && defaultArgs[index] )
			*out_defaultArg = defaultArgs[index]->AddressOf();
		else
			*out_defaultArg = 0;
	}

	return asSUCCESS;
}

// interface
asIScriptEngine *asCScriptFunction::GetEngine() const
{
	return engine;
}

// interface
const char *asCScriptFunction::GetDeclaration(bool includeObjectName, bool includeNamespace, bool includeParamNames) const
{
	asCString str = GetDeclarationStr(includeObjectName, includeNamespace, includeParamNames);
	asCString* tempString = &asCThreadManager::GetLocalData()->string;
	*tempString = str;
	return tempString->AddressOf();
}

// interface
const char *asCScriptFunction::GetScriptSectionName() const
{
	if( scriptData && scriptData->scriptSectionIdx >= 0 )
		return engine->scriptSectionNames[scriptData->scriptSectionIdx]->AddressOf();

	return 0;
}

// interface
const char *asCScriptFunction::GetConfigGroup() const
{
	asCConfigGroup *group = 0;
	if( funcType != asFUNC_FUNCDEF )
		group = engine->FindConfigGroupForFunction(id);
	else
		group = engine->FindConfigGroupForFuncDef(this->funcdefType);

	if( group == 0 )
		return 0;

	return group->groupName.AddressOf();
}

// interface
asDWORD asCScriptFunction::GetAccessMask() const
{
	return accessMask;
}

// interface
int asCScriptFunction::SetJITFunction(asJITFunction jitFunc)
{
	if (engine->ep.jitInterfaceVersion == 1)
		return asNOT_SUPPORTED;

	if (funcType != asFUNC_SCRIPT)
		return asERROR;

	if (scriptData->jitFunction && scriptData->jitFunction != jitFunc )
	{
		if (engine->ep.jitInterfaceVersion == 2)
			static_cast<asIJITCompilerV2*>(engine->jitCompiler)->CleanFunction(this, scriptData->jitFunction);
		scriptData->jitFunction = 0;
	}

	scriptData->jitFunction = jitFunc;

	return asSUCCESS;
}

// interface
asJITFunction asCScriptFunction::GetJITFunction() const
{
	if (scriptData)
		return scriptData->jitFunction;

	return 0;
}

// internal
void asCScriptFunction::JITCompile()
{
	if( funcType != asFUNC_SCRIPT )
		return;

	asASSERT( scriptData );

	if( !engine->jitCompiler )
		return;

	// Make sure the function has been compiled with JitEntry instructions
	// For functions that has JitEntry this will be a quick test
	asUINT length;
	asDWORD *byteCode = GetByteCode(&length);
	asDWORD *end = byteCode + length;
	bool foundJitEntry = false;
	while( byteCode < end )
	{
		// Determine the instruction
		asEBCInstr op = asEBCInstr(*(asBYTE*)byteCode);
		if( op == asBC_JitEntry )
		{
			foundJitEntry = true;
			break;
		}

		// Move to next instruction
		byteCode += asBCTypeSize[asBCInfo[op].type];
	}

	if( !foundJitEntry )
	{
		asCString msg;
		msg.Format(TXT_NO_JIT_IN_FUNC_s, GetDeclaration());
		engine->WriteMessage("", 0, 0, asMSGTYPE_WARNING, msg.AddressOf());
	}

	// Release the previous function, if any
	if( scriptData->jitFunction )
	{
		if (engine->ep.jitInterfaceVersion == 1)
			static_cast<asIJITCompiler*>(engine->jitCompiler)->ReleaseJITFunction(scriptData->jitFunction);
		else if (engine->ep.jitInterfaceVersion == 2)
			static_cast<asIJITCompilerV2*>(engine->jitCompiler)->CleanFunction(this, scriptData->jitFunction);
		scriptData->jitFunction = 0;
	}

	if (engine->ep.jitInterfaceVersion == 1)
	{
		// Compile for native system. If the JIT compiler decides to compile the function it
		// must do so now, as it is not allowed to do it later.
		int r = static_cast<asIJITCompiler*>(engine->jitCompiler)->CompileFunction(this, &scriptData->jitFunction);
		if (r < 0)
			asASSERT(scriptData->jitFunction == 0);
	}
	else if (engine->ep.jitInterfaceVersion == 2)
	{
		// Notify the JIT compiler about the new function, it may do JIT compilation now, or later, or not at all.
		// If it does the compilation now, it will use SetJITFunction to inform the compiled JIT function.
		static_cast<asIJITCompilerV2*>(engine->jitCompiler)->NewFunction(this);
	}
}

// interface
asDWORD *asCScriptFunction::GetByteCode(asUINT *length)
{
	if( scriptData == 0 ) return 0;

	if( length )
		*length = (asUINT)scriptData->byteCode.GetLength();

	if( scriptData->byteCode.GetLength() )
		return scriptData->byteCode.AddressOf();

	return 0;
}

// interface
void *asCScriptFunction::SetUserData(void *data, asPWORD type)
{
	// As a thread might add a new new user data at the same time as another
	// it is necessary to protect both read and write access to the userData member
	ACQUIREEXCLUSIVE(engine->engineRWLock);

	// It is not intended to store a lot of different types of userdata,
	// so a more complex structure like a associative map would just have
	// more overhead than a simple array.
	for( asUINT n = 0; n < userData.GetLength(); n += 2 )
	{
		if( userData[n] == type )
		{
			void *oldData = reinterpret_cast<void*>(userData[n+1]);
			userData[n+1] = reinterpret_cast<asPWORD>(data);

			RELEASEEXCLUSIVE(engine->engineRWLock);

			return oldData;
		}
	}

	userData.PushLast(type);
	userData.PushLast(reinterpret_cast<asPWORD>(data));

	RELEASEEXCLUSIVE(engine->engineRWLock);

	return 0;
}

// interface
void *asCScriptFunction::GetUserData(asPWORD type) const
{
	// There may be multiple threads reading, but when
	// setting the user data nobody must be reading.
	ACQUIRESHARED(engine->engineRWLock);

	for( asUINT n = 0; n < userData.GetLength(); n += 2 )
	{
		if( userData[n] == type )
		{
			RELEASESHARED(engine->engineRWLock);
			return reinterpret_cast<void*>(userData[n+1]);
		}
	}

	RELEASESHARED(engine->engineRWLock);

	return 0;
}

// internal
// TODO: cleanup: This method should probably be a member of the engine
asCGlobalProperty *asCScriptFunction::GetPropertyByGlobalVarPtr(void *gvarPtr)
{
	asSMapNode<void*, asCGlobalProperty*> *node;
	if( engine->varAddressMap.MoveTo(&node, gvarPtr) )
	{
		asASSERT(gvarPtr == node->value->GetAddressOfValue());
		return node->value;
	}
	return 0;
}

// internal
int asCScriptFunction::GetRefCount()
{
	asASSERT( funcType == asFUNC_DELEGATE );

	return externalRefCount.get();
}

// internal
void asCScriptFunction::SetFlag()
{
	gcFlag = true;
}

// internal
bool asCScriptFunction::GetFlag()
{
	return gcFlag;
}

// internal
void asCScriptFunction::EnumReferences(asIScriptEngine *)
{
	asASSERT( funcType == asFUNC_DELEGATE );

	// Delegate
	if( objForDelegate )
		engine->GCEnumCallback(objForDelegate);
}

// internal
void asCScriptFunction::ReleaseAllHandles(asIScriptEngine *)
{
	asASSERT( funcType == asFUNC_DELEGATE );

	// Release paramaters

	// Delegate
	if( objForDelegate )
		engine->ReleaseScriptObject(objForDelegate, funcForDelegate->GetObjectType());
	objForDelegate = 0;
}

// interface
bool asCScriptFunction::IsShared() const
{
	// All system functions are shared
	if( funcType == asFUNC_SYSTEM ) return true;

	// All class methods for shared classes are also shared
	asASSERT( objectType == 0 || objectType->engine == engine || objectType->engine == 0 );
	if( objectType && (objectType->flags & asOBJ_SHARED) ) return true;

	// funcdefs that are registered by the application are shared
	if (funcType == asFUNC_FUNCDEF && module == 0) return true;

	// Functions that have been specifically marked as shared are shared
	return traits.GetTrait(asTRAIT_SHARED);
}

// interface
bool asCScriptFunction::IsFinal() const
{
	return traits.GetTrait(asTRAIT_FINAL);
}

// interface
bool asCScriptFunction::IsOverride() const
{
	return traits.GetTrait(asTRAIT_OVERRIDE);
}

// interface
bool asCScriptFunction::IsExplicit() const
{
	return traits.GetTrait(asTRAIT_EXPLICIT);
}

// interface
bool asCScriptFunction::IsProperty() const
{
	return traits.GetTrait(asTRAIT_PROPERTY);
}

// interface
bool asCScriptFunction::IsVariadic() const
{
	return traits.GetTrait(asTRAIT_VARIADIC);
}

// internal
bool asCScriptFunction::IsFactory() const
{
	if (objectType) return false;

	asCObjectType* type = CastToObjectType(returnType.GetTypeInfo());
	if (type == 0) return false;

	if (type->name != name) return false;

	if (type->nameSpace != nameSpace) return false;

	return true;
}

// internal
asCScriptFunction* asCScriptFunction::FindNextFunctionCalled(asUINT startSearchFromProgramPos, int *outStackDelta, asUINT *outProgramPos)
{
	if (scriptData == 0)
		return 0;

	if (outProgramPos)
		*outProgramPos = startSearchFromProgramPos;

	// Find out which function that will be called
	asCScriptFunction* calledFunc = 0;
	int stackDelta = 0;
	for (asUINT n = startSearchFromProgramPos; n < scriptData->byteCode.GetLength(); )
	{
		asBYTE bc = *(asBYTE*)&scriptData->byteCode[n];
		if (bc == asBC_CALL ||
			bc == asBC_CALLSYS ||
			bc == asBC_Thiscall1 ||
			bc == asBC_CALLINTF ||
			bc == asBC_ALLOC ||
			bc == asBC_CALLBND ||
			bc == asBC_CallPtr)
		{
			calledFunc = GetCalledFunction(n);

			if (outProgramPos)
				*outProgramPos = n + asBCTypeSize[asBCInfo[bc].type];

			break;
		}

		// Keep track of the stack size between the
		// instruction that needs to be adjusted and the call
		stackDelta += asBCInfo[bc].stackInc;

		n += asBCTypeSize[asBCInfo[bc].type];
	}

	if (outStackDelta)
		*outStackDelta = stackDelta;

	return calledFunc;
}

// internal
asCScriptFunction* asCScriptFunction::GetCalledFunction(asDWORD programPos)
{
	if (scriptData == 0)
		return 0;

	asBYTE bc = *(asBYTE*)&scriptData->byteCode[programPos];

	if (bc == asBC_CALL ||
		bc == asBC_CALLSYS ||
		bc == asBC_Thiscall1 ||
		bc == asBC_CALLINTF)
	{
		// Find the function from the function id in bytecode
		int funcId = asBC_INTARG(&scriptData->byteCode[programPos]);
		return engine->scriptFunctions[funcId];
	}
	else if (bc == asBC_ALLOC)
	{
		// Find the function from the function id in the bytecode
		int funcId = asBC_INTARG(&scriptData->byteCode[programPos + AS_PTR_SIZE]);
		return engine->scriptFunctions[funcId];
	}
	else if (bc == asBC_CALLBND)
	{
		// Find the function from the engine's bind array
		int funcId = asBC_INTARG(&scriptData->byteCode[programPos]);
		return engine->importedFunctions[funcId & ~FUNC_IMPORTED]->importedFunctionSignature;
	}
	else if (bc == asBC_CallPtr)
	{
		asUINT v;
		int var = asBC_SWORDARG0(&scriptData->byteCode[programPos]);

		// Find the funcdef from the local variable
		for (v = 0; v < scriptData->variables.GetLength(); v++)
			if (scriptData->variables[v]->stackOffset == var)
				return CastToFuncdefType(scriptData->variables[v]->type.GetTypeInfo())->funcdef;

		// Look in parameters
		int paramPos = 0;
		if (objectType)
			paramPos -= AS_PTR_SIZE;
		if (DoesReturnOnStack())
			paramPos -= AS_PTR_SIZE;
		for (v = 0; v < parameterTypes.GetLength(); v++)
		{
			if (var == paramPos)
			{
				if (parameterTypes[v].IsFuncdef())
					return CastToFuncdefType(parameterTypes[v].GetTypeInfo())->funcdef;
				else
					return 0;
			}
			paramPos -= parameterTypes[v].GetSizeOnStackDWords();
		}
	}

	return 0;
}

// interface
asUINT asCScriptFunction::GetSubTypeCount() const
{
	return asUINT(templateSubTypes.GetLength());
}

// interface
int asCScriptFunction::GetSubTypeId(asUINT subtypeIndex) const
{
	// This method is only supported for templates and template specializations
	if (templateSubTypes.GetLength() == 0)
		return asERROR;

	if (subtypeIndex >= templateSubTypes.GetLength())
		return asINVALID_ARG;

	return engine->GetTypeIdFromDataType(templateSubTypes[subtypeIndex]);
}

// interface
asITypeInfo* asCScriptFunction::GetSubType(asUINT subtypeIndex) const
{
	if (subtypeIndex >= templateSubTypes.GetLength())
		return 0;

	return templateSubTypes[subtypeIndex].GetTypeInfo();
}

END_AS_NAMESPACE


code:
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2024 Andreas Jonsson

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
      must not claim that you wrote the original software. If you use
      this software in a product, an acknowledgment in the product
      documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
      must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
      distribution.

   The original version of this library can be located at:
   http://www.angelcode.com/angelscript/

   Andreas Jonsson
   andreas@angelcode.com
*/

//
// as_scriptfunction.h
//
// A container for a compiled script function
//

#ifndef AS_SCRIPTFUNCTION_H
#define AS_SCRIPTFUNCTION_H

#include "as_config.h"
#include "as_string.h"
#include "as_array.h"
#include "as_datatype.h"
#include "as_atomic.h"

BEGIN_AS_NAMESPACE

class asCScriptEngine;
class asCModule;
class asCConfigGroup;
class asCGlobalProperty;
class asCScriptNode;
class asCFuncdefType;
struct asSNameSpace;

struct asSScriptVariable
{
	asCString   name;
	asCDataType type;
	int         stackOffset;
	asUINT      onHeap : 1;
	asUINT      declaredAtProgramPos : 31;
};

enum asEListPatternNodeType
{
	asLPT_REPEAT,
	asLPT_REPEAT_SAME,
	asLPT_START,
	asLPT_END,
	asLPT_TYPE
};

struct asSListPatternNode
{
	asSListPatternNode(asEListPatternNodeType t) : type(t), next(0) {}
	virtual ~asSListPatternNode() {};
	virtual asSListPatternNode *Duplicate() { return asNEW(asSListPatternNode)(type); }
	asEListPatternNodeType  type;
	asSListPatternNode     *next;
};

struct asSListPatternDataTypeNode : public asSListPatternNode
{
	asSListPatternDataTypeNode(const asCDataType &dt) : asSListPatternNode(asLPT_TYPE), dataType(dt) {}
	asSListPatternNode *Duplicate() { return asNEW(asSListPatternDataTypeNode)(dataType); }
	asCDataType dataType;
};

enum asEObjVarInfoOption
{
	asOBJ_UNINIT,	// object is uninitialized/destroyed
	asOBJ_INIT,		// object is initialized
	asBLOCK_BEGIN,	// scope block begins
	asBLOCK_END,	// scope block ends
	asOBJ_VARDECL	// object variable is declared (but not necessarily initialized)
};

enum asEFuncTrait
{
	asTRAIT_CONSTRUCTOR = 1<<0,  // method
	asTRAIT_DESTRUCTOR  = 1<<1,  // method
	asTRAIT_CONST       = 1<<2,  // method
	asTRAIT_PRIVATE     = 1<<3,  // method
	asTRAIT_PROTECTED   = 1<<4,  // method
	asTRAIT_FINAL       = 1<<5,  // method
	asTRAIT_OVERRIDE    = 1<<6,  // method
	asTRAIT_SHARED      = 1<<7,  // function
	asTRAIT_EXTERNAL    = 1<<8,  // function
	asTRAIT_EXPLICIT    = 1<<9,  // method
	asTRAIT_PROPERTY    = 1<<10, // method/function
	asTRAIT_DELETED     = 1<<11, // method
	asTRAIT_VARIADIC    = 1<<12  // method/function
};

struct asSFunctionTraits
{
	asSFunctionTraits() : traits(0) {}
	void SetTrait(asEFuncTrait trait, bool set) { if (set) traits |= trait; else traits &= ~trait; }
	bool GetTrait(asEFuncTrait trait) const { return (traits & trait) ? true : false; }
protected:
	asDWORD traits;
};

struct asSObjectVariableInfo
{
	asUINT              programPos;
	int                 variableOffset;
	asEObjVarInfoOption option;
};

struct asSTryCatchInfo
{
	asUINT tryPos;
	asUINT catchPos;
};

struct asSSystemFunctionInterface;

// TODO: Might be interesting to allow enumeration of accessed global variables, and
//       also functions/methods that are being called. This could be used to build a
//       code database with call graphs, etc.

void RegisterScriptFunction(asCScriptEngine *engine);

class asCScriptFunction : public asIScriptFunction
{
public:
	// From asIScriptFunction
	asIScriptEngine     *GetEngine() const;

	// Memory management
	int AddRef() const;
	int Release() const;

	// Miscellaneous
	int                  GetId() const;
	asEFuncType          GetFuncType() const;
	const char          *GetModuleName() const;
	asIScriptModule     *GetModule() const;
	const char          *GetScriptSectionName() const;
	const char          *GetConfigGroup() const;
	asDWORD              GetAccessMask() const;
	void                *GetAuxiliary() const;

	// Function signature
	asITypeInfo         *GetObjectType() const;
	const char          *GetObjectName() const;
	const char          *GetName() const;
	const char          *GetNamespace() const;
	const char          *GetDeclaration(bool includeObjectName = true, bool includeNamespace = false, bool includeParamNames = false) const;
	bool                 IsReadOnly() const;
	bool                 IsPrivate() const;
	bool                 IsProtected() const;
	bool                 IsFinal() const;
	bool                 IsOverride() const;
	bool                 IsShared() const;
	bool                 IsExplicit() const;
	bool                 IsProperty() const;
	bool                 IsVariadic() const;
	asUINT               GetParamCount() const;
	int                  GetParam(asUINT index, int *typeId, asDWORD *flags = 0, const char **name = 0, const char **defaultArg = 0) const;
	int                  GetReturnTypeId(asDWORD *flags = 0) const;

	// Template functions
	asUINT       GetSubTypeCount() const;
	int          GetSubTypeId(asUINT subTypeIndex = 0) const;
	asITypeInfo* GetSubType(asUINT subTypeIndex = 0) const;

	// Type id for function pointers
	int                  GetTypeId() const;
	bool                 IsCompatibleWithTypeId(int typeId) const;

	// Delegates
	void                *GetDelegateObject() const;
	asITypeInfo         *GetDelegateObjectType() const;
	asIScriptFunction   *GetDelegateFunction() const;

	// Debug information
	asUINT               GetVarCount() const;
	int                  GetVar(asUINT index, const char **name, int *typeId = 0) const;
	const char *         GetVarDecl(asUINT index, bool includeNamespace = false) const;
	int                  FindNextLineWithCode(int line) const;
	int                  GetDeclaredAt(const char** scriptSection, int* row, int* col) const;

	// For JIT compilation
	asDWORD *            GetByteCode(asUINT *length = 0);
	int                  SetJITFunction(asJITFunction jitFunc);
	asJITFunction        GetJITFunction() const;

	// User data
	void                *SetUserData(void *userData, asPWORD type);
	void                *GetUserData(asPWORD type) const;

public:
	//-----------------------------------
	// Internal methods

	void SetShared(bool set) { traits.SetTrait(asTRAIT_SHARED, set); }
	void SetReadOnly(bool set) { traits.SetTrait(asTRAIT_CONST, set); }
	void SetFinal(bool set) { traits.SetTrait(asTRAIT_FINAL, set); }
	void SetOverride(bool set) { traits.SetTrait(asTRAIT_OVERRIDE, set); }
	void SetExplicit(bool set) { traits.SetTrait(asTRAIT_EXPLICIT, set); }
	void SetProtected(bool set) { traits.SetTrait(asTRAIT_PROTECTED, set); }
	void SetPrivate(bool set) { traits.SetTrait(asTRAIT_PRIVATE, set); }
	void SetProperty(bool set) { traits.SetTrait(asTRAIT_PROPERTY, set); }
	void SetVariadic(bool set) { traits.SetTrait(asTRAIT_VARIADIC, set); }
	bool IsFactory() const;

	asCScriptFunction(asCScriptEngine *engine, asCModule *mod, asEFuncType funcType);
	~asCScriptFunction();

	// Keep an internal reference counter to separate references coming from
	// application or script objects and references coming from the script code
	int AddRefInternal();
	int ReleaseInternal();

	void     DestroyHalfCreated();

	// TODO: operator==
	// TODO: The asIScriptFunction should provide operator== and operator!= that should do a
	//       a value comparison. Two delegate objects that point to the same object and class method should compare as equal
	// TODO: The operator== should also be provided in script as opEquals to allow the same comparison in script
	//       To do this we'll need some way to adapt the argtype for opEquals for each funcdef, preferrably without instantiating lots of different methods
	//       Perhaps reusing 'auto' to mean the same type as the object
	//bool      operator==(const asCScriptFunction &other) const;

	void      DestroyInternal();

	void      AddVariable(const asCString &name, const asCDataType &type, int stackOffset, bool onHeap);

	int       GetSpaceNeededForArguments();
	int       GetSpaceNeededForReturnValue();
	asCString GetDeclarationStr(bool includeObjectName = true, bool includeNamespace = false, bool includeParamNames = false) const;
	int       GetLineNumber(int programPosition, int *sectionIdx);
	void      ComputeSignatureId();
	bool      IsSignatureEqual(const asCScriptFunction *func) const;
	bool      IsSignatureExceptNameEqual(const asCScriptFunction *func) const;
	bool      IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
	bool      IsSignatureExceptNameAndReturnTypeEqual(const asCScriptFunction *fun) const;
	bool      IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &inOutFlags, const asCObjectType *type, bool isReadOnly) const;
	bool      IsSignatureExceptNameAndObjectTypeEqual(const asCScriptFunction *func) const;

	asCTypeInfo *GetTypeInfoOfLocalVar(short varOffset);

	void      MakeDelegate(asCScriptFunction *func, void *obj);

	int       RegisterListPattern(const char *decl, asCScriptNode *listPattern);
	int       ParseListPattern(asSListPatternNode *&target, const char *decl, asCScriptNode *listPattern);

	bool      DoesReturnOnStack() const;

	void      JITCompile();

	void      AddReferences();
	void      ReleaseReferences();

	void      AllocateScriptFunctionData();
	void      DeallocateScriptFunctionData();

	asCScriptFunction* FindNextFunctionCalled(asUINT startSearchFromProgramPos, int *stackDelta, asUINT *outProgramPos);
	asCScriptFunction* GetCalledFunction(asDWORD programPos);

	asCGlobalProperty *GetPropertyByGlobalVarPtr(void *gvarPtr);

	// GC methods (for delegates)
	int  GetRefCount();
	void SetFlag();
	bool GetFlag();
	void EnumReferences(asIScriptEngine *engine);
	void ReleaseAllHandles(asIScriptEngine *engine);

	// Don't allow the script function to be copied
	asCScriptFunction(const asCScriptFunction&) = delete;

public:
	//-----------------------------------
	// Properties

	mutable asCAtomic            externalRefCount; // Used for external referneces
	        asCAtomic            internalRefCount; // Used for internal references
	mutable bool                 gcFlag;
	asCScriptEngine             *engine;
	asCModule                   *module;

	asCArray<asPWORD>            userData;

	// Function signature
	asCString                    name;
	asCDataType                  returnType;
	asCArray<asCDataType>        parameterTypes;
	asCArray<asCDataType>        templateSubTypes; // Increase ref of template subtypes
	asCArray<asCString>          parameterNames;
	asCArray<asETypeModifiers>   inOutFlags;
	asCArray<asCString *>        defaultArgs;
	asSFunctionTraits            traits;
	asCObjectType               *objectType;
	int                          signatureId;

	int                          id;

	asEFuncType                  funcType;
	asDWORD                      accessMask;

	// Namespace will be null for funcdefs that are declared as child funcdefs
	// of a class. In this case the namespace shall be taken from the parentClass
	// in the funcdefType
	asSNameSpace                *nameSpace;

	asCFuncdefType              *funcdefType; // Doesn't increase refCount

	// Used by asFUNC_DELEGATE
	void              *objForDelegate;
	asCScriptFunction *funcForDelegate;

	// Used by list factory behaviour
	asSListPatternNode *listPattern;

	// Used by asFUNC_SCRIPT
	struct ScriptFunctionData
	{
		// Bytecode for the script function
		asCArray<asDWORD>               byteCode;

		// The stack space needed for the local variables
		asDWORD                         variableSpace;

		// Holds information on scope for object variables on the stack
		asCArray<asSObjectVariableInfo> objVariableInfo;

		// Holds information on try/catch blocks for exception handling
		asCArray<asSTryCatchInfo>       tryCatchInfo;

		// The stack needed to execute the function
		int                             stackNeeded;

		// JIT compiled code of this function
		asJITFunction                   jitFunction;

		// Holds type information on both explicitly declared variables and temporary variables
		// Used during exception handling, byte code serialization, debugging, and context serialization
		asCArray<asSScriptVariable*>    variables;

		// Store position, line number pairs for debug information
		asCArray<int>                   lineNumbers;
		// Store the script section where the code was declared
		int                             scriptSectionIdx;
		// Store the location where the function was declared  (row in the lower 20 bits, and column in the upper 12)
		int                             declaredAt;
		// Store position/index pairs if the bytecode is compiled from multiple script sections
		asCArray<int>                   sectionIdxs;
	};
	ScriptFunctionData          *scriptData;

	// Stub functions and delegates don't own the object and parameters
	bool                         dontCleanUpOnException;

	// Used by asFUNC_VIRTUAL
	int                          vfTableIdx;

	// Used by asFUNC_SYSTEM
	asSSystemFunctionInterface  *sysFuncIntf;
};

const char * const DELEGATE_FACTORY = "$dlgte";
asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj);

END_AS_NAMESPACE

#endif


code:
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2015 Andreas Jonsson

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
      must not claim that you wrote the original software. If you use
      this software in a product, an acknowledgment in the product
      documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
      must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
      distribution.

   The original version of this library can be located at:
   http://www.angelcode.com/angelscript/

   Andreas Jonsson
   andreas@angelcode.com
*/

//
// as_scriptnode.cpp
//
// A node in the script tree built by the parser for compilation
//

#include "as_scriptnode.h"
#include "as_scriptengine.h"

BEGIN_AS_NAMESPACE

asCScriptNode::asCScriptNode(eScriptNode type)
{
	nodeType    = type;
	tokenType   = ttUnrecognizedToken;
	tokenPos    = 0;
	tokenLength = 0;

	parent      = 0;
	next        = 0;
	prev        = 0;
	firstChild  = 0;
	lastChild   = 0;
}

void asCScriptNode::Destroy(asCScriptEngine *engine)
{
	// Destroy all children
	asCScriptNode *node = firstChild;
	asCScriptNode *nxt;

	while( node )
	{
		nxt = node->next;
		node->Destroy(engine);
		node = nxt;
	}

	// Return the memory to the memory manager
	engine->memoryMgr.FreeScriptNode(this);
}

asCScriptNode *asCScriptNode::CreateCopy(asCScriptEngine *engine)
{
	void *ptr = engine->memoryMgr.AllocScriptNode();
	if( ptr == 0 )
	{
		// Out of memory
		return 0;
	}

	new(ptr) asCScriptNode(nodeType);

	asCScriptNode *node = reinterpret_cast<asCScriptNode*>(ptr);
	node->tokenLength = tokenLength;
	node->tokenPos    = tokenPos;
	node->tokenType   = tokenType;

	asCScriptNode *child = firstChild;
	while( child )
	{
		node->AddChildLast(child->CreateCopy(engine));
		child = child->next;
	}

	return node;
}

void asCScriptNode::SetToken(sToken *token)
{
	tokenType   = token->type;
}

void asCScriptNode::UpdateSourcePos(size_t pos, size_t length)
{
	if( pos == 0 && length == 0 ) return;

	if( tokenPos == 0 && tokenLength == 0 )
	{
		tokenPos = pos;
		tokenLength = length;
	}
	else
	{
		if( tokenPos > pos )
		{
			tokenLength = tokenPos + tokenLength - pos;
			tokenPos = pos;
		}

		if( pos + length > tokenPos + tokenLength )
		{
			tokenLength = pos + length - tokenPos;
		}
	}
}

void asCScriptNode::AddChildLast(asCScriptNode *node)
{
	// We might get a null pointer if the parser encounter an out-of-memory situation
	if( node == 0 ) return;

	if( lastChild )
	{
		lastChild->next = node;
		node->next      = 0;
		node->prev      = lastChild;
		node->parent    = this;
		lastChild       = node;
	}
	else
	{
		firstChild   = node;
		lastChild    = node;
		node->next   = 0;
		node->prev   = 0;
		node->parent = this;
	}

	UpdateSourcePos(node->tokenPos, node->tokenLength);
}

void asCScriptNode::DisconnectParent()
{
	if( parent )
	{
		if( parent->firstChild == this )
			parent->firstChild = next;
		if( parent->lastChild == this )
			parent->lastChild = prev;
	}

	if( next )
		next->prev = prev;

	if( prev )
		prev->next = next;

	parent = 0;
	next = 0;
	prev = 0;
}

END_AS_NAMESPACE