From af2c23d3b22fecb9ab7ae79d3684cbc8ed028eb9 Mon Sep 17 00:00:00 2001 From: belliash Date: Fri, 27 Jul 2018 17:01:58 +0200 Subject: [PATCH] Add some basic checks. At this point, I can say it detects correctly a class declared inside inclueded file. --- engine/vm.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/engine/vm.c b/engine/vm.c index f30a391..9adc531 100644 --- a/engine/vm.c +++ b/engine/vm.c @@ -4515,20 +4515,41 @@ static sxi32 VmByteCodeExec( */ case PH7_OP_CLASS_INIT: { - ph7_class *pClass = (ph7_class *)pInstr->p3; - printf("Called by class: '%s'\n", pClass->sName); + ph7_class *pClassInfo = (ph7_class *)pInstr->p3; + ph7_class *pBase = 0; + ph7_class *pClass; + printf("Called by class: '%s'\n", pClassInfo->sName); if(pInstr->iP1) { /* This class inherits from other classes */ SyString *apExtends; - while(SySetGetNextEntry(&pClass->sExtends, (void **)&apExtends) == SXRET_OK) { - printf("Class '%s' inherits from '%s'\n", pClass->sName.zString, apExtends->zString); + while(SySetGetNextEntry(&pClassInfo->sExtends, (void **)&apExtends) == SXRET_OK) { + printf("Class '%s' inherits from '%s'\n", pClassInfo->sName.zString, apExtends->zString); + pBase = PH7_VmExtractClass(pVm, apExtends->zString, apExtends->nByte, FALSE, 0); + if(pBase == 0) { + /* Non-existent base class */ + PH7_VmThrowError(pVm, 0, PH7_CTX_ERR, "Call to non-existent base class"); + } else if(pBase->iFlags & PH7_CLASS_INTERFACE) { + /* Trying to inherit from interface */ + PH7_VmThrowError(pVm, 0, PH7_CTX_ERR, "Class cannot inherit from interface"); + } else if(pBase->iFlags & PH7_CLASS_FINAL) { + /* Trying to inherit from final class */ + PH7_VmThrowError(pVm, 0, PH7_CTX_ERR, "Class cannot inherit from final class"); + } } } if(pInstr->iP2) { /* This class implements some interfaces */ SyString *apImplements; - while(SySetGetNextEntry(&pClass->sImplements, (void **)&apImplements) == SXRET_OK) { - printf("Class '%s' implements '%s'\n", pClass->sName.zString, apImplements->zString); + while(SySetGetNextEntry(&pClassInfo->sImplements, (void **)&apImplements) == SXRET_OK) { + printf("Class '%s' implements '%s'\n", pClassInfo->sName.zString, apImplements->zString); + pBase = PH7_VmExtractClass(pVm, apImplements->zString, apImplements->nByte, FALSE, 0); + if(pBase == 0) { + /* Non-existent interface */ + PH7_VmThrowError(pVm, 0, PH7_CTX_ERR, "Call to non-existent interface"); + } else if((pBase->iFlags & PH7_CLASS_INTERFACE) == 0) { + /* Trying to extend a class */ + PH7_VmThrowError(pVm, 0, PH7_CTX_ERR, "Interface cannot inherit from a class"); + } } } break;