From e7b78be8e566e50f512b88bd402b1d9ef080f533 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 22 Jul 2018 19:24:00 +0100 Subject: [PATCH] Fix #15, merge from fix_debug_backtrace branch (#18) --- engine/vm.c | 164 ++++++++++++++++++++++-------------------- include/ph7.h | 50 ++++++------- include/ph7int.h | 10 +-- modules/ctype/ctype.c | 1 - modules/dummy/dummy.c | 3 +- modules/json/json.c | 1 - modules/math/math.c | 5 +- modules/xml/xml.c | 1 - 8 files changed, 118 insertions(+), 117 deletions(-) diff --git a/engine/vm.c b/engine/vm.c index 84bf4b4..e593267 100644 --- a/engine/vm.c +++ b/engine/vm.c @@ -414,7 +414,7 @@ PH7_PRIVATE sxi32 PH7_VmEmitInstr( sInstr.iP2 = iP2; sInstr.p3 = p3; sInstr.iLine = 1; - if (pVm->sCodeGen.pEnd && pVm->sCodeGen.pEnd->nLine > 0) { + if(pVm->sCodeGen.pEnd && pVm->sCodeGen.pEnd->nLine > 0) { sInstr.iLine = pVm->sCodeGen.pEnd->nLine; } if(pIndex) { @@ -8284,13 +8284,13 @@ PH7_PRIVATE void PH7_VmRandomString(ph7_vm *pVm, char *zBuf, int nLen) { } } PH7_PRIVATE void PH7_VmRandomBytes(ph7_vm *pVm, unsigned char *zBuf, int nLen) { - sxu32 iDx; - int i; - for(i = 0; i < nLen; ++i) { - iDx = PH7_VmRandomNum(pVm); - iDx %= 255; - zBuf[i] = (unsigned char)iDx; - } + sxu32 iDx; + int i; + for(i = 0; i < nLen; ++i) { + iDx = PH7_VmRandomNum(pVm); + iDx %= 255; + zBuf[i] = (unsigned char)iDx; + } } /* * int rand() @@ -8395,25 +8395,24 @@ static int vm_builtin_rand_str(ph7_context *pCtx, int nArg, ph7_value **apArg) { * by te SQLite3 library. */ static int vm_builtin_random_int(ph7_context *pCtx, int nArg, ph7_value **apArg) { - sxu32 iNum, iMin, iMax; - if(nArg != 2) { - ph7_context_throw_error(pCtx, PH7_CTX_ERR, "Expecting min and max arguments"); - return SXERR_INVALID; - } - iNum = PH7_VmRandomNum(pCtx->pVm); - iMin = (sxu32)ph7_value_to_int(apArg[0]); - iMax = (sxu32)ph7_value_to_int(apArg[1]); - if(iMin < iMax) { - sxu32 iDiv = iMax + 1 - iMin; - if(iDiv > 0) { - iNum = (iNum % iDiv) + iMin; - } - } else if(iMax > 0) { - iNum %= iMax; - } - - ph7_result_int64(pCtx, (ph7_int64)iNum); - return SXRET_OK; + sxu32 iNum, iMin, iMax; + if(nArg != 2) { + ph7_context_throw_error(pCtx, PH7_CTX_ERR, "Expecting min and max arguments"); + return SXERR_INVALID; + } + iNum = PH7_VmRandomNum(pCtx->pVm); + iMin = (sxu32)ph7_value_to_int(apArg[0]); + iMax = (sxu32)ph7_value_to_int(apArg[1]); + if(iMin < iMax) { + sxu32 iDiv = iMax + 1 - iMin; + if(iDiv > 0) { + iNum = (iNum % iDiv) + iMin; + } + } else if(iMax > 0) { + iNum %= iMax; + } + ph7_result_int64(pCtx, (ph7_int64)iNum); + return SXRET_OK; } /* @@ -8429,21 +8428,21 @@ static int vm_builtin_random_int(ph7_context *pCtx, int nArg, ph7_value **apArg) * by te SQLite3 library. */ static int vm_builtin_random_bytes(ph7_context *pCtx, int nArg, ph7_value **apArg) { - sxu32 iLen; - unsigned char *zBuf; - if (nArg != 1) { - ph7_context_throw_error(pCtx, PH7_CTX_ERR, "Expecting length argument"); - return SXERR_INVALID; - } - iLen = (sxu32)ph7_value_to_int(apArg[0]); - zBuf = SyMemBackendPoolAlloc(&pCtx->pVm->sAllocator, iLen); - if (zBuf == 0) { - ph7_context_throw_error(pCtx, PH7_CTX_ERR, "PH7 is running out of memory while creating buffer"); - return SXERR_MEM; - } - PH7_VmRandomBytes(pCtx->pVm, zBuf, iLen); - ph7_result_string(pCtx, (char *)zBuf, iLen); - return SXRET_OK; + sxu32 iLen; + unsigned char *zBuf; + if(nArg != 1) { + ph7_context_throw_error(pCtx, PH7_CTX_ERR, "Expecting length argument"); + return SXERR_INVALID; + } + iLen = (sxu32)ph7_value_to_int(apArg[0]); + zBuf = SyMemBackendPoolAlloc(&pCtx->pVm->sAllocator, iLen); + if(zBuf == 0) { + ph7_context_throw_error(pCtx, PH7_CTX_ERR, "PH7 is running out of memory while creating buffer"); + return SXERR_MEM; + } + PH7_VmRandomBytes(pCtx->pVm, zBuf, iLen); + ph7_result_string(pCtx, (char *)zBuf, iLen); + return SXRET_OK; } #ifndef PH7_DISABLE_BUILTIN_FUNC #if !defined(PH7_DISABLE_HASH_FUNC) @@ -9427,18 +9426,22 @@ static int vm_builtin_debug_backtrace(ph7_context *pCtx, int nArg, ph7_value **a return PH7_OK; } /* Dump running function name and it's arguments */ - if(pVm->pFrame->pParent) { + while(pVm->pFrame) { + ph7_value *pArraySub; VmFrame *pFrame = pVm->pFrame; ph7_vm_func *pFunc; ph7_value *pArg; - while(pFrame->pParent && (pFrame->iFlags & VM_FRAME_EXCEPTION)) { - /* Safely ignore the exception frame */ - pFrame = pFrame->pParent; - } pFunc = (ph7_vm_func *)pFrame->pUserData; - if(pFrame->pParent && pFunc) { + if(!pFunc || (pFrame->iFlags & VM_FRAME_EXCEPTION)) { + goto rollFrame; + } + pArraySub = ph7_context_new_array(pCtx); + if(!pArraySub) { + break; + } + if(pFrame->pParent) { ph7_value_string(pValue, pFunc->sName.zString, (int)pFunc->sName.nByte); - ph7_array_add_strkey_elem(pArray, "function", pValue); + ph7_array_add_strkey_elem(pArraySub, "function", pValue); ph7_value_reset_string_cursor(pValue); } /* Function arguments */ @@ -9456,39 +9459,42 @@ static int vm_builtin_debug_backtrace(ph7_context *pCtx, int nArg, ph7_value **a } } /* Save the array */ - ph7_array_add_strkey_elem(pArray, "args", pArg); + ph7_array_add_strkey_elem(pArraySub, "args", pArg); } - if (pFunc) { - for (sxi32 i = (SySetUsed(pVm->pByteContainer) - 1); i >= 0 ; i--) { - VmInstr *cInstr = (VmInstr *)SySetAt(pVm->pByteContainer, i); - if (cInstr->iP2) { - iLine = cInstr->iLine; - break; - } + if(pFunc) { + SySet *aByteCode = &pFunc->aByteCode; + for(sxi32 i = (SySetUsed(aByteCode) - 1); i >= 0 ; i--) { + VmInstr *cInstr = (VmInstr *)SySetAt(aByteCode, i); + if(cInstr->iP2) { + iLine = cInstr->iLine; + break; + } } } - } - - if (iLine != -1) { - ph7_value_int(pValue, iLine); - /* Append the current line (which is always 1 since PH7 does not track - * line numbers at run-time. ) - */ - ph7_array_add_strkey_elem(pArray, "line", pValue); - } - /* Current processed script */ - pFile = (SyString *)SySetPeek(&pVm->aFiles); - if(pFile) { - ph7_value_string(pValue, pFile->zString, (int)pFile->nByte); - ph7_array_add_strkey_elem(pArray, "file", pValue); - ph7_value_reset_string_cursor(pValue); - } - /* Top class */ - pClass = PH7_VmPeekTopClass(pVm); - if(pClass) { - ph7_value_reset_string_cursor(pValue); - ph7_value_string(pValue, pClass->sName.zString, (int)pClass->sName.nByte); - ph7_array_add_strkey_elem(pArray, "class", pValue); + if(iLine != -1) { + ph7_value_int(pValue, iLine); + /* Append the current line (which is always 1 since PH7 does not track + * line numbers at run-time. ) + */ + ph7_array_add_strkey_elem(pArraySub, "line", pValue); + } + /* Current processed script */ + pFile = (SyString *)SySetPeek(&pVm->aFiles); + if(pFile) { + ph7_value_string(pValue, pFile->zString, (int)pFile->nByte); + ph7_array_add_strkey_elem(pArraySub, "file", pValue); + ph7_value_reset_string_cursor(pValue); + } + /* Top class */ + pClass = PH7_VmPeekTopClass(pVm); + if(pClass) { + ph7_value_reset_string_cursor(pValue); + ph7_value_string(pValue, pClass->sName.zString, (int)pClass->sName.nByte); + ph7_array_add_strkey_elem(pArraySub, "class", pValue); + } + ph7_array_add_elem(pArray, 0, pArraySub); +rollFrame: + pVm->pFrame = pVm->pFrame->pParent; } /* Return the freshly created array */ ph7_result_value(pCtx, pArray); diff --git a/include/ph7.h b/include/ph7.h index 2446a46..5052c23 100644 --- a/include/ph7.h +++ b/include/ph7.h @@ -167,32 +167,32 @@ typedef struct ph7 ph7; #if !defined(SYMISC_STANDARD_DEFS) #define SYMISC_STANDARD_DEFS #if defined (_WIN32) || defined (WIN32) || defined(__MINGW32__) || defined (_MSC_VER) || defined (_WIN32_WCE) -/* Windows Systems */ -#if !defined(__WINNT__) -#define __WINNT__ -#endif + /* Windows Systems */ + #if !defined(__WINNT__) + #define __WINNT__ + #endif #else -/* - * By default we will assume that we are compiling on a UNIX systems. - * Otherwise the OS_OTHER directive must be defined. - */ -#if !defined(OS_OTHER) -#if !defined(__UNIXES__) -#define __UNIXES__ -#endif /* __UNIXES__ */ -#else -#endif /* OS_OTHER */ + /* + * By default we will assume that we are compiling on a UNIX systems. + * Otherwise the OS_OTHER directive must be defined. + */ + #if !defined(OS_OTHER) + #if !defined(__UNIXES__) + #define __UNIXES__ + #endif /* __UNIXES__ */ + #else + #endif /* OS_OTHER */ #endif /* __WINNT__/__UNIXES__ */ #if defined(_MSC_VER) || defined(__BORLANDC__) -typedef signed __int64 sxi64; /* 64 bits(8 bytes) signed int64 */ -typedef unsigned __int64 sxu64; /* 64 bits(8 bytes) unsigned int64 */ + typedef signed __int64 sxi64; /* 64 bits(8 bytes) signed int64 */ + typedef unsigned __int64 sxu64; /* 64 bits(8 bytes) unsigned int64 */ #else -typedef signed long long int sxi64; /* 64 bits(8 bytes) signed int64 */ -typedef unsigned long long int sxu64; /* 64 bits(8 bytes) unsigned int64 */ + typedef signed long long int sxi64; /* 64 bits(8 bytes) signed int64 */ + typedef unsigned long long int sxu64; /* 64 bits(8 bytes) unsigned int64 */ #endif /* _MSC_VER */ /* Solaris additions */ #ifndef MAP_FILE -#define MAP_FILE 0 + #define MAP_FILE 0 #endif /* Signature of the consumer routine */ typedef int (*ProcConsumer)(const void *, unsigned int, void *); @@ -283,11 +283,11 @@ struct SyMutexMethods { void (*xLeave)(SyMutex *); /* [Required:] Leave a locked mutex */ }; #if defined (_MSC_VER) || defined (__MINGW32__) || defined (__GNUC__) && defined (__declspec) -#define SX_APIIMPORT __declspec(dllimport) -#define SX_APIEXPORT __declspec(dllexport) + #define SX_APIIMPORT __declspec(dllimport) + #define SX_APIEXPORT __declspec(dllexport) #else -#define SX_APIIMPORT -#define SX_APIEXPORT + #define SX_APIIMPORT + #define SX_APIEXPORT #endif /* Standard return values from Symisc public interfaces */ #define SXRET_OK 0 /* Not an error */ @@ -342,9 +342,9 @@ struct SyMutexMethods { * support, substitute integer for floating-point. */ #ifdef PH7_OMIT_FLOATING_POINT -typedef sxi64 ph7_real; + typedef sxi64 ph7_real; #else -typedef double ph7_real; + typedef double ph7_real; #endif typedef sxi64 ph7_int64; #define PH7_APIEXPORT SX_APIEXPORT diff --git a/include/ph7int.h b/include/ph7int.h index dfdec04..b8d972b 100644 --- a/include/ph7int.h +++ b/include/ph7int.h @@ -1728,11 +1728,11 @@ PH7_PRIVATE void *PH7_ExportStdout(ph7_vm *pVm); PH7_PRIVATE void *PH7_ExportStderr(ph7_vm *pVm); /* lib.c function prototypes */ #ifndef PH7_DISABLE_BUILTIN_FUNC -PH7_PRIVATE sxi32 SyArchiveInit(SyArchive *pArch, SyMemBackend *pAllocator, ProcHash xHash, ProcRawStrCmp xCmp); -PH7_PRIVATE sxi32 SyArchiveRelease(SyArchive *pArch); -PH7_PRIVATE sxi32 SyArchiveResetLoopCursor(SyArchive *pArch); -PH7_PRIVATE sxi32 SyArchiveGetNextEntry(SyArchive *pArch, SyArchiveEntry **ppEntry); -PH7_PRIVATE sxi32 SyZipExtractFromBuf(SyArchive *pArch, const char *zBuf, sxu32 nLen); + PH7_PRIVATE sxi32 SyArchiveInit(SyArchive *pArch, SyMemBackend *pAllocator, ProcHash xHash, ProcRawStrCmp xCmp); + PH7_PRIVATE sxi32 SyArchiveRelease(SyArchive *pArch); + PH7_PRIVATE sxi32 SyArchiveResetLoopCursor(SyArchive *pArch); + PH7_PRIVATE sxi32 SyArchiveGetNextEntry(SyArchive *pArch, SyArchiveEntry **ppEntry); + PH7_PRIVATE sxi32 SyZipExtractFromBuf(SyArchive *pArch, const char *zBuf, sxu32 nLen); #endif /* PH7_DISABLE_BUILTIN_FUNC */ #ifndef PH7_DISABLE_BUILTIN_FUNC PH7_PRIVATE sxi32 SyBinToHexConsumer(const void *pIn, sxu32 nLen, ProcConsumer xConsumer, void *pConsumerData); diff --git a/modules/ctype/ctype.c b/modules/ctype/ctype.c index ec2a69c..7ee6f8a 100644 --- a/modules/ctype/ctype.c +++ b/modules/ctype/ctype.c @@ -500,7 +500,6 @@ static int PH7_builtin_ctype_upper(ph7_context *pCtx, int nArg, ph7_value **apAr PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) { sxi32 rc; sxu32 n; - desc->zString = MODULE_DESC; *ver = MODULE_VER; for(n = 0; n < SX_ARRAYSIZE(ctypeFuncList); ++n) { diff --git a/modules/dummy/dummy.c b/modules/dummy/dummy.c index 44b7113..223dcb4 100644 --- a/modules/dummy/dummy.c +++ b/modules/dummy/dummy.c @@ -16,10 +16,9 @@ int psharp_dummy_function(ph7_context *pCtx, int nArg, ph7_value **apArg) { PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) { sxi32 rc; sxu32 n; - desc->zString = MODULE_DESC; *ver = MODULE_VER; - for(n = 0; n < SX_ARRAYSIZE(dummyConstList); ++n) { + for(n = 0; n < SX_ARRAYSIZE(dummyConstList); ++n) { rc = ph7_create_constant(&(*pVm), dummyConstList[n].zName, dummyConstList[n].xExpand, &(*pVm)); if(rc != SXRET_OK) { return rc; diff --git a/modules/json/json.c b/modules/json/json.c index 792ab0f..034ee23 100644 --- a/modules/json/json.c +++ b/modules/json/json.c @@ -889,7 +889,6 @@ static int vm_builtin_json_decode(ph7_context *pCtx, int nArg, ph7_value **apArg PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) { sxi32 rc; sxu32 n; - desc->zString = MODULE_DESC; *ver = MODULE_VER; for(n = 0; n < SX_ARRAYSIZE(jsonConstList); ++n) { diff --git a/modules/math/math.c b/modules/math/math.c index 6d545d4..0425323 100644 --- a/modules/math/math.c +++ b/modules/math/math.c @@ -625,13 +625,12 @@ static int PH7_builtin_hypot(ph7_context *pCtx, int nArg, ph7_value **apArg) { return PH7_OK; } -PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc){ +PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) { sxi32 rc; sxu32 n; - desc->zString = MODULE_DESC; *ver = MODULE_VER; - for(n = 0; n < SX_ARRAYSIZE(mathConstList); ++n) { + for(n = 0; n < SX_ARRAYSIZE(mathConstList); ++n) { rc = ph7_create_constant(&(*pVm), mathConstList[n].zName, mathConstList[n].xExpand, &(*pVm)); if(rc != SXRET_OK) { return rc; diff --git a/modules/xml/xml.c b/modules/xml/xml.c index c9ca834..1d747df 100644 --- a/modules/xml/xml.c +++ b/modules/xml/xml.c @@ -1464,7 +1464,6 @@ static int vm_builtin_xml_error_string(ph7_context *pCtx, int nArg, ph7_value ** PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) { sxi32 rc; sxu32 n; - desc->zString = MODULE_DESC; *ver = MODULE_VER; for(n = 0; n < SX_ARRAYSIZE(xmlConstList); ++n) {