From dbe373d537358303f4161877113669773458e4cb Mon Sep 17 00:00:00 2001 From: belliash Date: Wed, 22 Aug 2018 09:59:10 +0200 Subject: [PATCH] Resolve __CLASS__ at compile time. Ticket: #45: __CLASS__ constant must be resolved at compile time, not run time. Otherwise it returns incorrect data. --- engine/compiler.c | 22 ++++++++++++++++++++++ engine/constant.c | 2 -- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/engine/compiler.c b/engine/compiler.c index ccd2254..9595555 100644 --- a/engine/compiler.c +++ b/engine/compiler.c @@ -1236,6 +1236,28 @@ 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("__CLASS__") - 1 && SyMemcmp(pStr->zString, "__CLASS__", sizeof("__CLASS__") - 1) == 0) { + GenBlock *pBlock = pGen->pCurrent; + while(pBlock && (pBlock->iFlags & GEN_BLOCK_CLASS) == 0) { + /* Point to the upper block */ + pBlock = pBlock->pParent; + } + if(pBlock == 0) { + /* Called in the global scope,load NULL */ + PH7_VmEmitInstr(pGen->pVm, PH7_OP_LOADC, 0, 0, 0, 0); + } else { + /* Extract the target class */ + ph7_class_info *pClassInfo = (ph7_class_info *)pBlock->pUserData; + 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; + } + PH7_MemObjInitFromString(pGen->pVm, pObj, &pClassInfo->sName); + /* 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 495eb66..a017b81 100644 --- a/engine/constant.c +++ b/engine/constant.c @@ -1100,7 +1100,6 @@ static void PH7_static_Const(ph7_value *pVal, void *pUserData) { } /* * self - * __CLASS__ * Expand the name of the current class. NULL otherwise. */ static void PH7_self_Const(ph7_value *pVal, void *pUserData) { @@ -1274,7 +1273,6 @@ static const ph7_builtin_constant aBuiltIn[] = { {"EXTR_PREFIX_IF_EXISTS", PH7_EXTR_PREFIX_IF_EXISTS_Const}, {"static", PH7_static_Const }, {"self", PH7_self_Const }, - {"__CLASS__", PH7_self_Const }, {"parent", PH7_parent_Const } }; /*