as_scriptengine.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. #ifdef AS_DEPRECATED
  74. // Deprecated since 2.24.0 - 2012-05-20
  75. virtual int GetGlobalFunctionIdByIndex(asUINT index) const;
  76. #endif
  77. virtual asIScriptFunction *GetGlobalFunctionByIndex(asUINT index) const;
  78. virtual asIScriptFunction *GetGlobalFunctionByDecl(const char *declaration) const;
  79. // Global properties
  80. virtual int RegisterGlobalProperty(const char *declaration, void *pointer);
  81. virtual asUINT GetGlobalPropertyCount() const;
  82. 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;
  83. virtual int GetGlobalPropertyIndexByName(const char *name) const;
  84. virtual int GetGlobalPropertyIndexByDecl(const char *decl) const;
  85. // Type registration
  86. virtual int RegisterObjectType(const char *obj, int byteSize, asDWORD flags);
  87. virtual int RegisterObjectProperty(const char *obj, const char *declaration, int byteOffset);
  88. virtual int RegisterObjectMethod(const char *obj, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  89. virtual int RegisterObjectBehaviour(const char *obj, asEBehaviours behaviour, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  90. virtual int RegisterInterface(const char *name);
  91. virtual int RegisterInterfaceMethod(const char *intf, const char *declaration);
  92. virtual asUINT GetObjectTypeCount() const;
  93. virtual asIObjectType *GetObjectTypeByIndex(asUINT index) const;
  94. virtual asIObjectType *GetObjectTypeByName(const char *name) const;
  95. // String factory
  96. virtual int RegisterStringFactory(const char *datatype, const asSFuncPtr &factoryFunc, asDWORD callConv);
  97. virtual int GetStringFactoryReturnTypeId() const;
  98. // Default array type
  99. virtual int RegisterDefaultArrayType(const char *type);
  100. virtual int GetDefaultArrayTypeId() const;
  101. // Enums
  102. virtual int RegisterEnum(const char *type);
  103. virtual int RegisterEnumValue(const char *type, const char *name, int value);
  104. virtual asUINT GetEnumCount() const;
  105. virtual const char *GetEnumByIndex(asUINT index, int *enumTypeId, const char **nameSpace, const char **configGroup = 0, asDWORD *accessMask = 0) const;
  106. virtual int GetEnumValueCount(int enumTypeId) const;
  107. virtual const char *GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const;
  108. // Funcdefs
  109. virtual int RegisterFuncdef(const char *decl);
  110. virtual asUINT GetFuncdefCount() const;
  111. virtual asIScriptFunction *GetFuncdefByIndex(asUINT index) const;
  112. // Typedefs
  113. virtual int RegisterTypedef(const char *type, const char *decl);
  114. virtual asUINT GetTypedefCount() const;
  115. virtual const char *GetTypedefByIndex(asUINT index, int *typeId, const char **nameSpace, const char **configGroup = 0, asDWORD *accessMask = 0) const;
  116. // Configuration groups
  117. virtual int BeginConfigGroup(const char *groupName);
  118. virtual int EndConfigGroup();
  119. virtual int RemoveConfigGroup(const char *groupName);
  120. virtual asDWORD SetDefaultAccessMask(asDWORD defaultMask);
  121. virtual int SetDefaultNamespace(const char *nameSpace);
  122. // Script modules
  123. virtual asIScriptModule *GetModule(const char *module, asEGMFlags flag);
  124. virtual int DiscardModule(const char *module);
  125. // Script functions
  126. virtual asIScriptFunction *GetFunctionById(int funcId) const;
  127. // Type identification
  128. virtual asIObjectType *GetObjectTypeById(int typeId) const;
  129. virtual int GetTypeIdByDecl(const char *decl) const;
  130. virtual const char *GetTypeDeclaration(int typeId, bool includeNamespace = false) const;
  131. virtual int GetSizeOfPrimitiveType(int typeId) const;
  132. // Script execution
  133. virtual asIScriptContext *CreateContext();
  134. // TODO: interface: Deprecate this, add a method that takes the asIObjectType instead
  135. virtual void *CreateScriptObject(int typeId);
  136. // TODO: interface: Deprecate this, add a method that takes the asIObjectType instead
  137. virtual void *CreateScriptObjectCopy(void *obj, int typeId);
  138. // TODO: interface: Deprecate this, add a method that takes the asIObjectType instead
  139. virtual void *CreateUninitializedScriptObject(int typeId);
  140. #ifdef AS_DEPRECATED
  141. // Deprecated since 2.24.0 - 2012-06-07
  142. virtual void CopyScriptObject(void *dstObj, void *srcObj, int typeId);
  143. #endif
  144. // TODO: interface: Deprecate this, add a method that takes the asIObjectType instead
  145. virtual void AssignScriptObject(void *dstObj, void *srcObj, int typeId);
  146. // TODO: interface: Deprecate this
  147. virtual void ReleaseScriptObject(void *obj, int typeId);
  148. virtual void ReleaseScriptObject(void *obj, const asIObjectType *type);
  149. // TODO: interface: Deprecate this
  150. virtual void AddRefScriptObject(void *obj, int typeId);
  151. virtual void AddRefScriptObject(void *obj, const asIObjectType *type);
  152. // TODO: interface: Should have a method void *CastObject(void *obj, asIObjectType *fromType, asIObjectType *toType);
  153. // For script objects it should simply check if the object implements or derives from the toType
  154. // For application objects it should look for ref cast behaviours and call the matching one
  155. // Once implemented the IsHandleCompatibleWithObject should be removed from the engine
  156. virtual bool IsHandleCompatibleWithObject(void *obj, int objTypeId, int handleTypeId) const;
  157. // String interpretation
  158. virtual asETokenClass ParseToken(const char *string, size_t stringLength = 0, int *tokenLength = 0) const;
  159. // Garbage collection
  160. virtual int GarbageCollect(asDWORD flags = asGC_FULL_CYCLE);
  161. virtual void GetGCStatistics(asUINT *currentSize, asUINT *totalDestroyed, asUINT *totalDetected, asUINT *newObjects, asUINT *totalNewDestroyed) const;
  162. virtual void NotifyGarbageCollectorOfNewObject(void *obj, asIObjectType *type);
  163. virtual void GCEnumCallback(void *reference);
  164. // User data
  165. virtual void *SetUserData(void *data, asPWORD type = 0);
  166. virtual void *GetUserData(asPWORD type = 0) const;
  167. virtual void SetEngineUserDataCleanupCallback(asCLEANENGINEFUNC_t callback, asPWORD type = 0);
  168. virtual void SetModuleUserDataCleanupCallback(asCLEANMODULEFUNC_t callback);
  169. virtual void SetContextUserDataCleanupCallback(asCLEANCONTEXTFUNC_t callback);
  170. virtual void SetFunctionUserDataCleanupCallback(asCLEANFUNCTIONFUNC_t callback);
  171. virtual void SetObjectTypeUserDataCleanupCallback(asCLEANOBJECTTYPEFUNC_t callback, asPWORD type);
  172. //===========================================================
  173. // internal methods
  174. //===========================================================
  175. public:
  176. asCScriptEngine();
  177. virtual ~asCScriptEngine();
  178. //protected:
  179. friend class asCBuilder;
  180. friend class asCCompiler;
  181. friend class asCContext;
  182. friend class asCDataType;
  183. friend class asCModule;
  184. friend class asCRestore;
  185. friend class asCByteCode;
  186. friend int PrepareSystemFunction(asCScriptFunction *func, asSSystemFunctionInterface *internal, asCScriptEngine *engine);
  187. int RegisterMethodToObjectType(asCObjectType *objectType, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  188. int RegisterBehaviourToObjectType(asCObjectType *objectType, asEBehaviours behaviour, const char *decl, const asSFuncPtr &funcPointer, asDWORD callConv);
  189. int VerifyVarTypeNotInFunction(asCScriptFunction *func);
  190. void *CallAlloc(asCObjectType *objType) const;
  191. void CallFree(void *obj) const;
  192. void *CallGlobalFunctionRetPtr(int func);
  193. void *CallGlobalFunctionRetPtr(int func, void *param1);
  194. void *CallGlobalFunctionRetPtr(asSSystemFunctionInterface *func, asCScriptFunction *desc);
  195. void *CallGlobalFunctionRetPtr(asSSystemFunctionInterface *i, asCScriptFunction *s, void *param1);
  196. void CallObjectMethod(void *obj, int func);
  197. void CallObjectMethod(void *obj, void *param, int func);
  198. void CallObjectMethod(void *obj, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  199. void CallObjectMethod(void *obj, void *param, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  200. bool CallObjectMethodRetBool(void *obj, int func);
  201. int CallObjectMethodRetInt(void *obj, int func);
  202. void CallGlobalFunction(void *param1, void *param2, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  203. bool CallGlobalFunctionRetBool(void *param1, void *param2, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  204. void ConstructScriptObjectCopy(void *mem, void *obj, asCObjectType *type);
  205. void ClearUnusedTypes();
  206. void RemoveTemplateInstanceType(asCObjectType *t);
  207. void RemoveTypeAndRelatedFromList(asCArray<asCObjectType*> &types, asCObjectType *ot);
  208. asCConfigGroup *FindConfigGroupForFunction(int funcId) const;
  209. asCConfigGroup *FindConfigGroupForGlobalVar(int gvarId) const;
  210. asCConfigGroup *FindConfigGroupForObjectType(const asCObjectType *type) const;
  211. asCConfigGroup *FindConfigGroupForFuncDef(const asCScriptFunction *funcDef) const;
  212. int RequestBuild();
  213. void BuildCompleted();
  214. void PrepareEngine();
  215. bool isPrepared;
  216. int CreateContext(asIScriptContext **context, bool isInternal);
  217. asCObjectType *GetObjectType(const char *type, const asCString &ns);
  218. int AddBehaviourFunction(asCScriptFunction &func, asSSystemFunctionInterface &internal);
  219. asCString GetFunctionDeclaration(int funcId);
  220. asCScriptFunction *GetScriptFunction(int funcId) const;
  221. asCModule *GetModule(const char *name, bool create);
  222. asCModule *GetModuleFromFuncId(int funcId);
  223. int GetMethodIdByDecl(const asCObjectType *ot, const char *decl, asCModule *mod);
  224. int GetFactoryIdByDecl(const asCObjectType *ot, const char *decl);
  225. int GetNextScriptFunctionId();
  226. void SetScriptFunction(asCScriptFunction *func);
  227. void FreeScriptFunctionId(int id);
  228. int ConfigError(int err, const char *funcName, const char *arg1, const char *arg2);
  229. int GetTypeIdFromDataType(const asCDataType &dt) const;
  230. asCDataType GetDataTypeFromTypeId(int typeId) const;
  231. asCObjectType *GetObjectTypeFromTypeId(int typeId) const;
  232. void RemoveFromTypeIdMap(asCObjectType *type);
  233. bool IsTemplateType(const char *name) const;
  234. asCObjectType *GetTemplateInstanceType(asCObjectType *templateType, asCDataType &subType);
  235. asCScriptFunction *GenerateTemplateFactoryStub(asCObjectType *templateType, asCObjectType *templateInstanceType, int origFactoryId);
  236. bool GenerateNewTemplateFunction(asCObjectType *templateType, asCObjectType *templateInstanceType, asCDataType &subType, asCScriptFunction *templateFunc, asCScriptFunction **newFunc);
  237. // String constants
  238. // TODO: Must free unused string constants, thus the ref count for each must be tracked
  239. int AddConstantString(const char *str, size_t length);
  240. const asCString &GetConstantString(int id);
  241. // Global property management
  242. asCGlobalProperty *AllocateGlobalProperty();
  243. void FreeUnusedGlobalProperties();
  244. int GetScriptSectionNameIndex(const char *name);
  245. //===========================================================
  246. // internal properties
  247. //===========================================================
  248. asCMemoryMgr memoryMgr;
  249. asUINT initialContextStackSize;
  250. asCObjectType *defaultArrayObjectType;
  251. asCObjectType scriptTypeBehaviours;
  252. asCObjectType functionBehaviours;
  253. asCObjectType objectTypeBehaviours;
  254. asCObjectType globalPropertyBehaviours;
  255. // Registered interface
  256. asCArray<asCObjectType *> registeredObjTypes;
  257. asCArray<asCObjectType *> registeredTypeDefs;
  258. asCArray<asCObjectType *> registeredEnums;
  259. asCArray<asCGlobalProperty *> registeredGlobalProps;
  260. asCArray<asCScriptFunction *> registeredGlobalFuncs;
  261. asCArray<asCScriptFunction *> registeredFuncDefs;
  262. asCScriptFunction *stringFactory;
  263. bool configFailed;
  264. // Stores all known object types, both application registered, and script declared
  265. asCArray<asCObjectType *> objectTypes;
  266. asCArray<asCObjectType *> templateSubTypes;
  267. // Store information about template types
  268. asCArray<asCObjectType *> templateTypes;
  269. // Stores all global properties, both those registered by application, and those declared by scripts.
  270. // The id of a global property is the index in this array.
  271. asCArray<asCGlobalProperty *> globalProperties;
  272. asCArray<int> freeGlobalPropertyIds;
  273. // Stores all functions, i.e. registered functions, script functions, class methods, behaviours, etc.
  274. asCArray<asCScriptFunction *> scriptFunctions;
  275. asCArray<int> freeScriptFunctionIds;
  276. asCArray<asCScriptFunction *> signatureIds;
  277. // An array with all module imported functions
  278. asCArray<sBindInfo *> importedFunctions;
  279. asCArray<int> freeImportedFunctionIdxs;
  280. // These resources must be protected for multiple accesses
  281. mutable asCAtomic refCount;
  282. asCArray<asCModule *> scriptModules;
  283. asCModule *lastModule;
  284. bool isBuilding;
  285. bool deferValidationOfTemplateTypes;
  286. // Tokenizer is instanciated once to share resources
  287. asCTokenizer tok;
  288. // Stores script declared object types
  289. asCArray<asCObjectType *> classTypes;
  290. // This array stores the template instances types, that have been generated from template types
  291. asCArray<asCObjectType *> templateInstanceTypes;
  292. // Stores the funcdefs
  293. asCArray<asCScriptFunction *> funcDefs;
  294. // Stores the names of the script sections for debugging purposes
  295. asCArray<asCString *> scriptSectionNames;
  296. // Type identifiers
  297. mutable int typeIdSeqNbr;
  298. mutable asCMap<int, asCDataType*> mapTypeIdToDataType;
  299. // Garbage collector
  300. asCGarbageCollector gc;
  301. // Dynamic groups
  302. asCConfigGroup defaultGroup;
  303. asCArray<asCConfigGroup*> configGroups;
  304. asCConfigGroup *currentGroup;
  305. asDWORD defaultAccessMask;
  306. asCString defaultNamespace;
  307. // Message callback
  308. bool msgCallback;
  309. asSSystemFunctionInterface msgCallbackFunc;
  310. void *msgCallbackObj;
  311. asIJITCompiler *jitCompiler;
  312. // String constants
  313. asCArray<asCString*> stringConstants;
  314. asCMap<asCStringPointer, int> stringToIdMap;
  315. // User data
  316. asCArray<asPWORD> userData;
  317. struct SEngineClean { asPWORD type; asCLEANENGINEFUNC_t cleanFunc; };
  318. asCArray<SEngineClean> cleanEngineFuncs;
  319. asCLEANMODULEFUNC_t cleanModuleFunc;
  320. asCLEANCONTEXTFUNC_t cleanContextFunc;
  321. asCLEANFUNCTIONFUNC_t cleanFunctionFunc;
  322. struct SObjTypeClean { asPWORD type; asCLEANOBJECTTYPEFUNC_t cleanFunc; };
  323. asCArray<SObjTypeClean> cleanObjectTypeFuncs;
  324. // Synchronization for threads
  325. DECLAREREADWRITELOCK(mutable engineRWLock)
  326. // Engine properties
  327. struct
  328. {
  329. bool allowUnsafeReferences;
  330. bool optimizeByteCode;
  331. bool copyScriptSections;
  332. asUINT maximumContextStackSize;
  333. bool useCharacterLiterals;
  334. bool allowMultilineStrings;
  335. bool allowImplicitHandleTypes;
  336. bool buildWithoutLineCues;
  337. bool initGlobalVarsAfterBuild;
  338. bool requireEnumScope;
  339. int scanner;
  340. bool includeJitInstructions;
  341. int stringEncoding;
  342. int propertyAccessorMode;
  343. bool expandDefaultArrayToTemplate;
  344. bool autoGarbageCollect;
  345. bool disallowGlobalVars;
  346. bool alwaysImplDefaultConstruct;
  347. } ep;
  348. };
  349. END_AS_NAMESPACE
  350. #endif