as_scriptengine.h 20 KB

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