Page 1 of 1

Catching HB_CompileFromBuf() errors

Posted: Mon Apr 02, 2018 3:14 am
by Antonio Linares
Its been a long time since this conversation happened in the Harbour development group:

https://groups.google.com/d/msg/harbour-devel/WFnOTGO91gk/RdLiu_JyAWYJ

Today I tried to understand Przemek's words again:
Such redirection should be implemented using GT API
because it will in all cases, is portable and much simpler.
Anyhow it's still not necessary because we have PRG API for it
so it's enough to catch RTE.


And after carefully reviewing Przemek's source code, I found the right way to do it. There is an undocumented second parameter than when specified as .T. it generates RTE (run time errors) that we can easily catch:

ErrorBlock( { | o | MsgInfo( o:Description ), DoBreak() } )
oHrb = HB_CompileFromBuf( cCode, .T., "-n" ) // The .T. does the magic :-)

better later than never ;-)

Re: Catching HB_CompileFromBuf() errors

Posted: Mon Apr 02, 2018 3:29 am
by Antonio Linares
How I discovered it:

function HB_CompileFromBuf() is declared in c:\harbour\src\compiler\hbcmplib.c and it makes this call:

hb_compGenArgList( 2, hb_pcount(), &argc, &argv, &pIncItem, &pOpenFunc, &pMsgFunc );

Notice the 2. In hb_compGenArgList() we find this code:

Code: Select all | Expand

  if( pMsgFunc )
   {
      *pMsgFunc = NULL;
      if( HB_ISLOG( iFirst ) )
      {
         if( hb_parl( iFirst ) )
            *pMsgFunc = s_pp_msg;
         ++iFirst;
      }
   }


Which means: If the second parameter (the number 2) is logical and it is true, then s_pp_msg is used to route the compiler msgs.

And from s_pp_msg() the runtime error is created:

Code: Select all | Expand

     pError = hb_errRT_New( ES_ERROR, "COMPILER", 1001, ( HB_ERRCODE ) iValue,
                             szMsgBuf, szLine, 0 /*OsCode*/, EF_NONE );
      hb_errLaunch( pError );
      hb_errRelease( pError );