as_scriptengine.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 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 asUINT GetGlobalFunctionCount() const;
  74. virtual int GetGlobalFunctionIdByIndex(asUINT index) const;
  75. virtual asIScriptFunction *GetGlobalFunctionByIndex(asUINT index) const;
  76. virtual asIScriptFunction *GetGlobalFunctionByDecl(const char *declaration) const;
  77. // Global properties
  78. virtual int RegisterGlobalProperty(const char *declaration, void *pointer);
  79. virtual asUINT GetGlobalPropertyCount() const;
  80. // TODO: access: Return the current access mask
  81. virtual int GetGlobalPropertyByIndex(asUINT index, const char **name, int *typeId = 0, bool *isConst = 0, const char **configGroup = 0, void **pointer = 0) const;
  82. // TODO: access: Allow changing the access mask
  83. // Type registration
  84. virtual int RegisterObjectType(const char *obj, int byteSize, asDWORD flags);
  85. virtual int RegisterObjectProperty(const char *obj, const char *declaration, int byteOffset);
  86. virtual int RegisterObjectMethod(const char *obj, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  87. virtual int RegisterObjectBehaviour(const char *obj, asEBehaviours behaviour, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  88. virtual int RegisterInterface(const char *name);
  89. virtual int RegisterInterfaceMethod(const char *intf, const char *declaration);
  90. virtual asUINT GetObjectTypeCount() const;
  91. virtual asIObjectType *GetObjectTypeByIndex(asUINT index) 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 **configGroup = 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 char **configGroup = 0) 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 **configGroup = 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. #ifdef AS_DEPRECATED
  119. // deprecated since 2011-10-04
  120. virtual int SetConfigGroupModuleAccess(const char *groupName, const char *module, bool hasAccess);
  121. #endif
  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. #ifdef AS_DEPRECATED
  128. // deprecated since 2011-10-03
  129. virtual asIScriptFunction *GetFunctionDescriptorById(int funcId) const;
  130. #endif
  131. // Type identification
  132. virtual asIObjectType *GetObjectTypeById(int typeId) const;
  133. virtual int GetTypeIdByDecl(const char *decl) const;
  134. virtual const char *GetTypeDeclaration(int typeId) const;
  135. virtual int GetSizeOfPrimitiveType(int typeId) const;
  136. // Script execution
  137. virtual asIScriptContext *CreateContext();
  138. virtual void *CreateScriptObject(int typeId);
  139. virtual void *CreateScriptObjectCopy(void *obj, int typeId);
  140. virtual void CopyScriptObject(void *dstObj, void *srcObj, int typeId);
  141. virtual void ReleaseScriptObject(void *obj, int typeId);
  142. virtual void AddRefScriptObject(void *obj, int typeId);
  143. virtual bool IsHandleCompatibleWithObject(void *obj, int objTypeId, int handleTypeId) const;
  144. // String interpretation
  145. virtual asETokenClass ParseToken(const char *string, size_t stringLength = 0, int *tokenLength = 0) const;
  146. // Garbage collection
  147. virtual int GarbageCollect(asDWORD flags = asGC_FULL_CYCLE);
  148. virtual void GetGCStatistics(asUINT *currentSize, asUINT *totalDestroyed, asUINT *totalDetected, asUINT *newObjects, asUINT *totalNewDestroyed) const;
  149. virtual void NotifyGarbageCollectorOfNewObject(void *obj, int typeId);
  150. virtual void GCEnumCallback(void *reference);
  151. // User data
  152. virtual void *SetUserData(void *data);
  153. virtual void *GetUserData() const;
  154. virtual void SetEngineUserDataCleanupCallback(asCLEANENGINEFUNC_t callback);
  155. virtual void SetModuleUserDataCleanupCallback(asCLEANMODULEFUNC_t callback);
  156. virtual void SetContextUserDataCleanupCallback(asCLEANCONTEXTFUNC_t callback);
  157. virtual void SetFunctionUserDataCleanupCallback(asCLEANFUNCTIONFUNC_t callback);
  158. virtual void SetObjectTypeUserDataCleanupCallback(asCLEANOBJECTTYPEFUNC_t callback);
  159. //===========================================================
  160. // internal methods
  161. //===========================================================
  162. public:
  163. asCScriptEngine();
  164. virtual ~asCScriptEngine();
  165. //protected:
  166. friend class asCBuilder;
  167. friend class asCCompiler;
  168. friend class asCContext;
  169. friend class asCDataType;
  170. friend class asCModule;
  171. friend class asCRestore;
  172. friend class asCByteCode;
  173. friend int PrepareSystemFunction(asCScriptFunction *func, asSSystemFunctionInterface *internal, asCScriptEngine *engine);
  174. int RegisterMethodToObjectType(asCObjectType *objectType, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  175. int RegisterBehaviourToObjectType(asCObjectType *objectType, asEBehaviours behaviour, const char *decl, const asSFuncPtr &funcPointer, asDWORD callConv);
  176. int VerifyVarTypeNotInFunction(asCScriptFunction *func);
  177. void *CallAlloc(asCObjectType *objType) const;
  178. void CallFree(void *obj) const;
  179. void *CallGlobalFunctionRetPtr(int func);
  180. void *CallGlobalFunctionRetPtr(int func, void *param1);
  181. void *CallGlobalFunctionRetPtr(asSSystemFunctionInterface *func, asCScriptFunction *desc);
  182. void *CallGlobalFunctionRetPtr(asSSystemFunctionInterface *i, asCScriptFunction *s, void *param1);
  183. void CallObjectMethod(void *obj, int func);
  184. void CallObjectMethod(void *obj, void *param, int func);
  185. void CallObjectMethod(void *obj, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  186. void CallObjectMethod(void *obj, void *param, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  187. bool CallObjectMethodRetBool(void *obj, int func);
  188. int CallObjectMethodRetInt(void *obj, int func);
  189. void CallGlobalFunction(void *param1, void *param2, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  190. bool CallGlobalFunctionRetBool(void *param1, void *param2, asSSystemFunctionInterface *func, asCScriptFunction *desc);
  191. void ConstructScriptObjectCopy(void *mem, void *obj, asCObjectType *type);
  192. void ClearUnusedTypes();
  193. void RemoveTemplateInstanceType(asCObjectType *t);
  194. void RemoveTypeAndRelatedFromList(asCArray<asCObjectType*> &types, asCObjectType *ot);
  195. asCConfigGroup *FindConfigGroupForFunction(int funcId) const;
  196. asCConfigGroup *FindConfigGroupForGlobalVar(int gvarId) const;
  197. asCConfigGroup *FindConfigGroupForObjectType(const asCObjectType *type) const;
  198. asCConfigGroup *FindConfigGroupForFuncDef(asCScriptFunction *funcDef) const;
  199. int RequestBuild();
  200. void BuildCompleted();
  201. void PrepareEngine();
  202. bool isPrepared;
  203. int CreateContext(asIScriptContext **context, bool isInternal);
  204. asCObjectType *GetObjectType(const char *type);
  205. int AddBehaviourFunction(asCScriptFunction &func, asSSystemFunctionInterface &internal);
  206. asCString GetFunctionDeclaration(int funcID);
  207. asCScriptFunction *GetScriptFunction(int funcID) const;
  208. asCModule *GetModule(const char *name, bool create);
  209. asCModule *GetModuleFromFuncId(int funcId);
  210. int GetMethodIdByDecl(const asCObjectType *ot, const char *decl, asCModule *mod);
  211. int GetFactoryIdByDecl(const asCObjectType *ot, const char *decl);
  212. int GetNextScriptFunctionId();
  213. void SetScriptFunction(asCScriptFunction *func);
  214. void FreeScriptFunctionId(int id);
  215. int ConfigError(int err);
  216. int GetTypeIdFromDataType(const asCDataType &dt) const;
  217. const asCDataType *GetDataTypeFromTypeId(int typeId) const;
  218. asCObjectType *GetObjectTypeFromTypeId(int typeId) const;
  219. void RemoveFromTypeIdMap(asCObjectType *type);
  220. bool IsTemplateType(const char *name) const;
  221. asCObjectType *GetTemplateInstanceType(asCObjectType *templateType, asCDataType &subType);
  222. asCScriptFunction *GenerateTemplateFactoryStub(asCObjectType *templateType, asCObjectType *templateInstanceType, int origFactoryId);
  223. bool GenerateNewTemplateFunction(asCObjectType *templateType, asCObjectType *templateInstanceType, asCDataType &subType, asCScriptFunction *templateFunc, asCScriptFunction **newFunc);
  224. // String constants
  225. // TODO: Must free unused string constants, thus the ref count for each must be tracked
  226. int AddConstantString(const char *str, size_t length);
  227. const asCString &GetConstantString(int id);
  228. // Global property management
  229. asCGlobalProperty *AllocateGlobalProperty();
  230. void FreeUnusedGlobalProperties();
  231. int GetScriptSectionNameIndex(const char *name);
  232. //===========================================================
  233. // internal properties
  234. //===========================================================
  235. asCMemoryMgr memoryMgr;
  236. int initialContextStackSize;
  237. asCObjectType *defaultArrayObjectType;
  238. asCObjectType scriptTypeBehaviours;
  239. asCObjectType functionBehaviours;
  240. asCObjectType objectTypeBehaviours;
  241. asCObjectType globalPropertyBehaviours;
  242. // Registered interface
  243. asCArray<asCObjectType *> registeredObjTypes;
  244. asCArray<asCObjectType *> registeredTypeDefs;
  245. asCArray<asCObjectType *> registeredEnums;
  246. asCArray<asCGlobalProperty *> registeredGlobalProps;
  247. asCArray<asCScriptFunction *> registeredGlobalFuncs;
  248. asCArray<asCScriptFunction *> registeredFuncDefs;
  249. asCScriptFunction *stringFactory;
  250. bool configFailed;
  251. // Stores all known object types, both application registered, and script declared
  252. asCArray<asCObjectType *> objectTypes;
  253. asCArray<asCObjectType *> templateSubTypes;
  254. // Store information about template types
  255. asCArray<asCObjectType *> templateTypes;
  256. // Stores all global properties, both those registered by application, and those declared by scripts.
  257. // The id of a global property is the index in this array.
  258. asCArray<asCGlobalProperty *> globalProperties;
  259. asCArray<int> freeGlobalPropertyIds;
  260. // Stores all functions, i.e. registered functions, script functions, class methods, behaviours, etc.
  261. asCArray<asCScriptFunction *> scriptFunctions;
  262. asCArray<int> freeScriptFunctionIds;
  263. asCArray<asCScriptFunction *> signatureIds;
  264. // An array with all module imported functions
  265. asCArray<sBindInfo *> importedFunctions;
  266. asCArray<int> freeImportedFunctionIdxs;
  267. // These resources must be protected for multiple accesses
  268. mutable asCAtomic refCount;
  269. asCArray<asCModule *> scriptModules;
  270. asCModule *lastModule;
  271. bool isBuilding;
  272. // Stores script declared object types
  273. asCArray<asCObjectType *> classTypes;
  274. // This array stores the template instances types, that have been generated from template types
  275. asCArray<asCObjectType *> templateInstanceTypes;
  276. // Stores the funcdefs
  277. asCArray<asCScriptFunction *> funcDefs;
  278. // Stores the names of the script sections for debugging purposes
  279. asCArray<asCString *> scriptSectionNames;
  280. // Type identifiers
  281. mutable int typeIdSeqNbr;
  282. mutable asCMap<int, asCDataType*> mapTypeIdToDataType;
  283. // Garbage collector
  284. asCGarbageCollector gc;
  285. // Dynamic groups
  286. asCConfigGroup defaultGroup;
  287. asCArray<asCConfigGroup*> configGroups;
  288. asCConfigGroup *currentGroup;
  289. asDWORD defaultAccessMask;
  290. // Message callback
  291. bool msgCallback;
  292. asSSystemFunctionInterface msgCallbackFunc;
  293. void *msgCallbackObj;
  294. asIJITCompiler *jitCompiler;
  295. // String constants
  296. asCArray<asCString*> stringConstants;
  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. } ep;
  327. };
  328. END_AS_NAMESPACE
  329. #endif