Resolve __FILE__ at compile time.
The build was successful. Details

Ticket: #45: __FILE__ constant must be resolved at compile time, not run time. Otherwise it returns incorrect data.
This commit is contained in:
Rafal Kupiec 2018-08-21 20:18:39 +02:00
parent 19a504fe11
commit 02242bb93d
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
2 changed files with 18 additions and 17 deletions

View File

@ -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 &&

View File

@ -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 },