current stable: 0.99.6 unstable: cvs (0.99.7) |
|||||||||||||||||||||||||||||||||||||||||||||||
|
Calling FunctionsVariables are fun for a while, but when you want to start doing things, you will want to play with calling of functions. For this you will need one vital ingredient a pointer to a FeriteFunction. Namespace FunctionsOnce you have a FeriteFunction *, the next thing you're probably going to want to do is call the function it to which it refers. This is one of the trickier things to do in ferite, but only because it involves several stages in order to complete. Firstly, you need your FeriteFunction *, which can be obtained by using the ferite_find_namespace() function. Then you'll need to create a parameter list that you wish to pass to the function. This is done with the following function:
This function does its best to make creating parameter lists simple. The first parameter is the script, the second is a format string that describes the types of variables that will make up the argument list, and the rest of the parameters are the values to be used as described by the format string. The format string must be zero or more of the following:
The function will return a parameter list (FeriteVariable **) which can then be used as a parameter in the next function to be dicussed. For your information, a parameter list is simply a NULL terminated c array of FeriteVariable* - these are easy to create by hand, but this function simply aids the creation.
This function will call the function and return a FeriteVariable *, which will be the returned value of the called function. It must be caught and destroyed, or you will leak memory. Even functions returning void will return a fully allocated FeriteVariable * of type F_VAR_VOID. The first parameter is the script, the second is the pointer to the FeriteFunction you wish to call, and the last is the parameter list you had created with the previously described ferite_create_parameter_list_from_data() function. When you are finished with the parameter list, simply delete it with this function:
So there you have it, three steps to calling another function within ferite. Here is a complete example which calls 'Console.println' with the string 'Hello World':
It should be noted that the above method for calling functions works for any function that is not an instance method within an object. This will be discussed further in the next section as there are a couple of things the have to be done on top of the above steps. Object and Class FunctionsCalling methods of objects is much like calling regular functions. There are really only two differences. Firstly, you look up the FeriteFunction * from within the object using the ferite_object_get_function() function, and you always pass the object itself as the last two parameters to the method, this must be done regardless of the number of arguments the object's function takes. Example: (assume obj is of type FeriteVariable * and is a valid object)
As you can see, it's very similar to calling normal functions. You may also want to know that there is a helper function (used in the example below) for ensuring proper placement of the object in the parameter list called ferite_object_add_self_variable_to_params(). Example: (assume obj is of type FeriteVariable * and is a valid object)
Calling methods of classes (static methods) is sort of a hybrid between calling normal functions and object methods. With classes you look up the FeriteFunction * from within the functions element of the FeriteClass struct using the ferite_class_get_function() function, and then you call it like a standard function. You do not pass the object to class methods because there is no object associated with the method. For all intents and purposes class functions within ferite are treated the same as namespace functions. Here is an example: (assume klass is of type FeriteClass * and it is a valid class)
And there you have it. You can now call class and object methods, and access variables within classes and objects. |
||||||||||||||||||||||||||||||||||||||||||||||
ferite et al © 2000-2004, Chris Ross |