ScummVM API documentation
as_module.h
1 /*
2  AngelCode Scripting Library
3  Copyright (c) 2003-2020 Andreas Jonsson
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any
7  damages arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any
10  purpose, including commercial applications, and to alter it and
11  redistribute it freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you
14  must not claim that you wrote the original software. If you use
15  this software in a product, an acknowledgment in the product
16  documentation would be appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and
19  must not be misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.
23 
24  The original version of this library can be located at:
25  http://www.angelcode.com/angelscript/
26 
27  Andreas Jonsson
28  andreas@angelcode.com
29 */
30 
31 
32 
33 //
34 // as_module.h
35 //
36 // A class that holds a script module
37 //
38 
39 #ifndef AS_MODULE_H
40 #define AS_MODULE_H
41 
42 #include "as_config.h"
43 #include "as_symboltable.h"
44 #include "as_atomic.h"
45 #include "as_string.h"
46 #include "as_memory.h"
47 #include "as_datatype.h"
48 #include "as_scriptfunction.h"
49 #include "as_property.h"
50 
51 BEGIN_AS_NAMESPACE
52 
53 // TODO: import: Remove this when the imported functions are removed
54 const int FUNC_IMPORTED = 0x40000000;
55 
56 class asCScriptEngine;
57 class asCCompiler;
58 class asCBuilder;
59 class asCContext;
60 class asCConfigGroup;
61 class asCTypedefType;
62 class asCFuncdefType;
63 struct asSNameSpace;
64 
65 struct sBindInfo {
66  asCScriptFunction *importedFunctionSignature;
67  asCString importFromModule;
68  int boundFunctionId;
69 };
70 
72  asCObjectType *a;
73  asCObjectType *b;
74 };
75 
76 
77 // TODO: import: Remove function imports. When I have implemented function
78 // pointers the function imports should be deprecated.
79 
80 // TODO: Need a separate interface for compiling scripts. The asIScriptCompiler
81 // will have a target module, and will allow the compilation of an entire
82 // script or just individual functions within the scope of the module
83 //
84 // With this separation it will be possible to compile the library without
85 // the compiler, thus giving a much smaller binary executable.
86 
87 // TODO: There should be a special compile option that will let the application
88 // recompile an already compiled script. The compiler should check if no
89 // destructive changes have been made (changing function signatures, etc)
90 // then it should simply replace the bytecode within the functions without
91 // changing the values of existing global properties, etc.
92 
93 class asCModule : public asIScriptModule {
94 //-------------------------------------------
95 // Public interface
96 //--------------------------------------------
97 public:
98  virtual asIScriptEngine *GetEngine() const;
99  virtual void SetName(const char *name);
100  virtual const char *GetName() const;
101  virtual void Discard();
102 
103  // Compilation
104  virtual int AddScriptSection(const char *name, const char *code, size_t codeLength, int lineOffset);
105  virtual int Build();
106  virtual int CompileFunction(const char *sectionName, const char *code, int lineOffset, asDWORD reserved, asIScriptFunction **outFunc);
107  virtual int CompileGlobalVar(const char *sectionName, const char *code, int lineOffset);
108  virtual asDWORD SetAccessMask(asDWORD accessMask);
109  virtual int SetDefaultNamespace(const char *nameSpace);
110  virtual const char *GetDefaultNamespace() const;
111 
112  // Script functions
113  virtual asUINT GetFunctionCount() const;
114  virtual asIScriptFunction *GetFunctionByIndex(asUINT index) const;
115  virtual asIScriptFunction *GetFunctionByDecl(const char *decl) const;
116  virtual asIScriptFunction *GetFunctionByName(const char *name) const;
117  virtual int RemoveFunction(asIScriptFunction *func);
118 
119  // Script global variables
120  // TODO: interface: Should be called InitGlobalVars, and should have a bool to reset in case already initialized
121  virtual int ResetGlobalVars(asIScriptContext *ctx);
122  virtual asUINT GetGlobalVarCount() const;
123  virtual int GetGlobalVarIndexByName(const char *name) const;
124  virtual int GetGlobalVarIndexByDecl(const char *decl) const;
125  virtual const char *GetGlobalVarDeclaration(asUINT index, bool includeNamespace) const;
126  virtual int GetGlobalVar(asUINT index, const char **name, const char **nameSpace, int *typeId, bool *isConst) const;
127  virtual void *GetAddressOfGlobalVar(asUINT index);
128  virtual int RemoveGlobalVar(asUINT index);
129 
130  // Type identification
131  virtual asUINT GetObjectTypeCount() const;
132  virtual asITypeInfo *GetObjectTypeByIndex(asUINT index) const;
133  virtual int GetTypeIdByDecl(const char *decl) const;
134  virtual asITypeInfo *GetTypeInfoByName(const char *name) const;
135  virtual asITypeInfo *GetTypeInfoByDecl(const char *decl) const;
136 
137  // Enums
138  virtual asUINT GetEnumCount() const;
139  virtual asITypeInfo *GetEnumByIndex(asUINT index) const;
140 
141  // Typedefs
142  virtual asUINT GetTypedefCount() const;
143  virtual asITypeInfo *GetTypedefByIndex(asUINT index) const;
144 
145  // Dynamic binding between modules
146  virtual asUINT GetImportedFunctionCount() const;
147  virtual int GetImportedFunctionIndexByDecl(const char *decl) const;
148  virtual const char *GetImportedFunctionDeclaration(asUINT importIndex) const;
149  virtual const char *GetImportedFunctionSourceModule(asUINT importIndex) const;
150  virtual int BindImportedFunction(asUINT index, asIScriptFunction *func);
151  virtual int UnbindImportedFunction(asUINT importIndex);
152  virtual int BindAllImportedFunctions();
153  virtual int UnbindAllImportedFunctions();
154 
155  // Bytecode Saving/Loading
156  virtual int SaveByteCode(asIBinaryStream *out, bool stripDebugInfo) const;
157  virtual int LoadByteCode(asIBinaryStream *in, bool *wasDebugInfoStripped);
158 
159  // User data
160  virtual void *SetUserData(void *data, asPWORD type);
161  virtual void *GetUserData(asPWORD type) const;
162 
163 //-----------------------------------------------
164 // Internal
165 //-----------------------------------------------
166  asCModule(const char *name, asCScriptEngine *engine);
167  ~asCModule();
168 
169 //protected:
170  friend class asCScriptEngine;
171  friend class asCBuilder;
172  friend class asCCompiler;
173  friend class asCContext;
174  friend class asCRestore;
175 
176  void InternalReset();
177  bool IsEmpty() const;
178  bool HasExternalReferences(bool shuttingDown);
179 
180  int CallInit(asIScriptContext *ctx);
181  void CallExit();
182  int InitGlobalProp(asCGlobalProperty *prop, asIScriptContext *ctx);
183 
184  void JITCompile();
185 
186 #ifndef AS_NO_COMPILER
187  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);
188  int AddScriptFunction(asCScriptFunction *func);
189  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);
190  int AddFuncDef(const asCString &name, asSNameSpace *ns, asCObjectType *parent);
191 #endif
192 
193  int GetNextImportedFunctionId();
194  asCScriptFunction *GetImportedFunction(int funcId) const;
195  asCTypeInfo *GetType(const asCString &type, asSNameSpace *ns) const;
196  asCObjectType *GetObjectType(const char *type, asSNameSpace *ns) const;
197  asCGlobalProperty *AllocateGlobalProperty(const char *name, const asCDataType &dt, asSNameSpace *ns);
198  void UninitializeGlobalProp(asCGlobalProperty *prop);
199 
200  // Adds the class type to the module. The module assumes ownership of the reference without increasing it
201  void AddClassType(asCObjectType *);
202  // Adds the enum type to the module. The module assumes ownership of the reference without increasing it
203  void AddEnumType(asCEnumType *);
204  // Adds the typedef to the module. The module assumes ownership of the reference without increasing it
205  void AddTypeDef(asCTypedefType *);
206  // Adds the funcdef to the module. The module assumes ownership of the reference without increasing it
207  void AddFuncDef(asCFuncdefType *);
208  // Replaces an existing funcdef with another (used for shared funcdefs). Doesn't add or release refCounts
209  void ReplaceFuncDef(asCFuncdefType *oldType, asCFuncdefType *newType);
210 
211  asCString m_name;
212  asCScriptEngine *m_engine;
213  asCBuilder *m_builder;
214  asCArray<asPWORD> m_userData;
215  asDWORD m_accessMask;
216  asSNameSpace *m_defaultNamespace;
217 
218  // This array holds all functions, class members, factories, etc that were compiled with the module.
219  // These references hold an internal reference to the function object.
220  asCArray<asCScriptFunction *> m_scriptFunctions; // increases ref count
221  // This array holds global functions declared in the module. These references are not counted,
222  // as the same pointer is always present in the scriptFunctions array too.
223  asCSymbolTable<asCScriptFunction> m_globalFunctions; // doesn't increase ref count
224  // This array holds imported functions in the module.
225  asCArray<sBindInfo *> m_bindInformations; // increases ref count
226  // This array holds template instance types created for the module's object types
227  asCArray<asCObjectType *> m_templateInstances; // increases ref count
228 
229  // This array holds the global variables declared in the script
230  asCSymbolTable<asCGlobalProperty> m_scriptGlobals; // increases ref count
231  bool m_isGlobalVarInitialized;
232 
233  // This array holds class and interface types
234  asCArray<asCObjectType *> m_classTypes; // increases ref count
235  // This array holds enum types
236  asCArray<asCEnumType *> m_enumTypes; // increases ref count
237  // This array holds typedefs
238  asCArray<asCTypedefType *> m_typeDefs; // increases ref count
239  // This array holds the funcdefs declared in the module
240  asCArray<asCFuncdefType *> m_funcDefs; // increases ref count
241 
242  // This map contains all the types (also contained in the arrays above) for quick lookup
243  // TODO: memory: Can we eliminate the arrays above?
244  asCMap<asSNameSpaceNamePair, asCTypeInfo *> m_typeLookup; // doesn't increase ref count
245 
246  // This array holds types that have been explicitly declared with 'external'
247  asCArray<asCTypeInfo *> m_externalTypes; // doesn't increase ref count
248  // This array holds functions that have been explicitly declared with 'external'
249  asCArray<asCScriptFunction *> m_externalFunctions; // doesn't increase ref count
250 };
251 
252 END_AS_NAMESPACE
253 
254 #endif
Definition: as_module.h:93
Definition: angelscript.h:1083
Definition: as_typeinfo.h:64
Definition: as_builder.h:140
Definition: angelscript.h:793
Definition: as_scriptfunction.h:146
Definition: as_objecttype.h:100
Definition: angelscript.h:863
Definition: as_namespace.h:39
Definition: as_scriptfunction.h:114
Definition: as_configgroup.h:50
Definition: as_module.h:71
Definition: as_map.h:44
Definition: as_compiler.h:209
Definition: as_typeinfo.h:263
Definition: as_datatype.h:60
Definition: as_module.h:65
Definition: as_property.h:68
Definition: angelscript.h:1145
Definition: as_typeinfo.h:277
Definition: as_typeinfo.h:292
Definition: as_context.h:54
Definition: as_scriptengine.h:64
Definition: angelscript.h:639
Definition: angelscript.h:1011
Definition: as_string.h:41