From 02242bb93dfaec5ad7f2b5f2ff1d5692e1c55458 Mon Sep 17 00:00:00 2001 From: belliash Date: Tue, 21 Aug 2018 20:18:39 +0200 Subject: [PATCH] Resolve __FILE__ at compile time. Ticket: #45: __FILE__ constant must be resolved at compile time, not run time. Otherwise it returns incorrect data. --- engine/compiler.c | 18 ++++++++++++++++++ engine/constant.c | 17 ----------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/engine/compiler.c b/engine/compiler.c index 379ca75..aa1260c 100644 --- a/engine/compiler.c +++ b/engine/compiler.c @@ -1203,6 +1203,24 @@ static sxi32 PH7_GenStateLoadLiteral(ph7_gen_state *pGen) { /* Emit the load constant instruction */ PH7_VmEmitInstr(pGen->pVm, PH7_OP_LOADC, 0, nIdx, 0, 0); return SXRET_OK; + } else if(pStr->nByte == sizeof("__FILE__") - 1 && + SyMemcmp(pStr->zString, "__FILE__", sizeof("__FILE__") - 1) == 0) { + pObj = PH7_ReserveConstObj(pGen->pVm, &nIdx); + if(pObj == 0) { + PH7_GenCompileError(pGen, E_ERROR, pToken->nLine, "Fatal, PH7 engine is running out of memory"); + return SXERR_ABORT; + } + SyString *pFile = (SyString *)SySetPeek(&pGen->pVm->aFiles); + if(pFile == 0) { + SyString pMemory; + SyStringInitFromBuf(&pMemory, ":MEMORY:", (int)sizeof(":MEMORY:") - 1); + PH7_MemObjInitFromString(pGen->pVm, pObj, &pMemory); + } else { + PH7_MemObjInitFromString(pGen->pVm, pObj, pFile); + } + /* Emit the load constant instruction */ + PH7_VmEmitInstr(pGen->pVm, PH7_OP_LOADC, 0, nIdx, 0, 0); + return SXRET_OK; } else if((pStr->nByte == sizeof("__FUNCTION__") - 1 && SyMemcmp(pStr->zString, "__FUNCTION__", sizeof("__FUNCTION__") - 1) == 0) || (pStr->nByte == sizeof("__METHOD__") - 1 && diff --git a/engine/constant.c b/engine/constant.c index a1ca3e5..3e35b19 100644 --- a/engine/constant.c +++ b/engine/constant.c @@ -144,22 +144,6 @@ static void PH7_DATE_Const(ph7_value *pVal, void *pUnused) { /* Expand */ ph7_value_string_format(pVal, "%04d-%02d-%02d", sTm.tm_year, sTm.tm_mon + 1, sTm.tm_mday); } -/* - * __FILE__ - * Path of the processed script. - */ -static void PH7_FILE_Const(ph7_value *pVal, void *pUserData) { - ph7_vm *pVm = (ph7_vm *)pUserData; - SyString *pFile; - /* Peek the top entry */ - pFile = (SyString *)SySetPeek(&pVm->aFiles); - if(pFile == 0) { - /* Expand the magic word: ":MEMORY:" */ - ph7_value_string(pVal, ":MEMORY:", (int)sizeof(":MEMORY:") - 1); - } else { - ph7_value_string(pVal, pFile->zString, pFile->nByte); - } -} /* * __DIR__ * Directory holding the processed script. @@ -1191,7 +1175,6 @@ static const ph7_builtin_constant aBuiltIn[] = { {"DIR_SEP", PH7_DIRSEP_Const }, {"__TIME__", PH7_TIME_Const }, {"__DATE__", PH7_DATE_Const }, - {"__FILE__", PH7_FILE_Const }, {"__DIR__", PH7_DIR_Const }, {"PHP_SHLIB_SUFFIX", PH7_PHP_SHLIB_SUFFIX_Const }, {"E_ERROR", PH7_E_ERROR_Const },