xHarbour Reference Documentation > Function Reference |
Executes a function, procedure or method indirectly.
HB_ExecFromArray( <aExecutableArray> ) --> xResult HB_ExecFromArray( <bBlock>|<cFuncName>|<pFuncPtr> ; [,<aParams>] ) --> xResult HB_ExecFromArray( <oObject>, <cMethodName>|<pMethodPtr> ; [,<aParams>] ) --> xResult
The function returns the return value of the executed code.
Function HB_ExecFromArray() is used for indirect execution of program code in xHarbour. The function can be called in different ways and accepts parameters of different data types, the first of which must contain an executable value, or item. Depending on the data type of an executable item, HB_ExecFromArray() uses different approaches for execution:
Paremeters and data types of executable items
1st param. | 2nd param. | 3rd param. | Description |
---|---|---|---|
Array | NIL | NIL | Executable array is processed |
Character string | <aParams> | NIL | Macro operator executes function or procedure |
Code block | <aParams> | NIL | Eval() executes code block |
Pointer | <aParams> | NIL | HB_Exec() executes function call |
Object | Character string | <aParams> | Macro operator executes method of object |
Object | Pointer | <aParams> | HB_Exec() executes method of object |
The following notes explain how HB_ExecFromArray() works depending on the data type of the first parameter passed:
Array
When an array is passed as first parameter, it must be a so called "executable array". This is a one dimensional array whose first element contains an "executable item". An executable item can be of Valtype()=="C" (symbolic name of function or procedure to execute), Valtype()=="B" (code block to execute) or Valtype()=="P" (pointer to function or procedure to execute). The following elements of the executable array are passed as parameters to the executable item.
If the executable item is of Valtype()=="O" (object), the second element of the executable array must contain either a character string specifying the name of the method to call, or it must be a pointer to a method obtained with HB_ObjMsgPtr(). All other values stored in the third to the last element of the executable array are passed as parameters to the object's method.
Character string
When the first parameter is a character string, it must contain the symbolic name of a function or procedure which is visible at runtime. HB_ExecFromArray() uses the macro operator to call the corresponding function or procedure. The second parameter can be optionally a one dimensional array. Its elements are passed as parameters to the function or procedure.
Code block
When the first parameter is a code block, the optional second parameter can be a one dimensional array whose elements are passed as parameters to the code block. It is executed with the Eval() function.
Pointer
When the first parameter is a pointer, it must be obtained with the function-reference operator or the HB_FuncPtr() function. The optional second parameter <aParams> contains the parameters to pass to the corresponding function or procedure. It is executed with HB_Exec().
Object
When the first parameter is an object, the second parameter must be one of
1. | character string containing the symbolic name of a method to execute |
2. | pointer to a method otained with HB_ObjMsgPtr() |
The optional third parameter <aParams> contains the parameters to pass to the corresponding method.
See also: | @(), {|| }, {}, Array(), Eval(), HB_Exec(), HB_FuncPtr(), HB_ObjMsgPtr() |
Category: | Indirect execution , Pointer functions , xHarbour extensions |
Source: | vm\eval.c |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// The example demonstrates various calls to HB_ExecFromArray() using // an executable array, code block, function name and pointer. PROCEDURE Main LOCAL cFunc := "QOUT" LOCAL pFunc := HB_FuncPtr( cFunc ) LOCAL aExec := { cFunc, "Hello", "World" } // executable array with function name HB_ExecFromArray( aExec ) // output: Hello World // function pointer and parameter array HB_ExecFromArray( pFunc, { "Hi", "there" } ) // output: Hi there // function name and parameter array ? HB_ExecFromArray( "CDOW", { Date() } ) // output: wednesday // executable array with code block and parameter bBlock := {|d| Year(d) } aExec := { bBlock, Date() } ? HB_ExecFromArray( aExec ) // output: 2006 RETURN
// This example uses an object for HB_ExecFromArray() and // demonstrates possibilities of calling a method. #include "hbclass.ch" PROCEDURE Main LOCAL pMethod, oObject LOCAL aExec, bBlock // function name and no parameters oObject := HB_ExecFromArray( "NumStat" ) // executable array with object, method name and parameters aExec := { oObject, "set", 1, 2, 3, 4, 5 } HB_ExecFromArray( aExec ) // executable array with code block and parameter bBlock := {|o| o:total() } aExec := { bBlock, oObject } ? HB_ExecFromArray( aExec ) // result: 15 // executable array with object, method pointer and no parameters pMethod := HB_ObjMsgPtr( oObject, "average" ) aExec := { oObject, pMethod } ? HB_ExecFromArray( aExec ) // result: 3 // code block and parameter array ? HB_ExecFromArray( bBlock, { oObject } ) // result: 15 RETURN CLASS NumStat PROTECTED: DATA numbers DATA count EXPORTED: METHOD set METHOD average METHOD total ENDCLASS METHOD set( ... ) CLASS NumStat ::numbers := HB_AParams() ::count := Len( ::numbers ) RETURN self METHOD average CLASS NumStat RETURN ::total() / ::count METHOD total CLASS NumStat LOCAL nTotal := 0 LOCAL nNumber FOR EACH nNumber IN ::numbers nTotal += nNumber NEXT RETURN nTotal
http://www.xHarbour.com