as_module.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2017 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. class asCTypedefType;
  47. class asCFuncdefType;
  48. struct asSNameSpace;
  49. struct sBindInfo
  50. {
  51. asCScriptFunction *importedFunctionSignature;
  52. asCString importFromModule;
  53. int boundFunctionId;
  54. };
  55. struct sObjectTypePair
  56. {
  57. asCObjectType *a;
  58. asCObjectType *b;
  59. };
  60. // TODO: import: Remove function imports. When I have implemented function
  61. // pointers the function imports should be deprecated.
  62. // TODO: Need a separate interface for compiling scripts. The asIScriptCompiler
  63. // will have a target module, and will allow the compilation of an entire
  64. // script or just individual functions within the scope of the module
  65. //
  66. // With this separation it will be possible to compile the library without
  67. // the compiler, thus giving a much smaller binary executable.
  68. // TODO: There should be a special compile option that will let the application
  69. // recompile an already compiled script. The compiler should check if no
  70. // destructive changes have been made (changing function signatures, etc)
  71. // then it should simply replace the bytecode within the functions without
  72. // changing the values of existing global properties, etc.
  73. class asCModule : public asIScriptModule
  74. {
  75. //-------------------------------------------
  76. // Public interface
  77. //--------------------------------------------
  78. public:
  79. virtual asIScriptEngine *GetEngine() const;
  80. virtual void SetName(const char *name);
  81. virtual const char *GetName() const;
  82. virtual void Discard();
  83. // Compilation
  84. virtual int AddScriptSection(const char *name, const char *code, size_t codeLength, int lineOffset);
  85. virtual int Build();
  86. virtual int CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD reserved, asIScriptFunction **outFunc);
  87. virtual int CompileGlobalVar(const char *sectionName, const char *code, int lineOffset);
  88. virtual asDWORD SetAccessMask(asDWORD accessMask);
  89. virtual int SetDefaultNamespace(const char *nameSpace);
  90. virtual const char *GetDefaultNamespace() const;
  91. // Script functions
  92. virtual asUINT GetFunctionCount() const;
  93. virtual asIScriptFunction *GetFunctionByIndex(asUINT index) const;
  94. virtual asIScriptFunction *GetFunctionByDecl(const char *decl) const;
  95. virtual asIScriptFunction *GetFunctionByName(const char *name) const;
  96. virtual int RemoveFunction(asIScriptFunction *func);
  97. // Script global variables
  98. // TODO: interface: Should be called InitGlobalVars, and should have a bool to reset in case already initialized
  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, bool includeNamespace) const;
  104. virtual int GetGlobalVar(asUINT index, const char **name, const char **nameSpace, 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 asITypeInfo *GetObjectTypeByIndex(asUINT index) const;
  110. #ifdef AS_DEPRECATED
  111. // Deprecated since 2.31.0, 2015-12-06
  112. virtual asITypeInfo *GetObjectTypeByName(const char *name) const;
  113. virtual asITypeInfo *GetObjectTypeByDecl(const char *decl) const;
  114. #endif
  115. virtual int GetTypeIdByDecl(const char *decl) const;
  116. virtual asITypeInfo *GetTypeInfoByName(const char *name) const;
  117. virtual asITypeInfo *GetTypeInfoByDecl(const char *decl) const;
  118. // Enums
  119. virtual asUINT GetEnumCount() const;
  120. virtual asITypeInfo *GetEnumByIndex(asUINT index) const;
  121. #ifdef AS_DEPRECATED
  122. // Deprecated since 2.31.0, 2015-12-06
  123. virtual int GetEnumValueCount(int enumTypeId) const;
  124. virtual const char * GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue) const;
  125. #endif
  126. // Typedefs
  127. virtual asUINT GetTypedefCount() const;
  128. virtual asITypeInfo *GetTypedefByIndex(asUINT index) const;
  129. // Dynamic binding between modules
  130. virtual asUINT GetImportedFunctionCount() const;
  131. virtual int GetImportedFunctionIndexByDecl(const char *decl) const;
  132. virtual const char *GetImportedFunctionDeclaration(asUINT importIndex) const;
  133. virtual const char *GetImportedFunctionSourceModule(asUINT importIndex) const;
  134. virtual int BindImportedFunction(asUINT index, asIScriptFunction *func);
  135. virtual int UnbindImportedFunction(asUINT importIndex);
  136. virtual int BindAllImportedFunctions();
  137. virtual int UnbindAllImportedFunctions();
  138. // Bytecode Saving/Loading
  139. virtual int SaveByteCode(asIBinaryStream *out, bool stripDebugInfo) const;
  140. virtual int LoadByteCode(asIBinaryStream *in, bool *wasDebugInfoStripped);
  141. // User data
  142. virtual void *SetUserData(void *data, asPWORD type);
  143. virtual void *GetUserData(asPWORD type) const;
  144. //-----------------------------------------------
  145. // Internal
  146. //-----------------------------------------------
  147. asCModule(const char *name, asCScriptEngine *engine);
  148. ~asCModule();
  149. //protected:
  150. friend class asCScriptEngine;
  151. friend class asCBuilder;
  152. friend class asCCompiler;
  153. friend class asCContext;
  154. friend class asCRestore;
  155. void InternalReset();
  156. bool IsEmpty() const;
  157. bool HasExternalReferences(bool shuttingDown);
  158. int CallInit(asIScriptContext *ctx);
  159. void CallExit();
  160. void JITCompile();
  161. #ifndef AS_NO_COMPILER
  162. 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 isProtected = false, bool isFinal = false, bool isOverride = false, bool isShared = false, asSNameSpace *ns = 0);
  163. int AddScriptFunction(asCScriptFunction *func);
  164. 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);
  165. int AddFuncDef(const asCString &name, asSNameSpace *ns, asCObjectType *parent);
  166. #endif
  167. int GetNextImportedFunctionId();
  168. asCScriptFunction *GetImportedFunction(int funcId) const;
  169. asCTypeInfo *GetType(const char *type, asSNameSpace *ns);
  170. asCObjectType *GetObjectType(const char *type, asSNameSpace *ns);
  171. asCGlobalProperty *AllocateGlobalProperty(const char *name, const asCDataType &dt, asSNameSpace *ns);
  172. void UninitializeGlobalProp(asCGlobalProperty *prop);
  173. asCString name;
  174. asCScriptEngine *engine;
  175. asCBuilder *builder;
  176. asCArray<asPWORD> userData;
  177. asDWORD accessMask;
  178. asSNameSpace *defaultNamespace;
  179. // This array holds all functions, class members, factories, etc that were compiled with the module.
  180. // These references hold an internal reference to the function object.
  181. asCArray<asCScriptFunction *> scriptFunctions; // increases ref count
  182. // This array holds global functions declared in the module. These references are not counted,
  183. // as the same pointer is always present in the scriptFunctions array too.
  184. asCSymbolTable<asCScriptFunction> globalFunctions; // doesn't increase ref count
  185. // This array holds imported functions in the module.
  186. asCArray<sBindInfo *> bindInformations; // increases ref count
  187. // This array holds template instance types created for the module's object types
  188. asCArray<asCObjectType*> templateInstances; // increases ref count
  189. // This array holds the global variables declared in the script
  190. asCSymbolTable<asCGlobalProperty> scriptGlobals; // increases ref count
  191. bool isGlobalVarInitialized;
  192. // This array holds class and interface types
  193. asCArray<asCObjectType*> classTypes; // increases ref count
  194. // This array holds enum types
  195. asCArray<asCEnumType*> enumTypes; // increases ref count
  196. // This array holds typedefs
  197. asCArray<asCTypedefType*> typeDefs; // increases ref count
  198. // This array holds the funcdefs declared in the module
  199. asCArray<asCFuncdefType*> funcDefs; // increases ref count
  200. #ifndef AS_NO_COMPILER
  201. // This array holds types that have been explicitly declared with 'external'
  202. asCArray<asCTypeInfo*> externalTypes; // doesn't increase ref count
  203. // This array holds functions that have been explicitly declared with 'external'
  204. asCArray<asCScriptFunction*> externalFunctions; // doesn't increase ref count
  205. #endif
  206. };
  207. END_AS_NAMESPACE
  208. #endif