as_scriptengine.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2010 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. BEGIN_AS_NAMESPACE
  43. class asCBuilder;
  44. class asCContext;
  45. // TODO: import: Remove this when import is removed
  46. struct sBindInfo;
  47. // TODO: Deprecate CreateScriptObject. Objects should be created by calling the factory function instead.
  48. // TODO: Deprecate GetSizeOfPrimitiveType. This function is not necessary now that all primitive types have fixed typeIds
  49. // TODO: DiscardModule should take an optional pointer to asIScriptModule instead of module name. If null, nothing is done.
  50. // TODO: Should have a CreateModule/GetModule instead of just GetModule with parameters.
  51. // TODO: Should allow enumerating modules, in case they have not been named.
  52. class asCScriptEngine : public asIScriptEngine
  53. {
  54. //=============================================================
  55. // From asIScriptEngine
  56. //=============================================================
  57. public:
  58. // Memory management
  59. virtual int AddRef() const;
  60. virtual int Release() const;
  61. // Engine properties
  62. virtual int SetEngineProperty(asEEngineProp property, asPWORD value);
  63. virtual asPWORD GetEngineProperty(asEEngineProp property) const;
  64. // Compiler messages
  65. virtual int SetMessageCallback(const asSFuncPtr &callback, void *obj, asDWORD callConv);
  66. virtual int ClearMessageCallback();
  67. virtual int WriteMessage(const char *section, int row, int col, asEMsgType type, const char *message);
  68. // JIT Compiler
  69. virtual int SetJITCompiler(asIJITCompiler *compiler);
  70. virtual asIJITCompiler *GetJITCompiler() const;
  71. // Global functions
  72. virtual int RegisterGlobalFunction(const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  73. virtual int GetGlobalFunctionCount() const;
  74. virtual int GetGlobalFunctionIdByIndex(asUINT index) const;
  75. // Global properties
  76. virtual int RegisterGlobalProperty(const char *declaration, void *pointer);
  77. virtual int GetGlobalPropertyCount() const;
  78. virtual int GetGlobalPropertyByIndex(asUINT index, const char **name, int *typeId = 0, bool *isConst = 0, const char **configGroup = 0, void **pointer = 0) const;
  79. // Type registration
  80. virtual int RegisterObjectType(const char *obj, int byteSize, asDWORD flags);
  81. virtual int RegisterObjectProperty(const char *obj, const char *declaration, int byteOffset);
  82. virtual int RegisterObjectMethod(const char *obj, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  83. virtual int RegisterObjectBehaviour(const char *obj, asEBehaviours behaviour, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  84. virtual int RegisterInterface(const char *name);
  85. virtual int RegisterInterfaceMethod(const char *intf, const char *declaration);
  86. virtual int GetObjectTypeCount() const;
  87. virtual asIObjectType *GetObjectTypeByIndex(asUINT index) const;
  88. // String factory
  89. virtual int RegisterStringFactory(const char *datatype, const asSFuncPtr &factoryFunc, asDWORD callConv);
  90. virtual int GetStringFactoryReturnTypeId() const;
  91. // Default array type
  92. virtual int RegisterDefaultArrayType(const char *type);
  93. virtual int GetDefaultArrayTypeId() const;
  94. // Enums
  95. virtual int RegisterEnum(const char *type);
  96. virtual int RegisterEnumValue(const char *type, const char *name, int value);
  97. virtual int GetEnumCount() const;
  98. virtual const char *GetEnumByIndex(asUINT index, int *enumTypeId, const char **configGroup = 0) const;
  99. virtual int GetEnumValueCount(int enumTypeId) const;
  100. virtual const char *GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const;
  101. // Funcdefs
  102. virtual int RegisterFuncdef(const char *decl);
  103. virtual int GetFuncdefCount() const;
  104. virtual asIScriptFunction *GetFuncdefByIndex(asUINT index, const char **configGroup = 0) const;
  105. // Typedefs
  106. virtual int RegisterTypedef(const char *type, const char *decl);
  107. virtual int GetTypedefCount() const;
  108. virtual const char *GetTypedefByIndex(asUINT index, int *typeId, const char **configGroup = 0) const;
  109. // Configuration groups
  110. virtual int BeginConfigGroup(const char *groupName);
  111. virtual int EndConfigGroup();
  112. virtual int RemoveConfigGroup(const char *groupName);
  113. virtual int SetConfigGroupModuleAccess(const char *groupName, const char *module, bool hasAccess);
  114. // Script modules
  115. virtual asIScriptModule *GetModule(const char *module, asEGMFlags flag);
  116. virtual int DiscardModule(const char *module);
  117. // Script functions
  118. virtual asIScriptFunction *GetFunctionDescriptorById(int funcId) const;
  119. // Type identification
  120. virtual asIObjectType *GetObjectTypeById(int typeId) const;
  121. virtual int GetTypeIdByDecl(const char *decl) const;
  122. virtual const char *GetTypeDeclaration(int typeId) const;
  123. virtual int GetSizeOfPrimitiveType(int typeId) const;
  124. // Script execution
  125. virtual asIScriptContext *CreateContext();
  126. virtual void *CreateScriptObject(int typeId);
  127. virtual void *CreateScriptObjectCopy(void *obj, int typeId);
  128. virtual void CopyScriptObject(void *dstObj, void *srcObj, int typeId);
  129. virtual void ReleaseScriptObject(void *obj, int typeId);
  130. virtual void AddRefScriptObject(void *obj, int typeId);
  131. virtual bool IsHandleCompatibleWithObject(void *obj, int objTypeId, int handleTypeId) const;
  132. // String interpretation
  133. virtual asETokenClass ParseToken(const char *string, size_t stringLength = 0, int *tokenLength = 0) const;
  134. // Garbage collection
  135. virtual int GarbageCollect(asDWORD flags = asGC_FULL_CYCLE);
  136. virtual void GetGCStatistics(asUINT *currentSize, asUINT *totalDestroyed, asUINT *totalDetected) const;
  137. virtual void NotifyGarbageCollectorOfNewObject(void *obj, int typeId);
  138. virtual void GCEnumCallback(void *reference);
  139. // User data
  140. virtual void *SetUserData(void *data);
  141. virtual void *GetUserData() const;
  142. virtual void SetEngineUserDataCleanupCallback(asCLEANENGINEFUNC_t callback);
  143. virtual void SetContextUserDataCleanupCallback(asCLEANCONTEXTFUNC_t callback);
  144. virtual void SetFunctionUserDataCleanupCallback(asCLEANFUNCTIONFUNC_t callback);
  145. //===========================================================
  146. // internal methods
  147. //===========================================================
  148. public:
  149. asCScriptEngine();
  150. virtual ~asCScriptEngine();
  151. //protected:
  152. friend class asCBuilder;
  153. friend class asCCompiler;
  154. friend class asCContext;
  155. friend class asCDataType;
  156. friend class asCModule;
  157. friend class asCRestore;
  158. friend class asCByteCode;
  159. friend int PrepareSystemFunction(asCScriptFunction *func, asSSystemFunctionInterface *internal, asCScriptEngine *engine);
  160. int RegisterMethodToObjectType(asCObjectType *objectType, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  161. int RegisterBehaviourToObjectType(asCObjectType *objectType, asEBehaviours behaviour, const char *decl, const asSFuncPtr &funcPointer, asDWORD callConv);
  162. int VerifyVarTypeNotInFunction(asCScriptFunction *func);
  163. void *CallAlloc(asCObjectType *objType) const;
  164. void CallFree(void *obj) const;
  165. void *CallGlobalFunctionRetPtr(int func);
  166. void *CallGlobalFunctionRetPtr(int func, void *param1);
  167. void *CallGlobalFunctionRetPtr(asSSystemFunctionInterface *func, asCScriptFunction *desc);
  168. void *CallGlobalFunctionRetPtr(asSSystemFunctionInterface *i, asCScriptFunction *s, void *param1);
  169. void CallObjectMethod(void *obj, int func);
  170. void CallObjectMethod(void *obj, void *param, int func);
  171. void CallObjectMethod(void *obj, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  172. void CallObjectMethod(void *obj, void *param, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  173. bool CallObjectMethodRetBool(void *obj, int func);
  174. int CallObjectMethodRetInt(void *obj, int func);
  175. void CallGlobalFunction(void *param1, void *param2, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  176. bool CallGlobalFunctionRetBool(void *param1, void *param2, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  177. void ClearUnusedTypes();
  178. void RemoveTemplateInstanceType(asCObjectType *t);
  179. void RemoveTypeAndRelatedFromList(asCArray<asCObjectType*> &types, asCObjectType *ot);
  180. asCConfigGroup *FindConfigGroupForFunction(int funcId) const;
  181. asCConfigGroup *FindConfigGroupForGlobalVar(int gvarId) const;
  182. asCConfigGroup *FindConfigGroupForObjectType(const asCObjectType *type) const;
  183. asCConfigGroup *FindConfigGroupForFuncDef(asCScriptFunction *funcDef) const;
  184. int RequestBuild();
  185. void BuildCompleted();
  186. void PrepareEngine();
  187. bool isPrepared;
  188. int CreateContext(asIScriptContext **context, bool isInternal);
  189. asCObjectType *GetObjectType(const char *type);
  190. int AddBehaviourFunction(asCScriptFunction &func, asSSystemFunctionInterface &internal);
  191. asCString GetFunctionDeclaration(int funcID);
  192. asCScriptFunction *GetScriptFunction(int funcID) const;
  193. asCModule *GetModule(const char *name, bool create);
  194. asCModule *GetModuleFromFuncId(int funcId);
  195. int GetMethodIdByDecl(const asCObjectType *ot, const char *decl, asCModule *mod);
  196. int GetFactoryIdByDecl(const asCObjectType *ot, const char *decl);
  197. int GetNextScriptFunctionId();
  198. void SetScriptFunction(asCScriptFunction *func);
  199. void FreeScriptFunctionId(int id);
  200. int ConfigError(int err);
  201. int GetTypeIdFromDataType(const asCDataType &dt) const;
  202. const asCDataType *GetDataTypeFromTypeId(int typeId) const;
  203. asCObjectType *GetObjectTypeFromTypeId(int typeId) const;
  204. void RemoveFromTypeIdMap(asCObjectType *type);
  205. bool IsTemplateType(const char *name) const;
  206. asCObjectType *GetTemplateInstanceType(asCObjectType *templateType, asCDataType &subType);
  207. asCScriptFunction *GenerateTemplateFactoryStub(asCObjectType *templateType, asCObjectType *templateInstanceType, int origFactoryId);
  208. bool GenerateNewTemplateFunction(asCObjectType *templateType, asCObjectType *templateInstanceType, asCDataType &subType, asCScriptFunction *templateFunc, asCScriptFunction **newFunc);
  209. // String constants
  210. // TODO: Must free unused string constants, thus the ref count for each must be tracked
  211. int AddConstantString(const char *str, size_t length);
  212. const asCString &GetConstantString(int id);
  213. // Global property management
  214. asCGlobalProperty *AllocateGlobalProperty();
  215. void FreeUnusedGlobalProperties();
  216. int GetScriptSectionNameIndex(const char *name);
  217. //===========================================================
  218. // internal properties
  219. //===========================================================
  220. asCMemoryMgr memoryMgr;
  221. int initialContextStackSize;
  222. asCObjectType *defaultArrayObjectType;
  223. asCObjectType scriptTypeBehaviours;
  224. asCObjectType functionBehaviours;
  225. asCObjectType objectTypeBehaviours;
  226. asCObjectType globalPropertyBehaviours;
  227. // Registered interface
  228. asCArray<asCObjectType *> registeredObjTypes;
  229. asCArray<asCObjectType *> registeredTypeDefs;
  230. asCArray<asCObjectType *> registeredEnums;
  231. asCArray<asCGlobalProperty *> registeredGlobalProps;
  232. asCArray<asCScriptFunction *> registeredGlobalFuncs;
  233. asCArray<asCScriptFunction *> registeredFuncDefs;
  234. asCScriptFunction *stringFactory;
  235. bool configFailed;
  236. // Stores all known object types, both application registered, and script declared
  237. asCArray<asCObjectType *> objectTypes;
  238. asCArray<asCObjectType *> templateSubTypes;
  239. // Store information about template types
  240. asCArray<asCObjectType *> templateTypes;
  241. // Stores all global properties, both those registered by application, and those declared by scripts.
  242. // The id of a global property is the index in this array.
  243. asCArray<asCGlobalProperty *> globalProperties;
  244. asCArray<int> freeGlobalPropertyIds;
  245. // Stores all functions, i.e. registered functions, script functions, class methods, behaviours, etc.
  246. asCArray<asCScriptFunction *> scriptFunctions;
  247. asCArray<int> freeScriptFunctionIds;
  248. asCArray<asCScriptFunction *> signatureIds;
  249. // An array with all module imported functions
  250. asCArray<sBindInfo *> importedFunctions;
  251. // These resources must be protected for multiple accesses
  252. mutable asCAtomic refCount;
  253. asCArray<asCModule *> scriptModules;
  254. asCModule *lastModule;
  255. bool isBuilding;
  256. // Stores script declared object types
  257. asCArray<asCObjectType *> classTypes;
  258. // This array stores the template instances types, that have been generated from template types
  259. asCArray<asCObjectType *> templateInstanceTypes;
  260. // Stores the funcdefs
  261. asCArray<asCScriptFunction *> funcDefs;
  262. // Stores the names of the script sections for debugging purposes
  263. asCArray<asCString *> scriptSectionNames;
  264. // Type identifiers
  265. mutable int typeIdSeqNbr;
  266. mutable asCMap<int, asCDataType*> mapTypeIdToDataType;
  267. // Garbage collector
  268. asCGarbageCollector gc;
  269. // Dynamic groups
  270. asCConfigGroup defaultGroup;
  271. asCArray<asCConfigGroup*> configGroups;
  272. asCConfigGroup *currentGroup;
  273. // Message callback
  274. bool msgCallback;
  275. asSSystemFunctionInterface msgCallbackFunc;
  276. void *msgCallbackObj;
  277. asIJITCompiler *jitCompiler;
  278. // String constants
  279. asCArray<asCString*> stringConstants;
  280. // User data
  281. void *userData;
  282. asCLEANENGINEFUNC_t cleanEngineFunc;
  283. asCLEANCONTEXTFUNC_t cleanContextFunc;
  284. asCLEANFUNCTIONFUNC_t cleanFunctionFunc;
  285. // Critical sections for threads
  286. DECLARECRITICALSECTION(engineCritical);
  287. // Engine properties
  288. struct
  289. {
  290. bool allowUnsafeReferences;
  291. bool optimizeByteCode;
  292. bool copyScriptSections;
  293. int maximumContextStackSize;
  294. bool useCharacterLiterals;
  295. bool allowMultilineStrings;
  296. bool allowImplicitHandleTypes;
  297. bool buildWithoutLineCues;
  298. bool initGlobalVarsAfterBuild;
  299. bool requireEnumScope;
  300. int scanner;
  301. bool includeJitInstructions;
  302. int stringEncoding;
  303. int propertyAccessorMode;
  304. bool expandDefaultArrayToTemplate;
  305. } ep;
  306. };
  307. END_AS_NAMESPACE
  308. #endif