Qore Programming Language
1.12.0
|
To add a builtin function to the library, you have to call BuiltinFunctionList::add(). The BuiltinFunctionList class only contains static functions, but it can also be accessed through the global builtinFunctions
object.
Builtin functions must have the following signature:
Then the function can be added to the library's builtin function list with the following command:
When adding a function that provides networking functionality, process control, filesystem access, threading functionality, or any other functionality that may potentially need to be restricted in QoreProgram objects, tag the function with one of the QDOM_* constants in Restrictions.h by including the domain constant as the third parameter to the BuiltinFunctionList::add() call. If a function provides functionality that falls into more than one domain, then binary or them together as in the following example:
Use the inline functions in params.h to access function arguments. These are:
The following inline functions provide arguments of a specific type:
Note that there are no functions for the integer (NT_INT) and floating-point (NT_FLOAT) types. These values should be acquired as necessary using the AbstractQoreNode::getAsInt(), AbstractQoreNode::getAsBigInt(), and AbstractQoreNode::getAsFloat() functions to allow for transparent type conversion from other data types.
Here are some examples:
To use a string value, but accept conversion from other data types, use the QoreStringValueHelper class. This class is best for getting QoreString values or simply getting a "char *" for the converted value. The QoreStringValueHelper class also provides a constructor that allows the target character encoding to be specified (there is an example in the class documentation). If you need a QoreStringNode (NT_STRING) value, then use the QoreStringNodeValueHelper class instead.
Here is an example of interpreting a function argument as a string value using the QoreStringValueHelper class:
To process arguments meant to be timeout values (or meant to specify a period of time) where an integer implies certain units (such as milliseconds or seconds) but also allow relative DateTimeNode (NT_DATE) values to be processed, use the following functions:
Each of the above functions returns an integer, assuming a certain unit for non DateTimeNode values, and also returns a default value if no argument was present (either 0 or -1).
The following is an example of using getMsZeroInt():
Each Qore function should return a pointer to an AbstractQoreNode giving the return value of the function. If the function does not return a value, then it should simply return 0 as follows:
Otherwise, the pointer's reference count will be owned by the caller of builtin function, so returning a descendent of AbstractQoreNode created with the C++ new operator is OK, otherwise you have to make sure that the value returned by the builtin function is referenced for the return. In other words, returning AbstractQoreNode descendents that have an incremented reference count is OK, but, for example, to return an argument of the function as the return value of the function, you have to increment the reference count manually – the easiest way to do this is to call AbstractQoreNode::refSelf() on the value to be returned, as in the following example:
See the section below on Handling Qore Data for more information.
If your function raises an exception, then you must call ExceptionSink::raiseException() against the ExceptionSink argument to the function. In the case that your function raises a Qore-language exception, the function must always return 0 as a return value. The following is an example: