Qore Programming Language Reference Manual
0.8.11.1
|
Program objects allow Qore programs to support subprograms with the option to restrict capabilities, for example, to support user-defined logic for application actions. More...
Public Member Functions | |
any | callFunction (string name,...) |
Calls a function in the program object and returns the return value. More... | |
any | callFunctionArgs (string name, *softlist vargs) |
Calls a function in the program object giving the arguments to the function as a list and returns the return value. More... | |
constructor (softint po=PO_DEFAULT) | |
Creates the program object and optionally sets program capabilities (parse options) More... | |
copy () | |
Throws an exception to prevent objects of this class from being copied. More... | |
nothing | define (string def, any val) |
Sets a parse define for the current Program. More... | |
destructor () | |
Waits for all threads to finish executing, then deletes all global variables, dereferences the internal Program object and deletes the Qore object. | |
nothing | disableParseOptions (softint opt) |
Removes the given parse options to the current parse option mask. More... | |
bool | existsFunction (string name) |
Checks if a user function exists in the program object. More... | |
any | getDefine (string def) |
Retrieves the value of the given parse define in the current Program. More... | |
any | getGlobalVariable (string varname, *reference rexists) |
Returns a the value of the global variable identified by the first string argument. More... | |
int | getParseOptions () |
Returns the current binary-or'ed parse option mask for the Program object. More... | |
*string | getScriptDir () |
Returns the current script directory as a string or NOTHING if not set. More... | |
*string | getScriptName () |
Returns the current script name as a string or NOTHING if not set. More... | |
*string | getScriptPath () |
Returns the current script directory and filename if known, otherwise returns NOTHING. More... | |
TimeZone | getTimeZone () |
Returns the default local time zone for the object. More... | |
list | getUserFunctionList () |
Returns a list of strings of all user functions defined in the program object. More... | |
nothing | importClass (string cls) |
Imports a class into the program object's space; any calls to the imported class's code will run with the parent Program object's permissions. More... | |
nothing | importFunction (string func) |
Imports a function into the program object's space; any calls to the imported function will run with the parent Program object's permissions. More... | |
nothing | importFunction (string func, string new_name) |
Imports a function into the program object's space and gives it a new name; any calls to the imported function will run with the parent Program object's permissions. More... | |
nothing | importGlobalVariable (string varname, bool readonly=False) |
Imports a global variable into the program object's space. More... | |
bool | isDefined (string def) |
Returns True if the given parse define is defined in the current Program (does not have to have a value defined to return True), False if not. More... | |
loadModule (string name) | |
Loads a Qore module into the Program object at run-time. More... | |
nothing | lockOptions () |
Locks parse options so that they cannot be changed. More... | |
*hash | parse (string code, string label, *softint warning_mask, *string source, *softint offset, softbool format_label=True) |
Parses the string argument and adds the code to the Program object. More... | |
nothing | parseCommit () |
Commits and pending code processed with Program::parsePending() to the Program object after resolving all outstanding references in the pending code. More... | |
*hash | parseCommit (int warning_mask) |
Commits and pending code processed with Program::parsePending() to the Program object after resolving all outstanding references in the pending code. More... | |
*hash | parsePending (string code, string label, *softint warning_mask, *string source, *softint offset, softbool format_label=True) |
Parses the text passed to pending lists in the Program object; does not resolve all references or commit the code to the Program object. More... | |
nothing | parseRollback () |
Rolls back any pending code processed with Program::parsePending() that has not yet been committed to the Program object with Program::parseCommit() More... | |
nothing | replaceParseOptions (softint opt) |
Replaces the parse options for the Program object. More... | |
any | run () |
Runs the program and optionally returns a value if the top-level code exits with a return statement. More... | |
nothing | setParseOptions (softint opt=PO_DEFAULT) |
Sets parse options in the parse option mask for the Program object. More... | |
nothing | setScriptPath (*string path) |
Sets (or clears) the script path (directory and filename) for the object. More... | |
nothing | setTimeZone (TimeZone zone) |
Sets the default local time zone for the object. More... | |
nothing | setTimeZoneRegion (string region) |
Sets the default local time zone for the object from a path to a zoneinfo time zone region file. More... | |
nothing | setTimeZoneUTCOffset (softint seconds_east) |
Sets the default time zone for the Program object based on the number of seconds east of UTC; for zones west of UTC, use negative numbers. More... | |
nothing | undefine (string def) |
Unsets a parse define for the current Program. More... | |
Program objects allow Qore programs to support subprograms with the option to restrict capabilities, for example, to support user-defined logic for application actions.
Parsing in Qore happens in two steps; first all code is parsed to pending data structures, and then in the second stage, all references are resolved, and, if there are no errors, then all changes are committed to the Program object. Note that all parse actions (Program::parse(), Program::parsePending(), Program::parseCommit(), and Program::parseRollback()) are atomic; there is a thread lock on each Program object to ensure atomicity, and if any parse errors occur in any stage of parsing, any pending changes to the Program object are automatically rolled back. However parse actions that affect only one stage of the two stages of parsing (Program::parsePending(), Program::parseCommit() and Program::parseRollback()) are atomic within themselves, but not between calls, so one thread may inadvertently commit changes to a Program object if two or more threads are trying to perform transaction-safe two-stage parsing on a Program object without explicit user locking.
Parse option constants can be used to limit the capabilities of a Program object. These options should be binary-OR'ed together and passed to the Program object's constructor. Also see qore Executable Command-Line Processing for equivalent command-line options, and Parse Directives for equivalent parse directives.
Note that a program can provide controlled access to functionality otherwise restricted by parse options by exporting a custom API into the child program object using either the Program::importFunction() or Program::importGlobalVariable() method. This is possible because code (functions or object methods) imported into and called from a subprogram will run in the parent's space and therefore with the parent's capabilities.
Classes, constants, namespaces, functions, and global variables are only inherited into child Program objects if they are declared public and no parse options prohibit it.
Symbols can also be imported into Program objects singly using methods such as Program::importClass() and Program::importGlobalVariable(), etc.
any Qore::Program::callFunction | ( | string | name, |
... | |||
) |
Calls a function in the program object and returns the return value.
The function runs with the permissions of the Program object containing the function.
name | The name of the function to call |
... | The remaining arguments passed to the method are passed to the function to be called |
INVALID-FUNCTION-ACCESS | Parse options do not allow this builtin function to be called |
NO-FUNCTION | The function does not exist |
ENCODING-CONVERSION-ERROR | the function name could not be converted to the default character encoding |
any Qore::Program::callFunctionArgs | ( | string | name, |
*softlist | vargs | ||
) |
Calls a function in the program object giving the arguments to the function as a list and returns the return value.
The function runs with the permissions of the Program object containing the function.
name | The name of the function to call |
vargs | The arguments to the function to be called |
INVALID-FUNCTION-ACCESS | Parse options do not allow this builtin function to be called |
NO-FUNCTION | The function does not exist |
ENCODING-CONVERSION-ERROR | the function name could not be converted to the default character encoding |
Qore::Program::constructor | ( | softint | po = PO_DEFAULT | ) |
Creates the program object and optionally sets program capabilities (parse options)
Note that if PO_NO_CHILD_PO_RESTRICTIONS is not set in the parent Program when the new Program object is created, then the created Program object will have the parent's parse options added to its parse options as given by the argument to the constructor.
In other words, if PO_NO_CHILD_PO_RESTRICTIONS is not set, it's not possible to create a child Program object with fewer restrictions than the parent Program object (any attempt to do so will be silently ignored).
However, if PO_NO_CHILD_PO_RESTRICTIONS is set in the parent Program object, then the parse option argument to the constructor will be applied literally to the child object.
po | A binary OR'ed product of parse options |
PROGRAM-OPTION-ERROR | invalid parse options used |
Qore::Program::copy | ( | ) |
Throws an exception to prevent objects of this class from being copied.
PROGRAM-COPY-ERROR | copying Program objects is currently unsupported |
nothing Qore::Program::define | ( | string | def, |
any | val | ||
) |
Sets a parse define for the current Program.
def | The parse define to assign |
val | The value to assign to the define |
nothing Qore::Program::disableParseOptions | ( | softint | opt | ) |
Removes the given parse options to the current parse option mask.
An OPTIONS-LOCKED
exception is thrown if parse options have been locked (for example with Program::lockOptions())
opt | A single parse option or binary-or'ed combination of parse options to unset in the parse option mask for the object; any bit set in this argument will be unset (ie zeroed out or combined with inverse binary and) in the Program's parse option mask |
OPTIONS-LOCKED | Parse options have been locked and cannot be changed |
PROGRAM-OPTION-ERROR | invalid parse options used |
bool Qore::Program::existsFunction | ( | string | name | ) |
Checks if a user function exists in the program object.
name | The name of the function to check |
ENCODING-CONVERSION-ERROR | the function name could not be converted to the default character encoding |
any Qore::Program::getDefine | ( | string | def | ) |
Retrieves the value of the given parse define in the current Program.
any Qore::Program::getGlobalVariable | ( | string | varname, |
*reference | rexists | ||
) |
Returns a the value of the global variable identified by the first string argument.
varname | The string name of the global variable to find, not including the leading "$" character |
rexists | An lvalue reference to a bool to determine if the global variable exists (because this method could return NOTHING when the variable exists as well as when it does not) |
"$"
symbol)int Qore::Program::getParseOptions | ( | ) |
*string Qore::Program::getScriptDir | ( | ) |
Returns the current script directory as a string or NOTHING if not set.
Gets the script directory set with Program::setScriptPath(). This is the same value that will be returned in the Qore program code with the get_script_dir() function if called from within the Program.
"/"
on UNIX, "\\"
on Windows)*string Qore::Program::getScriptName | ( | ) |
Returns the current script name as a string or NOTHING if not set.
Gets the script filename set with Program::setScriptPath(). This is the same value that will be returned in the Qore program code with the get_script_name() function if called from within the Program.
*string Qore::Program::getScriptPath | ( | ) |
Returns the current script directory and filename if known, otherwise returns NOTHING.
Gets the script directory and filename set with Program::setScriptPath(). This is the same value that will be returned in the Qore program code with the get_script_path() function if called from within the Program.
TimeZone Qore::Program::getTimeZone | ( | ) |
Returns the default local time zone for the object.
list Qore::Program::getUserFunctionList | ( | ) |
Returns a list of strings of all user functions defined in the program object.
nothing Qore::Program::importClass | ( | string | cls | ) |
Imports a class into the program object's space; any calls to the imported class's code will run with the parent Program object's permissions.
This allows a user-defined API with greater capabilities than the embedded Program object to be imported into the embedded code.
cls | the name of the class to import into the Program object |
CLASS-IMPORT-ERROR | Cannot import a class into the same Program object; class or namespace with this name already exists or the source class does not exist |
ENCODING-CONVERSION-ERROR | the class name could not be converted to the default character encoding |
nothing Qore::Program::importFunction | ( | string | func | ) |
Imports a function into the program object's space; any calls to the imported function will run with the parent Program object's permissions.
This allows a user-defined API with greater capabilities than the embedded Program object to be imported into the embedded code.
func | the name of the function to import into the Program object |
FUNCTION-IMPORT-ERROR | Cannot import a function into the same Program object; function with this name already exists |
PROGRAM-IMPORTFUNCTION-NO-FUNCTION | The function does not exist |
ENCODING-CONVERSION-ERROR | the function name could not be converted to the default character encoding |
"Namespace::func"
) Imports a function into the program object's space and gives it a new name; any calls to the imported function will run with the parent Program object's permissions.
This allows a user-defined API with greater capabilities than the embedded Program object to be imported into the embedded code.
func | the name of the function to import into the Program object |
new_name | the name of the function as it will be known in the Program object once imported |
FUNCTION-IMPORT-ERROR | Cannot import a function into the same Program object; function with this name already exists; target namespace does not exist |
PROGRAM-IMPORTFUNCTION-NO-FUNCTION | The function does not exist |
ENCODING-CONVERSION-ERROR | the function name could not be converted to the default character encoding |
"Namespace::func"
) Imports a global variable into the program object's space.
If the variable is an object, then any methods called from the subprogram will run in the parent's space
varname | The name of the global variable without the "$" |
readonly | If this argument is True, then the variable will be imported read-only, and cannot be changed by the subprogram (note that if the imported |
PROGRAM-IMPORTGLOBALVARIABLE-EXCEPTION | The global variable does not exist in the source program, or the target variable already exists |
ENCODING-CONVERSION-ERROR | the global variablt name could not be converted to the default character encoding |
bool Qore::Program::isDefined | ( | string | def | ) |
Returns True if the given parse define is defined in the current Program (does not have to have a value defined to return True), False if not.
def | The name of the define to check |
Qore::Program::loadModule | ( | string | name | ) |
Loads a Qore module into the Program object at run-time.
If a feature with the same name already exists, then this feature's code is imported into the current Program object if necessary and no further action is taken.
Note that modules providing objects resolved at parse time (classes, constants, functions, etc) must be loaded prior to parsing.
name | either a feature name (a module will be searched with this feature name) or a path to a module to load |
LOAD-MODULE-ERROR | module cannot be loaded: API incompatibility, module defines symbols already defined in the target object, etc |
nothing Qore::Program::lockOptions | ( | ) |
Locks parse options so that they cannot be changed.
*hash Qore::Program::parse | ( | string | code, |
string | label, | ||
*softint | warning_mask, | ||
*string | source, | ||
*softint | offset, | ||
softbool | format_label = True |
||
) |
Parses the string argument and adds the code to the Program object.
This method causes both stages of parsing to be executed; if this method is successful, then the code parsed is committed to the Program object. This method is equivalent to calling Program::parsePending() and Program::parseCommit() in one atomic call.
If an exception occurs in this method, all pending code is backed out, not just code parsed by this method (for example, in case uncommitted code added by Program::parsePending() also exists in the Program object before calling this method).
code | The code to parse into the Program object |
label | The label for the code; this label will be given if any parse or run-time errors are raised for the code given |
warning_mask | An optional warning mask; see Warning Constants for values to combine by binary-or; if this argument is 0 or not given then no warnings will be checked or issued and the return value will always be NOTHING |
source | An optional source file name for the code being parsed; this is useful if sections of a file are parsed |
offset | An optional line offset for use with the source parameter; this gives the line offset in the file to the code being parsed |
format_label | If this argument is True, then the label is formatted as "<run-time-loaded: label>" ; if False, then it is passed as-is |
nothing Qore::Program::parseCommit | ( | ) |
Commits and pending code processed with Program::parsePending() to the Program object after resolving all outstanding references in the pending code.
An exception in this method causes all pending code to be rolled back immediately.
Commits and pending code processed with Program::parsePending() to the Program object after resolving all outstanding references in the pending code.
An exception in this method causes all pending code to be rolled back immediately.
*hash Qore::Program::parsePending | ( | string | code, |
string | label, | ||
*softint | warning_mask, | ||
*string | source, | ||
*softint | offset, | ||
softbool | format_label = True |
||
) |
Parses the text passed to pending lists in the Program object; does not resolve all references or commit the code to the Program object.
References are resolved in the Program::parseCommit() method.
Program::parseCommit() must be called to resolve all references and commit the code to the Program object; until Program::parseCommit() is called, none of the code parsed by this method will be available for execution in the Program object.
If an exception occurs in this method, all pending code is backed out, not just code parsed by this method.
code | The code to parse into the Program object |
label | The label for the code; this label will be given if any parse or run-time errors are raised for the code given |
warning_mask | An optional warning mask; see Warning Constants for values to combine by binary-or; if this argument is 0 or not given then no warnings will be checked or issued and the return value will always be NOTHING |
source | An optional source file name for the code being parsed; this is useful if sections of a file are parsed |
offset | An optional line offset for use with the source parameter; this gives the line offset in the file to the code being parsed |
format_label | If this argument is True, then the label is formatted as "<run-time-loaded: label>" ; if False, then it is passed as-is |
nothing Qore::Program::parseRollback | ( | ) |
Rolls back any pending code processed with Program::parsePending() that has not yet been committed to the Program object with Program::parseCommit()
nothing Qore::Program::replaceParseOptions | ( | softint | opt | ) |
Replaces the parse options for the Program object.
An OPTION-ERROR
exception is thrown if the calling Program object does not have PO_NO_CHILD_PO_RESTRICTIONS set.
opt | A single parse option or binary-or'ed combination of parse options to unset in the parse option mask for the object |
OPTION-ERROR | The calling Program does not have the PO_NO_CHILD_PO_RESTRICTIONS option set, and therefore cannot call Program::replaceParseOptions() |
any Qore::Program::run | ( | ) |
Runs the program and optionally returns a value if the top-level code exits with a return statement.
nothing Qore::Program::setParseOptions | ( | softint | opt = PO_DEFAULT | ) |
Sets parse options in the parse option mask for the Program object.
An OPTIONS-LOCKED
exception is thrown if parse options have been locked (for example with Program::lockOptions())
opt | A single parse option or binary-or'ed combination of parse options to set in the parse option mask for the object; the given argument will be combined with binary or with the existing parse option mask |
OPTIONS-LOCKED | Parse options have been locked and cannot be changed |
PROGRAM-OPTION-ERROR | invalid parse options used |
nothing Qore::Program::setScriptPath | ( | *string | path | ) |
Sets (or clears) the script path (directory and filename) for the object.
path | The path (directory and filename) for the current script; if the directory component is missing, then the current directory is assumed |
nothing Qore::Program::setTimeZone | ( | TimeZone | zone | ) |
Sets the default local time zone for the object.
nothing Qore::Program::setTimeZoneRegion | ( | string | region | ) |
Sets the default local time zone for the object from a path to a zoneinfo time zone region file.
If there are errors opening, reading, or parsing the file (or the Windows registry entry, depending on the platform), an exception is thrown
region | The path to the zoneinfo file for the time zone region to set as the local time zone for the Program object |
TZINFO-ERROR | Unable to read zoneinfo file; invalid file magic; error parsing zoneinfo file, etc |
nothing Qore::Program::setTimeZoneUTCOffset | ( | softint | seconds_east | ) |
Sets the default time zone for the Program object based on the number of seconds east of UTC; for zones west of UTC, use negative numbers.
Time zones set with this method cannot have any daylight savings time information; to set a zone with daylight savings time information, use Program::setTimeZoneRegion() instead
seconds_east | The number of seconds east of UTC; for zones west of UTC, use negative numbers |
nothing Qore::Program::undefine | ( | string | def | ) |
Unsets a parse define for the current Program.
def | The name of the define to undefine; if the given define is not defined anyway, the operation is ignored |