as_module.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_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_symboltable.h"
  32. #include "as_atomic.h"
  33. #include "as_string.h"
  34. #include "as_array.h"
  35. #include "as_datatype.h"
  36. #include "as_scriptfunction.h"
  37. #include "as_property.h"
  38. BEGIN_AS_NAMESPACE
  39. // TODO: import: Remove this when the imported functions are removed
  40. const int FUNC_IMPORTED = 0x40000000;
  41. class asCScriptEngine;
  42. class asCCompiler;
  43. class asCBuilder;
  44. class asCContext;
  45. class asCConfigGroup;
  46. struct asSNameSpace;
  47. struct sBindInfo
  48. {
  49. asCScriptFunction *importedFunctionSignature;
  50. asCString importFromModule;
  51. int boundFunctionId;
  52. };
  53. struct sObjectTypePair
  54. {
  55. asCObjectType *a;
  56. asCObjectType *b;
  57. };
  58. // TODO: import: Remove function imports. When I have implemented function
  59. // pointers the function imports should be deprecated.
  60. // TODO: Need a separate interface for compiling scripts. The asIScriptCompiler
  61. // will have a target module, and will allow the compilation of an entire
  62. // script or just individual functions within the scope of the module
  63. //
  64. // With this separation it will be possible to compile the library without
  65. // the compiler, thus giving a much smaller binary executable.
  66. // TODO: There should be an special compile option that will let the application
  67. // recompile an already compiled script. The compiler should check if no
  68. // destructive changes have been made (changing function signatures, etc)
  69. // then it should simply replace the bytecode within the functions without
  70. // changing the values of existing global properties, etc.
  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. virtual void Discard();
  81. // Compilation
  82. virtual int AddScriptSection(const char *name, const char *code, size_t codeLength, int lineOffset);
  83. virtual int Build();
  84. virtual int CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD reserved, asIScriptFunction **outFunc);
  85. virtual int CompileGlobalVar(const char *sectionName, const char *code, int lineOffset);
  86. virtual asDWORD SetAccessMask(asDWORD accessMask);
  87. virtual int SetDefaultNamespace(const char *nameSpace);
  88. virtual const char *GetDefaultNamespace() const;
  89. // Script functions
  90. virtual asUINT GetFunctionCount() const;
  91. virtual asIScriptFunction *GetFunctionByIndex(asUINT index) const;
  92. virtual asIScriptFunction *GetFunctionByDecl(const char *decl) const;
  93. virtual asIScriptFunction *GetFunctionByName(const char *name) const;
  94. virtual int RemoveFunction(asIScriptFunction *func);
  95. // Script global variables
  96. // TODO: interface: Should be called InitGlobalVars, and should have a bool to reset in case already initialized
  97. virtual int ResetGlobalVars(asIScriptContext *ctx);
  98. virtual asUINT GetGlobalVarCount() const;
  99. virtual int GetGlobalVarIndexByName(const char *name) const;
  100. virtual int GetGlobalVarIndexByDecl(const char *decl) const;
  101. virtual const char *GetGlobalVarDeclaration(asUINT index, bool includeNamespace) const;
  102. virtual int GetGlobalVar(asUINT index, const char **name, const char **nameSpace, int *typeId, bool *isConst) const;
  103. virtual void *GetAddressOfGlobalVar(asUINT index);
  104. virtual int RemoveGlobalVar(asUINT index);
  105. // Type identification
  106. virtual asUINT GetObjectTypeCount() const;
  107. virtual asIObjectType *GetObjectTypeByIndex(asUINT index) const;
  108. virtual asIObjectType *GetObjectTypeByName(const char *name) const;
  109. virtual asIObjectType *GetObjectTypeByDecl(const char *decl) const;
  110. virtual int GetTypeIdByDecl(const char *decl) const;
  111. // Enums
  112. virtual asUINT GetEnumCount() const;
  113. virtual const char *GetEnumByIndex(asUINT index, int *enumTypeId, const char **nameSpace) const;
  114. virtual int GetEnumValueCount(int enumTypeId) const;
  115. virtual const char *GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const;
  116. // Typedefs
  117. virtual asUINT GetTypedefCount() const;
  118. virtual const char *GetTypedefByIndex(asUINT index, int *typeId, const char **nameSpace) const;
  119. // Dynamic binding between modules
  120. virtual asUINT GetImportedFunctionCount() const;
  121. virtual int GetImportedFunctionIndexByDecl(const char *decl) const;
  122. virtual const char *GetImportedFunctionDeclaration(asUINT importIndex) const;
  123. virtual const char *GetImportedFunctionSourceModule(asUINT importIndex) const;
  124. virtual int BindImportedFunction(asUINT index, asIScriptFunction *func);
  125. virtual int UnbindImportedFunction(asUINT importIndex);
  126. virtual int BindAllImportedFunctions();
  127. virtual int UnbindAllImportedFunctions();
  128. // Bytecode Saving/Loading
  129. virtual int SaveByteCode(asIBinaryStream *out, bool stripDebugInfo) const;
  130. virtual int LoadByteCode(asIBinaryStream *in, bool *wasDebugInfoStripped);
  131. // User data
  132. virtual void *SetUserData(void *data, asPWORD type);
  133. virtual void *GetUserData(asPWORD type) const;
  134. //-----------------------------------------------
  135. // Internal
  136. //-----------------------------------------------
  137. asCModule(const char *name, asCScriptEngine *engine);
  138. ~asCModule();
  139. //protected:
  140. friend class asCScriptEngine;
  141. friend class asCBuilder;
  142. friend class asCCompiler;
  143. friend class asCContext;
  144. friend class asCRestore;
  145. void InternalReset();
  146. bool IsEmpty() const;
  147. int CallInit(asIScriptContext *ctx);
  148. void CallExit();
  149. void JITCompile();
  150. #ifndef AS_NO_COMPILER
  151. int AddScriptFunction(int sectionIdx, int declaredAt, int id, const asCString &name, const asCDataType &returnType, const asCArray<asCDataType> &params, const asCArray<asCString> &paramNames, const asCArray<asETypeModifiers> &inOutFlags, const asCArray<asCString *> &defaultArgs, bool isInterface, asCObjectType *objType = 0, bool isConstMethod = false, bool isGlobalFunction = false, bool isPrivate = false, bool isFinal = false, bool isOverride = false, bool isShared = false, asSNameSpace *ns = 0);
  152. int AddScriptFunction(asCScriptFunction *func);
  153. int AddImportedFunction(int id, const asCString &name, const asCDataType &returnType, const asCArray<asCDataType> &params, const asCArray<asETypeModifiers> &inOutFlags, const asCArray<asCString *> &defaultArgs, asSNameSpace *ns, const asCString &moduleName);
  154. int AddFuncDef(const asCString &name, asSNameSpace *ns);
  155. #endif
  156. int GetNextImportedFunctionId();
  157. asCScriptFunction *GetImportedFunction(int funcId) const;
  158. asCObjectType *GetObjectType(const char *type, asSNameSpace *ns);
  159. asCGlobalProperty *AllocateGlobalProperty(const char *name, const asCDataType &dt, asSNameSpace *ns);
  160. asCString name;
  161. asCScriptEngine *engine;
  162. asCBuilder *builder;
  163. asCArray<asPWORD> userData;
  164. asDWORD accessMask;
  165. asSNameSpace *defaultNamespace;
  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. asCSymbolTable<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. asCSymbolTable<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