as_module.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // TODO: interface: Should have user data in the modules as well. This may be
  70. // used to store extra information, such as meta-data and source code files
  71. class asCModule : public asIScriptModule
  72. {
  73. //-------------------------------------------
  74. // Public interface
  75. //--------------------------------------------
  76. public:
  77. virtual asIScriptEngine *GetEngine() const;
  78. virtual void SetName(const char *name);
  79. virtual const char *GetName() const;
  80. // Compilation
  81. virtual int AddScriptSection(const char *name, const char *code, size_t codeLength, int lineOffset);
  82. virtual int Build();
  83. virtual int CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD reserved, asIScriptFunction **outFunc);
  84. virtual int CompileGlobalVar(const char *sectionName, const char *code, int lineOffset);
  85. // Script functions
  86. virtual asUINT GetFunctionCount() const;
  87. virtual int GetFunctionIdByIndex(asUINT index) const;
  88. virtual int GetFunctionIdByName(const char *name) const;
  89. virtual int GetFunctionIdByDecl(const char *decl) const;
  90. virtual asIScriptFunction *GetFunctionDescriptorByIndex(asUINT index) const;
  91. virtual asIScriptFunction *GetFunctionDescriptorById(int funcId) const;
  92. virtual int RemoveFunction(int funcId);
  93. // Script global variables
  94. virtual int ResetGlobalVars(asIScriptContext *ctx);
  95. virtual asUINT GetGlobalVarCount() const;
  96. virtual int GetGlobalVarIndexByName(const char *name) const;
  97. virtual int GetGlobalVarIndexByDecl(const char *decl) const;
  98. virtual const char *GetGlobalVarDeclaration(asUINT index) const;
  99. virtual int GetGlobalVar(asUINT index, const char **name, int *typeId, bool *isConst) const;
  100. virtual void *GetAddressOfGlobalVar(asUINT index);
  101. virtual int RemoveGlobalVar(asUINT index);
  102. // Type identification
  103. virtual asUINT GetObjectTypeCount() const;
  104. virtual asIObjectType *GetObjectTypeByIndex(asUINT index) const;
  105. virtual int GetTypeIdByDecl(const char *decl) const;
  106. // Enums
  107. virtual asUINT GetEnumCount() const;
  108. virtual const char *GetEnumByIndex(asUINT index, int *enumTypeId) const;
  109. virtual int GetEnumValueCount(int enumTypeId) const;
  110. virtual const char *GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const;
  111. // Typedefs
  112. virtual asUINT GetTypedefCount() const;
  113. virtual const char *GetTypedefByIndex(asUINT index, int *typeId) const;
  114. // Dynamic binding between modules
  115. virtual asUINT GetImportedFunctionCount() const;
  116. virtual int GetImportedFunctionIndexByDecl(const char *decl) const;
  117. virtual const char *GetImportedFunctionDeclaration(asUINT importIndex) const;
  118. virtual const char *GetImportedFunctionSourceModule(asUINT importIndex) const;
  119. virtual int BindImportedFunction(asUINT index, int sourceID);
  120. virtual int UnbindImportedFunction(asUINT importIndex);
  121. virtual int BindAllImportedFunctions();
  122. virtual int UnbindAllImportedFunctions();
  123. // Bytecode Saving/Loading
  124. virtual int SaveByteCode(asIBinaryStream *out) const;
  125. virtual int LoadByteCode(asIBinaryStream *in);
  126. //-----------------------------------------------
  127. // Internal
  128. //-----------------------------------------------
  129. asCModule(const char *name, asCScriptEngine *engine);
  130. ~asCModule();
  131. //protected:
  132. friend class asCScriptEngine;
  133. friend class asCBuilder;
  134. friend class asCCompiler;
  135. friend class asCContext;
  136. friend class asCRestore;
  137. void InternalReset();
  138. int CallInit(asIScriptContext *ctx);
  139. void CallExit();
  140. void JITCompile();
  141. 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);
  142. int AddScriptFunction(asCScriptFunction *func);
  143. int AddImportedFunction(int id, const char *name, const asCDataType &returnType, asCDataType *params, asETypeModifiers *inOutFlags, int paramCount, const asCString &moduleName);
  144. int AddFuncDef(const char *name);
  145. int GetNextImportedFunctionId();
  146. void ResolveInterfaceIds(asCArray<void*> *substitutions = 0);
  147. bool AreInterfacesEqual(asCObjectType *a, asCObjectType *b, asCArray<sObjectTypePair> &equals);
  148. bool AreTypesEqual(const asCDataType &a, const asCDataType &b, asCArray<sObjectTypePair> &equals);
  149. asCScriptFunction *GetImportedFunction(int funcId) const;
  150. asCObjectType *GetObjectType(const char *type);
  151. asCGlobalProperty *AllocateGlobalProperty(const char *name, const asCDataType &dt);
  152. asCString name;
  153. asCScriptEngine *engine;
  154. asCBuilder *builder;
  155. // This array holds all functions, class members, factories, etc that were compiled with the module
  156. asCArray<asCScriptFunction *> scriptFunctions;
  157. // This array holds global functions declared in the module
  158. asCArray<asCScriptFunction *> globalFunctions;
  159. // This array holds imported functions in the module
  160. asCArray<sBindInfo *> bindInformations;
  161. // This array holds the global variables declared in the script
  162. asCArray<asCGlobalProperty *> scriptGlobals;
  163. bool isGlobalVarInitialized;
  164. // This array holds class and interface types
  165. asCArray<asCObjectType*> classTypes;
  166. // This array holds enum types
  167. asCArray<asCObjectType*> enumTypes;
  168. // This array holds typedefs
  169. asCArray<asCObjectType*> typeDefs;
  170. // This array holds the funcdefs declared in the module
  171. asCArray<asCScriptFunction*> funcDefs;
  172. };
  173. END_AS_NAMESPACE
  174. #endif