Pritpal Bedi wrote:
! replaced hb_vmFunction() calls by hb_vmSend()
+ added .prg function hb_execMsg( <sFuncSym>, , [<params,...>] )
which executes <sFuncSym> with set as QSELF() value.
Can you provide more insight how this can be used in PRG code.
A small example will be nice. I can sense it may solve many issues with
classes if used the other ways.
Hi,
example is the same as with HB_EXECFROMARRAY(), just need some parameter
adjustment. I attach (almost the same) code, plus C implementation of
error handler if you want to see example of hb_vmProc().
The idea of "symbol type variables as methods" actually is taken from
JavaScript:
window.google = {};
google.isOpera = e.indexOf("opera") != -1;
google.isIE = document.all && e.indexOf("msie") != -1 && google.isOpera;
google.isSafari = e.indexOf("safari") != -1;
google.time = function() {
return (new Date).getTime()
};
function h(a, d) {
var b = document.defaultView &&
document.defaultView.getComputedStyle( a, "" ), c = b &&
b.getPropertyValue( d );
return document.defaultView && b && typeof c == "string" && parseInt(
c, 10)
}
google.getHeight = function(a) {
return h(a, "height") || a.offsetHeight
};
google.getWidth = function(a) {
return h(a, "width") || a.offsetWidth
};
Later you can use it as methods, ex.:
alert(google.time());
alert(google.getWidth(document.getElementById("my_input_id")));
Regards,
Mindaugas
#include "hbclass.ch"
PROC MAIN()
LOCAL hValue := HashObject()
hValue:VAR1 := 123.45
hValue:VAR2 := DATE()
? hValue:VAR1, hValue:VAR2
// Let's create method for HashObject()
hValue:PrintVar1 := @HO_PrintVar1()
hValue:PrintVar1( 10000 )
RETURN
STATIC PROC HO_PrintVar1( nOffset )
? "I'm a function used as method"
? QSELF():VAR1 + nOffset
RETURN
CREATE CLASS HashObject
VAR __hash INIT {=>}
ERROR HANDLER OnError()
ENDCLASS
#if 0
METHOD OnError( ... ) CLASS HashObject
LOCAL cMessage := __GETMESSAGE(), xValue
IF PCOUNT() == 1 .AND. LEFT( cMessage, 1 ) == "_"
RETURN QSELF():__hash[ SUBSTR( cMessage, 2 ) ] := HB_PVALUE( 1 )
ENDIF
xValue := QSELF():__hash[ cMessage ]
IF HB_ISSYMBOL( xValue )
RETURN HB_EXECMSG( xValue, QSELF(), ... )
ENDIF
RETURN xValue
#else
#pragma begindump
#include "hbapi.h"
#include "hbapiitm.h"
#include "hbapierr.h"
#include "hbapilng.h"
#include "hbvm.h"
#include "hbstack.h"
static PHB_DYNS s_pDyns_hash = NULL;
HB_FUNC( HASHOBJECT_ONERROR )
{
const char* szMethod;
int i, iPCount;
PHB_ITEM pSelf, pHash, pValue, pKey;
iPCount = hb_pcount();
pSelf = hb_stackSelfItem();
szMethod = hb_itemGetSymbol( hb_stackBaseItem() )->szName;
if( ! s_pDyns_hash )
{
s_pDyns_hash = hb_dynsymGetCase( "__HASH" );
}
hb_vmPushDynSym( s_pDyns_hash );
hb_vmPush( hb_stackSelfItem() );
hb_vmSend( 0 );
pHash = hb_param( -1, HB_IT_HASH );
if( ! pHash )
{
hb_errRT_BASE( EG_ARG, 2017, NULL, HB_ERR_FUNCNAME,
HB_ERR_ARGS_BASEPARAMS );
return;
}
if( iPCount == 1 && szMethod[ 0 ] == '_' )
{
pKey = hb_itemPutCConst( NULL, szMethod + 1 );
pValue = hb_param( 1, HB_IT_ANY );
hb_hashAdd( pHash, pKey, pValue );
hb_itemRelease( pKey );
hb_itemReturn( pValue );
return;
}
pKey = hb_itemPutCConst( NULL, szMethod );
pValue = hb_hashGetItemPtr( pHash, pKey, 0 );
if( pValue )
{
if( HB_IS_SYMBOL( pValue ) )
{
hb_vmPush( pValue );
hb_vmPush( pSelf );
for( i = 1; i <= iPCount; i++ )
{
hb_vmPush( hb_stackItemFromBase( i ) );
}
hb_vmProc( ( USHORT ) iPCount );
}
else
{
hb_itemReturn( pValue );
}
}
else
{
hb_errRT_BASE( EG_BOUND, 1132, NULL, hb_langDGetErrorDesc(
EG_ARRACCESS ), 2, pHash, pKey );
}
hb_itemRelease( pKey );
}
#pragma enddump
#endif
_______________________________________________
Harbour mailing list
[email protected]
http://lists.harbour-project.org/mailman/listinfo/harbour