#include <module.h>
Public Member Functions | |
void | addAttribute (const String &name, VarDef *vd) |
Adds a module-wide attribute. | |
Symbol * | addClass (const String &name, bool exp=true) |
Adds a class definition without a constructor. | |
Symbol * | addClass (const String &name, ext_func_t ctor, bool exp=true) |
Adds a class definition with an external constructor to this module. | |
Symbol * | addClass (const String &name, Symbol *ctor_sym, bool exp=true) |
Adds a class definition to this module. | |
VarDef & | addClassMethod (Symbol *cls, const String &prop, ext_func_t func) |
Shortcut to add external function methods to Classes in this module. | |
VarDef & | addClassMethod (Symbol *cls, const String &prop, Symbol *method) |
Adds methods to ClassDefs in symbols held by this module. | |
VarDef & | addClassProperty (Symbol *cls, const String &prop) |
Shortcut to add class properties that are to be considered variables. | |
Symbol * | addConstant (const String &name, const String &value, bool exp=true) |
Adds a constant to the module. | |
Symbol * | addConstant (const String &name, numeric value, bool exp=true) |
Adds a constant to the module. | |
Symbol * | addConstant (const String &name, int64 value, bool exp=true) |
Adds a constant to the module. | |
String * | addCString (const char *pos, uint32 size) |
void | addDepend (const String &alias, const String &module, bool bPrivate=false, bool bIsFilename=false) |
Add a dependency to an external module. | |
void | addDepend (const String &name, bool bPrivate=false, bool bIsFilename=false) |
Add a dependency to an external module. | |
Symbol * | addExternalRef (const String &name) |
Symbol * | addExtFunc (const String &name, ext_func_t func, void *extra, bool exp=true) |
Shortcut to create a symbol as an external function. | |
Symbol * | addExtFunc (const String &name, ext_func_t func, bool exp=true) |
Shortcut to create a symbol as an external function. | |
Symbol * | addFunction (const String &name, byte *code, uint32 size, bool exp=true) |
Adds an internal function to the module. | |
Symbol * | addGlobal (const String &name, bool exp=true) |
Adds a global variable symbol to the module. | |
Symbol * | addGlobalSymbol (Symbol *sym) |
Adds a symbol to the global symbol table. | |
void | addLineInfo (uint32 pc, uint32 line) |
Adds a line information to the module. | |
Symbol * | addSingleton (const String &name, ext_func_t ctor, bool exp=true) |
Adds a singleton object to this module. | |
Symbol * | addSingleton (const String &name, Symbol *ctor_sym=0, bool exp=true) |
Adds a singleton object to this module. | |
String * | addString (const String &st, bool exported) |
Add a string to the module string table and eventually exports it. | |
String * | addString (const String &st) |
Add a string to the module string table. | |
Symbol * | addSymbol () |
Shortcut to create a symbol and store it in the module symbol list. | |
Symbol * | addSymbol (const String &name) |
Shortcut to create a symbol. | |
void | addSymbol (Symbol *sym) |
Adds a generic symbol to the module. | |
void | adoptStringTable (StringTable *st, bool bOwn=false) |
AttribMap * | attributes () const |
Returns the module-wide attribute list. | |
void | decref () const |
const DependTable & | dependencies () const |
DllLoader & | dllLoader () |
Access the DllLoader attached with this module. | |
Module & | engineVersion (int major, int minor, int revision) |
Module & | engineVersion (uint32 version) |
Symbol * | findGlobalSymbol (const String &name) const |
void | getEngineVersion (int &major, int &minor, int &revision) const |
Get the version of the engine libraries under which this module has been compiled. | |
uint32 | getLineAt (uint32 pc) const |
Returns the line number that generated a certain code portion. | |
void | getModuleVersion (int &major, int &minor, int &revision) const |
Get the version declared by this module. | |
Service * | getService (const String &name) const |
Creates an istance for a certain service. | |
const Map & | getServiceMap () const |
Return a map of the provided services. | |
const String * | getString (uint32 id) const |
Return a string given an ID in the string table. | |
Symbol * | getSymbol (uint32 id) const |
void | incref () const |
virtual bool | isFlexy () const |
Determine this to be a flexy module. | |
const String & | language () const |
Module & | language (const String &lang) |
virtual bool | load (Stream *is, bool skipHeader=true) |
Reads the module from a stream. | |
Module () | |
Variable parameter initializer semantic. | |
const String & | name () const |
Module & | name (const String &n) |
const String & | path () const |
Module & | path (const String &p) |
virtual char | pcodeSubVersion () const |
virtual char | pcodeVersion () const |
bool | publishService (Service *sp) |
Publishes a service on the module. | |
void | rollBackSymbols (uint32 size) |
Unrolls the symbols back to a previous state. | |
virtual bool | save (Stream *os, bool skipCode=false) const |
Write the module to a stream. | |
bool | saveTableTemplate (Stream *stream, const String &encoding) const |
Save internationalization infos on required file. | |
void | setLineInfo (LineMap *infos) |
Adds pre-compiled line informations to the module. | |
StringTable & | stringTable () |
const StringTable & | stringTable () const |
SymbolVector & | symbols () |
const SymbolVector & | symbols () const |
Returns the symbol list (const version). | |
SymbolTable & | symbolTable () |
Returns the module symbol table. | |
const SymbolTable & | symbolTable () const |
Returns the module symbol table (const version). | |
Module & | version (int major, int minor, int revision) |
Module & | version (uint32 version) |
Static Public Member Functions | |
static String | absoluteName (const String &module_name, const String &parent_name) |
Extract the full logical module name from current module name and parent loader. | |
Static Public Attributes | |
static const uint32 | c_noEntry |
Protected Member Functions | |
DllLoader * | detachLoader () |
Detach the loader for decref. | |
virtual | ~Module () |
Protected Attributes | |
AttribMap * | m_attributes |
bool | m_bOwnStringTable |
DependTable | m_depend |
uint32 | m_engineVersion |
String | m_language |
LineMap * | m_lineInfo |
The line map is a pointer as it can be absent or given after the generation step. | |
DllLoader * | m_loader |
Dynamic link loader attached with this module. | |
uint32 | m_modVersion |
String | m_name |
String | m_path |
volatile int32 | m_refcount |
Map | m_serviceMap |
StringTable * | m_strTab |
char | m_subversion |
SymbolVector | m_symbols |
SymbolTable | m_symtab |
char | m_version |
A module is uniquely indicated by its name; loading a module which has the same name of a previous one will make the other existing module to be discarded.
When a module is added to the VM, it requests the module symbol table. The exported symbols are then loaded in the global VM symbol table. Only a module may be executed; when it is executed, it may load additional modules (the vm acts in its behalf) and then the VM creates the global context array.
The array is filled with the items provided in the symbols of the symbol table, or with NIL were missing; if symbols are external, they are searched in the global VM symbol table (and again filled with the provided item or with NIL).
A module may even exist without code. This is the case of pure C modules that register themselves using only C functions as symbol's initial items.
virtual Falcon::Module::~Module | ( | ) | [protected, virtual] |
Falcon::Module::Module | ( | ) |
Variable parameter initializer semantic.
Use the following functions to initialize the module. If a SymbolTable is not provided here, an empty one is immediately created.
static String Falcon::Module::absoluteName | ( | const String & | module_name, | |
const String & | parent_name | |||
) | [static] |
Extract the full logical module name from current module name and parent loader.
Adds a module-wide attribute.
Adds a class definition without a constructor.
name | The name of the class | |
exp | true if the symbol is to be exported |
Adds a class definition with an external constructor to this module.
This is a shortcut that creates a method in the module by getting the class name and adding a "._init". The created symbol is not exported, and is set as class constructor by calling addClass( name, Symbo *, bool ).
name | The name of the class | |
ctor | the external function that will be used as constructor. | |
exp | true if the symbol is to be exported |
Adds a class definition to this module.
This method creates a class which is pertinent to this modules and return the corresponding class symbol. The symbol may then be used to add properties to the class; every property must be a valid symbol that is already inserted in the same module of the class (at the moment, in future it may also be a symbol from another module).
name | The name of the class | |
ctor_sym | An optional symbol that is registered in the same module acting as a constructor (pass zero if no constructor is needed). | |
exp | true if the symbol is to be exported |
Shortcut to add external function methods to Classes in this module.
This is a shortcut that creates a non-exported symbol represented by the class name, a dot, and the property name. This symbol is created in the module as non-exported, and then is fed into addClassMethod( Symbol *, const String, Symbol * ).
cls | the symbol holding the ClassDef that must be operated on | |
prop | the property name | |
func | the function that will be used as a method |
Adds methods to ClassDefs in symbols held by this module.
This method takes care to create the module strings needed to represent the class properties.
cls | the symbol holding the ClassDef that must be operated on | |
prop | the property name | |
method | the symbol that will be used as a method |
Shortcut to add class properties that are to be considered variables.
This method takes care to create the module strings needed to represent the class properties.
cls | the symbol holding the ClassDef that must be operated on. | |
prop | the property name. |
Adds a constant to the module.
This object will become a constant item in the VM.
exp | true if the symbol is to be exported. | |
value | the value that the constant will assume in the vm. | |
name | the name of the symbol in the vm. |
Adds a constant to the module.
This object will become a constant item in the VM.
exp | true if the symbol is to be exported. | |
value | the value that the constant will assume in the vm. | |
name | the name of the symbol in the vm. |
Adds a constant to the module.
This object will become a constant item in the VM.
exp | true if the symbol is to be exported. | |
value | the value that the constant will assume in the vm. | |
name | the name of the symbol in the vm. |
void Falcon::Module::addDepend | ( | const String & | alias, | |
const String & | module, | |||
bool | bPrivate = false , |
|||
bool | bIsFilename = false | |||
) |
Add a dependency to an external module.
alias | The local alias under which the module is known. | |
module | The name of the module from which this module depends. | |
bPrivate | if true import the module only locally. | |
bIsFilename | if true the load name is a filename. |
void Falcon::Module::addDepend | ( | const String & | name, | |
bool | bPrivate = false , |
|||
bool | bIsFilename = false | |||
) |
Add a dependency to an external module.
name | The name of the module from which this module depends. | |
bPrivate | if true import the module only locally. | |
bIsFilename | if true the load name is a filename. |
Symbol* Falcon::Module::addExtFunc | ( | const String & | name, | |
ext_func_t | func, | |||
void * | extra, | |||
bool | exp = true | |||
) |
Shortcut to create a symbol as an external function.
This also sets extra data for the callable symbol.
name | the name of the symbol that must be created. | |
func | a function pointer to the external function. | |
extra | extra data for this function definition. | |
exp | true if the symbol myst be exported (true by default) |
Symbol* Falcon::Module::addExtFunc | ( | const String & | name, | |
ext_func_t | func, | |||
bool | exp = true | |||
) | [inline] |
Shortcut to create a symbol as an external function.
Binary modules may use this as a shortcut to create external function definitions.
The function will also store the symbol int the global symbol table, and eventually add the string to the module string table if needed. In case the symbol is already used, it will return 0, else it will return the newly created symbol.
If the symbol has to be set as a class method, or otherwise used internally by the module, then set the exp parameter to false, so that the symbol is not exported in the VM, sparing link time and memory.
name | the name of the symbol that must be created. | |
func | a function pointer to the external function. | |
exp | true if the symbol myst be exported (true by default) |
Symbol* Falcon::Module::addFunction | ( | const String & | name, | |
byte * | code, | |||
uint32 | size, | |||
bool | exp = true | |||
) |
Adds an internal function to the module.
As the functions and other callable are usually created during module load and added directly to the symbol table, this method serves only as a shortcut to define pseudo-functions. The most notable of them is the "__main__" function that starts at module entry point and never accepts parameters or locals
The name parameter will be copied in a new string ar stored in the module string table. If the string already exists, that instance will be used as the symbol name.
name | the name of the symbol | |
code | the code of the function to be executed. The data will be destroyed with memFree at symbol destruction. | |
size | size of the function code in bytes. | |
exp | true if the symbol is to be exported |
Adds a global variable symbol to the module.
name | the name of the symbol | |
exp | true if the symbol is exported, false otherwise |
Adds a symbol to the global symbol table.
The function checks if the symbol is already in the module table; if not it adds it to the module table and sets the proper id.
Adds a line information to the module.
The line informations are used in debug and error reporting to provide the users with the line number of the source falcon script that generated a certain code portion in the bytecode.
The the assembler calls this function each time a new line generates new code.
pc | the current code position. | |
line | the line that generated the code starting at PC. |
Adds a singleton object to this module.
This is like addSingleton( const String &, Symbol *, bool ), but this version of the function takes an external function as a constructor and assigns it a name that coresponds to the class name followed by "._init". If the constructor name, the class name or the instance name are not free, the method returns false.
name | the name of the singleton to be created | |
ctor | the constructor of the base class; cannot be zero (use the other version instead) | |
exp | true to export the singleton. The class is always private; to export it get it with Symbol::instanceOf() and export it with Symbol::exported(). |
Symbol* Falcon::Module::addSingleton | ( | const String & | name, | |
Symbol * | ctor_sym = 0 , |
|||
bool | exp = true | |||
) |
Adds a singleton object to this module.
This method creates a singleton object, that is a private class which is instantiated at link time in an unique, eventually exported object. The given name is the name by which the object will be known; the private class is given the special name "%[object name]", where "[object name]" is the name parameter.
If both the name and the "%" prefixed name are currently unassigned, a new symbol containing the singletone instance declaration is returned. On failure, 0 is returned.
The class giving form to this singleton can be retrieved using the Symbol::instanceOf() method. That symbol can then be used to add methods and properties to the singleton, or to derive the private class from other classes.
name | the name of the singleton to be created | |
ctor_sym | the constructor of the base class, or 0 for none | |
exp | true to export the singleton. The class is always private; to export it get it with Symbol::instanceOf() and export it with Symbol::exported(). |
Add a string to the module string table and eventually exports it.
If the string already exists, the previous entry is returned. This method also set the "exported" bit of the returned string, so that the string can be directly used in exported intenrationalization tables.
st | the string to be added. | |
exported | set to true to make this string subject to internationalization table exports. |
Add a string to the module string table.
If the string already exists, the previous entry is returned.
st | the string to be added. |
Symbol* Falcon::Module::addSymbol | ( | ) |
Shortcut to create a symbol and store it in the module symbol list.
Shortcut to create a symbol.
Creates a symbol and adds it to the module symbol vector. It is not added to the global symbol table, though. For that, use addGlobalSymbol().
name | the name of the symbol that must be created. |
void Falcon::Module::addSymbol | ( | Symbol * | sym | ) |
Adds a generic symbol to the module.
void Falcon::Module::adoptStringTable | ( | StringTable * | st, | |
bool | bOwn = false | |||
) |
AttribMap* Falcon::Module::attributes | ( | ) | const [inline] |
Returns the module-wide attribute list.
May be zero if this module has no attributes.
void Falcon::Module::decref | ( | ) | const |
Referenced by Falcon::ModuleDep::~ModuleDep().
const DependTable& Falcon::Module::dependencies | ( | ) | const [inline] |
DllLoader* Falcon::Module::detachLoader | ( | ) | [protected] |
Detach the loader for decref.
If the module has been loaded through a DLL, decreffing it to death should unload the module. If this was the last instance of the module in memory, the return address of the DllLoader desctructor may not be available anymore when the destructor returns.
For this reason, the decref() member calls this method and destroys autonomously the handler after having destroyed the module.
DllLoader& Falcon::Module::dllLoader | ( | ) |
Access the DllLoader attached with this module.
The internal dll loader is created the first time this mehtod gets called. This is to avoid application-only modules to have a Dll loader attached.
Module& Falcon::Module::engineVersion | ( | int | major, | |
int | minor, | |||
int | revision | |||
) | [inline] |
void Falcon::Module::getEngineVersion | ( | int & | major, | |
int & | minor, | |||
int & | revision | |||
) | const |
Get the version of the engine libraries under which this module has been compiled.
Returns the line number that generated a certain code portion.
Given a Program Counter index, this funtion returns the code line that originally generated the part of the code to which the Program Counter is pointing to, if this information is available.
The function will return zero if the information cannot be retreived (i.e. be cause debug informations have been stripped).
pc | a code position for which the original code line must be discovered. |
void Falcon::Module::getModuleVersion | ( | int & | major, | |
int & | minor, | |||
int & | revision | |||
) | const |
Get the version declared by this module.
Creates an istance for a certain service.
Returns 0 if the module does not provide the required service. The caller will have to delete the service.
const Map& Falcon::Module::getServiceMap | ( | ) | const [inline] |
Return a map of the provided services.
Should be used only in the vm.
Return a string given an ID in the string table.
This function doesn't check for out-of-bounds access, so be careful.
id | the ID of the string in the string table (insertion order). |
void Falcon::Module::incref | ( | ) | const |
Referenced by Falcon::ModuleDep::ModuleDep().
virtual bool Falcon::Module::isFlexy | ( | ) | const [inline, virtual] |
Determine this to be a flexy module.
Flexy modules can be queried for unknown symbols at runtime; they declare themselves as sticking only with this VM (or otherwise providing a VM-sensible local environment), and are available for late-lazy binding of symbols.
Reimplemented in Falcon::FlexyModule.
const String& Falcon::Module::language | ( | ) | const [inline] |
virtual bool Falcon::Module::load | ( | Stream * | is, | |
bool | skipHeader = true | |||
) | [virtual] |
Reads the module from a stream.
This method may only load a Falcon binary module; it has no capability to compile a source module or to load a native module in DLL or shared object format.
This method is used as a convenience by the module loader or by other agents when they discover that the module is a native Falcon module.
For this reason, the 4 bytes containing the Falcon module intestation should be skipped (read) by the caller before calling this method. In case they are not, i.e. because the caller knows the module being right without reading the header, then it should call this function with skipHeader set to false.
The skipHeader parameter only refers to the first 4 bytes of the module file, which contain a signature, the version and the subversion of the module; the whole header may be much longer and depends on the module version.
is | an input stream | |
skipHeader | wheter to skip or read the first 4 module bytes. |
const String& Falcon::Module::name | ( | ) | const [inline] |
const String& Falcon::Module::path | ( | ) | const [inline] |
virtual char Falcon::Module::pcodeSubVersion | ( | ) | const [virtual] |
virtual char Falcon::Module::pcodeVersion | ( | ) | const [virtual] |
bool Falcon::Module::publishService | ( | Service * | sp | ) |
Publishes a service on the module.
sp | the ServiceProvider (function pointer) that will create the service on request. |
void Falcon::Module::rollBackSymbols | ( | uint32 | size | ) |
Unrolls the symbols back to a previous state.
This function removes the symbols added past a size. This allows to return to a previous state in the module declaration, that can be simply recored by getting the size of the symbol table.
For example.
int modSize = module->symbols().size(); try { // ... do something altering the module } catch( Error* e ) { // roll back module->rollBackSymbols( modSize ); throw; }
virtual bool Falcon::Module::save | ( | Stream * | os, | |
bool | skipCode = false | |||
) | const [virtual] |
Write the module to a stream.
The module is written to a stream (that does not requires seeking capabilities, like i.e. stdout). If skipCode is true, the code member of the module (m_code) is not written and when the operation reaches the point where the code should be written, it skips forward of m_codeSize bytes. If skipCode is false (default), the m_code member is regularily written. The code is usually skipped by the compiler and assembler if they have the byte code handy in another place (i.e. stored on a seekable temporary stream). In that case, importing the bytecode in the module just to serialize it would be a waste.
The very last things to be written are 4 bytes representing the de-endianized code size and then the bytecode itself. This allow a caller that sets skipCode=true to write those data right after save() has returned successfully.
os | an output stream with seek capabilities | |
skipCode | true if the caller doesn't want to save the bytecode. |
std::ostream::failure | in case the stream reports a write error |
Save internationalization infos on required file.
Creates an XML file staring with ?xml tag. Shouldn't be called if it's table has no international strintgs.
void Falcon::Module::setLineInfo | ( | LineMap * | infos | ) |
Adds pre-compiled line informations to the module.
The line informations are used in debug and error reporting to provide the users with the line number of the source falcon script that generated a certain code portion in the bytecode.
Generators knowing the line informations (i.e. the code generator) can create a ready-to-use line information map, that can then be inserted into the same module where the target code will find place.
The given line map is then owned by the module, and it will destroyed at module destruction; so the creator must give off its ownership.
If the module already had some line infrmations, they are destroyed.
infos | a map containing line informations relative to the code used in this module. |
StringTable& Falcon::Module::stringTable | ( | ) | [inline] |
const StringTable& Falcon::Module::stringTable | ( | ) | const [inline] |
SymbolVector& Falcon::Module::symbols | ( | ) | [inline] |
const SymbolVector& Falcon::Module::symbols | ( | ) | const [inline] |
Returns the symbol list (const version).
The symbol list is a vector which contains the list of all the symbols in the module (including local, static, private, parameters and the like). Each Symbol contains an id that backreferences the position in the module symbol list so that
symbols()[i]->id() == symbols[i];
SymbolTable& Falcon::Module::symbolTable | ( | ) | [inline] |
Returns the module symbol table.
This holds the global variables that are available at module level.
const SymbolTable& Falcon::Module::symbolTable | ( | ) | const [inline] |
Returns the module symbol table (const version).
This holds the global variables that are available at module level.
Module& Falcon::Module::version | ( | int | major, | |
int | minor, | |||
int | revision | |||
) | [inline] |
const uint32 Falcon::Module::c_noEntry [static] |
AttribMap* Falcon::Module::m_attributes [protected] |
bool Falcon::Module::m_bOwnStringTable [protected] |
DependTable Falcon::Module::m_depend [protected] |
uint32 Falcon::Module::m_engineVersion [protected] |
String Falcon::Module::m_language [protected] |
LineMap* Falcon::Module::m_lineInfo [protected] |
The line map is a pointer as it can be absent or given after the generation step.
DllLoader* Falcon::Module::m_loader [protected] |
Dynamic link loader attached with this module.
If this module is created using a DLL, then it is necessary to close the DLL handle as soon as the module is disposed (or, at least, not before). For this reason, the loader that generated the module should be contained in the module itself.
The loader provides internally a system to provide resource reference counting, so that if it insists on the same handler as other loaders (contained by other module instances), the correct library handler count is kept (Actually, this service is usually available at kernel level on many OSs).
Also, DllLoader instance contains usually a minimal representation for the DLL handler and points to a minimal set of non-virtual methods to manage it, so it has the same weight as a reference count or even a pointer to the an handler. This justifies the presence of this field also in modules that are not generated by DLLs, but are loaded from binary Falcon objects.
uint32 Falcon::Module::m_modVersion [protected] |
String Falcon::Module::m_name [protected] |
String Falcon::Module::m_path [protected] |
volatile int32 Falcon::Module::m_refcount [mutable, protected] |
Map Falcon::Module::m_serviceMap [protected] |
StringTable* Falcon::Module::m_strTab [protected] |
char Falcon::Module::m_subversion [protected] |
SymbolVector Falcon::Module::m_symbols [protected] |
SymbolTable Falcon::Module::m_symtab [protected] |
char Falcon::Module::m_version [protected] |