as_scriptengine.h 20 KB

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