as_scriptengine.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2014 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. class asCScriptEngine : public asIScriptEngine
  48. {
  49. //=============================================================
  50. // From asIScriptEngine
  51. //=============================================================
  52. public:
  53. // Memory management
  54. virtual int AddRef() const;
  55. virtual int Release() const;
  56. // Engine properties
  57. virtual int SetEngineProperty(asEEngineProp property, asPWORD value);
  58. virtual asPWORD GetEngineProperty(asEEngineProp property) const;
  59. // Compiler messages
  60. virtual int SetMessageCallback(const asSFuncPtr &callback, void *obj, asDWORD callConv);
  61. virtual int ClearMessageCallback();
  62. virtual int WriteMessage(const char *section, int row, int col, asEMsgType type, const char *message);
  63. // JIT Compiler
  64. virtual int SetJITCompiler(asIJITCompiler *compiler);
  65. virtual asIJITCompiler *GetJITCompiler() const;
  66. // Global functions
  67. virtual int RegisterGlobalFunction(const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv, void *objForThiscall = 0);
  68. virtual asUINT GetGlobalFunctionCount() const;
  69. virtual asIScriptFunction *GetGlobalFunctionByIndex(asUINT index) const;
  70. virtual asIScriptFunction *GetGlobalFunctionByDecl(const char *declaration) const;
  71. // Global properties
  72. virtual int RegisterGlobalProperty(const char *declaration, void *pointer);
  73. virtual asUINT GetGlobalPropertyCount() const;
  74. 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;
  75. virtual int GetGlobalPropertyIndexByName(const char *name) const;
  76. virtual int GetGlobalPropertyIndexByDecl(const char *decl) const;
  77. // Type registration
  78. virtual int RegisterObjectType(const char *obj, int byteSize, asDWORD flags);
  79. virtual int RegisterObjectProperty(const char *obj, const char *declaration, int byteOffset);
  80. virtual int RegisterObjectMethod(const char *obj, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
  81. virtual int RegisterObjectBehaviour(const char *obj, asEBehaviours behaviour, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv, void *objForThiscall = 0);
  82. virtual int RegisterInterface(const char *name);
  83. virtual int RegisterInterfaceMethod(const char *intf, const char *declaration);
  84. virtual asUINT GetObjectTypeCount() const;
  85. virtual asIObjectType *GetObjectTypeByIndex(asUINT index) const;
  86. virtual asIObjectType *GetObjectTypeByName(const char *name) const;
  87. // String factory
  88. virtual int RegisterStringFactory(const char *datatype, const asSFuncPtr &factoryFunc, asDWORD callConv, void *objForThiscall = 0);
  89. virtual int GetStringFactoryReturnTypeId() const;
  90. // Default array type
  91. virtual int RegisterDefaultArrayType(const char *type);
  92. virtual int GetDefaultArrayTypeId() const;
  93. // Enums
  94. virtual int RegisterEnum(const char *type);
  95. virtual int RegisterEnumValue(const char *type, const char *name, int value);
  96. virtual asUINT GetEnumCount() const;
  97. virtual const char *GetEnumByIndex(asUINT index, int *enumTypeId, const char **nameSpace, const char **configGroup = 0, asDWORD *accessMask = 0) const;
  98. virtual int GetEnumValueCount(int enumTypeId) const;
  99. virtual const char *GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const;
  100. // Funcdefs
  101. virtual int RegisterFuncdef(const char *decl);
  102. virtual asUINT GetFuncdefCount() const;
  103. virtual asIScriptFunction *GetFuncdefByIndex(asUINT index) const;
  104. // Typedefs
  105. // TODO: interface: Should perhaps rename this to Alias, since it doesn't really create a new type
  106. virtual int RegisterTypedef(const char *type, const char *decl);
  107. virtual asUINT GetTypedefCount() const;
  108. virtual const char *GetTypedefByIndex(asUINT index, int *typeId, const char **nameSpace, const char **configGroup = 0, asDWORD *accessMask = 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 asDWORD SetDefaultAccessMask(asDWORD defaultMask);
  114. virtual int SetDefaultNamespace(const char *nameSpace);
  115. virtual const char *GetDefaultNamespace() const;
  116. // Script modules
  117. virtual asIScriptModule *GetModule(const char *module, asEGMFlags flag);
  118. virtual int DiscardModule(const char *module);
  119. virtual asUINT GetModuleCount() const;
  120. virtual asIScriptModule *GetModuleByIndex(asUINT index) const;
  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 *GetRegisteredObjectType(const asCString &name, asSNameSpace *ns) const;
  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; // TODO: memory savings: Since there can be only one property with the same name a simpler symbol table should be used
  267. asCSymbolTable<asCScriptFunction> registeredGlobalFuncs;
  268. asCArray<asCScriptFunction *> registeredFuncDefs;
  269. asCArray<asCObjectType *> registeredTemplateTypes;
  270. asCScriptFunction *stringFactory;
  271. bool configFailed;
  272. // Stores all registered types except funcdefs
  273. asCMap<asSNameSpaceNamePair, asCObjectType*> allRegisteredTypes;
  274. // Dummy types used to name the subtypes in the template objects
  275. asCArray<asCObjectType *> templateSubTypes;
  276. // Store information about template types
  277. // This list will contain all instances of templates, both registered specialized
  278. // types and those automacially instanciated from scripts
  279. asCArray<asCObjectType *> templateInstanceTypes;
  280. // Store information about list patterns
  281. asCArray<asCObjectType *> listPatternTypes;
  282. // Stores all global properties, both those registered by application, and those declared by scripts.
  283. // The id of a global property is the index in this array.
  284. asCArray<asCGlobalProperty *> globalProperties;
  285. // This map is used to quickly find a property by its memory address
  286. // It is used principally during building, cleanup, and garbage detection for script functions
  287. asCMap<void*, asCGlobalProperty*> varAddressMap;
  288. asCArray<int> freeGlobalPropertyIds;
  289. // Stores all functions, i.e. registered functions, script functions, class methods, behaviours, etc.
  290. asCArray<asCScriptFunction *> scriptFunctions;
  291. asCArray<int> freeScriptFunctionIds;
  292. asCArray<asCScriptFunction *> signatureIds;
  293. // An array with all module imported functions
  294. asCArray<sBindInfo *> importedFunctions;
  295. asCArray<int> freeImportedFunctionIdxs;
  296. // These resources must be protected for multiple accesses
  297. mutable asCAtomic refCount;
  298. asCArray<asCModule *> scriptModules;
  299. asCModule *lastModule;
  300. bool isBuilding;
  301. bool deferValidationOfTemplateTypes;
  302. // Tokenizer is instanciated once to share resources
  303. asCTokenizer tok;
  304. // Stores script declared object types
  305. asCArray<asCObjectType *> classTypes;
  306. // This array stores the template instances types that have been automatically generated from template types
  307. asCArray<asCObjectType *> generatedTemplateTypes;
  308. // Stores the funcdefs
  309. asCArray<asCScriptFunction *> funcDefs;
  310. // Stores the names of the script sections for debugging purposes
  311. asCArray<asCString *> scriptSectionNames;
  312. // Type identifiers
  313. mutable int typeIdSeqNbr;
  314. mutable asCMap<int, asCDataType*> mapTypeIdToDataType;
  315. // Garbage collector
  316. asCGarbageCollector gc;
  317. // Dynamic groups
  318. asCConfigGroup defaultGroup;
  319. asCArray<asCConfigGroup*> configGroups;
  320. asCConfigGroup *currentGroup;
  321. asDWORD defaultAccessMask;
  322. asSNameSpace *defaultNamespace;
  323. // Message callback
  324. bool msgCallback;
  325. asSSystemFunctionInterface msgCallbackFunc;
  326. void *msgCallbackObj;
  327. asIJITCompiler *jitCompiler;
  328. // Namespaces
  329. // These are shared between all entities and are
  330. // only deleted once the engine is destroyed
  331. asCArray<asSNameSpace*> nameSpaces;
  332. // String constants
  333. // These are shared between all scripts and are
  334. // only deleted once the engine is destroyed
  335. asCArray<asCString*> stringConstants;
  336. asCMap<asCStringPointer, int> stringToIdMap;
  337. // User data
  338. asCArray<asPWORD> userData;
  339. struct SEngineClean { asPWORD type; asCLEANENGINEFUNC_t cleanFunc; };
  340. asCArray<SEngineClean> cleanEngineFuncs;
  341. asCLEANMODULEFUNC_t cleanModuleFunc;
  342. asCLEANCONTEXTFUNC_t cleanContextFunc;
  343. asCLEANFUNCTIONFUNC_t cleanFunctionFunc;
  344. struct SObjTypeClean { asPWORD type; asCLEANOBJECTTYPEFUNC_t cleanFunc; };
  345. asCArray<SObjTypeClean> cleanObjectTypeFuncs;
  346. // Synchronization for threads
  347. DECLAREREADWRITELOCK(mutable engineRWLock)
  348. // Engine properties
  349. struct
  350. {
  351. bool allowUnsafeReferences;
  352. bool optimizeByteCode;
  353. bool copyScriptSections;
  354. asUINT maximumContextStackSize;
  355. bool useCharacterLiterals;
  356. bool allowMultilineStrings;
  357. bool allowImplicitHandleTypes;
  358. bool buildWithoutLineCues;
  359. bool initGlobalVarsAfterBuild;
  360. bool requireEnumScope;
  361. int scanner;
  362. bool includeJitInstructions;
  363. int stringEncoding;
  364. int propertyAccessorMode;
  365. bool expandDefaultArrayToTemplate;
  366. bool autoGarbageCollect;
  367. bool disallowGlobalVars;
  368. bool alwaysImplDefaultConstruct;
  369. int compilerWarnings;
  370. bool disallowValueAssignForRefType;
  371. } ep;
  372. // This flag is to allow a quicker shutdown when releasing the engine
  373. bool shuttingDown;
  374. };
  375. END_AS_NAMESPACE
  376. #endif