00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef FLC_MODULE_H
00017 #define FLC_MODULE_H
00018
00019 #include <falcon/setup.h>
00020 #include <falcon/string.h>
00021 #include <falcon/symbol.h>
00022 #include <falcon/symtab.h>
00023 #include <falcon/types.h>
00024 #include <falcon/deptab.h>
00025 #include <falcon/linemap.h>
00026 #include <falcon/dll.h>
00027 #include <falcon/strtable.h>
00028 #include <falcon/service.h>
00029 #include <falcon/genericmap.h>
00030 #include <falcon/basealloc.h>
00031
00032 namespace Falcon {
00033
00034 class String;
00035 class Stream;
00036 class AttribMap;
00037
00056 class FALCON_DYN_CLASS Module: public BaseAlloc
00057 {
00058 protected:
00059
00060 mutable volatile int32 m_refcount;
00061
00062
00063
00064
00065 char m_version;
00066 char m_subversion;
00067 String m_name;
00068 String m_path;
00069 String m_language;
00070 uint32 m_modVersion;
00071 uint32 m_engineVersion;
00072
00073
00074
00075
00076 StringTable* m_strTab;
00077 bool m_bOwnStringTable;
00078 SymbolVector m_symbols;
00079 SymbolTable m_symtab;
00080 DependTable m_depend;
00082 LineMap *m_lineInfo;
00083
00103 DllLoader *m_loader;
00104
00114 DllLoader *detachLoader();
00115
00116 Map m_serviceMap;
00117
00118 AttribMap* m_attributes;
00119
00120 virtual ~Module();
00121
00122 public:
00123
00124 static const uint32 c_noEntry;
00129 Module();
00130 Module &name( const String &n ) { m_name.bufferize( n ); return *this; }
00131 Module &path( const String &p ) { m_path.bufferize( p ); return *this; }
00132 Module &language( const String &lang ) { m_language = lang; return *this; }
00133
00134
00135 Module &version( uint32 version ) { m_modVersion = version; return *this; }
00136 Module &version( int major, int minor, int revision ) {
00137 m_modVersion = major << 16 | minor << 8 | revision;
00138 return *this;
00139 }
00140
00141 Module &engineVersion( uint32 version ) { m_engineVersion = version; return *this; }
00142 Module &engineVersion( int major, int minor, int revision ) {
00143 m_engineVersion = major << 16 | minor << 8 | revision;
00144 return *this;
00145 }
00146
00147 virtual char pcodeVersion() const;
00148 virtual char pcodeSubVersion() const;
00149
00150 void incref() const;
00151 void decref() const;
00152
00159 virtual bool isFlexy() const { return false; }
00160
00161
00162 Symbol *findGlobalSymbol( const String &name ) const
00163 {
00164 return m_symtab.findByName( name );
00165 }
00166
00167 const String &name() const { return m_name; }
00168 const String &path() const { return m_path; }
00169 const String &language() const { return m_language;}
00170
00171 String *addCString( const char *pos, uint32 size );
00172
00182 String *addString( const String &st );
00183
00194 String *addString( const String &st, bool exported );
00195
00202 const String *getString( uint32 id ) const { return m_strTab->get(id); }
00203
00204 Symbol *getSymbol( uint32 id ) const
00205 {
00206 if ( id > m_symbols.size() )
00207 return 0;
00208 return *(Symbol **) m_symbols.at(id);
00209 }
00210
00215 const SymbolTable &symbolTable() const {
00216 return m_symtab;
00217 }
00218
00223 SymbolTable &symbolTable() {
00224 return m_symtab;
00225 }
00226
00227
00237 const SymbolVector &symbols() const {
00238 return m_symbols;
00239 }
00240
00241 SymbolVector &symbols() {
00242 return m_symbols;
00243 }
00244
00245 const DependTable &dependencies() const {
00246 return m_depend;
00247 }
00248
00254 void addDepend( const String &name, bool bPrivate = false, bool bIsFilename = false );
00255
00262 void addDepend( const String &alias, const String &module, bool bPrivate=false, bool bIsFilename = false );
00263
00264
00266 void addSymbol( Symbol *sym );
00267
00278 Symbol *addSymbol( const String &name );
00279
00280 Symbol *addExternalRef( const String &name ) {
00281 return addGlobalSymbol( addSymbol( name ) );
00282 }
00283
00304 Symbol *addExtFunc( const String &name, ext_func_t func, bool exp = true )
00305 {
00306 return addExtFunc( name, func, 0, exp );
00307 }
00308
00320 Symbol *addExtFunc( const String &name, ext_func_t func, void *extra, bool exp = true );
00321
00322
00326 Symbol *addSymbol();
00327
00328
00335 Symbol *addGlobalSymbol( Symbol *sym );
00336
00337
00343 Symbol *addGlobal( const String &name, bool exp=true );
00344
00354 VarDef& addClassProperty( Symbol *cls, const String &prop );
00355
00367 VarDef& addClassMethod( Symbol *cls, const String &prop, Symbol *method );
00368
00382 VarDef& addClassMethod( Symbol *cls, const String &prop, ext_func_t func );
00383
00384
00402 Symbol *addFunction( const String &name, byte *code, uint32 size, bool exp=true );
00403
00417 Symbol *addClass( const String &name, Symbol *ctor_sym, bool exp=true );
00418
00429 Symbol *addClass( const String &name, ext_func_t ctor, bool exp=true );
00430
00437 Symbol *addClass( const String &name, bool exp=true ) {
00438 return addClass( name, static_cast<Symbol *>( 0 ), exp );
00439 }
00440
00462 Symbol *addSingleton( const String &name, Symbol *ctor_sym=0, bool exp = true );
00463
00476 Symbol *addSingleton( const String &name, ext_func_t ctor, bool exp = true );
00477
00489 uint32 getLineAt( uint32 pc ) const;
00490
00501 void addLineInfo( uint32 pc, uint32 line );
00502
00520 void setLineInfo( LineMap *infos );
00521
00530 DllLoader &dllLoader();
00531
00532
00555 virtual bool save( Stream *os, bool skipCode=false ) const;
00556
00576 virtual bool load( Stream *is, bool skipHeader=true );
00577
00578 const StringTable &stringTable() const { return *m_strTab; }
00579 StringTable &stringTable() { return *m_strTab; }
00580
00581 void adoptStringTable( StringTable *st, bool bOwn = false );
00582
00587 Service *getService( const String &name ) const;
00588
00593 bool publishService( Service *sp );
00594
00598 const Map &getServiceMap() const { return m_serviceMap; }
00599
00606 Symbol *addConstant( const String &name, int64 value, bool exp = true );
00607
00614 Symbol *addConstant( const String &name, numeric value, bool exp = true );
00615
00622 Symbol *addConstant( const String &name, const String &value, bool exp = true );
00623
00625 void getModuleVersion( int &major, int &minor, int &revision ) const;
00626
00628 void getEngineVersion( int &major, int &minor, int &revision ) const;
00629
00634 bool saveTableTemplate( Stream *stream, const String &encoding ) const;
00635
00637 static String absoluteName( const String &module_name, const String &parent_name );
00638
00640 void addAttribute( const String &name, VarDef* vd );
00641
00645 AttribMap* attributes() const { return m_attributes; }
00646
00667 void rollBackSymbols( uint32 size );
00668 };
00669
00670 }
00671
00672
00673
00674
00675
00676
00677 #define DEFALUT_FALCON_MODULE_INIT falcon_module_init
00678
00679 #define FALCON_MODULE_DECL \
00680 FALCON_MODULE_TYPE DEFALUT_FALCON_MODULE_INIT()
00681
00682
00683 #ifndef FAL_STR
00684 #define FAL_STR( id ) vm->moduleString( id )
00685 #endif
00686
00687 #endif
00688
00689