created a function to set callback after context creation.
- qse_awk_rtx_setcb(), qse_awk_rtx_getcb()
This commit is contained in:
parent
464f43b821
commit
9d6816c51c
@ -1109,8 +1109,6 @@ private:
|
||||
Awk (const Awk&);
|
||||
Awk& operator= (const Awk&);
|
||||
|
||||
bool triggerOnRunStart (Run& run);
|
||||
|
||||
mmgr_t mmgr;
|
||||
ccls_t ccls;
|
||||
qse_awk_prm_t prm;
|
||||
|
@ -1071,7 +1071,6 @@ qse_awk_rtx_t* qse_awk_rtx_open (
|
||||
qse_awk_t* awk,
|
||||
qse_size_t xtn,
|
||||
qse_awk_rio_t* rio,
|
||||
qse_awk_rcb_t* rcb,
|
||||
const qse_cstr_t* arg
|
||||
);
|
||||
/******/
|
||||
@ -1173,6 +1172,27 @@ void qse_awk_rtx_stop (
|
||||
);
|
||||
/******/
|
||||
|
||||
/****f* AWK/qse_awk_rtx_getcb
|
||||
* NAME
|
||||
* qse_awk_rtx_setcb - get callback
|
||||
* SYNOPSIS
|
||||
*/
|
||||
qse_awk_rcb_t* qse_awk_rtx_getcb (
|
||||
qse_awk_rtx_t* rtx
|
||||
);
|
||||
/******/
|
||||
|
||||
/****f* AWK/qse_awk_rtx_setcb
|
||||
* NAME
|
||||
* qse_awk_rtx_setcb - set callback
|
||||
* SYNOPSIS
|
||||
*/
|
||||
void qse_awk_rtx_setcb (
|
||||
qse_awk_rtx_t* rtx,
|
||||
qse_awk_rcb_t* rcb
|
||||
);
|
||||
/******/
|
||||
|
||||
/****f* AWK/qse_awk_rtx_getnargs
|
||||
* NAME
|
||||
* qse_awk_rtx_getnargs - get the number of arguments passed to qse_awk_run()
|
||||
|
@ -1336,7 +1336,8 @@ int Awk::run (const char_t** args, size_t nargs)
|
||||
qse_awk_rcb_t rcb;
|
||||
qse_xstr_t* runarg = QSE_NULL;
|
||||
|
||||
// make sure that the run field is set in Awk::onRunStart.
|
||||
// note that the run field is set below after qse_awk_rtx_open() is
|
||||
// executed.
|
||||
Run runctx (this);
|
||||
|
||||
rio.pipe = pipeHandler;
|
||||
@ -1344,16 +1345,16 @@ int Awk::run (const char_t** args, size_t nargs)
|
||||
rio.console = consoleHandler;
|
||||
rio.data = this;
|
||||
|
||||
QSE_MEMSET (&rcb, 0, QSE_SIZEOF(rcb));
|
||||
rcb.on_start = onRunStart;
|
||||
if (runCallback)
|
||||
{
|
||||
QSE_MEMSET (&rcb, 0, QSE_SIZEOF(rcb));
|
||||
rcb.on_start = onRunStart;
|
||||
rcb.on_end = onRunEnd;
|
||||
rcb.on_enter = onRunEnter;
|
||||
rcb.on_statement = onRunStatement;
|
||||
rcb.on_exit = onRunExit;
|
||||
rcb.data = &runctx;
|
||||
}
|
||||
rcb.data = &runctx;
|
||||
|
||||
if (nargs > 0)
|
||||
{
|
||||
@ -1385,7 +1386,9 @@ int Awk::run (const char_t** args, size_t nargs)
|
||||
|
||||
int n = 0;
|
||||
qse_awk_rtx_t* rtx = qse_awk_rtx_open (
|
||||
awk, QSE_SIZEOF(Run*), &rio, &rcb, (qse_cstr_t*)runarg);
|
||||
awk, QSE_SIZEOF(Run*), &rio,
|
||||
(qse_cstr_t*)runarg
|
||||
);
|
||||
if (rtx == QSE_NULL)
|
||||
{
|
||||
retrieveError();
|
||||
@ -1393,23 +1396,15 @@ int Awk::run (const char_t** args, size_t nargs)
|
||||
}
|
||||
else
|
||||
{
|
||||
Run** xtn = (Run**)qse_awk_rtx_getxtn (rtx);
|
||||
*xtn = &runctx;
|
||||
runctx.run = rtx;
|
||||
*((Run**)qse_awk_rtx_getxtn(rtx)) = &runctx;
|
||||
|
||||
if (runCallback) qse_awk_rtx_cbs (rtx, &rcb);
|
||||
n = qse_awk_rtx_loop (rtx);
|
||||
if (n == -1) retrieveError ();
|
||||
qse_awk_rtx_close (rtx);
|
||||
}
|
||||
|
||||
#if 0
|
||||
int n = qse_awk_run (
|
||||
awk, &rio, &rcb,
|
||||
(qse_cstr_t*)runarg, &runctx
|
||||
);
|
||||
if (n == -1) retrieveError ();
|
||||
#endif
|
||||
|
||||
if (runarg != QSE_NULL)
|
||||
{
|
||||
while (i > 0) qse_awk_free (awk, runarg[--i].ptr);
|
||||
@ -1567,12 +1562,6 @@ void Awk::disableRunCallback ()
|
||||
runCallback = false;
|
||||
}
|
||||
|
||||
bool Awk::triggerOnRunStart (Run& run)
|
||||
{
|
||||
if (runCallback) return onRunStart (run);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Awk::onRunStart (Run& run)
|
||||
{
|
||||
return true;
|
||||
@ -1744,17 +1733,8 @@ void Awk::freeFunctionMapValue (map_t* map, void* dptr, size_t dlen)
|
||||
int Awk::onRunStart (run_t* run, void* data)
|
||||
{
|
||||
Run* r = (Run*)data;
|
||||
|
||||
// the actual run_t value for the run-time callback is set here.
|
||||
// r here refers to runctx declared in Awk::run. As onRunStart
|
||||
// is executed immediately after the run method is invoked,
|
||||
// the run field can be set safely here. This seems to be the
|
||||
// only place to acquire the run_t value safely as Awk::run
|
||||
// is blocking.
|
||||
r->run = run;
|
||||
|
||||
r->callbackFailed = false;
|
||||
return r->awk->triggerOnRunStart(*r)? 0: -1;
|
||||
return r->awk->onRunStart(*r)? 0: -1;
|
||||
}
|
||||
|
||||
void Awk::onRunEnd (run_t* run, int errnum, void* data)
|
||||
@ -1782,14 +1762,8 @@ void Awk::onRunExit (run_t* run, val_t* ret, void* data)
|
||||
if (r->callbackFailed) return;
|
||||
|
||||
Argument x (r);
|
||||
if (x.init (ret) == -1)
|
||||
{
|
||||
r->callbackFailed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
r->awk->onRunExit (*r, x);
|
||||
}
|
||||
if (x.init (ret) == -1) r->callbackFailed = true;
|
||||
else r->awk->onRunExit (*r, x);
|
||||
}
|
||||
|
||||
void Awk::onRunStatement (run_t* run, size_t line, void* data)
|
||||
|
@ -184,8 +184,6 @@ qse_awk_t* qse_awk_open (qse_mmgr_t* mmgr, qse_size_t ext)
|
||||
qse_awk_setmaxdepth (awk, QSE_AWK_DEPTH_REX_BUILD, 0);
|
||||
qse_awk_setmaxdepth (awk, QSE_AWK_DEPTH_REX_MATCH, 0);
|
||||
|
||||
awk->assoc_data = QSE_NULL;
|
||||
|
||||
if (qse_awk_initgbls (awk) == -1) goto oops;
|
||||
|
||||
return awk;
|
||||
|
@ -89,8 +89,6 @@ struct qse_awk_t
|
||||
qse_ccls_t* ccls;
|
||||
qse_awk_prm_t* prm;
|
||||
|
||||
void* assoc_data;
|
||||
|
||||
/* options */
|
||||
int option;
|
||||
|
||||
|
@ -637,13 +637,13 @@ qse_map_t* qse_awk_rtx_getnvmap (qse_awk_rtx_t* rtx)
|
||||
}
|
||||
|
||||
qse_awk_rtx_t* qse_awk_rtx_open (
|
||||
qse_awk_t* awk, qse_size_t xtn, qse_awk_rio_t* rio,
|
||||
qse_awk_rcb_t* cbs, const qse_cstr_t* arg)
|
||||
qse_awk_t* awk, qse_size_t xtn,
|
||||
qse_awk_rio_t* rio, const qse_cstr_t* arg)
|
||||
{
|
||||
qse_awk_rtx_t* rtx;
|
||||
|
||||
QSE_ASSERTX (awk->ccls != QSE_NULL, "Call qse_setccls() first");
|
||||
QSE_ASSERTX (awk->prm != QSE_NULL, "Call qse_setprm() first");
|
||||
QSE_ASSERTX (awk->ccls != QSE_NULL, "Call qse_awk_setccls() first");
|
||||
QSE_ASSERTX (awk->prm != QSE_NULL, "Call qse_awk_setprm() first");
|
||||
|
||||
/* clear the awk error code */
|
||||
qse_awk_seterror (awk, QSE_AWK_ENOERR, 0, QSE_NULL, 0);
|
||||
@ -660,7 +660,8 @@ qse_awk_rtx_t* qse_awk_rtx_open (
|
||||
}
|
||||
|
||||
/* allocate the storage for the rtx object */
|
||||
rtx = (qse_awk_rtx_t*) QSE_AWK_ALLOC (awk, QSE_SIZEOF(qse_awk_rtx_t) + xtn);
|
||||
rtx = (qse_awk_rtx_t*) QSE_AWK_ALLOC (
|
||||
awk, QSE_SIZEOF(qse_awk_rtx_t) + xtn);
|
||||
if (rtx == QSE_NULL)
|
||||
{
|
||||
/* if it fails, the failure is reported thru
|
||||
@ -681,7 +682,6 @@ qse_awk_rtx_t* qse_awk_rtx_open (
|
||||
rtx->errnum = QSE_AWK_ENOERR;
|
||||
rtx->errlin = 0;
|
||||
rtx->errmsg[0] = QSE_T('\0');
|
||||
rtx->cbs = cbs;
|
||||
|
||||
if (init_globals (rtx, arg) == -1)
|
||||
{
|
||||
@ -710,6 +710,16 @@ qse_bool_t qse_awk_rtx_shouldstop (qse_awk_rtx_t* rtx)
|
||||
return (rtx->exit_level == EXIT_ABORT || rtx->awk->stopall);
|
||||
}
|
||||
|
||||
qse_awk_rcb_t* qse_awk_rtx_getcb (qse_awk_rtx_t* rtx)
|
||||
{
|
||||
return rtx->cbs;
|
||||
}
|
||||
|
||||
void qse_awk_rtx_setcb (qse_awk_rtx_t* rtx, qse_awk_rcb_t* rcb)
|
||||
{
|
||||
rtx->cbs = rcb;
|
||||
}
|
||||
|
||||
static void free_namedval (qse_map_t* map, void* dptr, qse_size_t dlen)
|
||||
{
|
||||
qse_awk_rtx_refdownval (
|
||||
@ -728,6 +738,7 @@ static int init_rtx (qse_awk_rtx_t* rtx, qse_awk_t* awk, qse_awk_rio_t* rio)
|
||||
QSE_MEMSET (rtx, 0, QSE_SIZEOF(qse_awk_rtx_t));
|
||||
|
||||
rtx->awk = awk;
|
||||
rtx->cbs = QSE_NULL;
|
||||
|
||||
rtx->stack = QSE_NULL;
|
||||
rtx->stack_top = 0;
|
||||
@ -1836,13 +1847,13 @@ static int run_block0 (qse_awk_rtx_t* run, qse_awk_nde_blk_t* nde)
|
||||
return n;
|
||||
}
|
||||
|
||||
#define ON_STATEMENT(run,nde) \
|
||||
if ((run)->awk->stopall) (run)->exit_level = EXIT_ABORT; \
|
||||
if ((run)->cbs != QSE_NULL && \
|
||||
(run)->cbs->on_statement != QSE_NULL) \
|
||||
#define ON_STATEMENT(rtx,nde) \
|
||||
if ((rtx)->awk->stopall) (rtx)->exit_level = EXIT_ABORT; \
|
||||
if ((rtx)->cbs != QSE_NULL && \
|
||||
(rtx)->cbs->on_statement != QSE_NULL) \
|
||||
{ \
|
||||
(run)->cbs->on_statement ( \
|
||||
run, (nde)->line, (run)->cbs->data); \
|
||||
(rtx)->cbs->on_statement ( \
|
||||
rtx, (nde)->line, (rtx)->cbs->data); \
|
||||
}
|
||||
|
||||
static int run_statement (qse_awk_rtx_t* run, qse_awk_nde_t* nde)
|
||||
|
@ -755,21 +755,21 @@ qse_awk_rtx_t* qse_awk_opensimple (qse_awk_t* awk, qse_char_t** icf, qse_awk_rcb
|
||||
rio.console = awk_eio_console;
|
||||
rio.data = &rd;
|
||||
|
||||
|
||||
rtx = qse_awk_rtx_open (
|
||||
awk,
|
||||
QSE_SIZEOF(rxtn),
|
||||
xtn + QSE_SIZEOF(rxtn),
|
||||
&rio,
|
||||
rcb,
|
||||
QSE_NULL/*runarg*/
|
||||
);
|
||||
if (rtx == QSE_NULL) return QSE_NULL;
|
||||
|
||||
rxtn = (rxtn_t*)qse_awk_rtx_getxtn (rtx);
|
||||
rxtn = (rxtn_t*) qse_awk_rtx_getxtn (rtx);
|
||||
if (qse_gettime (&now) == -1) rxtn->seed = 0;
|
||||
else rxtn->seed = (unsigned int)now;
|
||||
else rxtn->seed = (unsigned int) now;
|
||||
srand (rxtn->seed);
|
||||
|
||||
//qse_awk_rtx_setcb (rtx, rcb);
|
||||
|
||||
return rtx;
|
||||
}
|
||||
#endif
|
||||
@ -795,16 +795,17 @@ int qse_awk_runsimple (qse_awk_t* awk, qse_char_t** icf, qse_awk_rcb_t* rcb)
|
||||
awk,
|
||||
QSE_SIZEOF(rxtn),
|
||||
&rio,
|
||||
rcb,
|
||||
QSE_NULL/*runarg*/
|
||||
);
|
||||
if (rtx == QSE_NULL) return -1;
|
||||
|
||||
rxtn = (rxtn_t*)qse_awk_rtx_getxtn (rtx);
|
||||
rxtn = (rxtn_t*) qse_awk_rtx_getxtn (rtx);
|
||||
if (qse_gettime (&now) == -1) rxtn->seed = 0;
|
||||
else rxtn->seed = (unsigned int)now;
|
||||
else rxtn->seed = (unsigned int) now;
|
||||
srand (rxtn->seed);
|
||||
|
||||
qse_awk_rtx_setcb (rtx, rcb);
|
||||
|
||||
/* execute the start callback if it exists */
|
||||
if (rcb != QSE_NULL && rcb->on_start != QSE_NULL)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user