diff --git a/lib/framework/frame.h b/lib/framework/frame.h index 063b52c32..5de46bd09 100644 --- a/lib/framework/frame.h +++ b/lib/framework/frame.h @@ -176,7 +176,7 @@ UINT HashStringIgnoreCase( char *String ); /* Endianness hacks */ -static inline void endian_uword(UWORD *uword) { +static __inline void endian_uword(UWORD *uword) { #ifdef __BIG_ENDIAN__ UBYTE tmp, *ptr; @@ -187,7 +187,7 @@ static inline void endian_uword(UWORD *uword) { #endif } -static inline void endian_sword(SWORD *sword) { +static __inline void endian_sword(SWORD *sword) { #ifdef __BIG_ENDIAN__ UBYTE tmp, *ptr; @@ -198,7 +198,7 @@ static inline void endian_sword(SWORD *sword) { #endif } -static inline void endian_udword(UDWORD *udword) { +static __inline void endian_udword(UDWORD *udword) { #ifdef __BIG_ENDIAN__ UBYTE tmp, *ptr; @@ -212,7 +212,7 @@ static inline void endian_udword(UDWORD *udword) { #endif } -static inline void endian_sdword(SDWORD *sdword) { +static __inline void endian_sdword(SDWORD *sdword) { #ifdef __BIG_ENDIAN__ UBYTE tmp, *ptr; @@ -226,7 +226,7 @@ static inline void endian_sdword(SDWORD *sdword) { #endif } -static inline void endian_fract(FRACT *fract) { +static __inline void endian_fract(FRACT *fract) { #ifdef __BIG_ENDIAN__ UBYTE tmp, *ptr; diff --git a/lib/script/event.c b/lib/script/event.c index b14e17e9e..d3af0dc21 100644 --- a/lib/script/event.c +++ b/lib/script/event.c @@ -359,7 +359,7 @@ BOOL eventNewContext(SCRIPT_CODE *psCode, CONTEXT_RELEASE release, SCRIPT_CONTEXT **ppsContext) { SCRIPT_CONTEXT *psContext; - SDWORD val, storeIndex, type, arrayNum, i, arraySize; + SDWORD val, storeIndex, type, arrayNum, i,j, arraySize; VAL_CHUNK *psNewChunk, *psNextChunk; ASSERT((PTRVALID(psCode, sizeof(SCRIPT_CODE)), @@ -368,6 +368,7 @@ BOOL eventNewContext(SCRIPT_CODE *psCode, CONTEXT_RELEASE release, // Get a new context if (!HEAP_ALLOC(psContHeap, (void*) &psContext)) { + debug(LOG_ERROR,"eventNewContext: HEAP_ALLOC failed"); return FALSE; } @@ -387,6 +388,66 @@ BOOL eventNewContext(SCRIPT_CODE *psCode, CONTEXT_RELEASE release, arraySize *= psCode->psArrayInfo[arrayNum].elements[i]; } } + + + //prepare local variables (initialize, store type) + //------------------------------- + psCode->ppsLocalVarVal = (INTERP_VAL **)MALLOC(sizeof(INTERP_VAL*) * psCode->numEvents); //allocate space for array of local var arrays for each event + + debug(LOG_SCRIPT,"allocated space for %d events", psCode->numEvents); + + for(i=0;i < psCode->numEvents; i++) + { + if(psCode->numLocalVars[i] > 0) //this event has any local vars declared + { + psCode->ppsLocalVarVal[i] = (INTERP_VAL*)MALLOC(sizeof(INTERP_VAL) * psCode->numLocalVars[i]); //allocate space for local vars array (for the current event) + + debug(LOG_SCRIPT,"Event %d has %d local variables", i, psCode->numLocalVars[i]); + + for(j=0; j < psCode->numLocalVars[i]; j++) + { + type = psCode->ppsLocalVars[i][j]; + psCode->ppsLocalVarVal[i][j].type = type; //store (copy) var type (data used during parsing -> data used during interpreting) + + //debug(LOG_SCRIPT,"var %d's type: %d", i, type); + + + //initialize Strings and integers + if(type == VAL_STRING) + { + debug(LOG_SCRIPT,"eventNewContext: STRING type variables are not implemented"); + + psCode->ppsLocalVarVal[i][j].v.sval = (char*)MALLOC(255); //TODO: MAXSTRLEN + strcpy(psCode->ppsLocalVarVal[i][j].v.sval,"\0"); + } + else + { + psCode->ppsLocalVarVal[i][j].v.ival = 0; + } + + //Initialize objects + if (asCreateFuncs != NULL && type < numFuncs && asCreateFuncs[type]) + { + if (!asCreateFuncs[type](&(psCode->ppsLocalVarVal[i][j]) )) + { + debug(LOG_ERROR,"eventNewContext: asCreateFuncs failed for local var"); + return FALSE; + } + } + + //debug(LOG_SCRIPT, "i=%d, j=%d, value=%d",i,j,psCode->ppsLocalVarVal[i][j].v.ival); + } + + debug(LOG_SCRIPT,"------"); + + } + else //this event has no local vars + { + psCode->ppsLocalVarVal[i] = NULL; + } + + } + while (val >= 0) { if (!HEAP_ALLOC(psValHeap, (void*) &psNewChunk)) diff --git a/lib/script/interp.c b/lib/script/interp.c index 983509838..34e8d6fb4 100644 --- a/lib/script/interp.c +++ b/lib/script/interp.c @@ -202,6 +202,50 @@ BOOL interpInitialise(void) return TRUE; } +//reset local vars +BOOL resetLocalVars(SCRIPT_CODE *psCode, UDWORD EventIndex) +{ + + SDWORD i; + + if(EventIndex >= psCode->numEvents) + { + debug(LOG_ERROR, "resetLocalVars: wrong event index: %d", EventIndex); + return FALSE; + } + + for(i=0; i < psCode->numLocalVars[EventIndex]; i++) + { + //Initialize main value + if(psCode->ppsLocalVarVal[EventIndex][i].type == VAL_STRING) + { + debug(LOG_ERROR , "resetLocalVars: String type is not implemented"); + psCode->ppsLocalVarVal[EventIndex][i].v.sval = (char*)MALLOC(255); //MAXSTRLEN + + strcpy(psCode->ppsLocalVarVal[EventIndex][i].v.sval,"\0"); + } + else + { + psCode->ppsLocalVarVal[EventIndex][i].v.ival = 0; + } + + /* only group (!) must be re-created each time */ + //if (psCode->ppsLocalVarVal[EventIndex][i].type == ST_GROUP) + //{ + // //DB_INTERP(("resetLocalVars - created\n")); + // + // if (!asCreateFuncs[psCode->ppsLocalVarVal[EventIndex][i].type](&(psCode->ppsLocalVarVal[EventIndex][i]) )) + // { + // debug(LOG_ERROR, "asCreateFuncs failed for local var (re-init)"); + // return FALSE; + // } + //} + } + + //debug(LOG_SCRIPT, "Reset local vars for event %d", EventIndex); + return TRUE; +} + /* Run a compiled script */ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD index, UDWORD offset) { @@ -217,7 +261,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i SDWORD instructionCount = 0; UDWORD CurEvent; - BOOL bStop; + BOOL bStop,bEvent; ASSERT((PTRVALID(psContext, sizeof(SCRIPT_CONTEXT)), "interpRunScript: invalid context pointer")); @@ -227,6 +271,8 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i if (bInterpRunning) { + debug(LOG_ERROR,"interpRunScript: interpreter already running" + " - callback being called from within a script function?"); ASSERT((FALSE, "interpRunScript: interpreter already running" " - callback being called from within a script function?")); @@ -249,12 +295,15 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i numGlobals = psProg->numGlobals; psGlobals = psContext->psGlobals; + bEvent = FALSE; + // Find the code range switch (runType) { case IRT_TRIGGER: if (index > psProg->numTriggers) { + debug(LOG_ERROR,"interpRunScript: trigger index out of range"); ASSERT((FALSE, "interpRunScript: trigger index out of range")); return FALSE; } @@ -265,14 +314,18 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i case IRT_EVENT: if (index > psProg->numEvents) { + debug(LOG_ERROR,"interpRunScript: trigger index out of range"); ASSERT((FALSE, "interpRunScript: trigger index out of range")); return FALSE; } pCodeBase = psProg->pCode + psProg->pEventTab[index]; pCodeStart = pCodeBase + offset; pCodeEnd = psProg->pCode + psProg->pEventTab[index+1]; + + bEvent = TRUE; //remember it's an event break; default: + debug(LOG_ERROR,"interpRunScript: unknown run type"); ASSERT((FALSE, "interpRunScript: unknown run type")); return FALSE; } @@ -292,6 +345,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i { if (instructionCount > INTERP_MAXINSTRUCTIONS) { + debug( LOG_ERROR, "interpRunScript: max instruction count exceeded - infinite loop ?" ); ASSERT((FALSE, "interpRunScript: max instruction count exceeded - infinite loop ?")); goto exit_with_error; @@ -305,8 +359,9 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i { /* Custom function call */ case OP_FUNC: - debug( LOG_SCRIPT, "OP_FUNC" ); - if(!RetStackRemember(CurEvent, (UDWORD)(ip + 2))) //Remember where to jump back later + //debug( LOG_SCRIPT, "-OP_FUNC" ); + //debug( LOG_SCRIPT, "OP_FUNC: remember event %d, ip=%d", CurEvent, (ip + 2) ); + if(!RetStackRemember(CurEvent, (ip + 2))) //Remember where to jump back later { debug( LOG_ERROR, "interpRunScript() - RetStackRemember() failed."); return FALSE; @@ -314,6 +369,8 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i CurEvent = *(ip+1); //Current event = event to jump to + + if (CurEvent > psProg->numEvents) { debug( LOG_ERROR, "interpRunScript: trigger index out of range"); @@ -328,8 +385,96 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i ip = pCodeStart; //Start at the beginning of the new event + //debug( LOG_SCRIPT, "-OP_FUNC: jumped to event %d; ip=%d, numLocalVars: %d", CurEvent, ip, psContext->psCode->numLocalVars[CurEvent] ); + + //debug( LOG_SCRIPT, "-END OP_FUNC" ); + break; + //handle local variables + case OP_PUSHLOCAL: + + //debug( LOG_SCRIPT, "OP_PUSHLOCAL"); + //debug( LOG_SCRIPT, "OP_PUSHLOCAL, (CurEvent=%d, data =%d) num loc vars: %d; pushing: %d", CurEvent, data, psContext->psCode->numLocalVars[CurEvent], psContext->psCode->ppsLocalVarVal[CurEvent][data].v.ival); + + if (data >= psContext->psCode->numLocalVars[CurEvent]) + { + debug(LOG_ERROR, "interpRunScript: OP_PUSHLOCAL: variable index out of range"); + ASSERT((FALSE, "interpRunScript: OP_PUSHLOCAL: variable index out of range")); + goto exit_with_error; + } + + //debug( LOG_SCRIPT, "OP_PUSHLOCAL 2"); + //debug(LOG_SCRIPT, "OP_PUSHLOCAL type: %d", psContext->psCode->ppsLocalVarVal[CurEvent][data].type); + + if (!stackPush( &(psContext->psCode->ppsLocalVarVal[CurEvent][data]) )) + { + debug(LOG_ERROR, "interpRunScript: OP_PUSHLOCAL: push failed"); + goto exit_with_error; + } + + //debug( LOG_SCRIPT, "OP_PUSHLOCAL 3"); + + ip += 1;//aOpSize[opcode]; + break; + case OP_POPLOCAL: + + //debug( LOG_SCRIPT, "OP_POPLOCAL, event index: '%d', data: '%d'", CurEvent, data); + //debug( LOG_SCRIPT, "OP_POPLOCAL, numLocalVars: '%d'", psContext->psCode->numLocalVars[CurEvent]); + + if (data >= psContext->psCode->numLocalVars[CurEvent]) + { + debug(LOG_ERROR, "interpRunScript: OP_POPLOCAL: variable index out of range"); + ASSERT((FALSE, "interpRunScript: variable index out of range")); + goto exit_with_error; + } + + //debug( LOG_SCRIPT, "OP_POPLOCAL 2"); + //DbgMsg("OP_POPLOCAL type: %d, CurEvent=%d, data=%d", psContext->psCode->ppsLocalVarVal[CurEvent][data].type, CurEvent, data); + + if (!stackPopType( &(psContext->psCode->ppsLocalVarVal[CurEvent][data]) )) + { + debug(LOG_ERROR, "interpRunScript: OP_POPLOCAL: pop failed"); + goto exit_with_error; + } + //debug(LOG_SCRIPT, "OP_POPLOCAL: type=%d, val=%d", psContext->psCode->ppsLocalVarVal[CurEvent][data].type, psContext->psCode->ppsLocalVarVal[CurEvent][data].v.ival); + + //debug( LOG_SCRIPT, "OP_POPLOCAL 3"); + ip += 1; //aOpSize[opcode]; + + break; + + case OP_PUSHLOCALREF: + + // The type of the variable is stored in with the opcode + sVal.type = (*ip) & OPCODE_DATAMASK; + + /* get local var index */ + data = *(ip + 1); + + if (data >= psContext->psCode->numLocalVars[CurEvent]) + { + debug(LOG_ERROR, "interpRunScript: OP_PUSHLOCALREF: variable index out of range"); + ASSERT((FALSE, "interpRunScript: OP_PUSHLOCALREF: variable index out of range")); + goto exit_with_error; + } + /* get local variable */ + psVar = &(psContext->psCode->ppsLocalVarVal[CurEvent][data]); + + sVal.v.oval = &(psVar->v.ival); + + TRCPRINTF(("PUSHREF ")); + TRCPRINTVAL(&sVal); + TRCPRINTF(("\n")); + + if (!stackPush(&sVal)) + { + debug(LOG_ERROR, "interpRunScript: OP_PUSHLOCALREF: push failed"); + goto exit_with_error; + } + ip += 2; + break; + case OP_PUSH: // The type of the value is stored in with the opcode sVal.type = (*ip) & OPCODE_DATAMASK; @@ -341,6 +486,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i if (!stackPush(&sVal)) { // Eeerk, out of memory + debug( LOG_ERROR, "interpRunScript: out of memory!" ); ASSERT((FALSE, "interpRunScript: out of memory!")); goto exit_with_error; } @@ -358,6 +504,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i if (!stackPush(&sVal)) { // Eeerk, out of memory + debug( LOG_ERROR, "interpRunScript: out of memory!" ); ASSERT((FALSE, "interpRunScript: out of memory!")); goto exit_with_error; } @@ -367,6 +514,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i TRCPRINTF(("POP\n")); if (!stackPop(&sVal)) { + debug( LOG_ERROR, "interpRunScript: could not do stack pop" ); ASSERT((FALSE, "interpRunScript: could not do stack pop")); goto exit_with_error; } @@ -376,6 +524,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i TRCPRINTMATHSOP(data); if (!stackBinaryOp((OPCODE)data)) { + debug( LOG_ERROR, "interpRunScript: could not do binary op" ); ASSERT((FALSE, "interpRunScript: could not do binary op")); goto exit_with_error; } @@ -387,6 +536,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i TRCPRINTMATHSOP(data); if (!stackUnaryOp((OPCODE)data)) { + debug( LOG_ERROR, "interpRunScript: could not do unary op" ); ASSERT((FALSE, "interpRunScript: could not do unary op")); goto exit_with_error; } @@ -398,11 +548,13 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i TRCPRINTF(("PUSHGLOBAL %d\n", data)); if (data >= numGlobals) { + debug( LOG_ERROR, "interpRunScript: variable index out of range" ); ASSERT((FALSE, "interpRunScript: variable index out of range")); goto exit_with_error; } if (!stackPush(interpGetVarData(psGlobals, data))) { + debug( LOG_ERROR, "interpRunScript: could not do stack push" ); ASSERT((FALSE, "interpRunScript: could not do stack push")); goto exit_with_error; } @@ -414,11 +566,13 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i TRCPRINTF(("\n")); if (data >= numGlobals) { + debug( LOG_ERROR, "interpRunScript: variable index out of range" ); ASSERT((FALSE, "interpRunScript: variable index out of range")); goto exit_with_error; } if (!stackPopType(interpGetVarData(psGlobals, data))) { + debug( LOG_ERROR, "interpRunScript: could not do stack pop" ); ASSERT((FALSE, "interpRunScript: could not do stack pop")); goto exit_with_error; } @@ -447,12 +601,14 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i TRCPRINTF(("PUSHARRAYGLOBAL ")); if (!interpGetArrayVarData(&ip, psGlobals, psProg, &psVar)) { + debug( LOG_ERROR, "interpRunScript: could not get array var data, CurEvent=%d", CurEvent ); ASSERT((FALSE, "interpRunScript: could not get array var data")); goto exit_with_error; } TRCPRINTF(("\n")); if (!stackPush(psVar)) { + debug( LOG_ERROR, "interpRunScript: could not do stack push" ); ASSERT((FALSE, "interpRunScript: could not do stack push")); goto exit_with_error; } @@ -489,6 +645,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i TRCPRINTF(("POPARRAYGLOBAL ")); if (!interpGetArrayVarData(&ip, psGlobals, psProg, &psVar)) { + debug( LOG_ERROR, "interpRunScript: could not get array var data" ); ASSERT((FALSE, "interpRunScript: could not get array var data")); goto exit_with_error; } @@ -496,6 +653,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i TRCPRINTF(("\n")); if (!stackPopType(psVar)) { + debug( LOG_ERROR, "interpRunScript: could not do pop stack of type" ); ASSERT((FALSE, "interpRunScript: could not do pop stack of type")); goto exit_with_error; } @@ -505,6 +663,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i (SWORD)data, ip - psProg->pCode + (SWORD)data)); if (!stackPop(&sVal)) { + debug( LOG_ERROR, "interpRunScript: could not do pop of stack" ); ASSERT((FALSE, "interpRunScript: could not do pop of stack")); goto exit_with_error; } @@ -515,6 +674,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i ip += (SWORD)data; if (ip < pCodeStart || ip > pCodeEnd) { + debug( LOG_ERROR, "interpRunScript: jump out of range" ); ASSERT((FALSE, "interpRunScript: jump out of range")); goto exit_with_error; } @@ -532,6 +692,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i ip += (SWORD)data; if (ip < pCodeStart || ip > pCodeEnd) { + debug( LOG_ERROR, "interpRunScript: jump out of range" ); ASSERT((FALSE, "interpRunScript: jump out of range")); goto exit_with_error; } @@ -543,6 +704,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i scriptFunc = (SCRIPT_FUNC)*(ip+1); if (!scriptFunc()) { + debug( LOG_ERROR, "interpRunScript: could not do func" ); ASSERT((FALSE, "interpRunScript: could not do func")); goto exit_with_error; } @@ -555,6 +717,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i scriptVarFunc = (SCRIPT_VARFUNC)*(ip+1); if (!scriptVarFunc(data)) { + debug( LOG_ERROR, "interpRunScript: could not do var func" ); ASSERT((FALSE, "interpRunScript: could not do var func")); goto exit_with_error; } @@ -572,6 +735,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i // tell the event system to reschedule this event if (!eventAddPauseTrigger(psContext, index, ip - pCodeBase, data)) { + debug( LOG_ERROR, "interpRunScript: could not add pause trigger" ); ASSERT((FALSE, "interpRunScript: could not add pause trigger")); goto exit_with_error; } @@ -579,6 +743,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i ip = pCodeEnd; break; default: + debug(LOG_ERROR, "interpRunScript: unknown opcode"); ASSERT((FALSE, "interpRunScript: unknown opcode")); goto exit_with_error; break; @@ -587,26 +752,34 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i else //End of the event reached, see if we have to jump back to the caller function or just exit { + //debug(LOG_SCRIPT, "End of event reached"); //reset local vars - //if(!resetLocalVars(psProg, CurEvent)) - // goto exit_with_error; + if(!resetLocalVars(psProg, CurEvent)) + { + debug( LOG_ERROR, "interpRunScript: could not reset local vars for event %d", CurEvent ); + goto exit_with_error; + } if(!IsRetStackEmpty()) //There was a caller function before this one { - if (!PopRetStack((UDWORD *)&ip)) //Pop return address + //debug(LOG_SCRIPT, "GetCallDepth = %d", GetCallDepth()); + + if(!PopRetStack((UDWORD *)&ip)) //Pop return address { debug( LOG_ERROR, "interpRunScript() - PopRetStack(): failed to pop return adress."); return FALSE; } + //debug(LOG_SCRIPT, "Return adress = %d", ip); + if(!PopRetStack(&CurEvent)) //Pop event index { debug( LOG_ERROR, "interpRunScript() - PopRetStack(): failed to pop return event index."); return FALSE; } - debug( LOG_SCRIPT, "RETURN" ); + //debug( LOG_SCRIPT, "RETURNED TO CALLER EVENT %d", CurEvent ); //Set new boundries //-------------------------- @@ -619,9 +792,14 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i } else { + //debug( LOG_SCRIPT, " *** CALL STACK EMPTY ***" ); + //reset local vars - //if(!resetLocalVars(psProg, index)) - // goto exit_with_error; + if(!resetLocalVars(psProg, index)) + { + debug( LOG_ERROR, "interpRunScript: could not reset local vars" ); + goto exit_with_error; + } bStop = TRUE; //Stop execution of this event here, no more calling functions stored } @@ -640,6 +818,7 @@ BOOL interpRunScript(SCRIPT_CONTEXT *psContext, INTERP_RUNTYPE runType, UDWORD i exit_with_error: // Deal with the script crashing or running out of memory + debug(LOG_ERROR,"interpRunScript: *** ERROR EXIT *** (CurEvent=%d)", CurEvent); TRCPRINTF(("*** ERROR EXIT ***\n")); bInterpRunning = FALSE; return FALSE; @@ -753,6 +932,8 @@ UDWORD RetStackRemember(UDWORD EvTrigIndex, UDWORD address) retStack[retStackPos - 1] = EvTrigIndex; //First store event index retStack[retStackPos] = address; //current ip + //debug( LOG_SCRIPT, "RetStackRemember: ip=%d, event=%d", address, EvTrigIndex); + return TRUE; } @@ -764,9 +945,9 @@ BOOL IsRetStackEmpty() BOOL PopRetStack(UDWORD *psVal) { - //DbgMsg("Popping: %d", retStackPos); if(retStackPos < 0) { + debug( LOG_ERROR, "PopRetStack: retStackPos < 0"); return FALSE; } @@ -774,7 +955,7 @@ BOOL PopRetStack(UDWORD *psVal) retStackPos = retStackPos - 1; - //DbgMsg("Popped: %d, %d", retStackPos, *psVal); + //debug( LOG_SCRIPT, "PopRetStack: val=%d", *psVal); return TRUE; } diff --git a/lib/script/interp.h b/lib/script/interp.h index 82513be32..c5c44560f 100644 --- a/lib/script/interp.h +++ b/lib/script/interp.h @@ -105,6 +105,9 @@ typedef enum _op_code OP_LESS, OP_FUNC, //custom (in-script) function call + OP_POPLOCAL, //local var + OP_PUSHLOCAL, + OP_PUSHLOCALREF, } OPCODE; /* How far the opcode is shifted up a UDWORD to allow other data to be @@ -135,6 +138,7 @@ typedef enum _storage_type ST_PRIVATE, // Private variable ST_OBJECT, // A value stored in an objects data space. ST_EXTERN, // An external value accessed by function call + ST_LOCAL, // A local variable } enum_STORAGE_TYPE; typedef UBYTE STORAGE_TYPE; @@ -208,6 +212,14 @@ typedef struct _script_code UWORD numArrays; // the number of arrays in the program UDWORD arraySize; // the number of elements in all the defined arrays INTERP_TYPE *pGlobals; // Types of the global variables + + + INTERP_TYPE **ppsLocalVars; //storage for local vars + UDWORD *numLocalVars; //number of local vars each event has + INTERP_VAL **ppsLocalVarVal; //Values of the local vars used during interpreting process + UDWORD *numParams; //number of arguments this event has + + VAR_DEBUG *psVarDebug; // The names and storage types of variables ARRAY_DATA *psArrayInfo; // The sizes of the program arrays ARRAY_DEBUG *psArrayDebug; // Debug info for the arrays diff --git a/lib/script/parse.h b/lib/script/parse.h index e49443941..65b09f10c 100644 --- a/lib/script/parse.h +++ b/lib/script/parse.h @@ -306,5 +306,8 @@ extern BOOL scriptSetCode(CODE_BLOCK *psBlock); // The code block /* Look up a function symbol */ extern BOOL scriptLookUpFunction(STRING *pIdent, FUNC_SYMBOL **ppsSym); +/* Look up an in-script custom function symbol */ +extern BOOL scriptLookUpCustomFunction(STRING *pIdent, EVENT_SYMBOL **ppsSym); + #endif diff --git a/lib/script/script.c b/lib/script/script.c index ec21a1bfc..787fec6e6 100644 --- a/lib/script/script.c +++ b/lib/script/script.c @@ -103,6 +103,23 @@ void scriptFreeCode(SCRIPT_CODE *psCode) } FREE(psCode->psArrayDebug); } + + /* Free local vars */ + for(i=0; i < psCode->numEvents; i++) + { + FREE(psCode->ppsLocalVars[i]); + FREE(psCode->ppsLocalVarVal[i]); + + psCode->numParams = 0; + psCode->numLocalVars = 0; + + FREE(psCode->numParams); + FREE(psCode->numLocalVars); + } + + FREE(psCode->ppsLocalVars); + FREE(psCode->ppsLocalVarVal); + FREE(psCode); } diff --git a/lib/script/script_lexer.l b/lib/script/script_lexer.l index a2a374c3a..999d6d94c 100644 --- a/lib/script/script_lexer.l +++ b/lib/script/script_lexer.l @@ -44,6 +44,8 @@ SDWORD scriptGetVarToken(VAR_SYMBOL *psVar) { BOOL object; + //debug( LOG_SCRIPT, "scriptGetVarToken" ); + // See if this is an object pointer if (!asScrTypeTab || psVar->type < VAL_USERTYPESTART) { @@ -118,6 +120,7 @@ SDWORD scriptGetVarToken(VAR_SYMBOL *psVar) break; case VAL_INT: // case VAL_FLOAT: + //debug( LOG_SCRIPT, "scriptGetVarToken: VAL_INT (NUM_VAR)" ); return NUM_VAR; break; default: @@ -126,6 +129,8 @@ SDWORD scriptGetVarToken(VAR_SYMBOL *psVar) } } } + + //debug( LOG_SCRIPT, "END scriptGetVarToken" ); } /* Get the token type for a constant symbol */ @@ -133,6 +138,8 @@ SDWORD scriptGetConstToken(CONST_SYMBOL *psConst) { BOOL object; + //debug( LOG_SCRIPT, "scriptGetConstToken" ); + // See if this is an object constant if (!asScrTypeTab || psConst->type < VAL_USERTYPESTART) { @@ -163,6 +170,8 @@ SDWORD scriptGetConstToken(CONST_SYMBOL *psConst) } break; } + + //debug( LOG_SCRIPT, "END scriptGetConstToken" ); } /* Get the token type for a function symbol */ @@ -170,6 +179,8 @@ SDWORD scriptGetFuncToken(FUNC_SYMBOL *psFunc) { BOOL object; + //debug( LOG_SCRIPT, "scriptGetFuncToken" ); + // See if this is an object pointer if (!asScrTypeTab || psFunc->type < VAL_USERTYPESTART) { @@ -203,6 +214,8 @@ SDWORD scriptGetFuncToken(FUNC_SYMBOL *psFunc) break; } } + + //debug( LOG_SCRIPT, "END scriptGetFuncToken" ); } @@ -211,16 +224,16 @@ SDWORD scriptGetCustomFuncToken(EVENT_SYMBOL *psFunc) { BOOL object; - debug( LOG_SCRIPT, "scriptGetCustomFuncToken" ); + //debug( LOG_SCRIPT, "scriptGetCustomFuncToken" ); // See if this is an object pointer object = asScrTypeTab[psFunc->retType - VAL_USERTYPESTART].accessType == AT_OBJECT; - debug( LOG_SCRIPT, "scriptGetCustomFuncToken 3" ); + //debug( LOG_SCRIPT, "scriptGetCustomFuncToken 3" ); if (object) { - debug( LOG_SCRIPT, "scriptGetCustomFuncToken: object" ); + //debug( LOG_SCRIPT, "scriptGetCustomFuncToken: object" ); return OBJ_FUNC_CUST; } else @@ -228,30 +241,30 @@ SDWORD scriptGetCustomFuncToken(EVENT_SYMBOL *psFunc) switch (psFunc->retType) { case VAL_BOOL: - debug( LOG_SCRIPT, "scriptGetCustomFuncToken: VAL_BOOL" ); + //debug( LOG_SCRIPT, "scriptGetCustomFuncToken: VAL_BOOL" ); return BOOL_FUNC_CUST; break; case VAL_INT: // case VAL_FLOAT: - debug( LOG_SCRIPT, "scriptGetCustomFuncToken: VAL_INT" ); + //debug( LOG_SCRIPT, "scriptGetCustomFuncToken: VAL_INT" ); return NUM_FUNC_CUST; break; case VAL_STRING: /* */ - debug( LOG_SCRIPT, "scriptGetCustomFuncToken: VAL_STRING" ); + //debug( LOG_SCRIPT, "scriptGetCustomFuncToken: VAL_STRING" ); return STRING_FUNC_CUST; break; case VAL_VOID: - debug( LOG_SCRIPT, "scriptGetCustomFuncToken: void function" ); + //debug( LOG_SCRIPT, "scriptGetCustomFuncToken: void function" ); return VOID_FUNC_CUST; break; default: - debug( LOG_SCRIPT, "scriptGetCustomFuncToken: default" ); + //debug( LOG_SCRIPT, "scriptGetCustomFuncToken: default" ); return USER_FUNC_CUST; break; } } - debug( LOG_SCRIPT, "END scriptGetCustomFuncToken" ); + //debug( LOG_SCRIPT, "END scriptGetCustomFuncToken" ); } @@ -281,10 +294,11 @@ ref return REF; /* new stuff */ function return FUNCTION; -return return RETURN; +return { return RET; } public { scr_lval.stype = ST_PUBLIC; return STORAGE; } private { scr_lval.stype = ST_PRIVATE; return STORAGE; } +local { scr_lval.stype = ST_LOCAL; return STORAGE; } while return WHILE; if return IF; else return ELSE; @@ -333,6 +347,8 @@ NOT return _NOT; /* Match identifiers */ [a-zA-Z][0-9_a-zA-Z]* { + //debug( LOG_SCRIPT, "FLEX: looking up '%s'", scr_text ); + /* See if this identifier has been defined as a type */ if (scriptLookUpType(scr_text, &scr_lval.tval)) { @@ -361,7 +377,7 @@ NOT return _NOT; /* See if this identifier has been defined as a custom function */ else if (scriptLookUpCustomFunction(scr_text, &scr_lval.eSymbol)) { - debug( LOG_SCRIPT, "scriptLookUpCustomFunction: '%s' - custom function", scr_text ); + //debug( LOG_SCRIPT, "scriptLookUpCustomFunction: '%s' - custom function", scr_text ); return scriptGetCustomFuncToken(scr_lval.eSymbol); } @@ -382,6 +398,8 @@ NOT return _NOT; } else { + //debug( LOG_SCRIPT, "FLEX: '%s' is an ident", scr_text ); + strcpy(aText[currText], scr_text); scr_lval.sval = aText[currText]; currText = (currText + 1) % TEXT_BUFFERS; @@ -440,7 +458,7 @@ int scr_wrap(void) { if (inComment) { - DBERROR(("Warning: reached end of file in a comment")); + DBERROR(("Warning: reched end of file in a comment")); } return 1; } diff --git a/lib/script/script_parser.y b/lib/script/script_parser.y index 18587ecf5..08358f59a 100644 --- a/lib/script/script_parser.y +++ b/lib/script/script_parser.y @@ -24,7 +24,12 @@ typedef enum _code_error CE_PARSE // A parse error occured } CODE_ERROR; -EVENT_SYMBOL *psCurEvent = NULL; /* function stuff: stores current event: for local var declaration */ + +#define maxEventsLocalVars 1200 /* should be big enough */ +static VAR_SYMBOL *psLocalVarsB[maxEventsLocalVars]; /* local var storage */ +static UDWORD numEventLocalVars[maxEventsLocalVars]; /* number of declared local vars for each event */ +static VAR_SYMBOL *psLocalVarsTemp; /* temporary storage for local vars, before current event declaration is found */ +EVENT_SYMBOL *psCurEvent = NULL; /* function stuff: stores current event: for local var declaration */ /* Pointer to the compiled code */ static SCRIPT_CODE *psFinalProg=NULL; @@ -820,6 +825,8 @@ CODE_ERROR scriptCodeAssignment(VAR_SYMBOL *psVariable, // The variable to assig { SDWORD size; + //debug( LOG_SCRIPT, "scriptCodeAssignment" ); + ASSERT((psVariable != NULL, "scriptCodeAssignment: Invalid variable symbol pointer")); ASSERT((PTRVALID(psValue, sizeof(CODE_BLOCK)), @@ -835,6 +842,7 @@ CODE_ERROR scriptCodeAssignment(VAR_SYMBOL *psVariable, // The variable to assig // Check there is a set function if (psVariable->set == NULL) { + debug( LOG_ERROR, "scriptCodeAssignment: No set function for external variable" ); scr_error("No set function for external variable"); return CE_PARSE; } @@ -854,6 +862,11 @@ CODE_ERROR scriptCodeAssignment(VAR_SYMBOL *psVariable, // The variable to assig case ST_PRIVATE: PUT_PKOPCODE(ip, OP_POPGLOBAL, psVariable->index); break; + case ST_LOCAL: /* local cariable assignment code */ + //PUT_DATA(ip, psCurEvent->index); //event index of the local var + //debug( LOG_SCRIPT, "scriptCodeAssignment: added local var assignment code"); + PUT_PKOPCODE(ip, OP_POPLOCAL, psVariable->index); + break; case ST_EXTERN: PUT_PKOPCODE(ip, OP_VARCALL, psVariable->index); PUT_VARFUNC(ip, psVariable->set); @@ -868,6 +881,8 @@ CODE_ERROR scriptCodeAssignment(VAR_SYMBOL *psVariable, // The variable to assig break; } + //debug( LOG_SCRIPT, "END scriptCodeAssignment" ); + return CE_OK; } @@ -1165,6 +1180,50 @@ CODE_ERROR scriptCodeBinaryOperator(CODE_BLOCK *psFirst, // Code for first param } +/* check if the arguments in the function definition body match the argument types +and names from function declaration (if there was any) */ +BOOL checkFuncParamType(SDWORD argIndex, SDWORD argType) +{ + VAR_SYMBOL *psCurr; + SDWORD i,j; + + if(psCurEvent == NULL) + { + debug( LOG_ERROR, "checkFuncParamType() - psCurEvent == NULL" ); + return FALSE; + } + + if(argIndex < psCurEvent->numParams) + { + /* find the argument by the index */ + i=psCurEvent->index; + j=0; + for(psCurr =psLocalVarsB[i]; psCurr != NULL; psCurr = psCurr->psNext) + { + if((psCurEvent->numParams - j - 1)==argIndex) /* got to the right argument */ + { + if(argType != psCurr->type) + { + debug( LOG_ERROR, "Argument type with index %d in event '%s' doesn't match function declaration (%d/%d)",argIndex,psCurEvent->pIdent,argType,psCurr->type); + return FALSE; + } + else + { + //debug( LOG_SCRIPT, "arg matched"); + return TRUE; + } + } + j++; + } + } + else + { + debug( LOG_ERROR, "checkFuncParamType() - argument %d has wrong argument index, event: '%s'", argIndex, psCurEvent->pIdent); + return FALSE; + } +} + + /* Generate code for accessing an object variable. The variable symbol is * stored with the code for the object value so that this block can be used for * both setting and retrieving an object value. @@ -1282,6 +1341,8 @@ CODE_ERROR scriptCodeVarGet(VAR_SYMBOL *psVariable, // The object variable symb { SDWORD size; + //debug( LOG_SCRIPT, "scriptCodeVarGet" ); + size = sizeof(OPCODE); if (psVariable->storage == ST_EXTERN) { @@ -1304,6 +1365,13 @@ CODE_ERROR scriptCodeVarGet(VAR_SYMBOL *psVariable, // The object variable symb case ST_PRIVATE: PUT_PKOPCODE(ip, OP_PUSHGLOBAL, psVariable->index); break; + + case ST_LOCAL: /* local variable */ + //PUT_DATA(ip, psCurEvent->index); //event index of the local var + //debug( LOG_SCRIPT, "scriptCodeVarGet: OP_PUSHLOCAL" ); + PUT_PKOPCODE(ip, OP_PUSHLOCAL, psVariable->index); + break; + case ST_EXTERN: PUT_PKOPCODE(ip, OP_VARCALL, psVariable->index); PUT_VARFUNC(ip, psVariable->get); @@ -1318,6 +1386,8 @@ CODE_ERROR scriptCodeVarGet(VAR_SYMBOL *psVariable, // The object variable symb break; } + //debug( LOG_SCRIPT, "END scriptCodeVarGet" ); + return CE_OK; } @@ -1341,6 +1411,13 @@ CODE_ERROR scriptCodeVarRef(VAR_SYMBOL *psVariable, // The object variable symb PUT_PKOPCODE(ip, OP_PUSHREF, (*ppsBlock)->aParams[0]); PUT_DATA(ip, psVariable->index); break; + + case ST_LOCAL: + + PUT_PKOPCODE(ip, OP_PUSHLOCALREF, (*ppsBlock)->aParams[0]); + PUT_DATA(ip, psVariable->index); + break; + case ST_EXTERN: scr_error("Cannot use external variables in this context"); return CE_PARSE; @@ -1523,7 +1600,7 @@ static void scriptStoreVarTypes(VAR_SYMBOL *psVar) /* NEW STUFF */ %token FUNCTION -%token RETURN +%token RET %left _AND _OR %left BOOLEQUAL NOTEQUAL GREATEQUAL LESSEQUAL GREATER LESS @@ -1629,6 +1706,9 @@ static void scriptStoreVarTypes(VAR_SYMBOL *psVar) %token OBJ_FUNC_CUST /* A defined object pointer function */ %token STRING_FUNC_CUST /* A defined string function */ +%type return_statement +%type var_line + %% @@ -1652,6 +1732,9 @@ script: header var_list EVENT_SYMBOL *psEvent; UDWORD numVars; + INTERP_TYPE *pCurEvLocalVars; + UDWORD j; + // Calculate the code size size = 0; debugEntries = 0; @@ -1687,6 +1770,48 @@ script: header var_list } ALLOC_PROG(psFinalProg, size, psCurrBlock->pCode, numVars, numArrays, numTriggers, numEvents); + + + + + /* store local vars */ + //allocate array for holding an array of local vars for each event + psFinalProg->ppsLocalVars = (INTERP_TYPE **)MALLOC(sizeof(INTERP_TYPE*) * numEvents); + psFinalProg->numLocalVars = (UDWORD *)MALLOC(sizeof(UDWORD) * numEvents); //how many local vars each event has + psFinalProg->numParams = (UDWORD *)MALLOC(sizeof(UDWORD) * numEvents); //how many arguments each event has + + i=0; + for(psEvent = psEvents; psEvent; psEvent = psEvent->psNext) + { + psEvent->numLocalVars = numEventLocalVars[i]; + + psFinalProg->numLocalVars[i] = numEventLocalVars[i]; //remember how many local vars this event has + psFinalProg->numParams[i] = psEvent->numParams; //remember how many parameters this event has + + if(numEventLocalVars[i] > 0) + { + pCurEvLocalVars = (INTERP_TYPE*)MALLOC(sizeof(INTERP_TYPE) * numEventLocalVars[i]); + + j=0; + for(psCurr =psLocalVarsB[i]; psCurr != NULL; psCurr = psCurr->psNext) + { + //DB_INTERP(("remembering loc var \n")); + //DB_INTERP(("%d - %d \n",i,j)); + pCurEvLocalVars[numEventLocalVars[i] - j - 1] = psCurr->type; //save type, order is reversed + j++; + } + } + else + { + pCurEvLocalVars = NULL; //this event has no local vars + } + + psFinalProg->ppsLocalVars[i] = pCurEvLocalVars; + i++; + } + + + ALLOC_DEBUG(psFinalProg, debugEntries); psFinalProg->debugEntries = 0; ip = psFinalProg->pCode; @@ -1856,25 +1981,69 @@ function_type: VOID_FUNC_CUST | STRING_FUNC_CUST ; + +return_statement_void: RET ';' + ; + + +return_statement: return_statement_void /* nothing or "return;" */ + { + //debug( LOG_SCRIPT, "return_statement: return_statement_void" ); + + if(psCurEvent == NULL) /* no events declared or defined yet */ + { + debug( LOG_ERROR, "Return statement outside of function or event"); + YYABORT; + } + + if(!psCurEvent->bFunction) + { + debug( LOG_ERROR, "Return statement inside of an event '%s'", psCurEvent->pIdent); + YYABORT; + } + + if(psCurEvent->retType != VAL_VOID) + { + debug( LOG_ERROR, "Wrong return statement syntax for a non-void function '%s'", psCurEvent->pIdent); + YYABORT; + } + + //debug( LOG_SCRIPT, "return_statement: found 'return;' in '%s' function", psCurEvent->pIdent ); + + + /* Allocate code block for exit instruction */ + ALLOC_BLOCK(psCurrBlock, sizeof(OPCODE)); + ip = psCurrBlock->pCode; + PUT_OPCODE(ip, OP_EXIT); /* just exit function */ + + psCurrBlock->type = VAL_VOID; /* make return statement of type VOID manually */ + + //debug( LOG_SCRIPT, "END return_statement: return_statement_void" ); + + $$ = psCurrBlock; + } + ; + + /* Function */ func_subdecl: FUNCTION TYPE IDENT /* declaration of a function */ { EVENT_SYMBOL *psEvent; - debug( LOG_SCRIPT, "func_subdecl: FUNCTION TYPE IDENT" ); + //debug( LOG_SCRIPT, "func_subdecl: FUNCTION TYPE IDENT: '%s'", $3 ); - /* allow local vars to have the same names as global vars (don't check global vars) */ - /* localVariableDef = TRUE; */ - /* debug( LOG_SCRIPT, "localVariableDef = TRUE 1"); */ + //allow local vars to have the same names as global vars (don't check global vars) + localVariableDef = TRUE; + //debug( LOG_SCRIPT, "localVariableDef = TRUE 1"); - debug( LOG_SCRIPT, "func_subdecl: scriptDeclareEvent" ); + //debug( LOG_SCRIPT, "func_subdecl: scriptDeclareEvent" ); if (!scriptDeclareEvent($3, &psEvent,0)) { - debug( LOG_SCRIPT, "func_subdecl: scriptDeclareEvent() failed" ); + debug( LOG_ERROR, "func_subdecl: scriptDeclareEvent() failed" ); YYABORT; } - debug( LOG_SCRIPT, "END func_subdecl: scriptDeclareEvent" ); + //debug( LOG_SCRIPT, "END func_subdecl: scriptDeclareEvent" ); psEvent->retType = $2; psCurEvent = psEvent; @@ -1882,30 +2051,24 @@ func_subdecl: FUNCTION TYPE IDENT /* declaration of a function */ $$ = psEvent; - debug( LOG_SCRIPT, "END func_subdecl:FUNCTION IDENT"); + //debug( LOG_SCRIPT, "END func_subdecl:FUNCTION TYPE IDENT"); } /* definition of a function that was already declared (TODO: move to a new rule) */ | FUNCTION TYPE function_type /* function that was already declared */ { - debug( LOG_SCRIPT, "func_subdecl: FUNCTION TYPE function_type"); + //debug( LOG_SCRIPT, "func_subdecl: FUNCTION TYPE function_type, function: '%s'", $3->pIdent); psCurEvent = $3; - /* allow local vars to have the same names as global vars (don't check global vars) */ - /* localVariableDef = TRUE; */ - /* DB_INTERP(("localVariableDef = TRUE 2\n")); */ - /* make sure this event was already declared as function before */ if(!$3->bFunction) { - debug(LOG_SCRIPT, "'%s' was declared as event before and can't be redefined as function", $3->pIdent); - debug(LOG_SCRIPT, "Wrong function definition"); + debug(LOG_ERROR, "'%s' was declared as event before and can't be redefined as function", $3->pIdent); + debug(LOG_ERROR, "Wrong function definition"); YYABORT; } - /* psCurEvent->bFunction = TRUE; */ - /* psEvent->retType = $2; */ $$ = $3; - debug(LOG_SCRIPT, "END func_subdecl: FUNCTION TYPE function_type"); + //debug(LOG_SCRIPT, "END func_subdecl: FUNCTION TYPE function_type"); } ; @@ -1914,7 +2077,7 @@ func_subdecl: FUNCTION TYPE IDENT /* declaration of a function */ * Header declarations */ -header: /* NULL token */ +header: /* empty */ | header_decl | header header_decl ; @@ -1935,23 +2098,42 @@ header_decl: LINK TYPE ';' * Variable and function declarations */ -var_list: /* NULL token */ - | variable_decl ';' +var_list: /* empty */ + | var_line { FREE_VARDECL($1); } - | var_list variable_decl ';' + | var_list var_line { FREE_VARDECL($2); } ; +var_line: variable_decl ';' + { + /* remember that local var declaration is over */ + localVariableDef = FALSE; + //debug( LOG_SCRIPT, "localVariableDef = FALSE 3"); + $$ = $1; + } + ; + variable_decl_head: STORAGE TYPE { ALLOC_VARDECL(psCurrVDecl); psCurrVDecl->storage = $1; psCurrVDecl->type = $2; + + /* allow local vars to have the same names as global vars (don't check global vars) */ + if($1 == ST_LOCAL) + { + //debug( LOG_SCRIPT, "variable_decl_head: STORAGE TYPE: local" ); + + localVariableDef = TRUE; + //debug( LOG_SCRIPT, "localVariableDef = TRUE 4"); + } + $$ = psCurrVDecl; } | STORAGE TRIGGER @@ -2035,6 +2217,9 @@ variable_ident: IDENT variable_decl: variable_decl_head variable_ident { + //if($1->storage == ST_LOCAL) + // debug( LOG_SCRIPT, "variable_decl: variable_decl_head variable_ident: local" ); + if (!scriptAddVariable($1, $2)) { /* Out of memory - error already given */ @@ -2066,7 +2251,7 @@ variable_decl: variable_decl_head variable_ident * Trigger declarations */ -trigger_list: /* NULL token */ +trigger_list: /* empty */ | trigger_decl | trigger_list trigger_decl ; @@ -2145,16 +2330,16 @@ event_list: event_decl event_subdecl: EVENT IDENT { EVENT_SYMBOL *psEvent; - debug( LOG_SCRIPT, "event_subdecl: scriptDeclareEvent" ); + //debug( LOG_SCRIPT, "event_subdecl: scriptDeclareEvent" ); if (!scriptDeclareEvent($2, &psEvent, 0)) { - debug( LOG_SCRIPT, "event_subdecl: scriptDeclareEvent() failed" ); + debug( LOG_ERROR, "event_subdecl: scriptDeclareEvent() failed" ); YYABORT; } psCurEvent = psEvent; - debug( LOG_SCRIPT, "END event_subdecl: scriptDeclareEvent, '%s'", $2); + //debug( LOG_SCRIPT, "END event_subdecl: scriptDeclareEvent, '%s'", $2); $$ = psEvent; } | EVENT EVENT_SYM @@ -2165,6 +2350,11 @@ event_subdecl: EVENT IDENT ; argument_decl: '(' ')' + { + /* remember that local var declaration is over */ + localVariableDef = FALSE; + //debug( LOG_SCRIPT, "localVariableDef = FALSE 6"); + } event_decl: event_subdecl ';' { @@ -2174,7 +2364,7 @@ event_decl: event_subdecl ';' { if (!scriptDefineEvent($1, $6, $3->index)) { - debug( LOG_SCRIPT, "event_decl: scriptDefineEvent() failed" ); + debug( LOG_ERROR, "event_decl: scriptDefineEvent() failed" ); YYABORT; } @@ -2200,7 +2390,7 @@ event_decl: event_subdecl ';' if (!scriptDefineEvent($1, $7, numTriggers - 1)) { - debug( LOG_SCRIPT, "event_decl: scriptDefineEvent() failed 2" ); + debug( LOG_ERROR, "event_decl: scriptDefineEvent() failed 2" ); YYABORT; } @@ -2213,7 +2403,7 @@ event_decl: event_subdecl ';' { if (!scriptDefineEvent($1, $6, -1)) { - debug( LOG_SCRIPT, "event_decl: scriptDefineEvent() failed 3" ); + debug( LOG_ERROR, "event_decl: scriptDefineEvent() failed 3" ); YYABORT; } @@ -2226,50 +2416,53 @@ event_decl: event_subdecl ';' /* Function declaration, like: function bool myFunc(); */ | func_subdecl argument_decl ';' { - debug( LOG_SCRIPT, "func_subdecl argument_decl ';'"); + //debug( LOG_SCRIPT, "func_subdecl argument_decl ';', function '%s'", $1->pIdent); psCurEvent->bDeclared = TRUE; /* remember current function was declared */ - debug( LOG_SCRIPT, "END func_subdecl argument_decl ';'"); + localVariableDef = FALSE; + //debug( LOG_SCRIPT, "localVariableDef = FALSE 5"); + //debug( LOG_SCRIPT, "END func_subdecl argument_decl ';'"); } /* new function (not declared) */ /* func_subdecl '(' funcbody_var_def ')' '{' var_list statement_list return_statement '}' */ - | func_subdecl '(' ')' '{' statement_list '}' + | func_subdecl '(' ')' '{' var_list statement_list return_statement '}' { - debug(LOG_SCRIPT, "START func_subdecl ( ) { statement_list } "); + //debug(LOG_SCRIPT, "START func_subdecl '(' ')' '{' var_list statement_list return_statement '}' "); /* if(!psCurEvent->bDeclared) { - debug( LOG_SCRIPT, "Event %s's definition doesn't match with declaration.", psCurEvent->pIdent); - debug( LOG_SCRIPT, "Wrong event definition:\n event %s's definition doesn't match with declaration", psCurEvent->pIdent); + debug( LOG_ERROR, "Event %s's definition doesn't match with declaration.", psCurEvent->pIdent); + debug( LOG_ERROR, "Wrong event definition:\n event %s's definition doesn't match with declaration", psCurEvent->pIdent); YYABORT; } */ /* stays the same if no params (just gets copied) */ - ALLOC_BLOCK(psCurrBlock, $5->size + sizeof(OPCODE)); + ALLOC_BLOCK(psCurrBlock, $6->size + $7->size + sizeof(OPCODE)); ip = psCurrBlock->pCode; /* Copy the old (main) code and free it */ - PUT_BLOCK(ip, $5); + PUT_BLOCK(ip, $6); /* put statement_list code */ + PUT_BLOCK(ip, $7); /* put return_statement code */ PUT_OPCODE(ip, OP_EXIT); /* must exit after return */ /* copy debug info */ - ALLOC_DEBUG(psCurrBlock, $5->debugEntries); - PUT_DEBUG(psCurrBlock, $5); + ALLOC_DEBUG(psCurrBlock, $6->debugEntries); + PUT_DEBUG(psCurrBlock, $6); if (!scriptDefineEvent($1, psCurrBlock, -1)) { - debug( LOG_SCRIPT, "event_decl: func_subdecl '(' ')' '{' statement_list '}': scriptDefineEvent() failed 4" ); + debug( LOG_ERROR, "event_decl: func_subdecl '(' ')' '{' statement_list '}': scriptDefineEvent() failed 4" ); YYABORT; } - FREE_DEBUG($5); - /* FREE_DEBUG($8); */ - FREE_BLOCK($5); + FREE_DEBUG($6); + FREE_BLOCK($6); + FREE_BLOCK($7); /* end of event */ psCurEvent = NULL; - debug(LOG_SCRIPT, "END func_subdecl '(' ')' '{' statement_list '}' "); + //debug(LOG_SCRIPT, "END func_subdecl '(' ')' '{' var_list statement_list return_statement '}'"); } ; @@ -2278,7 +2471,7 @@ event_decl: event_subdecl ';' * Statements */ -statement_list: /* NULL token */ +statement_list: /* empty */ { // Allocate a dummy code block ALLOC_BLOCK(psCurrBlock, 1); @@ -2354,20 +2547,20 @@ statement: assignment ';' UDWORD line; STRING *pDummy; - debug( LOG_SCRIPT, "expression: VOID_FUNC_CUST '(' ')' ';'" ); + //debug( LOG_SCRIPT, "expression: VOID_FUNC_CUST '(' ')' ';'" ); /* Allocate the code block */ ALLOC_BLOCK(psCurrBlock, sizeof(OPCODE) + sizeof(UDWORD)); //Opcode + event index ALLOC_DEBUG(psCurrBlock, 1); ip = psCurrBlock->pCode; - debug( LOG_SCRIPT, "expression: VOID_FUNC_CUST '(' ')' ';' 1" ); + //debug( LOG_SCRIPT, "expression: VOID_FUNC_CUST '(' ')' ';' 1" ); /* Store the instruction */ PUT_OPCODE(ip, OP_FUNC); PUT_FUNC(ip,$1->index); //Put event index - debug( LOG_SCRIPT, "expression: VOID_FUNC_CUST '(' ')' ';' 2" ); + //debug( LOG_SCRIPT, "expression: VOID_FUNC_CUST '(' ')' ';' 2" ); /* Add the debugging information */ if (genDebugInfo) @@ -2377,7 +2570,7 @@ statement: assignment ';' psCurrBlock->psDebug[0].line = line; } - debug( LOG_SCRIPT, "END expression: VOID_FUNC_CUST '(' ')' ';'" ); + //debug( LOG_SCRIPT, "END expression: VOID_FUNC_CUST '(' ')' ';'" ); $$ = psCurrBlock; } @@ -2414,6 +2607,54 @@ statement: assignment ';' $$ = psCurrBlock; } + + | return_statement + { + UDWORD line; + STRING *pDummy; + + //debug( LOG_SCRIPT, "statement: return_statement"); + + if(psCurEvent == NULL) + { + debug( LOG_ERROR, "'return' outside of function or event"); + YYABORT; + } + + if(!psCurEvent->bFunction) + { + debug( LOG_ERROR, "'return' can only be used in functions, not in events, event: '%s'", psCurEvent->pIdent); + YYABORT; + } + + if($1->type != psCurEvent->retType) + { + debug( LOG_ERROR, "'return' type doesn't match with function return type, function: '%s' (%d / %d)", psCurEvent->pIdent, $1->type, psCurEvent->retType); + YYABORT; + } + + ALLOC_BLOCK(psCurrBlock, $1->size + sizeof(OPCODE)); + ALLOC_DEBUG(psCurrBlock, 1); + ip = psCurrBlock->pCode; + + PUT_BLOCK(ip, $1); + PUT_OPCODE(ip, OP_EXIT); + + FREE_BLOCK($1); + + if (genDebugInfo) + { + psCurrBlock->psDebug[0].offset = 0; + scriptGetErrorData((SDWORD *)&line, &pDummy); + psCurrBlock->psDebug[0].line = line; + } + + //debug( LOG_SCRIPT, "END statement: return_statement"); + + $$ = psCurrBlock; + } + + | PAUSE '(' INTEGER ')' ';' { UDWORD line; @@ -2646,7 +2887,7 @@ func_call: NUM_FUNC '(' param_list ')' * These rules return PARAM_BLOCKS which contain the type of each * parameter as well as the code for the parameter. */ -param_list: /* NULL token */ +param_list: /* empty */ { /* create a dummy pblock containing nothing */ ALLOC_PBLOCK(psCurrPBlock, sizeof(UDWORD), 1); @@ -3597,6 +3838,13 @@ static void scriptResetTables(void) TRIGGER_SYMBOL *psTCurr, *psTNext; EVENT_SYMBOL *psECurr, *psENext; FUNC_SYMBOL *psFCurr, *psFNext; + UDWORD i; + + //debug( LOG_SCRIPT, "scriptResetTables" ); + + /* we start with global vars definition */ + localVariableDef = FALSE; + //debug( LOG_SCRIPT, "localVariableDef = FALSE reset"); /* Reset the global variable symbol table */ for(psCurr = psGlobalVars; psCurr != NULL; psCurr = psNext) @@ -3609,6 +3857,29 @@ static void scriptResetTables(void) psCurEvent = NULL; + /* Reset the local variable symbol table */ + for(i=0; ipsNext; + FREE(psCurr->pIdent); + FREE(psCurr); + } + psLocalVarsB[i] = NULL; + } + + /* Reset the temp local variable symbol table */ + for(psCurr = psLocalVarsTemp; psCurr != NULL; psCurr = psNext) + { + psNext = psCurr->psNext; + FREE(psCurr->pIdent); + FREE(psCurr); + } + psLocalVarsTemp = NULL; + /* Reset the global array symbol table */ for(psCurr = psGlobalArrays; psCurr != NULL; psCurr = psNext) { @@ -3661,12 +3932,16 @@ static void scriptResetTables(void) FREE(psFCurr); } psFunctions = NULL; + + //debug( LOG_SCRIPT, "END scriptResetTables" ); } /* Compile a script program */ BOOL scriptCompile(char *pData, UDWORD fileSize, SCRIPT_CODE **ppsProg, SCR_DEBUGTYPE debugType) { + //debug( LOG_SCRIPT, "scriptCompile" ); + // Tell lex about the input buffer scriptSetInputBuffer(pData, fileSize); @@ -3690,6 +3965,8 @@ BOOL scriptCompile(char *pData, UDWORD fileSize, *ppsProg = psFinalProg; + //debug( LOG_SCRIPT, "END scriptCompile" ); + return TRUE; } @@ -3713,6 +3990,8 @@ void scr_error(char *pMessage, ...) DBERROR(("script parse error:\n%s at line %d\nToken: %d, Text: '%s'\n", pMessage, line, scr_char, text)); #endif + + debug( LOG_ERROR, "Script error" ); } @@ -3721,6 +4000,8 @@ BOOL scriptLookUpType(STRING *pIdent, INTERP_TYPE *pType) { UDWORD i; + //debug( LOG_SCRIPT, "scriptLookUpType" ); + if (asScrTypeTab) { for(i=0; asScrTypeTab[i].typeID != 0; i++) @@ -3733,6 +4014,8 @@ BOOL scriptLookUpType(STRING *pIdent, INTERP_TYPE *pType) } } + //debug( LOG_SCRIPT, "END scriptLookUpType" ); + return FALSE; } @@ -3742,12 +4025,16 @@ void scriptClearLocalVariables(void) { VAR_SYMBOL *psCurr, *psNext; + //debug( LOG_SCRIPT, "scriptClearLocalVariables" ); + for(psCurr = psLocalVars; psCurr != NULL; psCurr = psNext) { psNext = psCurr->psNext; FREE(psCurr->pIdent); FREE(psCurr); } + + //debug( LOG_SCRIPT, "END scriptClearLocalVariables" ); } /* Add a new variable symbol. @@ -3760,6 +4047,22 @@ BOOL scriptAddVariable(VAR_DECL *psStorage, VAR_IDENT_DECL *psVarIdent) VAR_SYMBOL *psNew; SDWORD i;//, size; + VAR_SYMBOL **ppsVarSym; + + //debug( LOG_SCRIPT, "scriptAddVariable" ); + + + //if(psStorage->storage == ST_LOCAL) + //{ + // debug( LOG_SCRIPT, "scriptAddVariable: looking up local var" ); + // + // if(scriptLookUpVariable(psVarIdent->pIdent, &ppsVarSym)) + // { + // debug( LOG_SCRIPT, "var found" ); + // //DbgMsg("var=%s, index=%d of %d", psVarIdent->pIdent, (*ppsVarSym)->index, psCurEvent->numParams); + // } + //} + /* Allocate the memory for the symbol structure */ psNew = (VAR_SYMBOL *)MALLOC(sizeof(VAR_SYMBOL)); if (psNew == NULL) @@ -3767,36 +4070,72 @@ BOOL scriptAddVariable(VAR_DECL *psStorage, VAR_IDENT_DECL *psVarIdent) scr_error("Out of memory"); return FALSE; } + psNew->pIdent = psVarIdent->pIdent; //(STRING *)MALLOC(strlen(pIdent) + 1); + /* if (psNew->pIdent == NULL) { scr_error("Out of memory"); return FALSE; }*/ + /* Intialise the symbol structure */ // strcpy(psNew->pIdent, pIdent); + psNew->type = psStorage->type; psNew->storage = psStorage->storage; psNew->dimensions = psVarIdent->dimensions; + for(i=0; idimensions; i++) { psNew->elements[i] = psVarIdent->elements[i]; } if (psNew->dimensions == 0) { - if (psGlobalVars == NULL) + if(psStorage->storage != ST_LOCAL) //if not a local var { - psNew->index = 0; - } - else - { - psNew->index = psGlobalVars->index + 1; - } + if (psGlobalVars == NULL) + { + psNew->index = 0; + } + else + { + psNew->index = psGlobalVars->index + 1; + } - /* Add the symbol to the list */ - psNew->psNext = psGlobalVars; - psGlobalVars = psNew; + /* Add the symbol to the list */ + psNew->psNext = psGlobalVars; + psGlobalVars = psNew; + + //debug( LOG_SCRIPT, "scriptAddVariable: added global: '%s'", psVarIdent->pIdent); + } + else /* local var */ + { + if(psCurEvent == NULL) + debug( LOG_ERROR, "Can't declare local variables before defining an event" ); + + //DB_INTERP(("local variable declared for event %d, type=%d \n", psCurEvent->index, psNew->type)); + //DB_INTERP(("%s \n", psNew->pIdent)); + + if (psLocalVarsB[psCurEvent->index] == NULL) + { + psNew->index = 0; + //debug( LOG_SCRIPT, "*first* local var of event %d", psCurEvent->index ); + } + else + { + psNew->index = psLocalVarsB[psCurEvent->index]->index + 1; + //debug( LOG_SCRIPT, "next local var of event %d, new index is %d", psCurEvent->index, psNew->index ); + } + + numEventLocalVars[psCurEvent->index] = numEventLocalVars[psCurEvent->index] + 1; + + psNew->psNext = psLocalVarsB[psCurEvent->index]; + psLocalVarsB[psCurEvent->index] = psNew; + + //debug( LOG_SCRIPT, "local variable declared: '%s'", psVarIdent->pIdent); + } } else { @@ -3813,6 +4152,8 @@ BOOL scriptAddVariable(VAR_DECL *psStorage, VAR_IDENT_DECL *psVarIdent) psGlobalArrays = psNew; } + //debug( LOG_SCRIPT, "END scriptAddVariable" ); + return TRUE; } @@ -3821,6 +4162,14 @@ BOOL scriptAddVariable(VAR_DECL *psStorage, VAR_IDENT_DECL *psVarIdent) BOOL scriptLookUpVariable(STRING *pIdent, VAR_SYMBOL **ppsSym) { VAR_SYMBOL *psCurr; + UDWORD i; + + //debug( LOG_SCRIPT, "scriptLookUpVariable" ); + + //if(localVariableDef) + // debug( LOG_SCRIPT, "scriptLookUpVariable: looking for '%s', localVariableDef = TRUE", pIdent ); + //else + // debug( LOG_SCRIPT, "scriptLookUpVariable: looking for '%s', localVariableDef = FALSE", pIdent ); /* See if the symbol is an object variable */ if (asScrObjectVarTab && objVarContext != 0) @@ -3859,6 +4208,48 @@ BOOL scriptLookUpVariable(STRING *pIdent, VAR_SYMBOL **ppsSym) } } + + /* check local vars if we are inside of an event or function */ + if(psCurEvent != NULL) + { + if(psCurEvent->index >= maxEventsLocalVars) + debug( LOG_ERROR, "Event index too high: %d", psCurEvent->index); + + i = psCurEvent->index; + + if(psLocalVarsB[i] != NULL) //any vars stored for this event + { + int line; + char *text; + + //debug( LOG_SCRIPT, "now checking event %s, event index = %d, looking for '%s'", psCurEvent->pIdent, psCurEvent->index, pIdent); + + scriptGetErrorData(&line, &text); + + for(psCurr =psLocalVarsB[i]; psCurr != NULL; psCurr = psCurr->psNext) + { + if(psCurr->pIdent == NULL) + debug(LOG_ERROR, "psCurr->pIdent == NULL"); + + //DB_INTERP(("start comparing, num local vars=%d, at line %d\n", numEventLocalVars[i], line)); + //DB_INTERP(("current var=%s\n", psCurr->pIdent)); + //DB_INTERP(("passed string=%s\n", pIdent)); + + //DB_INTERP(("comparing %s with %s \n", psCurr->pIdent, pIdent)); + if (strcmp(psCurr->pIdent, pIdent) == 0) + { + //DB_INTERP(("4\n")); + //debug( LOG_SCRIPT, "scriptLookUpVariable - local var found, type=%d", psCurr->type); + *ppsSym = psCurr; + return TRUE; + } + } + } + } + + + + /* See if the symbol is in the global variable list. * This is not checked for when local variables are being defined. * This allows local variables to have the same name as global ones. @@ -3885,6 +4276,9 @@ BOOL scriptLookUpVariable(STRING *pIdent, VAR_SYMBOL **ppsSym) /* Failed to find the variable */ *ppsSym = NULL; + + //debug( LOG_SCRIPT, "END scriptLookUpVariable" ); + return FALSE; } @@ -3894,6 +4288,8 @@ BOOL scriptAddTrigger(STRING *pIdent, TRIGGER_DECL *psDecl, UDWORD line) { TRIGGER_SYMBOL *psTrigger, *psCurr, *psPrev; + //debug( LOG_SCRIPT, "scriptAddTrigger" ); + // Allocate the trigger psTrigger = MALLOC(sizeof(TRIGGER_SYMBOL)); if (!psTrigger) @@ -3958,6 +4354,8 @@ BOOL scriptAddTrigger(STRING *pIdent, TRIGGER_DECL *psDecl, UDWORD line) psTriggers = psTrigger; } + //debug( LOG_SCRIPT, "END scriptAddTrigger" ); + return TRUE; } @@ -3967,6 +4365,8 @@ BOOL scriptLookUpTrigger(STRING *pIdent, TRIGGER_SYMBOL **ppsTrigger) { TRIGGER_SYMBOL *psCurr; + //debug( LOG_SCRIPT, "scriptLookUpTrigger" ); + for(psCurr = psTriggers; psCurr; psCurr=psCurr->psNext) { if (strcmp(pIdent, psCurr->pIdent) == 0) @@ -3976,6 +4376,8 @@ BOOL scriptLookUpTrigger(STRING *pIdent, TRIGGER_SYMBOL **ppsTrigger) } } + //debug( LOG_SCRIPT, "END scriptLookUpTrigger" ); + return FALSE; } @@ -3985,6 +4387,8 @@ BOOL scriptLookUpCallback(STRING *pIdent, CALLBACK_SYMBOL **ppsCallback) { CALLBACK_SYMBOL *psCurr; + //debug( LOG_SCRIPT, "scriptLookUpCallback" ); + if (!asScrCallbackTab) { return FALSE; @@ -3998,6 +4402,9 @@ BOOL scriptLookUpCallback(STRING *pIdent, CALLBACK_SYMBOL **ppsCallback) return TRUE; } } + + //debug( LOG_SCRIPT, "END scriptLookUpCallback" ); + return FALSE; } @@ -4006,6 +4413,8 @@ BOOL scriptDeclareEvent(STRING *pIdent, EVENT_SYMBOL **ppsEvent, UDWORD numArgs) { EVENT_SYMBOL *psEvent, *psCurr, *psPrev; + //debug( LOG_SCRIPT, "scriptDeclareEvent" ); + // Allocate the event psEvent = MALLOC(sizeof(EVENT_SYMBOL)); if (!psEvent) @@ -4050,12 +4459,17 @@ BOOL scriptDeclareEvent(STRING *pIdent, EVENT_SYMBOL **ppsEvent, UDWORD numArgs) *ppsEvent = psEvent; + //debug( LOG_SCRIPT, "END scriptDeclareEvent" ); + return TRUE; } // Add the code to a defined event BOOL scriptDefineEvent(EVENT_SYMBOL *psEvent, CODE_BLOCK *psCode, SDWORD trigger) { + + //debug( LOG_SCRIPT, "scriptDefineEvent" ); + // Store the event code psEvent->pCode = MALLOC(psCode->size); if (!psEvent->pCode) @@ -4086,6 +4500,8 @@ BOOL scriptDefineEvent(EVENT_SYMBOL *psEvent, CODE_BLOCK *psCode, SDWORD trigger psEvent->psDebug = NULL; } + //debug( LOG_SCRIPT, "END scriptDefineEvent" ); + return TRUE; } @@ -4094,6 +4510,8 @@ BOOL scriptLookUpEvent(STRING *pIdent, EVENT_SYMBOL **ppsEvent) { EVENT_SYMBOL *psCurr; + //debug( LOG_SCRIPT, "scriptLookUpEvent" ); + for(psCurr = psEvents; psCurr; psCurr=psCurr->psNext) { if (strcmp(pIdent, psCurr->pIdent) == 0) @@ -4103,6 +4521,8 @@ BOOL scriptLookUpEvent(STRING *pIdent, EVENT_SYMBOL **ppsEvent) } } + //debug( LOG_SCRIPT, "END scriptLookUpEvent" ); + return FALSE; } @@ -4112,6 +4532,8 @@ BOOL scriptLookUpConstant(STRING *pIdent, CONST_SYMBOL **ppsSym) { CONST_SYMBOL *psCurr; + //debug( LOG_SCRIPT, "scriptLookUpConstant" ); + /* Scan the Constant list */ if (asScrConstantTab) { @@ -4125,6 +4547,8 @@ BOOL scriptLookUpConstant(STRING *pIdent, CONST_SYMBOL **ppsSym) } } + //debug( LOG_SCRIPT, "END scriptLookUpConstant" ); + return FALSE; } @@ -4135,6 +4559,8 @@ BOOL scriptLookUpFunction(STRING *pIdent, FUNC_SYMBOL **ppsSym) UDWORD i; FUNC_SYMBOL *psCurr; + //debug( LOG_SCRIPT, "scriptLookUpFunction" ); + /* See if the function is defined as an instinct function */ if (asScrInstinctTab) { @@ -4160,6 +4586,9 @@ BOOL scriptLookUpFunction(STRING *pIdent, FUNC_SYMBOL **ppsSym) /* Failed to find the indentifier */ *ppsSym = NULL; + + //debug( LOG_SCRIPT, "END scriptLookUpFunction" ); + return FALSE; } @@ -4169,6 +4598,8 @@ BOOL scriptLookUpCustomFunction(STRING *pIdent, EVENT_SYMBOL **ppsSym) UDWORD i; EVENT_SYMBOL *psCurr; + //debug( LOG_SCRIPT, "scriptLookUpCustomFunction" ); + /* See if the function is defined as a script function */ for(psCurr = psEvents; psCurr; psCurr = psCurr->psNext) { @@ -4184,6 +4615,9 @@ BOOL scriptLookUpCustomFunction(STRING *pIdent, EVENT_SYMBOL **ppsSym) /* Failed to find the indentifier */ *ppsSym = NULL; + + //debug( LOG_SCRIPT, "END scriptLookUpCustomFunction" ); + return FALSE; } diff --git a/lib/script/stack.c b/lib/script/stack.c index d63039fe0..db5bfc72f 100644 --- a/lib/script/stack.c +++ b/lib/script/stack.c @@ -142,12 +142,17 @@ BOOL stackPopType(INTERP_VAL *psVal) { INTERP_VAL *psTop; + //debug(LOG_SCRIPT, "stackPopType 1"); + if ((psCurrChunk->psPrev == NULL) && (currEntry == 0)) { + debug(LOG_ERROR, "stackPopType: stack empty"); ASSERT((FALSE, "stackPopType: stack empty")); return FALSE; } + //debug(LOG_SCRIPT, "stackPopType 2"); + /* move the stack pointer down one */ if (currEntry == 0) { @@ -160,16 +165,26 @@ BOOL stackPopType(INTERP_VAL *psVal) currEntry--; } + //debug(LOG_SCRIPT, "stackPopType 3"); + psTop = psCurrChunk->aVals + currEntry; + + //debug(LOG_SCRIPT, "stackPopType 4"); + if (!interpCheckEquiv(psVal->type,psTop->type)) { + debug(LOG_ERROR, "stackPopType: type mismatch"); ASSERT((FALSE, "stackPopType: type mismatch")); return FALSE; } + //debug(LOG_SCRIPT, "stackPopType 5"); + /* copy the value off the stack */ psVal->v.ival = psTop->v.ival; + //debug(LOG_SCRIPT, "stackPopType 6"); + return TRUE; }