as_module.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2020 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. // 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. virtual int GetTypeIdByDecl(const char *decl) const;
  111. virtual asITypeInfo *GetTypeInfoByName(const char *name) const;
  112. virtual asITypeInfo *GetTypeInfoByDecl(const char *decl) const;
  113. // Enums
  114. virtual asUINT GetEnumCount() const;
  115. virtual asITypeInfo *GetEnumByIndex(asUINT index) const;
  116. // Typedefs
  117. virtual asUINT GetTypedefCount() const;
  118. virtual asITypeInfo *GetTypedefByIndex(asUINT index) 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. bool HasExternalReferences(bool shuttingDown);
  148. int CallInit(asIScriptContext *ctx);
  149. void CallExit();
  150. int InitGlobalProp(asCGlobalProperty *prop, asIScriptContext *ctx);
  151. void JITCompile();
  152. #ifndef AS_NO_COMPILER
  153. 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 isGlobalFunction = false, asSFunctionTraits funcTraits = asSFunctionTraits(), asSNameSpace *ns = 0);
  154. int AddScriptFunction(asCScriptFunction *func);
  155. int AddImportedFunction(int id, const asCString &name, const asCDataType &returnType, const asCArray<asCDataType> &params, const asCArray<asETypeModifiers> &inOutFlags, const asCArray<asCString *> &defaultArgs, asSFunctionTraits funcTraits, asSNameSpace *ns, const asCString &moduleName);
  156. int AddFuncDef(const asCString &name, asSNameSpace *ns, asCObjectType *parent);
  157. #endif
  158. int GetNextImportedFunctionId();
  159. asCScriptFunction *GetImportedFunction(int funcId) const;
  160. asCTypeInfo *GetType(const asCString &type, asSNameSpace *ns) const;
  161. asCObjectType *GetObjectType(const char *type, asSNameSpace *ns) const;
  162. asCGlobalProperty *AllocateGlobalProperty(const char *name, const asCDataType &dt, asSNameSpace *ns);
  163. void UninitializeGlobalProp(asCGlobalProperty *prop);
  164. // Adds the class type to the module. The module assumes ownership of the reference without increasing it
  165. void AddClassType(asCObjectType*);
  166. // Adds the enum type to the module. The module assumes ownership of the reference without increasing it
  167. void AddEnumType(asCEnumType*);
  168. // Adds the typedef to the module. The module assumes ownership of the reference without increasing it
  169. void AddTypeDef(asCTypedefType*);
  170. // Adds the funcdef to the module. The module assumes ownership of the reference without increasing it
  171. void AddFuncDef(asCFuncdefType*);
  172. // Replaces an existing funcdef with another (used for shared funcdefs). Doesn't add or release refCounts
  173. void ReplaceFuncDef(asCFuncdefType *oldType, asCFuncdefType *newType);
  174. asCString m_name;
  175. asCScriptEngine *m_engine;
  176. asCBuilder *m_builder;
  177. asCArray<asPWORD> m_userData;
  178. asDWORD m_accessMask;
  179. asSNameSpace *m_defaultNamespace;
  180. // This array holds all functions, class members, factories, etc that were compiled with the module.
  181. // These references hold an internal reference to the function object.
  182. asCArray<asCScriptFunction *> m_scriptFunctions; // increases ref count
  183. // This array holds global functions declared in the module. These references are not counted,
  184. // as the same pointer is always present in the scriptFunctions array too.
  185. asCSymbolTable<asCScriptFunction> m_globalFunctions; // doesn't increase ref count
  186. // This array holds imported functions in the module.
  187. asCArray<sBindInfo *> m_bindInformations; // increases ref count
  188. // This array holds template instance types created for the module's object types
  189. asCArray<asCObjectType*> m_templateInstances; // increases ref count
  190. // This array holds the global variables declared in the script
  191. asCSymbolTable<asCGlobalProperty> m_scriptGlobals; // increases ref count
  192. bool m_isGlobalVarInitialized;
  193. // This array holds class and interface types
  194. asCArray<asCObjectType*> m_classTypes; // increases ref count
  195. // This array holds enum types
  196. asCArray<asCEnumType*> m_enumTypes; // increases ref count
  197. // This array holds typedefs
  198. asCArray<asCTypedefType*> m_typeDefs; // increases ref count
  199. // This array holds the funcdefs declared in the module
  200. asCArray<asCFuncdefType*> m_funcDefs; // increases ref count
  201. // This map contains all the types (also contained in the arrays above) for quick lookup
  202. // TODO: memory: Can we eliminate the arrays above?
  203. asCMap<asSNameSpaceNamePair, asCTypeInfo*> m_typeLookup; // doesn't increase ref count
  204. // This array holds types that have been explicitly declared with 'external'
  205. asCArray<asCTypeInfo*> m_externalTypes; // doesn't increase ref count
  206. // This array holds functions that have been explicitly declared with 'external'
  207. asCArray<asCScriptFunction*> m_externalFunctions; // doesn't increase ref count
  208. };
  209. END_AS_NAMESPACE
  210. #endif