as_module.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_module.h
  25. //
  26. // A class that holds a script module
  27. //
  28. #ifndef AS_MODULE_H
  29. #define AS_MODULE_H
  30. #include "as_config.h"
  31. #include "as_atomic.h"
  32. #include "as_string.h"
  33. #include "as_array.h"
  34. #include "as_datatype.h"
  35. #include "as_scriptfunction.h"
  36. #include "as_property.h"
  37. BEGIN_AS_NAMESPACE
  38. // TODO: import: Remove this when the imported functions are removed
  39. const int FUNC_IMPORTED = 0x40000000;
  40. class asCScriptEngine;
  41. class asCCompiler;
  42. class asCBuilder;
  43. class asCContext;
  44. class asCConfigGroup;
  45. struct sBindInfo
  46. {
  47. asCScriptFunction *importedFunctionSignature;
  48. asCString importFromModule;
  49. int boundFunctionId;
  50. };
  51. struct sObjectTypePair
  52. {
  53. asCObjectType *a;
  54. asCObjectType *b;
  55. };
  56. // TODO: import: Remove function imports. When I have implemented function
  57. // pointers the function imports should be deprecated.
  58. // TODO: Need a separate interface for compiling scripts. The asIScriptCompiler
  59. // will have a target module, and will allow the compilation of an entire
  60. // script or just individual functions within the scope of the module
  61. //
  62. // With this separation it will be possible to compile the library without
  63. // the compiler, thus giving a much smaller binary executable.
  64. // TODO: There should be an special compile option that will let the application
  65. // recompile an already compiled script. The compiler should check if no
  66. // destructive changes have been made (changing function signatures, etc)
  67. // then it should simply replace the bytecode within the functions without
  68. // changing the values of existing global properties, etc.
  69. class asCModule : public asIScriptModule
  70. {
  71. //-------------------------------------------
  72. // Public interface
  73. //--------------------------------------------
  74. public:
  75. virtual asIScriptEngine *GetEngine() const;
  76. virtual void SetName(const char *name);
  77. virtual const char *GetName() const;
  78. // Compilation
  79. virtual int AddScriptSection(const char *name, const char *code, size_t codeLength, int lineOffset);
  80. virtual int Build();
  81. virtual int CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD reserved, asIScriptFunction **outFunc);
  82. virtual int CompileGlobalVar(const char *sectionName, const char *code, int lineOffset);
  83. virtual asDWORD SetAccessMask(asDWORD accessMask);
  84. // Script functions
  85. virtual asUINT GetFunctionCount() const;
  86. virtual int GetFunctionIdByIndex(asUINT index) const;
  87. virtual int GetFunctionIdByName(const char *name) const;
  88. virtual int GetFunctionIdByDecl(const char *decl) const;
  89. virtual asIScriptFunction *GetFunctionByIndex(asUINT index) const;
  90. virtual asIScriptFunction *GetFunctionByDecl(const char *decl) const;
  91. virtual asIScriptFunction *GetFunctionByName(const char *name) const;
  92. #ifdef AS_DEPRECATED
  93. // deprecated since 2011-10-03
  94. virtual asIScriptFunction *GetFunctionDescriptorByIndex(asUINT index) const;
  95. virtual asIScriptFunction *GetFunctionDescriptorById(int funcId) const;
  96. #endif
  97. virtual int RemoveFunction(int funcId);
  98. // Script global variables
  99. virtual int ResetGlobalVars(asIScriptContext *ctx);
  100. virtual asUINT GetGlobalVarCount() const;
  101. virtual int GetGlobalVarIndexByName(const char *name) const;
  102. virtual int GetGlobalVarIndexByDecl(const char *decl) const;
  103. virtual const char *GetGlobalVarDeclaration(asUINT index) const;
  104. virtual int GetGlobalVar(asUINT index, const char **name, int *typeId, bool *isConst) const;
  105. virtual void *GetAddressOfGlobalVar(asUINT index);
  106. virtual int RemoveGlobalVar(asUINT index);
  107. // Type identification
  108. virtual asUINT GetObjectTypeCount() const;
  109. virtual asIObjectType *GetObjectTypeByIndex(asUINT index) const;
  110. // TODO: interface: Should have GetObjectTypeByName
  111. virtual int GetTypeIdByDecl(const char *decl) const;
  112. // Enums
  113. virtual asUINT GetEnumCount() const;
  114. virtual const char *GetEnumByIndex(asUINT index, int *enumTypeId) const;
  115. virtual int GetEnumValueCount(int enumTypeId) const;
  116. virtual const char *GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const;
  117. // Typedefs
  118. virtual asUINT GetTypedefCount() const;
  119. virtual const char *GetTypedefByIndex(asUINT index, int *typeId) const;
  120. // Dynamic binding between modules
  121. virtual asUINT GetImportedFunctionCount() const;
  122. virtual int GetImportedFunctionIndexByDecl(const char *decl) const;
  123. virtual const char *GetImportedFunctionDeclaration(asUINT importIndex) const;
  124. virtual const char *GetImportedFunctionSourceModule(asUINT importIndex) const;
  125. virtual int BindImportedFunction(asUINT index, int sourceID);
  126. virtual int UnbindImportedFunction(asUINT importIndex);
  127. virtual int BindAllImportedFunctions();
  128. virtual int UnbindAllImportedFunctions();
  129. // Bytecode Saving/Loading
  130. virtual int SaveByteCode(asIBinaryStream *out) const;
  131. virtual int LoadByteCode(asIBinaryStream *in);
  132. // User data
  133. virtual void *SetUserData(void *data);
  134. virtual void *GetUserData() const;
  135. //-----------------------------------------------
  136. // Internal
  137. //-----------------------------------------------
  138. asCModule(const char *name, asCScriptEngine *engine);
  139. ~asCModule();
  140. //protected:
  141. friend class asCScriptEngine;
  142. friend class asCBuilder;
  143. friend class asCCompiler;
  144. friend class asCContext;
  145. friend class asCRestore;
  146. void InternalReset();
  147. int CallInit(asIScriptContext *ctx);
  148. void CallExit();
  149. void JITCompile();
  150. int AddScriptFunction(int sectionIdx, int id, const char *name, const asCDataType &returnType, asCDataType *params, asETypeModifiers *inOutFlags, asCString **defaultArgs, int paramCount, bool isInterface, asCObjectType *objType = 0, bool isConstMethod = false, bool isGlobalFunction = false, bool isPrivate = false, bool isFinal = false, bool isOverride = false);
  151. int AddScriptFunction(asCScriptFunction *func);
  152. int AddImportedFunction(int id, const char *name, const asCDataType &returnType, asCDataType *params, asETypeModifiers *inOutFlags, int paramCount, const asCString &moduleName);
  153. int AddFuncDef(const char *name);
  154. int GetNextImportedFunctionId();
  155. void ResolveInterfaceIds(asCArray<void*> *substitutions = 0);
  156. bool AreInterfacesEqual(asCObjectType *a, asCObjectType *b, asCArray<sObjectTypePair> &equals);
  157. bool AreTypesEqual(const asCDataType &a, const asCDataType &b, asCArray<sObjectTypePair> &equals);
  158. asCScriptFunction *GetImportedFunction(int funcId) const;
  159. asCObjectType *GetObjectType(const char *type);
  160. asCGlobalProperty *AllocateGlobalProperty(const char *name, const asCDataType &dt);
  161. asCString name;
  162. asCScriptEngine *engine;
  163. asCBuilder *builder;
  164. void *userData;
  165. asDWORD accessMask;
  166. // This array holds all functions, class members, factories, etc that were compiled with the module
  167. asCArray<asCScriptFunction *> scriptFunctions;
  168. // This array holds global functions declared in the module
  169. asCArray<asCScriptFunction *> globalFunctions;
  170. // This array holds imported functions in the module
  171. asCArray<sBindInfo *> bindInformations;
  172. // This array holds the global variables declared in the script
  173. asCArray<asCGlobalProperty *> scriptGlobals;
  174. bool isGlobalVarInitialized;
  175. // This array holds class and interface types
  176. asCArray<asCObjectType*> classTypes;
  177. // This array holds enum types
  178. asCArray<asCObjectType*> enumTypes;
  179. // This array holds typedefs
  180. asCArray<asCObjectType*> typeDefs;
  181. // This array holds the funcdefs declared in the module
  182. asCArray<asCScriptFunction*> funcDefs;
  183. };
  184. END_AS_NAMESPACE
  185. #endif