as_module.h

Go to the documentation of this file.
00001 /*
00002    AngelCode Scripting Library
00003    Copyright (c) 2003-2009 Andreas Jonsson
00004 
00005    This software is provided 'as-is', without any express or implied 
00006    warranty. In no event will the authors be held liable for any 
00007    damages arising from the use of this software.
00008 
00009    Permission is granted to anyone to use this software for any 
00010    purpose, including commercial applications, and to alter it and 
00011    redistribute it freely, subject to the following restrictions:
00012 
00013    1. The origin of this software must not be misrepresented; you 
00014       must not claim that you wrote the original software. If you use
00015       this software in a product, an acknowledgment in the product 
00016       documentation would be appreciated but is not required.
00017 
00018    2. Altered source versions must be plainly marked as such, and 
00019       must not be misrepresented as being the original software.
00020 
00021    3. This notice may not be removed or altered from any source 
00022       distribution.
00023 
00024    The original version of this library can be located at:
00025    http://www.angelcode.com/angelscript/
00026 
00027    Andreas Jonsson
00028    andreas@angelcode.com
00029 */
00030 
00031 
00032 
00033 //
00034 // as_module.h
00035 //
00036 // A class that holds a script module
00037 //
00038 
00039 #ifndef AS_MODULE_H
00040 #define AS_MODULE_H
00041 
00042 #include "as_config.h"
00043 #include "as_atomic.h"
00044 #include "as_string.h"
00045 #include "as_array.h"
00046 #include "as_datatype.h"
00047 #include "as_scriptfunction.h"
00048 #include "as_property.h"
00049 
00050 BEGIN_AS_NAMESPACE
00051 
00052 const int asFUNC_INIT   = 0xFFFF;
00053 const int asFUNC_STRING = 0xFFFE;
00054 
00055 const int FUNC_IMPORTED = 0x40000000;
00056 
00057 class asCScriptEngine;
00058 class asCCompiler;
00059 class asCBuilder;
00060 class asCContext;
00061 class asCConfigGroup;
00062 
00063 struct sBindInfo
00064 {
00065     asCString  importFromModule;
00066     int        importedFunction;
00067 };
00068 
00069 struct sObjectTypePair
00070 {
00071     asCObjectType *a;
00072     asCObjectType *b;
00073 };
00074 
00075 // TODO: addref/release for the module. The Module should have addref/release methods so that 
00076 //       the application can keep its own references. If DiscardModule is called on the module from
00077 //       the engine the module is not discarded immediately if the application holds it's own reference.
00078 //       Only when the application releases its references is the module discarded. Discard will however
00079 //       remove the reference from the engine's list of valid modules, so GetModule won't return it any
00080 //       more.
00081 
00082 // TODO: global: The module represents the current scope. Global variables may be added/removed
00083 //               from the scope through DeclareGlobalVar, UndeclareGlobalVar. Undeclaring a global variable
00084 //               doesn't destroy it, it just means the variable is no longer visible from the module, e.g. for
00085 //               new function compilations. Only when no more functions are accessing the global variables is
00086 //               the variable removed.
00087 
00088 // TODO: dynamic functions: String constants must be stored in the engine and shared between modules.
00089 //                          Bound functions must be stored in the engine and shared between modules. 
00090 //                          This way a script function can be disconnected from a module without breaking.
00091 
00092 // TODO: dynamic functions: It must be possible to compile new functions dynamically within the 
00093 //                          scope of a module. The new functions can be added to the scope of the module, or it can be 
00094 //                          left outside, thus only accessible through the function id that is returned. This can be used
00095 //                          by scripts to dynamically compile new functions. It will also be possible to undeclare functions,
00096 //                          in which case the function is removed from the scope of the module. When no one else is accessing
00097 //                          the function anymore, will it be removed. In order to keep track of references between functions
00098 //                          I need to implement reference counting, which also needs a GC for resolving cyclic references.
00099 
00100 // TODO: Remove function imports. When I have implemented function 
00101 //       pointers the function imports should be deprecated.
00102 
00103 class asCModule : public asIScriptModule
00104 {
00105 //-------------------------------------------
00106 // Public interface
00107 //--------------------------------------------
00108 public:
00109     virtual asIScriptEngine *GetEngine();
00110     virtual void             SetName(const char *name);
00111     virtual const char      *GetName();
00112 
00113     // Compilation
00114     virtual int  AddScriptSection(const char *name, const char *code, size_t codeLength, int lineOffset);
00115     virtual int  Build();
00116 
00117     // Script functions
00118     virtual int                GetFunctionCount();
00119     virtual int                GetFunctionIdByIndex(int index);
00120     virtual int                GetFunctionIdByName(const char *name);
00121     virtual int                GetFunctionIdByDecl(const char *decl);
00122     virtual asIScriptFunction *GetFunctionDescriptorByIndex(int index);
00123     virtual asIScriptFunction *GetFunctionDescriptorById(int funcId);
00124 
00125     // Script global variables
00126     virtual int         ResetGlobalVars();
00127     virtual int         GetGlobalVarCount();
00128     virtual int         GetGlobalVarIndexByName(const char *name);
00129     virtual int         GetGlobalVarIndexByDecl(const char *decl);
00130     virtual const char *GetGlobalVarDeclaration(int index);
00131     virtual const char *GetGlobalVarName(int index);
00132     virtual int         GetGlobalVarTypeId(int index, bool *isConst);
00133     virtual void       *GetAddressOfGlobalVar(int index);
00134 
00135     // Type identification
00136     virtual int            GetObjectTypeCount();
00137     virtual asIObjectType *GetObjectTypeByIndex(asUINT index);
00138     virtual int            GetTypeIdByDecl(const char *decl);
00139 
00140     // Enums
00141     virtual int         GetEnumCount();
00142     virtual const char *GetEnumByIndex(asUINT index, int *enumTypeId);
00143     virtual int         GetEnumValueCount(int enumTypeId);
00144     virtual const char *GetEnumValueByIndex(int enumTypeId, asUINT index, int *outValue);
00145 
00146     // Typedefs
00147     virtual int         GetTypedefCount();
00148     virtual const char *GetTypedefByIndex(asUINT index, int *typeId);
00149 
00150     // Dynamic binding between modules
00151     virtual int         GetImportedFunctionCount();
00152     virtual int         GetImportedFunctionIndexByDecl(const char *decl);
00153     virtual const char *GetImportedFunctionDeclaration(int importIndex);
00154     virtual const char *GetImportedFunctionSourceModule(int importIndex);
00155     virtual int         BindImportedFunction(int index, int sourceID);
00156     virtual int         UnbindImportedFunction(int importIndex);
00157     virtual int         BindAllImportedFunctions();
00158     virtual int         UnbindAllImportedFunctions();
00159 
00160     // Bytecode Saving/Loading
00161     virtual int SaveByteCode(asIBinaryStream *out);
00162     virtual int LoadByteCode(asIBinaryStream *in);
00163 
00164 //-----------------------------------------------
00165 // Internal
00166 //-----------------------------------------------
00167     asCModule(const char *name, int id, asCScriptEngine *engine);
00168     ~asCModule();
00169 
00170 //protected:
00171     friend class asCScriptEngine;
00172     friend class asCBuilder;
00173     friend class asCCompiler;
00174     friend class asCContext;
00175     friend class asCRestore;
00176 
00177     void Discard();
00178     void InternalReset();
00179 
00180     int  AddContextRef();
00181     int  ReleaseContextRef();
00182     asCAtomic contextCount;
00183 
00184     int  AddModuleRef();
00185     int  ReleaseModuleRef();
00186     asCAtomic moduleCount;
00187 
00188     int CallInit();
00189     void CallExit();
00190     bool isGlobalVarInitialized;
00191 
00192     bool IsUsed();
00193 
00194     void JITCompile();
00195 
00196     int  AddConstantString(const char *str, size_t length);
00197     const asCString &GetConstantString(int id);
00198 
00199     int  AddScriptFunction(int sectionIdx, int id, const char *name, const asCDataType &returnType, asCDataType *params, asETypeModifiers *inOutFlags, int paramCount, bool isInterface, asCObjectType *objType = 0, bool isConstMethod = false, bool isGlobalFunction = false);
00200     int  AddScriptFunction(asCScriptFunction *func);
00201     int  AddImportedFunction(int id, const char *name, const asCDataType &returnType, asCDataType *params, asETypeModifiers *inOutFlags, int paramCount, const asCString &moduleName);
00202 
00203     bool CanDeleteAllReferences(asCArray<asCModule*> &modules);
00204 
00205     int  GetNextImportedFunctionId();
00206 
00207     void ResolveInterfaceIds();
00208     bool AreInterfacesEqual(asCObjectType *a, asCObjectType *b, asCArray<sObjectTypePair> &equals);
00209     bool AreTypesEqual(const asCDataType &a, const asCDataType &b, asCArray<sObjectTypePair> &equals);
00210 
00211     asCScriptFunction *GetImportedFunction(int funcId);
00212     asCScriptFunction *GetSpecialFunction(int funcId);
00213 
00214     asCObjectType *GetObjectType(const char *type);
00215     asCConfigGroup *GetConfigGroupByGlobalVarPtrIndex(int index);
00216 
00217     int  GetScriptSectionIndex(const char *name);
00218     bool CanDelete();
00219 
00220     asCGlobalProperty *AllocateGlobalProperty(const char *name, const asCDataType &dt);
00221     int GetGlobalVarPtrIndex(int gvarId);
00222 
00223     asCString name;
00224 
00225     asCScriptEngine *engine;
00226     asCBuilder      *builder;
00227     bool             isBuildWithoutErrors;
00228 
00229     int  moduleId;
00230     bool isDiscarded;
00231 
00232     asCScriptFunction             *initFunction;
00233     asCArray<asCString *>          scriptSections;
00234     // This array holds all functions, class members, factories, etc that were compiled with the module
00235     asCArray<asCScriptFunction *>  scriptFunctions;
00236     // This array holds global functions declared in the module
00237     asCArray<asCScriptFunction *>  globalFunctions;
00238     // This array holds imported functions in the module
00239     asCArray<asCScriptFunction *>  importedFunctions;
00240     asCArray<sBindInfo *>          bindInformations;
00241 
00242     // This array holds the global variables declared in the script
00243     asCArray<asCGlobalProperty *>  scriptGlobals;
00244 
00245     // This array holds pointers to all global variables that the functions in the module access.
00246     // The byte code holds an index into this table to refer to a global variable.
00247     asCArray<void*>                globalVarPointers;
00248 
00249     asCArray<asCString*>           stringConstants;
00250 
00251     // This array holds class and interface types
00252     asCArray<asCObjectType*>       classTypes;
00253     // This array holds enum types
00254     asCArray<asCObjectType*>       enumTypes;
00255     // This array holds typedefs
00256     asCArray<asCObjectType*>       typeDefs;
00257 };
00258 
00259 END_AS_NAMESPACE
00260 
00261 #endif

Generated on Mon Jan 11 12:03:40 2010 for Werewolf by  doxygen 1.6.1