You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1108 lines
35KB

  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2009 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. andreas@angelcode.com
  22. */
  23. //
  24. // angelscript.h
  25. //
  26. // The script engine interface
  27. //
  28. #ifndef ANGELSCRIPT_H
  29. #define ANGELSCRIPT_H
  30. #include <stddef.h>
  31. #ifdef AS_USE_NAMESPACE
  32. #define BEGIN_AS_NAMESPACE namespace AngelScript {
  33. #define END_AS_NAMESPACE }
  34. #else
  35. #define BEGIN_AS_NAMESPACE
  36. #define END_AS_NAMESPACE
  37. #endif
  38. BEGIN_AS_NAMESPACE
  39. // AngelScript version
  40. #define ANGELSCRIPT_VERSION 21602
  41. #define ANGELSCRIPT_VERSION_MAJOR 2
  42. #define ANGELSCRIPT_VERSION_MINOR 16
  43. #define ANGELSCRIPT_VERSION_BUILD 2
  44. #define ANGELSCRIPT_VERSION_STRING "2.16.2"
  45. // Data types
  46. class asIScriptEngine;
  47. class asIScriptModule;
  48. class asIScriptContext;
  49. class asIScriptGeneric;
  50. class asIScriptObject;
  51. class asIScriptArray;
  52. class asIObjectType;
  53. class asIScriptFunction;
  54. class asIBinaryStream;
  55. #ifdef AS_DEPRECATED
  56. typedef asIScriptObject asIScriptStruct;
  57. #endif
  58. // Enumerations and constants
  59. // Engine properties
  60. enum asEEngineProp
  61. {
  62. asEP_ALLOW_UNSAFE_REFERENCES = 1,
  63. asEP_OPTIMIZE_BYTECODE = 2,
  64. asEP_COPY_SCRIPT_SECTIONS = 3,
  65. asEP_MAX_STACK_SIZE = 4,
  66. asEP_USE_CHARACTER_LITERALS = 5,
  67. asEP_ALLOW_MULTILINE_STRINGS = 6,
  68. asEP_ALLOW_IMPLICIT_HANDLE_TYPES = 7,
  69. asEP_BUILD_WITHOUT_LINE_CUES = 8,
  70. asEP_INIT_GLOBAL_VARS_AFTER_BUILD = 9,
  71. asEP_REQUIRE_ENUM_SCOPE = 10,
  72. asEP_SCRIPT_SCANNER = 11
  73. };
  74. // Calling conventions
  75. enum asECallConvTypes
  76. {
  77. asCALL_CDECL = 0,
  78. asCALL_STDCALL = 1,
  79. asCALL_THISCALL = 2,
  80. asCALL_CDECL_OBJLAST = 3,
  81. asCALL_CDECL_OBJFIRST = 4,
  82. asCALL_GENERIC = 5
  83. };
  84. // Object type flags
  85. enum asEObjTypeFlags
  86. {
  87. asOBJ_REF = 0x01,
  88. asOBJ_VALUE = 0x02,
  89. asOBJ_GC = 0x04,
  90. asOBJ_POD = 0x08,
  91. asOBJ_NOHANDLE = 0x10,
  92. asOBJ_SCOPED = 0x20,
  93. asOBJ_TEMPLATE = 0x40,
  94. asOBJ_APP_CLASS = 0x100,
  95. asOBJ_APP_CLASS_CONSTRUCTOR = 0x200,
  96. asOBJ_APP_CLASS_DESTRUCTOR = 0x400,
  97. asOBJ_APP_CLASS_ASSIGNMENT = 0x800,
  98. asOBJ_APP_CLASS_C = (asOBJ_APP_CLASS + asOBJ_APP_CLASS_CONSTRUCTOR),
  99. asOBJ_APP_CLASS_CD = (asOBJ_APP_CLASS + asOBJ_APP_CLASS_CONSTRUCTOR + asOBJ_APP_CLASS_DESTRUCTOR),
  100. asOBJ_APP_CLASS_CA = (asOBJ_APP_CLASS + asOBJ_APP_CLASS_CONSTRUCTOR + asOBJ_APP_CLASS_ASSIGNMENT),
  101. asOBJ_APP_CLASS_CDA = (asOBJ_APP_CLASS + asOBJ_APP_CLASS_CONSTRUCTOR + asOBJ_APP_CLASS_DESTRUCTOR + asOBJ_APP_CLASS_ASSIGNMENT),
  102. asOBJ_APP_CLASS_D = (asOBJ_APP_CLASS + asOBJ_APP_CLASS_DESTRUCTOR),
  103. asOBJ_APP_CLASS_A = (asOBJ_APP_CLASS + asOBJ_APP_CLASS_ASSIGNMENT),
  104. asOBJ_APP_CLASS_DA = (asOBJ_APP_CLASS + asOBJ_APP_CLASS_DESTRUCTOR + asOBJ_APP_CLASS_ASSIGNMENT),
  105. asOBJ_APP_PRIMITIVE = 0x1000,
  106. asOBJ_APP_FLOAT = 0x2000,
  107. asOBJ_MASK_VALID_FLAGS = 0x3F7F,
  108. asOBJ_SCRIPT_OBJECT = 0x10000
  109. };
  110. // Behaviours
  111. enum asEBehaviours
  112. {
  113. // Value object memory management
  114. asBEHAVE_CONSTRUCT,
  115. asBEHAVE_DESTRUCT,
  116. // Reference object memory management
  117. asBEHAVE_FACTORY,
  118. asBEHAVE_ADDREF,
  119. asBEHAVE_RELEASE,
  120. // Object operators
  121. asBEHAVE_VALUE_CAST,
  122. asBEHAVE_IMPLICIT_VALUE_CAST,
  123. asBEHAVE_INDEX,
  124. asBEHAVE_NEGATE,
  125. // Assignment operators
  126. asBEHAVE_FIRST_ASSIGN,
  127. asBEHAVE_ASSIGNMENT = asBEHAVE_FIRST_ASSIGN,
  128. asBEHAVE_ADD_ASSIGN,
  129. asBEHAVE_SUB_ASSIGN,
  130. asBEHAVE_MUL_ASSIGN,
  131. asBEHAVE_DIV_ASSIGN,
  132. asBEHAVE_MOD_ASSIGN,
  133. asBEHAVE_OR_ASSIGN,
  134. asBEHAVE_AND_ASSIGN,
  135. asBEHAVE_XOR_ASSIGN,
  136. asBEHAVE_SLL_ASSIGN,
  137. asBEHAVE_SRL_ASSIGN,
  138. asBEHAVE_SRA_ASSIGN,
  139. asBEHAVE_LAST_ASSIGN = asBEHAVE_SRA_ASSIGN,
  140. // Global operators
  141. asBEHAVE_FIRST_DUAL,
  142. asBEHAVE_ADD = asBEHAVE_FIRST_DUAL,
  143. asBEHAVE_SUBTRACT,
  144. asBEHAVE_MULTIPLY,
  145. asBEHAVE_DIVIDE,
  146. asBEHAVE_MODULO,
  147. asBEHAVE_EQUAL,
  148. asBEHAVE_NOTEQUAL,
  149. asBEHAVE_LESSTHAN,
  150. asBEHAVE_GREATERTHAN,
  151. asBEHAVE_LEQUAL,
  152. asBEHAVE_GEQUAL,
  153. asBEHAVE_BIT_OR,
  154. asBEHAVE_BIT_AND,
  155. asBEHAVE_BIT_XOR,
  156. asBEHAVE_BIT_SLL,
  157. asBEHAVE_BIT_SRL,
  158. asBEHAVE_BIT_SRA,
  159. asBEHAVE_LAST_DUAL = asBEHAVE_BIT_SRA,
  160. asBEHAVE_REF_CAST,
  161. asBEHAVE_IMPLICIT_REF_CAST,
  162. // Garbage collection behaviours
  163. asBEHAVE_FIRST_GC,
  164. asBEHAVE_GETREFCOUNT = asBEHAVE_FIRST_GC,
  165. asBEHAVE_SETGCFLAG,
  166. asBEHAVE_GETGCFLAG,
  167. asBEHAVE_ENUMREFS,
  168. asBEHAVE_RELEASEREFS,
  169. asBEHAVE_LAST_GC = asBEHAVE_RELEASEREFS
  170. };
  171. // Return codes
  172. enum asERetCodes
  173. {
  174. asSUCCESS = 0,
  175. asERROR = -1,
  176. asCONTEXT_ACTIVE = -2,
  177. asCONTEXT_NOT_FINISHED = -3,
  178. asCONTEXT_NOT_PREPARED = -4,
  179. asINVALID_ARG = -5,
  180. asNO_FUNCTION = -6,
  181. asNOT_SUPPORTED = -7,
  182. asINVALID_NAME = -8,
  183. asNAME_TAKEN = -9,
  184. asINVALID_DECLARATION = -10,
  185. asINVALID_OBJECT = -11,
  186. asINVALID_TYPE = -12,
  187. asALREADY_REGISTERED = -13,
  188. asMULTIPLE_FUNCTIONS = -14,
  189. asNO_MODULE = -15,
  190. asNO_GLOBAL_VAR = -16,
  191. asINVALID_CONFIGURATION = -17,
  192. asINVALID_INTERFACE = -18,
  193. asCANT_BIND_ALL_FUNCTIONS = -19,
  194. asLOWER_ARRAY_DIMENSION_NOT_REGISTERED = -20,
  195. asWRONG_CONFIG_GROUP = -21,
  196. asCONFIG_GROUP_IS_IN_USE = -22,
  197. asILLEGAL_BEHAVIOUR_FOR_TYPE = -23,
  198. asWRONG_CALLING_CONV = -24,
  199. asMODULE_IS_IN_USE = -25,
  200. asBUILD_IN_PROGRESS = -26,
  201. asINIT_GLOBAL_VARS_FAILED = -27
  202. };
  203. // Context states
  204. enum asEContextState
  205. {
  206. asEXECUTION_FINISHED = 0,
  207. asEXECUTION_SUSPENDED = 1,
  208. asEXECUTION_ABORTED = 2,
  209. asEXECUTION_EXCEPTION = 3,
  210. asEXECUTION_PREPARED = 4,
  211. asEXECUTION_UNINITIALIZED = 5,
  212. asEXECUTION_ACTIVE = 6,
  213. asEXECUTION_ERROR = 7
  214. };
  215. // ExecuteString flags
  216. enum asEExecStrFlags
  217. {
  218. asEXECSTRING_ONLY_PREPARE = 1,
  219. asEXECSTRING_USE_MY_CONTEXT = 2
  220. };
  221. // Message types
  222. enum asEMsgType
  223. {
  224. asMSGTYPE_ERROR = 0,
  225. asMSGTYPE_WARNING = 1,
  226. asMSGTYPE_INFORMATION = 2
  227. };
  228. // Garbage collector flags
  229. enum asEGCFlags
  230. {
  231. asGC_FULL_CYCLE = 1,
  232. asGC_ONE_STEP = 2,
  233. asGC_DESTROY_GARBAGE = 4,
  234. asGC_DETECT_GARBAGE = 8
  235. };
  236. // Token classes
  237. enum asETokenClass
  238. {
  239. asTC_UNKNOWN = 0,
  240. asTC_KEYWORD = 1,
  241. asTC_VALUE = 2,
  242. asTC_IDENTIFIER = 3,
  243. asTC_COMMENT = 4,
  244. asTC_WHITESPACE = 5
  245. };
  246. // Prepare flags
  247. const int asPREPARE_PREVIOUS = -1;
  248. // Config groups
  249. const char * const asALL_MODULES = (const char * const)-1;
  250. // Type id flags
  251. enum asETypeIdFlags
  252. {
  253. asTYPEID_VOID = 0,
  254. asTYPEID_BOOL = 1,
  255. asTYPEID_INT8 = 2,
  256. asTYPEID_INT16 = 3,
  257. asTYPEID_INT32 = 4,
  258. asTYPEID_INT64 = 5,
  259. asTYPEID_UINT8 = 6,
  260. asTYPEID_UINT16 = 7,
  261. asTYPEID_UINT32 = 8,
  262. asTYPEID_UINT64 = 9,
  263. asTYPEID_FLOAT = 10,
  264. asTYPEID_DOUBLE = 11,
  265. asTYPEID_OBJHANDLE = 0x40000000,
  266. asTYPEID_HANDLETOCONST = 0x20000000,
  267. asTYPEID_MASK_OBJECT = 0x1C000000,
  268. asTYPEID_APPOBJECT = 0x04000000,
  269. #ifdef AS_DEPRECATED
  270. asTYPEID_SCRIPTSTRUCT = 0x0C000000,
  271. #endif
  272. asTYPEID_SCRIPTOBJECT = 0x08000000,
  273. asTYPEID_SCRIPTARRAY = 0x10000000,
  274. asTYPEID_MASK_SEQNBR = 0x03FFFFFF
  275. };
  276. // Type modifiers
  277. enum asETypeModifiers
  278. {
  279. asTM_NONE = 0,
  280. asTM_INREF = 1,
  281. asTM_OUTREF = 2,
  282. asTM_INOUTREF = 3
  283. };
  284. // GetModule flags
  285. enum asEGMFlags
  286. {
  287. asGM_ONLY_IF_EXISTS = 0,
  288. asGM_CREATE_IF_NOT_EXISTS = 1,
  289. asGM_ALWAYS_CREATE = 2
  290. };
  291. //
  292. // asBYTE = 8 bits
  293. // asWORD = 16 bits
  294. // asDWORD = 32 bits
  295. // asQWORD = 64 bits
  296. // asPWORD = size of pointer
  297. //
  298. typedef unsigned char asBYTE;
  299. typedef unsigned short asWORD;
  300. typedef unsigned int asUINT;
  301. typedef size_t asPWORD;
  302. #ifdef __LP64__
  303. typedef unsigned int asDWORD;
  304. typedef unsigned long asQWORD;
  305. typedef long asINT64;
  306. #else
  307. typedef unsigned long asDWORD;
  308. #if defined(__GNUC__) || defined(__MWERKS__)
  309. typedef unsigned long long asQWORD;
  310. typedef long long asINT64;
  311. #else
  312. typedef unsigned __int64 asQWORD;
  313. typedef __int64 asINT64;
  314. #endif
  315. #endif
  316. typedef void (*asFUNCTION_t)();
  317. typedef void (*asGENFUNC_t)(asIScriptGeneric *);
  318. typedef void *(*asALLOCFUNC_t)(size_t);
  319. typedef void (*asFREEFUNC_t)(void *);
  320. #define asFUNCTION(f) asFunctionPtr(f)
  321. #if defined(_MSC_VER) && _MSC_VER <= 1200
  322. // MSVC 6 has a bug that prevents it from properly compiling using the correct asFUNCTIONPR with operator >
  323. // so we need to use ordinary C style cast instead of static_cast. The drawback is that the compiler can't
  324. // check that the cast is really valid.
  325. #define asFUNCTIONPR(f,p,r) asFunctionPtr((void (*)())((r (*)p)(f)))
  326. #else
  327. #define asFUNCTIONPR(f,p,r) asFunctionPtr((void (*)())(static_cast<r (*)p>(f)))
  328. #endif
  329. #ifndef AS_NO_CLASS_METHODS
  330. class asCUnknownClass;
  331. typedef void (asCUnknownClass::*asMETHOD_t)();
  332. struct asSFuncPtr
  333. {
  334. union
  335. {
  336. // The largest known method point is 20 bytes (MSVC 64bit),
  337. // but with 8byte alignment this becomes 24 bytes. So we need
  338. // to be able to store at least that much.
  339. char dummy[25];
  340. struct {asMETHOD_t mthd; char dummy[25-sizeof(asMETHOD_t)];} m;
  341. struct {asFUNCTION_t func; char dummy[25-sizeof(asFUNCTION_t)];} f;
  342. } ptr;
  343. asBYTE flag; // 1 = generic, 2 = global func, 3 = method
  344. };
  345. #define asMETHOD(c,m) asSMethodPtr<sizeof(void (c::*)())>::Convert((void (c::*)())(&c::m))
  346. #define asMETHODPR(c,m,p,r) asSMethodPtr<sizeof(void (c::*)())>::Convert(static_cast<r (c::*)p>(&c::m))
  347. #else // Class methods are disabled
  348. struct asSFuncPtr
  349. {
  350. union
  351. {
  352. char dummy[25]; // largest known class method pointer
  353. struct {asFUNCTION_t func; char dummy[25-sizeof(asFUNCTION_t)];} f;
  354. } ptr;
  355. asBYTE flag; // 1 = generic, 2 = global func
  356. };
  357. #endif
  358. struct asSMessageInfo
  359. {
  360. const char *section;
  361. int row;
  362. int col;
  363. asEMsgType type;
  364. const char *message;
  365. };
  366. // API functions
  367. // ANGELSCRIPT_EXPORT is defined when compiling the dll or lib
  368. // ANGELSCRIPT_DLL_LIBRARY_IMPORT is defined when dynamically linking to the
  369. // dll through the link lib automatically generated by MSVC++
  370. // ANGELSCRIPT_DLL_MANUAL_IMPORT is defined when manually loading the dll
  371. // Don't define anything when linking statically to the lib
  372. #ifdef WIN32
  373. #ifdef ANGELSCRIPT_EXPORT
  374. #define AS_API __declspec(dllexport)
  375. #elif defined ANGELSCRIPT_DLL_LIBRARY_IMPORT
  376. #define AS_API __declspec(dllimport)
  377. #else // statically linked library
  378. #define AS_API
  379. #endif
  380. #else
  381. #define AS_API
  382. #endif
  383. #ifndef ANGELSCRIPT_DLL_MANUAL_IMPORT
  384. extern "C"
  385. {
  386. // Engine
  387. AS_API asIScriptEngine * asCreateScriptEngine(asDWORD version);
  388. AS_API const char * asGetLibraryVersion();
  389. AS_API const char * asGetLibraryOptions();
  390. // Context
  391. AS_API asIScriptContext * asGetActiveContext();
  392. // Thread support
  393. AS_API int asThreadCleanup();
  394. // Memory management
  395. AS_API int asSetGlobalMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc);
  396. AS_API int asResetGlobalMemoryFunctions();
  397. }
  398. #endif // ANGELSCRIPT_DLL_MANUAL_IMPORT
  399. // Interface declarations
  400. class asIScriptEngine
  401. {
  402. public:
  403. // Memory management
  404. virtual int AddRef() = 0;
  405. virtual int Release() = 0;
  406. // Engine properties
  407. virtual int SetEngineProperty(asEEngineProp property, asPWORD value) = 0;
  408. virtual asPWORD GetEngineProperty(asEEngineProp property) = 0;
  409. // Compiler messages
  410. virtual int SetMessageCallback(const asSFuncPtr &callback, void *obj, asDWORD callConv) = 0;
  411. virtual int ClearMessageCallback() = 0;
  412. virtual int WriteMessage(const char *section, int row, int col, asEMsgType type, const char *message) = 0;
  413. // Global functions
  414. virtual int RegisterGlobalFunction(const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv) = 0;
  415. virtual int GetGlobalFunctionCount() = 0;
  416. virtual int GetGlobalFunctionIdByIndex(asUINT index) = 0;
  417. // Global properties
  418. virtual int RegisterGlobalProperty(const char *declaration, void *pointer) = 0;
  419. virtual int GetGlobalPropertyCount() = 0;
  420. virtual int GetGlobalPropertyByIndex(asUINT index, const char **name, int *typeId = 0, bool *isConst = 0, const char **configGroup = 0, void **pointer = 0) = 0;
  421. // Object types
  422. virtual int RegisterObjectType(const char *obj, int byteSize, asDWORD flags) = 0;
  423. virtual int RegisterObjectProperty(const char *obj, const char *declaration, int byteOffset) = 0;
  424. virtual int RegisterObjectMethod(const char *obj, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv) = 0;
  425. virtual int RegisterObjectBehaviour(const char *obj, asEBehaviours behaviour, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv) = 0;
  426. virtual int RegisterGlobalBehaviour(asEBehaviours behaviour, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv) = 0;
  427. virtual int RegisterInterface(const char *name) = 0;
  428. virtual int RegisterInterfaceMethod(const char *intf, const char *declaration) = 0;
  429. virtual int GetObjectTypeCount() = 0;
  430. virtual asIObjectType *GetObjectTypeByIndex(asUINT index) = 0;
  431. virtual int GetGlobalBehaviourCount() = 0;
  432. virtual int GetGlobalBehaviourByIndex(asUINT index, asEBehaviours *outBehaviour) = 0;
  433. // String factory
  434. virtual int RegisterStringFactory(const char *datatype, const asSFuncPtr &factoryFunc, asDWORD callConv) = 0;
  435. virtual int GetStringFactoryReturnTypeId() = 0;
  436. // Enums
  437. virtual int RegisterEnum(const char *type) = 0;
  438. virtual int RegisterEnumValue(const char *type, const char *name, int value) = 0;
  439. virtual int GetEnumCount() = 0;
  440. virtual const char *GetEnumByIndex(asUINT index, int *enumTypeId, const char **configGroup = 0) = 0;
  441. virtual int GetEnumValueCount(int enumTypeId) = 0;
  442. virtual const char *GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) = 0;
  443. // Typedefs
  444. virtual int RegisterTypedef(const char *type, const char *decl) = 0;
  445. virtual int GetTypedefCount() = 0;
  446. virtual const char *GetTypedefByIndex(asUINT index, int *typeId, const char **configGroup = 0) = 0;
  447. // Configuration groups
  448. virtual int BeginConfigGroup(const char *groupName) = 0;
  449. virtual int EndConfigGroup() = 0;
  450. virtual int RemoveConfigGroup(const char *groupName) = 0;
  451. virtual int SetConfigGroupModuleAccess(const char *groupName, const char *module, bool hasAccess) = 0;
  452. // Script modules
  453. virtual asIScriptModule *GetModule(const char *module, asEGMFlags flag = asGM_ONLY_IF_EXISTS) = 0;
  454. virtual int DiscardModule(const char *module) = 0;
  455. // Script functions
  456. virtual asIScriptFunction *GetFunctionDescriptorById(int funcId) = 0;
  457. // Type identification
  458. virtual asIObjectType *GetObjectTypeById(int typeId) = 0;
  459. virtual int GetTypeIdByDecl(const char *decl) = 0;
  460. virtual const char *GetTypeDeclaration(int typeId) = 0;
  461. virtual int GetSizeOfPrimitiveType(int typeId) = 0;
  462. // Script execution
  463. virtual asIScriptContext *CreateContext() = 0;
  464. virtual void *CreateScriptObject(int typeId) = 0;
  465. virtual void *CreateScriptObjectCopy(void *obj, int typeId) = 0;
  466. virtual void CopyScriptObject(void *dstObj, void *srcObj, int typeId) = 0;
  467. virtual void ReleaseScriptObject(void *obj, int typeId) = 0;
  468. virtual void AddRefScriptObject(void *obj, int typeId) = 0;
  469. virtual bool IsHandleCompatibleWithObject(void *obj, int objTypeId, int handleTypeId) = 0;
  470. virtual int CompareScriptObjects(bool &result, int behaviour, void *leftObj, void *rightObj, int typeId) = 0;
  471. // String interpretation
  472. virtual asETokenClass ParseToken(const char *string, size_t stringLength = 0, int *tokenLength = 0) = 0;
  473. virtual int ExecuteString(const char *module, const char *script, asIScriptContext **ctx = 0, asDWORD flags = 0) = 0;
  474. // Garbage collection
  475. virtual int GarbageCollect(asDWORD flags = asGC_FULL_CYCLE) = 0;
  476. virtual void GetGCStatistics(asUINT *currentSize, asUINT *totalDestroyed = 0, asUINT *totalDetected = 0) = 0;
  477. virtual void NotifyGarbageCollectorOfNewObject(void *obj, int typeId) = 0;
  478. virtual void GCEnumCallback(void *reference) = 0;
  479. // User data
  480. virtual void *SetUserData(void *data) = 0;
  481. virtual void *GetUserData() = 0;
  482. #ifdef AS_DEPRECATED
  483. virtual int AddScriptSection(const char *module, const char *name, const char *code, size_t codeLength = 0, int lineOffset = 0) = 0;
  484. virtual int Build(const char *module) = 0;
  485. virtual int Discard(const char *module) = 0;
  486. virtual int ResetModule(const char *module) = 0;
  487. virtual int GetFunctionCount(const char *module) = 0;
  488. virtual int GetFunctionIDByIndex(const char *module, int index) = 0;
  489. virtual int GetFunctionIDByName(const char *module, const char *name) = 0;
  490. virtual int GetFunctionIDByDecl(const char *module, const char *decl) = 0;
  491. virtual asIScriptFunction *GetFunctionDescriptorByIndex(const char *module, int index) = 0;
  492. virtual int GetGlobalVarCount(const char *module) = 0;
  493. virtual int GetGlobalVarIndexByName(const char *module, const char *name) = 0;
  494. virtual int GetGlobalVarIndexByDecl(const char *module, const char *decl) = 0;
  495. virtual const char *GetGlobalVarDeclaration(const char *module, int index, int *length = 0) = 0;
  496. virtual const char *GetGlobalVarName(const char *module, int index, int *length = 0) = 0;
  497. virtual void *GetAddressOfGlobalVar(const char *module, int index) = 0;
  498. virtual int GetTypeIdByDecl(const char *module, const char *decl) = 0;
  499. virtual int GetImportedFunctionCount(const char *module) = 0;
  500. virtual int GetImportedFunctionIndexByDecl(const char *module, const char *decl) = 0;
  501. virtual const char *GetImportedFunctionDeclaration(const char *module, int importIndex, int *length = 0) = 0;
  502. virtual const char *GetImportedFunctionSourceModule(const char *module, int importIndex, int *length = 0) = 0;
  503. virtual int BindImportedFunction(const char *module, int importIndex, int funcId) = 0;
  504. virtual int UnbindImportedFunction(const char *module, int importIndex) = 0;
  505. virtual int BindAllImportedFunctions(const char *module) = 0;
  506. virtual int UnbindAllImportedFunctions(const char *module) = 0;
  507. virtual int GetObjectsInGarbageCollectorCount() = 0;
  508. virtual int SaveByteCode(const char *module, asIBinaryStream *out) = 0;
  509. virtual int LoadByteCode(const char *module, asIBinaryStream *in) = 0;
  510. #endif
  511. protected:
  512. virtual ~asIScriptEngine() {}
  513. };
  514. class asIScriptModule
  515. {
  516. public:
  517. virtual asIScriptEngine *GetEngine() = 0;
  518. virtual void SetName(const char *name) = 0;
  519. virtual const char *GetName() = 0;
  520. // Compilation
  521. virtual int AddScriptSection(const char *name, const char *code, size_t codeLength = 0, int lineOffset = 0) = 0;
  522. virtual int Build() = 0;
  523. // Functions
  524. virtual int GetFunctionCount() = 0;
  525. virtual int GetFunctionIdByIndex(int index) = 0;
  526. virtual int GetFunctionIdByName(const char *name) = 0;
  527. virtual int GetFunctionIdByDecl(const char *decl) = 0;
  528. virtual asIScriptFunction *GetFunctionDescriptorByIndex(int index) = 0;
  529. virtual asIScriptFunction *GetFunctionDescriptorById(int funcId) = 0;
  530. // Global variables
  531. virtual int ResetGlobalVars() = 0;
  532. virtual int GetGlobalVarCount() = 0;
  533. virtual int GetGlobalVarIndexByName(const char *name) = 0;
  534. virtual int GetGlobalVarIndexByDecl(const char *decl) = 0;
  535. virtual const char *GetGlobalVarDeclaration(int index) = 0;
  536. virtual const char *GetGlobalVarName(int index) = 0;
  537. virtual int GetGlobalVarTypeId(int index, bool *isConst = 0) = 0;
  538. virtual void *GetAddressOfGlobalVar(int index) = 0;
  539. // Type identification
  540. virtual int GetObjectTypeCount() = 0;
  541. virtual asIObjectType *GetObjectTypeByIndex(asUINT index) = 0;
  542. virtual int GetTypeIdByDecl(const char *decl) = 0;
  543. // Enums
  544. virtual int GetEnumCount() = 0;
  545. virtual const char *GetEnumByIndex(asUINT index, int *enumTypeId) = 0;
  546. virtual int GetEnumValueCount(int enumTypeId) = 0;
  547. virtual const char *GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) = 0;
  548. // Typedefs
  549. virtual int GetTypedefCount() = 0;
  550. virtual const char *GetTypedefByIndex(asUINT index, int *typeId) = 0;
  551. // Dynamic binding between modules
  552. virtual int GetImportedFunctionCount() = 0;
  553. virtual int GetImportedFunctionIndexByDecl(const char *decl) = 0;
  554. virtual const char *GetImportedFunctionDeclaration(int importIndex) = 0;
  555. virtual const char *GetImportedFunctionSourceModule(int importIndex) = 0;
  556. virtual int BindImportedFunction(int importIndex, int funcId) = 0;
  557. virtual int UnbindImportedFunction(int importIndex) = 0;
  558. virtual int BindAllImportedFunctions() = 0;
  559. virtual int UnbindAllImportedFunctions() = 0;
  560. // Bytecode saving and loading
  561. virtual int SaveByteCode(asIBinaryStream *out) = 0;
  562. virtual int LoadByteCode(asIBinaryStream *in) = 0;
  563. protected:
  564. virtual ~asIScriptModule() {}
  565. };
  566. class asIScriptContext
  567. {
  568. public:
  569. // Memory management
  570. virtual int AddRef() = 0;
  571. virtual int Release() = 0;
  572. // Miscellaneous
  573. virtual asIScriptEngine *GetEngine() = 0;
  574. // Execution
  575. virtual int Prepare(int funcId) = 0;
  576. virtual int Unprepare() = 0;
  577. virtual int SetObject(void *obj) = 0;
  578. virtual int Execute() = 0;
  579. virtual int Abort() = 0;
  580. virtual int Suspend() = 0;
  581. virtual asEContextState GetState() = 0;
  582. // Arguments
  583. virtual int SetArgByte(asUINT arg, asBYTE value) = 0;
  584. virtual int SetArgWord(asUINT arg, asWORD value) = 0;
  585. virtual int SetArgDWord(asUINT arg, asDWORD value) = 0;
  586. virtual int SetArgQWord(asUINT arg, asQWORD value) = 0;
  587. virtual int SetArgFloat(asUINT arg, float value) = 0;
  588. virtual int SetArgDouble(asUINT arg, double value) = 0;
  589. virtual int SetArgAddress(asUINT arg, void *addr) = 0;
  590. virtual int SetArgObject(asUINT arg, void *obj) = 0;
  591. virtual void *GetArgPointer(asUINT arg) = 0;
  592. // Return value
  593. virtual asBYTE GetReturnByte() = 0;
  594. virtual asWORD GetReturnWord() = 0;
  595. virtual asDWORD GetReturnDWord() = 0;
  596. virtual asQWORD GetReturnQWord() = 0;
  597. virtual float GetReturnFloat() = 0;
  598. virtual double GetReturnDouble() = 0;
  599. virtual void *GetReturnAddress() = 0;
  600. virtual void *GetReturnObject() = 0;
  601. virtual void *GetAddressOfReturnValue() = 0;
  602. // Exception handling
  603. virtual int SetException(const char *string) = 0;
  604. virtual int GetExceptionLineNumber(int *column = 0) = 0;
  605. virtual int GetExceptionFunction() = 0;
  606. virtual const char *GetExceptionString() = 0;
  607. virtual int SetExceptionCallback(asSFuncPtr callback, void *obj, int callConv) = 0;
  608. virtual void ClearExceptionCallback() = 0;
  609. // Debugging
  610. virtual int SetLineCallback(asSFuncPtr callback, void *obj, int callConv) = 0;
  611. virtual void ClearLineCallback() = 0;
  612. virtual int GetCurrentLineNumber(int *column = 0) = 0;
  613. virtual int GetCurrentFunction() = 0;
  614. virtual int GetCallstackSize() = 0;
  615. virtual int GetCallstackFunction(int index) = 0;
  616. virtual int GetCallstackLineNumber(int index, int *column = 0) = 0;
  617. virtual int GetVarCount(int stackLevel = -1) = 0;
  618. virtual const char *GetVarName(int varIndex, int stackLevel = -1) = 0;
  619. virtual const char *GetVarDeclaration(int varIndex, int stackLevel = -1) = 0;
  620. virtual int GetVarTypeId(int varIndex, int stackLevel = -1) = 0;
  621. virtual void *GetAddressOfVar(int varIndex, int stackLevel = -1) = 0;
  622. virtual int GetThisTypeId(int stackLevel = -1) = 0;
  623. virtual void *GetThisPointer(int stackLevel = -1) = 0;
  624. // User data
  625. virtual void *SetUserData(void *data) = 0;
  626. virtual void *GetUserData() = 0;
  627. #ifdef AS_DEPRECATED
  628. virtual void *GetReturnPointer() = 0;
  629. virtual void *GetVarPointer(int varIndex, int stackLevel = -1) = 0;
  630. #endif
  631. protected:
  632. virtual ~asIScriptContext() {}
  633. };
  634. class asIScriptGeneric
  635. {
  636. public:
  637. // Miscellaneous
  638. virtual asIScriptEngine *GetEngine() = 0;
  639. virtual int GetFunctionId() = 0;
  640. // Object
  641. virtual void *GetObject() = 0;
  642. virtual int GetObjectTypeId() = 0;
  643. // Arguments
  644. virtual int GetArgCount() = 0;
  645. virtual int GetArgTypeId(asUINT arg) = 0;
  646. virtual asBYTE GetArgByte(asUINT arg) = 0;
  647. virtual asWORD GetArgWord(asUINT arg) = 0;
  648. virtual asDWORD GetArgDWord(asUINT arg) = 0;
  649. virtual asQWORD GetArgQWord(asUINT arg) = 0;
  650. virtual float GetArgFloat(asUINT arg) = 0;
  651. virtual double GetArgDouble(asUINT arg) = 0;
  652. virtual void *GetArgAddress(asUINT arg) = 0;
  653. virtual void *GetArgObject(asUINT arg) = 0;
  654. virtual void *GetAddressOfArg(asUINT arg) = 0;
  655. // Return value
  656. virtual int GetReturnTypeId() = 0;
  657. virtual int SetReturnByte(asBYTE val) = 0;
  658. virtual int SetReturnWord(asWORD val) = 0;
  659. virtual int SetReturnDWord(asDWORD val) = 0;
  660. virtual int SetReturnQWord(asQWORD val) = 0;
  661. virtual int SetReturnFloat(float val) = 0;
  662. virtual int SetReturnDouble(double val) = 0;
  663. virtual int SetReturnAddress(void *addr) = 0;
  664. virtual int SetReturnObject(void *obj) = 0;
  665. virtual void *GetAddressOfReturnLocation() = 0;
  666. #ifdef AS_DEPRECATED
  667. virtual void *GetReturnPointer() = 0;
  668. virtual void *GetArgPointer(asUINT arg) = 0;
  669. #endif
  670. protected:
  671. virtual ~asIScriptGeneric() {}
  672. };
  673. class asIScriptObject
  674. {
  675. public:
  676. // Memory management
  677. virtual int AddRef() = 0;
  678. virtual int Release() = 0;
  679. // Type info
  680. virtual int GetTypeId() const = 0;
  681. virtual asIObjectType *GetObjectType() const = 0;
  682. // Class properties
  683. virtual int GetPropertyCount() const = 0;
  684. virtual int GetPropertyTypeId(asUINT prop) const = 0;
  685. virtual const char *GetPropertyName(asUINT prop) const = 0;
  686. virtual void *GetPropertyPointer(asUINT prop) = 0;
  687. virtual asIScriptEngine *GetEngine() const = 0;
  688. virtual int CopyFrom(asIScriptObject *other) = 0;
  689. #ifdef AS_DEPRECATED
  690. virtual int GetStructTypeId() const = 0;
  691. #endif
  692. protected:
  693. virtual ~asIScriptObject() {}
  694. };
  695. class asIScriptArray
  696. {
  697. public:
  698. virtual asIScriptEngine *GetEngine() const = 0;
  699. // Memory management
  700. virtual int AddRef() = 0;
  701. virtual int Release() = 0;
  702. // Array type
  703. virtual int GetArrayTypeId() = 0;
  704. // Elements
  705. virtual int GetElementTypeId() = 0;
  706. virtual asUINT GetElementCount() = 0;
  707. virtual void * GetElementPointer(asUINT index) = 0;
  708. virtual void Resize(asUINT size) = 0;
  709. virtual int CopyFrom(asIScriptArray *other) = 0;
  710. protected:
  711. virtual ~asIScriptArray() {}
  712. };
  713. class asIObjectType
  714. {
  715. public:
  716. virtual asIScriptEngine *GetEngine() const = 0;
  717. virtual const char *GetConfigGroup() const = 0;
  718. // Type info
  719. virtual const char *GetName() const = 0;
  720. virtual asIObjectType *GetBaseType() const = 0;
  721. virtual asDWORD GetFlags() const = 0;
  722. virtual asUINT GetSize() const = 0;
  723. // Interfaces
  724. virtual int GetInterfaceCount() const = 0;
  725. virtual asIObjectType *GetInterface(asUINT index) const = 0;
  726. // Factories
  727. virtual int GetFactoryCount() const = 0;
  728. virtual int GetFactoryIdByIndex(int index) const = 0;
  729. virtual int GetFactoryIdByDecl(const char *decl) const = 0;
  730. // Methods
  731. virtual int GetMethodCount() const = 0;
  732. virtual int GetMethodIdByIndex(int index) const = 0;
  733. virtual int GetMethodIdByName(const char *name) const = 0;
  734. virtual int GetMethodIdByDecl(const char *decl) const = 0;
  735. virtual asIScriptFunction *GetMethodDescriptorByIndex(int index) const = 0;
  736. // Properties
  737. virtual int GetPropertyCount() const = 0;
  738. virtual int GetPropertyTypeId(asUINT prop) const = 0;
  739. virtual const char *GetPropertyName(asUINT prop) const = 0;
  740. virtual int GetPropertyOffset(asUINT prop) const = 0;
  741. // Behaviours
  742. virtual int GetBehaviourCount() const = 0;
  743. virtual int GetBehaviourByIndex(asUINT index, asEBehaviours *outBehaviour) const = 0;
  744. #ifdef AS_DEPRECATED
  745. virtual asIObjectType *GetSubType() const = 0;
  746. virtual bool IsInterface() const = 0;
  747. #endif
  748. protected:
  749. virtual ~asIObjectType() {}
  750. };
  751. class asIScriptFunction
  752. {
  753. public:
  754. virtual asIScriptEngine *GetEngine() const = 0;
  755. virtual const char *GetModuleName() const = 0;
  756. virtual const char *GetScriptSectionName() const = 0;
  757. virtual const char *GetConfigGroup() const = 0;
  758. virtual asIObjectType *GetObjectType() const = 0;
  759. virtual const char *GetObjectName() const = 0;
  760. virtual const char *GetName() const = 0;
  761. virtual const char *GetDeclaration(bool includeObjectName = true) const = 0;
  762. virtual bool IsClassMethod() const = 0;
  763. virtual bool IsInterfaceMethod() const = 0;
  764. virtual int GetParamCount() const = 0;
  765. virtual int GetParamTypeId(int index, asDWORD *flags = 0) const = 0;
  766. virtual int GetReturnTypeId() const = 0;
  767. protected:
  768. virtual ~asIScriptFunction() {};
  769. };
  770. class asIBinaryStream
  771. {
  772. public:
  773. virtual void Read(void *ptr, asUINT size) = 0;
  774. virtual void Write(const void *ptr, asUINT size) = 0;
  775. public:
  776. virtual ~asIBinaryStream() {}
  777. };
  778. //-----------------------------------------------------------------
  779. // Function pointers
  780. // Use our own memset() and memcpy() implementations for better portability
  781. inline void asMemClear(void *_p, int size)
  782. {
  783. char *p = (char *)_p;
  784. const char *e = p + size;
  785. for( ; p < e; p++ )
  786. *p = 0;
  787. }
  788. inline void asMemCopy(void *_d, const void *_s, int size)
  789. {
  790. char *d = (char *)_d;
  791. const char *s = (const char *)_s;
  792. const char *e = s + size;
  793. for( ; s < e; d++, s++ )
  794. *d = *s;
  795. }
  796. // Template function to capture all global functions,
  797. // except the ones using the generic calling convention
  798. template <class T>
  799. inline asSFuncPtr asFunctionPtr(T func)
  800. {
  801. asSFuncPtr p;
  802. asMemClear(&p, sizeof(p));
  803. p.ptr.f.func = (asFUNCTION_t)(size_t)func;
  804. // Mark this as a global function
  805. p.flag = 2;
  806. return p;
  807. }
  808. // Specialization for functions using the generic calling convention
  809. template<>
  810. inline asSFuncPtr asFunctionPtr<asGENFUNC_t>(asGENFUNC_t func)
  811. {
  812. asSFuncPtr p;
  813. asMemClear(&p, sizeof(p));
  814. p.ptr.f.func = (asFUNCTION_t)func;
  815. // Mark this as a generic function
  816. p.flag = 1;
  817. return p;
  818. }
  819. #ifndef AS_NO_CLASS_METHODS
  820. // Method pointers
  821. // Declare a dummy class so that we can determine the size of a simple method pointer
  822. class asCSimpleDummy {};
  823. typedef void (asCSimpleDummy::*asSIMPLEMETHOD_t)();
  824. const int SINGLE_PTR_SIZE = sizeof(asSIMPLEMETHOD_t);
  825. // Define template
  826. template <int N>
  827. struct asSMethodPtr
  828. {
  829. template<class M>
  830. static asSFuncPtr Convert(M Mthd)
  831. {
  832. // This version of the function should never be executed, nor compiled,
  833. // as it would mean that the size of the method pointer cannot be determined.
  834. int ERROR_UnsupportedMethodPtr[N-100];
  835. return 0;
  836. }
  837. };
  838. // Template specialization
  839. template <>
  840. struct asSMethodPtr<SINGLE_PTR_SIZE>
  841. {
  842. template<class M>
  843. static asSFuncPtr Convert(M Mthd)
  844. {
  845. asSFuncPtr p;
  846. asMemClear(&p, sizeof(p));
  847. asMemCopy(&p, &Mthd, SINGLE_PTR_SIZE);
  848. // Mark this as a class method
  849. p.flag = 3;
  850. return p;
  851. }
  852. };
  853. #if defined(_MSC_VER) && !defined(__MWERKS__)
  854. // MSVC and Intel uses different sizes for different class method pointers
  855. template <>
  856. struct asSMethodPtr<SINGLE_PTR_SIZE+1*sizeof(int)>
  857. {
  858. template <class M>
  859. static asSFuncPtr Convert(M Mthd)
  860. {
  861. asSFuncPtr p;
  862. asMemClear(&p, sizeof(p));
  863. asMemCopy(&p, &Mthd, SINGLE_PTR_SIZE+sizeof(int));
  864. // Mark this as a class method
  865. p.flag = 3;
  866. return p;
  867. }
  868. };
  869. template <>
  870. struct asSMethodPtr<SINGLE_PTR_SIZE+2*sizeof(int)>
  871. {
  872. template <class M>
  873. static asSFuncPtr Convert(M Mthd)
  874. {
  875. // This is where a class with virtual inheritance falls, or
  876. // on 64bit platforms with 8byte data alignments.
  877. // Method pointers for virtual inheritance is not supported,
  878. // as it requires the location of the vbase table, which is
  879. // only available to the C++ compiler, but not in the method
  880. // pointer.
  881. // You can get around this by forward declaring the class and
  882. // storing the sizeof its method pointer in a constant. Example:
  883. // class ClassWithVirtualInheritance;
  884. // const int ClassWithVirtualInheritance_workaround = sizeof(void ClassWithVirtualInheritance::*());
  885. // This will force the compiler to use the unknown type
  886. // for the class, which falls under the next case
  887. // TODO: We need to try to identify if this is really a method pointer
  888. // with virtual inheritance, or just a method pointer for multiple
  889. // inheritance with pad bytes to produce a 16byte structure.
  890. asSFuncPtr p;
  891. asMemClear(&p, sizeof(p));
  892. asMemCopy(&p, &Mthd, SINGLE_PTR_SIZE+2*sizeof(int));
  893. // Mark this as a class method
  894. p.flag = 3;
  895. return p;
  896. }
  897. };
  898. template <>
  899. struct asSMethodPtr<SINGLE_PTR_SIZE+3*sizeof(int)>
  900. {
  901. template <class M>
  902. static asSFuncPtr Convert(M Mthd)
  903. {
  904. asSFuncPtr p;
  905. asMemClear(&p, sizeof(p));
  906. asMemCopy(&p, &Mthd, SINGLE_PTR_SIZE+3*sizeof(int));
  907. // Mark this as a class method
  908. p.flag = 3;
  909. return p;
  910. }
  911. };
  912. template <>
  913. struct asSMethodPtr<SINGLE_PTR_SIZE+4*sizeof(int)>
  914. {
  915. template <class M>
  916. static asSFuncPtr Convert(M Mthd)
  917. {
  918. // On 64bit platforms with 8byte data alignment
  919. // the unknown class method pointers will come here.
  920. asSFuncPtr p;
  921. asMemClear(&p, sizeof(p));
  922. asMemCopy(&p, &Mthd, SINGLE_PTR_SIZE+4*sizeof(int));
  923. // Mark this as a class method
  924. p.flag = 3;
  925. return p;
  926. }
  927. };
  928. #endif
  929. #endif // AS_NO_CLASS_METHODS
  930. END_AS_NAMESPACE
  931. #endif