as_scriptengine.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2012 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_scriptengine.h
  25. //
  26. // The implementation of the script engine interface
  27. //
  28. #ifndef AS_SCRIPTENGINE_H
  29. #define AS_SCRIPTENGINE_H
  30. #include "as_config.h"
  31. #include "as_atomic.h"
  32. #include "as_scriptfunction.h"
  33. #include "as_array.h"
  34. #include "as_datatype.h"
  35. #include "as_objecttype.h"
  36. #include "as_module.h"
  37. #include "as_restore.h"
  38. #include "as_callfunc.h"
  39. #include "as_configgroup.h"
  40. #include "as_memory.h"
  41. #include "as_gc.h"
  42. #include "as_tokenizer.h"
  43. BEGIN_AS_NAMESPACE
  44. class asCBuilder;
  45. class asCContext;
  46. // TODO: import: Remove this when import is removed
  47. struct sBindInfo;
  48. // TODO: DiscardModule should take an optional pointer to asIScriptModule instead of module name. If null, nothing is done.
  49. // TODO: Should have a CreateModule/GetModule instead of just GetModule with parameters.
  50. // TODO: Should allow enumerating modules, in case they have not been named.
  51. class asCScriptEngine : public asIScriptEngine
  52. {
  53. //=============================================================
  54. // From asIScriptEngine
  55. //=============================================================
  56. public:
  57. // Memory management
  58. virtual int AddRef() const;
  59. virtual int Release() const;
  60. // Engine properties
  61. virtual int SetEngineProperty(asEEngineProp property, asPWORD value);
  62. virtual asPWORD GetEngineProperty(asEEngineProp property) const;
  63. // Compiler messages
  64. virtual int SetMessageCallback(const asSFuncPtr &callback, void *obj, asDWORD callConv);
  65. virtual int ClearMessageCallback();
  66. virtual int WriteMessage(const char *section, int row, int col, asEMsgType type, const char *message);
  67. // JIT Compiler
  68. virtual int SetJITCompiler(asIJITCompiler *compiler);
  69. virtual asIJITCompiler *GetJITCompiler() const;
  70. // Global functions
  71. virtual int RegisterGlobalFunction(const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  72. virtual asUINT GetGlobalFunctionCount() const;
  73. virtual int GetGlobalFunctionIdByIndex(asUINT index) const;
  74. virtual asIScriptFunction *GetGlobalFunctionByIndex(asUINT index) const;
  75. virtual asIScriptFunction *GetGlobalFunctionByDecl(const char *declaration) const;
  76. // Global properties
  77. virtual int RegisterGlobalProperty(const char *declaration, void *pointer);
  78. virtual asUINT GetGlobalPropertyCount() const;
  79. 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;
  80. virtual int GetGlobalPropertyIndexByName(const char *name) const;
  81. virtual int GetGlobalPropertyIndexByDecl(const char *decl) const;
  82. // Type registration
  83. virtual int RegisterObjectType(const char *obj, int byteSize, asDWORD flags);
  84. virtual int RegisterObjectProperty(const char *obj, const char *declaration, int byteOffset);
  85. virtual int RegisterObjectMethod(const char *obj, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  86. virtual int RegisterObjectBehaviour(const char *obj, asEBehaviours behaviour, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  87. virtual int RegisterInterface(const char *name);
  88. virtual int RegisterInterfaceMethod(const char *intf, const char *declaration);
  89. virtual asUINT GetObjectTypeCount() const;
  90. virtual asIObjectType *GetObjectTypeByIndex(asUINT index) const;
  91. virtual asIObjectType *GetObjectTypeByName(const char *name) const;
  92. // String factory
  93. virtual int RegisterStringFactory(const char *datatype, const asSFuncPtr &factoryFunc, asDWORD callConv);
  94. virtual int GetStringFactoryReturnTypeId() const;
  95. // Default array type
  96. virtual int RegisterDefaultArrayType(const char *type);
  97. virtual int GetDefaultArrayTypeId() const;
  98. // Enums
  99. virtual int RegisterEnum(const char *type);
  100. virtual int RegisterEnumValue(const char *type, const char *name, int value);
  101. virtual asUINT GetEnumCount() const;
  102. virtual const char *GetEnumByIndex(asUINT index, int *enumTypeId, const char **nameSpace, const char **configGroup = 0, asDWORD *accessMask = 0) const;
  103. virtual int GetEnumValueCount(int enumTypeId) const;
  104. virtual const char *GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const;
  105. // Funcdefs
  106. virtual int RegisterFuncdef(const char *decl);
  107. virtual asUINT GetFuncdefCount() const;
  108. virtual asIScriptFunction *GetFuncdefByIndex(asUINT index) const;
  109. // Typedefs
  110. virtual int RegisterTypedef(const char *type, const char *decl);
  111. virtual asUINT GetTypedefCount() const;
  112. virtual const char *GetTypedefByIndex(asUINT index, int *typeId, const char **nameSpace, const char **configGroup = 0, asDWORD *accessMask = 0) const;
  113. // Configuration groups
  114. virtual int BeginConfigGroup(const char *groupName);
  115. virtual int EndConfigGroup();
  116. virtual int RemoveConfigGroup(const char *groupName);
  117. virtual asDWORD SetDefaultAccessMask(asDWORD defaultMask);
  118. virtual int SetDefaultNamespace(const char *nameSpace);
  119. // Script modules
  120. virtual asIScriptModule *GetModule(const char *module, asEGMFlags flag);
  121. virtual int DiscardModule(const char *module);
  122. // Script functions
  123. virtual asIScriptFunction *GetFunctionById(int funcId) const;
  124. // Type identification
  125. virtual asIObjectType *GetObjectTypeById(int typeId) const;
  126. virtual int GetTypeIdByDecl(const char *decl) const;
  127. virtual const char *GetTypeDeclaration(int typeId, bool includeNamespace = false) const;
  128. virtual int GetSizeOfPrimitiveType(int typeId) const;
  129. // Script execution
  130. virtual asIScriptContext *CreateContext();
  131. virtual void *CreateScriptObject(int typeId);
  132. virtual void *CreateScriptObjectCopy(void *obj, int typeId);
  133. virtual void CopyScriptObject(void *dstObj, void *srcObj, int typeId);
  134. virtual void ReleaseScriptObject(void *obj, int typeId);
  135. virtual void ReleaseScriptObject(void *obj, const asIObjectType *type);
  136. virtual void AddRefScriptObject(void *obj, int typeId);
  137. virtual void AddRefScriptObject(void *obj, const asIObjectType *type);
  138. virtual bool IsHandleCompatibleWithObject(void *obj, int objTypeId, int handleTypeId) const;
  139. // String interpretation
  140. virtual asETokenClass ParseToken(const char *string, size_t stringLength = 0, int *tokenLength = 0) const;
  141. // Garbage collection
  142. virtual int GarbageCollect(asDWORD flags = asGC_FULL_CYCLE);
  143. virtual void GetGCStatistics(asUINT *currentSize, asUINT *totalDestroyed, asUINT *totalDetected, asUINT *newObjects, asUINT *totalNewDestroyed) const;
  144. virtual void NotifyGarbageCollectorOfNewObject(void *obj, asIObjectType *type);
  145. virtual void GCEnumCallback(void *reference);
  146. // User data
  147. virtual void *SetUserData(void *data);
  148. virtual void *GetUserData() const;
  149. virtual void SetEngineUserDataCleanupCallback(asCLEANENGINEFUNC_t callback);
  150. virtual void SetModuleUserDataCleanupCallback(asCLEANMODULEFUNC_t callback);
  151. virtual void SetContextUserDataCleanupCallback(asCLEANCONTEXTFUNC_t callback);
  152. virtual void SetFunctionUserDataCleanupCallback(asCLEANFUNCTIONFUNC_t callback);
  153. virtual void SetObjectTypeUserDataCleanupCallback(asCLEANOBJECTTYPEFUNC_t callback);
  154. //===========================================================
  155. // internal methods
  156. //===========================================================
  157. public:
  158. asCScriptEngine();
  159. virtual ~asCScriptEngine();
  160. //protected:
  161. friend class asCBuilder;
  162. friend class asCCompiler;
  163. friend class asCContext;
  164. friend class asCDataType;
  165. friend class asCModule;
  166. friend class asCRestore;
  167. friend class asCByteCode;
  168. friend int PrepareSystemFunction(asCScriptFunction *func, asSSystemFunctionInterface *internal, asCScriptEngine *engine);
  169. int RegisterMethodToObjectType(asCObjectType *objectType, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  170. int RegisterBehaviourToObjectType(asCObjectType *objectType, asEBehaviours behaviour, const char *decl, const asSFuncPtr &funcPointer, asDWORD callConv);
  171. int VerifyVarTypeNotInFunction(asCScriptFunction *func);
  172. void *CallAlloc(asCObjectType *objType) const;
  173. void CallFree(void *obj) const;
  174. void *CallGlobalFunctionRetPtr(int func);
  175. void *CallGlobalFunctionRetPtr(int func, void *param1);
  176. void *CallGlobalFunctionRetPtr(asSSystemFunctionInterface *func, asCScriptFunction *desc);
  177. void *CallGlobalFunctionRetPtr(asSSystemFunctionInterface *i, asCScriptFunction *s, void *param1);
  178. void CallObjectMethod(void *obj, int func);
  179. void CallObjectMethod(void *obj, void *param, int func);
  180. void CallObjectMethod(void *obj, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  181. void CallObjectMethod(void *obj, void *param, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  182. bool CallObjectMethodRetBool(void *obj, int func);
  183. int CallObjectMethodRetInt(void *obj, int func);
  184. void CallGlobalFunction(void *param1, void *param2, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  185. bool CallGlobalFunctionRetBool(void *param1, void *param2, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  186. void ConstructScriptObjectCopy(void *mem, void *obj, asCObjectType *type);
  187. void ClearUnusedTypes();
  188. void RemoveTemplateInstanceType(asCObjectType *t);
  189. void RemoveTypeAndRelatedFromList(asCArray<asCObjectType*> &types, asCObjectType *ot);
  190. asCConfigGroup *FindConfigGroupForFunction(int funcId) const;
  191. asCConfigGroup *FindConfigGroupForGlobalVar(int gvarId) const;
  192. asCConfigGroup *FindConfigGroupForObjectType(const asCObjectType *type) const;
  193. asCConfigGroup *FindConfigGroupForFuncDef(const asCScriptFunction *funcDef) const;
  194. int RequestBuild();
  195. void BuildCompleted();
  196. void PrepareEngine();
  197. bool isPrepared;
  198. int CreateContext(asIScriptContext **context, bool isInternal);
  199. asCObjectType *GetObjectType(const char *type);
  200. int AddBehaviourFunction(asCScriptFunction &func, asSSystemFunctionInterface &internal);
  201. asCString GetFunctionDeclaration(int funcId);
  202. asCScriptFunction *GetScriptFunction(int funcId) const;
  203. asCModule *GetModule(const char *name, bool create);
  204. asCModule *GetModuleFromFuncId(int funcId);
  205. int GetMethodIdByDecl(const asCObjectType *ot, const char *decl, asCModule *mod);
  206. int GetFactoryIdByDecl(const asCObjectType *ot, const char *decl);
  207. int GetNextScriptFunctionId();
  208. void SetScriptFunction(asCScriptFunction *func);
  209. void FreeScriptFunctionId(int id);
  210. int ConfigError(int err, const char *funcName, const char *arg1, const char *arg2);
  211. int GetTypeIdFromDataType(const asCDataType &dt) const;
  212. asCDataType GetDataTypeFromTypeId(int typeId) const;
  213. asCObjectType *GetObjectTypeFromTypeId(int typeId) const;
  214. void RemoveFromTypeIdMap(asCObjectType *type);
  215. bool IsTemplateType(const char *name) const;
  216. asCObjectType *GetTemplateInstanceType(asCObjectType *templateType, asCDataType &subType);
  217. asCScriptFunction *GenerateTemplateFactoryStub(asCObjectType *templateType, asCObjectType *templateInstanceType, int origFactoryId);
  218. bool GenerateNewTemplateFunction(asCObjectType *templateType, asCObjectType *templateInstanceType, asCDataType &subType, asCScriptFunction *templateFunc, asCScriptFunction **newFunc);
  219. // String constants
  220. // TODO: Must free unused string constants, thus the ref count for each must be tracked
  221. int AddConstantString(const char *str, size_t length);
  222. const asCString &GetConstantString(int id);
  223. // Global property management
  224. asCGlobalProperty *AllocateGlobalProperty();
  225. void FreeUnusedGlobalProperties();
  226. int GetScriptSectionNameIndex(const char *name);
  227. //===========================================================
  228. // internal properties
  229. //===========================================================
  230. asCMemoryMgr memoryMgr;
  231. int initialContextStackSize;
  232. asCObjectType *defaultArrayObjectType;
  233. asCObjectType scriptTypeBehaviours;
  234. asCObjectType functionBehaviours;
  235. asCObjectType objectTypeBehaviours;
  236. asCObjectType globalPropertyBehaviours;
  237. // Registered interface
  238. asCArray<asCObjectType *> registeredObjTypes;
  239. asCArray<asCObjectType *> registeredTypeDefs;
  240. asCArray<asCObjectType *> registeredEnums;
  241. asCArray<asCGlobalProperty *> registeredGlobalProps;
  242. asCArray<asCScriptFunction *> registeredGlobalFuncs;
  243. asCArray<asCScriptFunction *> registeredFuncDefs;
  244. asCScriptFunction *stringFactory;
  245. bool configFailed;
  246. // Stores all known object types, both application registered, and script declared
  247. asCArray<asCObjectType *> objectTypes;
  248. asCArray<asCObjectType *> templateSubTypes;
  249. // Store information about template types
  250. asCArray<asCObjectType *> templateTypes;
  251. // Stores all global properties, both those registered by application, and those declared by scripts.
  252. // The id of a global property is the index in this array.
  253. asCArray<asCGlobalProperty *> globalProperties;
  254. asCArray<int> freeGlobalPropertyIds;
  255. // Stores all functions, i.e. registered functions, script functions, class methods, behaviours, etc.
  256. asCArray<asCScriptFunction *> scriptFunctions;
  257. asCArray<int> freeScriptFunctionIds;
  258. asCArray<asCScriptFunction *> signatureIds;
  259. // An array with all module imported functions
  260. asCArray<sBindInfo *> importedFunctions;
  261. asCArray<int> freeImportedFunctionIdxs;
  262. // These resources must be protected for multiple accesses
  263. mutable asCAtomic refCount;
  264. asCArray<asCModule *> scriptModules;
  265. asCModule *lastModule;
  266. bool isBuilding;
  267. bool deferValidationOfTemplateTypes;
  268. // Tokenizer is instanciated once to share resources
  269. asCTokenizer tok;
  270. // Stores script declared object types
  271. asCArray<asCObjectType *> classTypes;
  272. // This array stores the template instances types, that have been generated from template types
  273. asCArray<asCObjectType *> templateInstanceTypes;
  274. // Stores the funcdefs
  275. asCArray<asCScriptFunction *> funcDefs;
  276. // Stores the names of the script sections for debugging purposes
  277. asCArray<asCString *> scriptSectionNames;
  278. // Type identifiers
  279. mutable int typeIdSeqNbr;
  280. mutable asCMap<int, asCDataType*> mapTypeIdToDataType;
  281. // Garbage collector
  282. asCGarbageCollector gc;
  283. // Dynamic groups
  284. asCConfigGroup defaultGroup;
  285. asCArray<asCConfigGroup*> configGroups;
  286. asCConfigGroup *currentGroup;
  287. asDWORD defaultAccessMask;
  288. asCString defaultNamespace;
  289. // Message callback
  290. bool msgCallback;
  291. asSSystemFunctionInterface msgCallbackFunc;
  292. void *msgCallbackObj;
  293. asIJITCompiler *jitCompiler;
  294. // String constants
  295. asCArray<asCString*> stringConstants;
  296. asCMap<asCStringPointer, int> stringToIdMap;
  297. // User data
  298. void *userData;
  299. asCLEANENGINEFUNC_t cleanEngineFunc;
  300. asCLEANMODULEFUNC_t cleanModuleFunc;
  301. asCLEANCONTEXTFUNC_t cleanContextFunc;
  302. asCLEANFUNCTIONFUNC_t cleanFunctionFunc;
  303. asCLEANOBJECTTYPEFUNC_t cleanObjectTypeFunc;
  304. // Critical sections for threads
  305. DECLARECRITICALSECTION(engineCritical)
  306. // Engine properties
  307. struct
  308. {
  309. bool allowUnsafeReferences;
  310. bool optimizeByteCode;
  311. bool copyScriptSections;
  312. int maximumContextStackSize;
  313. bool useCharacterLiterals;
  314. bool allowMultilineStrings;
  315. bool allowImplicitHandleTypes;
  316. bool buildWithoutLineCues;
  317. bool initGlobalVarsAfterBuild;
  318. bool requireEnumScope;
  319. int scanner;
  320. bool includeJitInstructions;
  321. int stringEncoding;
  322. int propertyAccessorMode;
  323. bool expandDefaultArrayToTemplate;
  324. bool autoGarbageCollect;
  325. bool disallowGlobalVars;
  326. bool alwaysImplDefaultConstruct;
  327. } ep;
  328. };
  329. END_AS_NAMESPACE
  330. #endif