This commit is contained in:
+668
-53
@@ -618,6 +618,33 @@ static void VmLeaveFrame(ph7_vm *pVm) {
|
||||
}
|
||||
/* Forward declaration */
|
||||
static int VmInstanceOf(ph7_vm *pVm, ph7_class *pThis, ph7_class *pClass);
|
||||
static sxi32 VmInstanceOfResult(ph7_vm *pVm, ph7_class *pThis, ph7_class *pClass, sxi32 bRecoverable);
|
||||
/* Names, not class pointers, survive forward declarations and autoloading. */
|
||||
PH7_PRIVATE sxi32 PH7_VmInternType(ph7_vm *pVm, const SyString *pName, sxu32 *pType) {
|
||||
SyString sName;
|
||||
sxu32 i;
|
||||
char *zName;
|
||||
for(i = 0; i < SySetUsed(&pVm->aTypeNames); i++) {
|
||||
if(SyStringCmp(pName, (SyString *)SySetAt(&pVm->aTypeNames, i), SyMemcmp) == 0) {
|
||||
*pType = ((i + 1) << 12) | MEMOBJ_OBJ;
|
||||
return SXRET_OK;
|
||||
}
|
||||
}
|
||||
if(i >= (MEMOBJ_NAMED >> 12)) {
|
||||
return SXERR_MEM;
|
||||
}
|
||||
zName = SyMemBackendStrDup(&pVm->sAllocator, pName->zString, pName->nByte);
|
||||
if(zName == 0) {
|
||||
return SXERR_MEM;
|
||||
}
|
||||
SyStringInitFromBuf(&sName, zName, pName->nByte);
|
||||
if(SySetPut(&pVm->aTypeNames, &sName) != SXRET_OK) {
|
||||
SyMemBackendFree(&pVm->sAllocator, zName);
|
||||
return SXERR_MEM;
|
||||
}
|
||||
*pType = ((i + 1) << 12) | MEMOBJ_OBJ;
|
||||
return SXRET_OK;
|
||||
}
|
||||
PH7_PRIVATE ph7_class *PH7_VmTypeClass(ph7_vm *pVm, sxu32 nType, sxi32 bResolve) {
|
||||
SyString *pName;
|
||||
SyHashEntry *pEntry;
|
||||
@@ -645,7 +672,7 @@ PH7_PRIVATE ph7_class *PH7_VmTypeClass(ph7_vm *pVm, sxu32 nType, sxi32 bResolve)
|
||||
return pClass;
|
||||
}
|
||||
/* Read-only validation of all leaves; never cast or manufacture an instance. */
|
||||
PH7_PRIVATE sxi32 PH7_VmTypeCheck(ph7_value *pValue, sxu32 nType) {
|
||||
static sxi32 VmTypeCheck(ph7_value *pValue, sxu32 nType, sxi32 bRecoverable) {
|
||||
ph7_class *pClass = PH7_VmTypeClass(pValue->pVm, nType, FALSE);
|
||||
ph7_class_instance *pInstance;
|
||||
if(pClass == 0) {
|
||||
@@ -664,8 +691,9 @@ PH7_PRIVATE sxi32 PH7_VmTypeCheck(ph7_value *pValue, sxu32 nType) {
|
||||
for(pNode = pMap ? pMap->pFirst : 0; pNode; pNode = pNode->pPrev) {
|
||||
ph7_value *pLeaf = (ph7_value *)SySetAt(&pValue->pVm->aMemObj, pNode->nValIdx);
|
||||
sxu32 nLeafType = (nType & ~MEMOBJ_HASHMAP) | (pLeaf->nType & MEMOBJ_HASHMAP);
|
||||
if(PH7_VmTypeCheck(pLeaf, nLeafType) != SXRET_OK) {
|
||||
return SXERR_NOMATCH;
|
||||
sxi32 rc = VmTypeCheck(pLeaf, nLeafType, bRecoverable);
|
||||
if(rc != SXRET_OK) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
return SXRET_OK;
|
||||
@@ -674,7 +702,10 @@ PH7_PRIVATE sxi32 PH7_VmTypeCheck(ph7_value *pValue, sxu32 nType) {
|
||||
return SXERR_NOMATCH;
|
||||
}
|
||||
pInstance = (ph7_class_instance *)pValue->x.pOther;
|
||||
return !pInstance || VmInstanceOf(pValue->pVm, pInstance->pClass, pClass) ? SXRET_OK : SXERR_NOMATCH;
|
||||
return pInstance ? VmInstanceOfResult(pValue->pVm, pInstance->pClass, pClass, bRecoverable) : SXRET_OK;
|
||||
}
|
||||
PH7_PRIVATE sxi32 PH7_VmTypeCheck(ph7_value *pValue, sxu32 nType) {
|
||||
return VmTypeCheck(pValue, nType, FALSE);
|
||||
}
|
||||
/*
|
||||
* Ranks for general parameter types. Nominal specificity is compared by
|
||||
@@ -1387,6 +1418,471 @@ static ph7_vm_func *VmOverload(
|
||||
/* Appropriate function for the current call context */
|
||||
return pLink;
|
||||
}
|
||||
/* Map public descriptors to the existing language type representation. */
|
||||
static sxi32 VmNativeType(ph7_vm *pVm, ph7_class *pClass,
|
||||
const ph7_type_def *pDef, sxu32 *pType) {
|
||||
static const sxu32 aType[] = {
|
||||
0, MEMOBJ_BOOL, MEMOBJ_CALL, MEMOBJ_CHAR, MEMOBJ_REAL, MEMOBJ_INT,
|
||||
MEMOBJ_MIXED, MEMOBJ_OBJ, MEMOBJ_RES, MEMOBJ_STRING, MEMOBJ_VOID
|
||||
};
|
||||
sxi32 rc = SXRET_OK;
|
||||
if(pDef->bArray != 0 && pDef->bArray != 1) {
|
||||
return PH7_CORRUPT;
|
||||
}
|
||||
if(pDef->iType == PH7_TYPE_NAMED) {
|
||||
SyString sName;
|
||||
if(!PH7_IsIdentifier(pDef->zClass, FALSE)) {
|
||||
return PH7_CORRUPT;
|
||||
}
|
||||
SyStringInitFromBuf(&sName, pDef->zClass, SyStrlen(pDef->zClass));
|
||||
if(sName.nByte == 4 && SyMemcmp(sName.zString, "self", 4) == 0) {
|
||||
sName = pClass->sName;
|
||||
}
|
||||
rc = PH7_VmInternType(pVm, &sName, pType);
|
||||
} else {
|
||||
if(pDef->iType < PH7_TYPE_BOOL || pDef->iType > PH7_TYPE_VOID || pDef->zClass) {
|
||||
return PH7_CORRUPT;
|
||||
}
|
||||
*pType = aType[pDef->iType];
|
||||
}
|
||||
if(rc == SXRET_OK && pDef->bArray) {
|
||||
*pType |= MEMOBJ_HASHMAP;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
static void VmNativeMethodFree(ph7_vm *pVm, ph7_class_method *pMeth) {
|
||||
sxu32 n;
|
||||
for(n = 0; n < SySetUsed(&pMeth->sFunc.aArgs); n++) {
|
||||
ph7_vm_func_arg *pArg = (ph7_vm_func_arg *)SySetAt(&pMeth->sFunc.aArgs, n);
|
||||
SyMemBackendFree(&pVm->sAllocator, (void *)pArg->sName.zString);
|
||||
SySetRelease(&pArg->aByteCode);
|
||||
}
|
||||
SySetRelease(&pMeth->sFunc.aArgs);
|
||||
SySetRelease(&pMeth->sFunc.aByteCode);
|
||||
SySetRelease(&pMeth->sFunc.aStatic);
|
||||
SySetRelease(&pMeth->sFunc.aClosureEnv);
|
||||
SySetRelease(&pMeth->sFunc.sNative.aAux);
|
||||
SyMemBackendFree(&pVm->sAllocator, (void *)pMeth->sFunc.sNative.sName.zString);
|
||||
SyMemBackendFree(&pVm->sAllocator, (void *)pMeth->sVmName.zString);
|
||||
SyMemBackendPoolFree(&pVm->sAllocator, pMeth);
|
||||
}
|
||||
static sxi32 VmNativeInitialType(ph7_value *pValue, sxu32 nType) {
|
||||
if(nType == 0) {
|
||||
return SXRET_OK; /* Constants have no annotation in source either. */
|
||||
}
|
||||
if(nType & MEMOBJ_NAMED) {
|
||||
/* NULL and empty arrays do not need the late class declaration yet. */
|
||||
if(MemObjIsNullReference(pValue)) {
|
||||
return SXRET_OK;
|
||||
}
|
||||
if((nType & MEMOBJ_HASHMAP) && (pValue->nType & MEMOBJ_HASHMAP) &&
|
||||
((ph7_hashmap *)pValue->x.pOther)->nEntry == 0) {
|
||||
return SXRET_OK;
|
||||
}
|
||||
return VmTypeCheck(pValue, nType, TRUE);
|
||||
}
|
||||
if(nType & MEMOBJ_HASHMAP) {
|
||||
ph7_hashmap *pMap;
|
||||
ph7_hashmap_node *pNode;
|
||||
if(pValue->nType & MEMOBJ_NULL) {
|
||||
ph7_hashmap *pMap = PH7_NewHashmap(pValue->pVm, 0, 0);
|
||||
if(pMap == 0) {
|
||||
return PH7_NOMEM;
|
||||
}
|
||||
pValue->x.pOther = pMap;
|
||||
MemObjSetType(pValue, nType);
|
||||
} else if((pValue->nType & MEMOBJ_HASHMAP) == 0) {
|
||||
return PH7_CORRUPT;
|
||||
}
|
||||
if(nType & MEMOBJ_MIXED) {
|
||||
/* Safe-store mixed[] keeps the source leaves' concrete types. */
|
||||
pValue->nType |= MEMOBJ_MIXED;
|
||||
return SXRET_OK;
|
||||
}
|
||||
pMap = (ph7_hashmap *)pValue->x.pOther;
|
||||
for(pNode = pMap ? pMap->pFirst : 0; pNode; pNode = pNode->pPrev) {
|
||||
ph7_value *pLeaf = (ph7_value *)SySetAt(&pValue->pVm->aMemObj, pNode->nValIdx);
|
||||
sxi32 rc = VmNativeInitialType(pLeaf, (nType & ~MEMOBJ_HASHMAP) | (pLeaf->nType & MEMOBJ_HASHMAP));
|
||||
if(rc != SXRET_OK) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
pValue->nType = nType;
|
||||
return SXRET_OK;
|
||||
}
|
||||
if(nType & MEMOBJ_MIXED) {
|
||||
if(pValue->nType & MEMOBJ_HASHMAP) {
|
||||
return PH7_CORRUPT;
|
||||
}
|
||||
/* Preserve the same scalar mixed contract as PH7_MemObjSafeStore,
|
||||
* including explicit NULL, when the snapshot becomes static storage. */
|
||||
if(pValue->nType == MEMOBJ_NULL) {
|
||||
MemObjSetType(pValue, (MEMOBJ_MIXED | MEMOBJ_VOID));
|
||||
} else {
|
||||
pValue->nType |= MEMOBJ_MIXED;
|
||||
}
|
||||
return SXRET_OK;
|
||||
}
|
||||
if(pValue->nType != nType) {
|
||||
ProcMemObjCast xCast;
|
||||
if(PH7_CheckVarCompat(pValue, nType) != SXRET_OK) {
|
||||
return PH7_CORRUPT;
|
||||
}
|
||||
xCast = PH7_MemObjCastMethod(nType);
|
||||
if(xCast) {
|
||||
return xCast(pValue);
|
||||
}
|
||||
}
|
||||
return SXRET_OK;
|
||||
}
|
||||
static void VmNativeClassFree(ph7_vm *pVm, ph7_class *pClass) {
|
||||
SyHashEntry *pEntry;
|
||||
SyHashResetLoopCursor(&pClass->hMethod);
|
||||
while((pEntry = SyHashGetNextEntry(&pClass->hMethod)) != 0) {
|
||||
ph7_class_method *pMeth = (ph7_class_method *)pEntry->pUserData;
|
||||
SyHashEntry *pInstalled = SyHashGet(&pVm->hFunction, pMeth->sVmName.zString, pMeth->sVmName.nByte);
|
||||
if(pInstalled && ((ph7_vm_func *)pInstalled->pUserData)->pClass == pClass) {
|
||||
SyHashDeleteEntry2(pInstalled);
|
||||
}
|
||||
}
|
||||
SyHashResetLoopCursor(&pClass->hMethod);
|
||||
while((pEntry = SyHashGetNextEntry(&pClass->hMethod)) != 0) {
|
||||
VmNativeMethodFree(pVm, (ph7_class_method *)pEntry->pUserData);
|
||||
}
|
||||
SyHashResetLoopCursor(&pClass->hAttr);
|
||||
while((pEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) {
|
||||
ph7_class_attr *pAttr = (ph7_class_attr *)pEntry->pUserData;
|
||||
if(pAttr->nIdx != SXU32_HIGH) {
|
||||
PH7_VmDestroyMemObj(pVm, (ph7_value *)SySetAt(&pVm->aMemObj, pAttr->nIdx));
|
||||
}
|
||||
if(pAttr->pNativeInitial) {
|
||||
PH7_MemObjRelease(pAttr->pNativeInitial);
|
||||
SyMemBackendPoolFree(&pVm->sAllocator, pAttr->pNativeInitial);
|
||||
}
|
||||
SySetRelease(&pAttr->aByteCode);
|
||||
SyMemBackendFree(&pVm->sAllocator, (void *)pAttr->sName.zString);
|
||||
SyMemBackendPoolFree(&pVm->sAllocator, pAttr);
|
||||
}
|
||||
SyHashRelease(&pClass->hMethod);
|
||||
SyHashRelease(&pClass->hAttr);
|
||||
SyHashRelease(&pClass->hDerived);
|
||||
SySetRelease(&pClass->aInterface);
|
||||
SySetRelease(&pClass->aBase);
|
||||
SySetRelease(&pClass->aMro);
|
||||
SyMemBackendFree(&pVm->sAllocator, (void *)pClass->sName.zString);
|
||||
SyMemBackendPoolFree(&pVm->sAllocator, pClass);
|
||||
}
|
||||
static sxi32 VmNativeMagicName(const char *zName) {
|
||||
static const char *azName[] = {
|
||||
"__construct", "__destruct", "__clone", "__invoke",
|
||||
"__toInt", "__toFloat", "__toString", "__toBool"
|
||||
};
|
||||
sxu32 n;
|
||||
for(n = 0; n < SX_ARRAYSIZE(azName); n++) {
|
||||
if(SyStrncmp(zName, azName[n], SyStrlen(azName[n]) + 1) == 0) {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
/*
|
||||
* Prepare privately, then publish. No compiler diagnostics or VM fatal OOM
|
||||
* paths are used here: a host may correct a declaration and retry.
|
||||
*/
|
||||
PH7_PRIVATE sxi32 PH7_VmCreateNativeClass(ph7_vm *pVm, const ph7_class_def *pDef) {
|
||||
ph7_class *pClass;
|
||||
SyString sName;
|
||||
SyHashEntry *pEntry;
|
||||
sxu32 n, i, nTypes = SySetUsed(&pVm->aTypeNames);
|
||||
sxu32 nMem = SySetUsed(&pVm->aMemObj), nFree = SySetUsed(&pVm->aFreeObj);
|
||||
VmSlot *aFree = 0;
|
||||
sxi32 rc = PH7_CORRUPT;
|
||||
if((pVm->nMagic != PH7_VM_INIT && pVm->nMagic != PH7_VM_RUN &&
|
||||
pVm->nMagic != PH7_VM_EXEC && pVm->nMagic != PH7_VM_INCL) ||
|
||||
!PH7_IsIdentifier(pDef->zName, FALSE) ||
|
||||
(pDef->nField && !pDef->aField) || (pDef->nConstant && !pDef->aConstant) ||
|
||||
(pDef->nMethod && !pDef->aMethod) || pDef->nField > SXI32_HIGH ||
|
||||
pDef->nConstant > SXI32_HIGH - pDef->nField || pDef->nMethod > SXI32_HIGH) {
|
||||
return PH7_CORRUPT;
|
||||
}
|
||||
SyStringInitFromBuf(&sName, pDef->zName, SyStrlen(pDef->zName));
|
||||
if(SyHashGet(&pVm->hClass, sName.zString, sName.nByte)) {
|
||||
return PH7_CORRUPT;
|
||||
}
|
||||
/* Hosts may register after vm_init, before compiling their program. */
|
||||
if(pVm->apRefObj == 0) {
|
||||
VmRefObj **apRef = (VmRefObj **)SyMemBackendAlloc(&pVm->sAllocator, 16 * sizeof(VmRefObj *));
|
||||
if(apRef == 0) {
|
||||
return PH7_NOMEM;
|
||||
}
|
||||
SyZero(apRef, 16 * sizeof(VmRefObj *));
|
||||
pVm->apRefObj = apRef;
|
||||
pVm->nRefSize = 16;
|
||||
}
|
||||
/* SySetPut's first failed growth leaves a nonzero capacity without
|
||||
* storage. Preallocate the VM sets that must survive a failed transaction. */
|
||||
if((pVm->aFreeObj.nSize == 0 && SySetAlloc(&pVm->aFreeObj, 8) != SXRET_OK) ||
|
||||
(pVm->aTypeNames.nSize == 0 && SySetAlloc(&pVm->aTypeNames, 8) != SXRET_OK)) {
|
||||
return PH7_NOMEM;
|
||||
}
|
||||
if(nFree) {
|
||||
aFree = (VmSlot *)SyMemBackendAlloc(&pVm->sAllocator, nFree * sizeof(VmSlot));
|
||||
if(aFree == 0) {
|
||||
return PH7_NOMEM;
|
||||
}
|
||||
SyMemcpy(SySetBasePtr(&pVm->aFreeObj), aFree, nFree * sizeof(VmSlot));
|
||||
}
|
||||
pClass = PH7_NewRawClass(pVm, &sName);
|
||||
if(pClass == 0) {
|
||||
SyMemBackendFree(&pVm->sAllocator, aFree);
|
||||
return PH7_NOMEM;
|
||||
}
|
||||
pClass->bNative = TRUE;
|
||||
if(!pClass->hAttr.apBucket || !pClass->hMethod.apBucket || !pClass->hDerived.apBucket) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Fail;
|
||||
}
|
||||
for(n = 0; n < pDef->nField + pDef->nConstant; n++) {
|
||||
ph7_class_attr *pAttr;
|
||||
const ph7_value *pInitial;
|
||||
sxu32 nType = 0;
|
||||
sxi32 iFlags;
|
||||
if(n < pDef->nField) {
|
||||
const ph7_field_def *pField = &pDef->aField[n];
|
||||
if(pField->iFlags & ~PH7_MEMBER_STATIC) {
|
||||
rc = PH7_CORRUPT;
|
||||
goto Fail;
|
||||
}
|
||||
rc = VmNativeType(pVm, pClass, &pField->sType, &nType);
|
||||
if(rc != SXRET_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
sName.zString = pField->zName;
|
||||
pInitial = pField->pInitial;
|
||||
iFlags = pField->iFlags & PH7_MEMBER_STATIC ? PH7_CLASS_ATTR_STATIC : 0;
|
||||
} else {
|
||||
const ph7_constant_def *pConst = &pDef->aConstant[n - pDef->nField];
|
||||
sName.zString = pConst->zName;
|
||||
pInitial = pConst->pValue;
|
||||
iFlags = PH7_CLASS_ATTR_CONSTANT;
|
||||
if(pInitial == 0) {
|
||||
rc = PH7_CORRUPT;
|
||||
goto Fail;
|
||||
}
|
||||
}
|
||||
if(!PH7_IsIdentifier(sName.zString, (iFlags & PH7_CLASS_ATTR_CONSTANT) == 0) ||
|
||||
(pInitial && pInitial->pVm != pVm)) {
|
||||
rc = PH7_CORRUPT;
|
||||
goto Fail;
|
||||
}
|
||||
sName.nByte = SyStrlen(sName.zString);
|
||||
if((iFlags & PH7_CLASS_ATTR_CONSTANT) && PH7_IsReservedConstant(&sName)) {
|
||||
rc = PH7_CORRUPT;
|
||||
goto Fail;
|
||||
}
|
||||
if(SyHashGet(&pClass->hAttr, sName.zString, sName.nByte)) {
|
||||
rc = PH7_CORRUPT;
|
||||
goto Fail;
|
||||
}
|
||||
pAttr = PH7_NewClassAttr(pVm, pClass, &sName, 0, PH7_CLASS_PROT_PUBLIC, iFlags, nType);
|
||||
if(pAttr == 0) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Fail;
|
||||
}
|
||||
pAttr->pClass = pClass;
|
||||
rc = SyHashInsert(&pClass->hAttr, pAttr->sName.zString, pAttr->sName.nByte, pAttr);
|
||||
if(rc != SXRET_OK) {
|
||||
SyMemBackendFree(&pVm->sAllocator, (void *)pAttr->sName.zString);
|
||||
SyMemBackendPoolFree(&pVm->sAllocator, pAttr);
|
||||
goto Fail;
|
||||
}
|
||||
if(pInitial) {
|
||||
pAttr->pNativeInitial = (ph7_value *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(ph7_value));
|
||||
if(pAttr->pNativeInitial == 0) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Fail;
|
||||
}
|
||||
PH7_MemObjInit(pVm, pAttr->pNativeInitial);
|
||||
rc = PH7_MemObjSnapshot((ph7_value *)pInitial, pAttr->pNativeInitial);
|
||||
if(rc != SXRET_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
rc = VmNativeInitialType(pAttr->pNativeInitial, nType);
|
||||
if(rc != SXRET_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(n = 0; n < pDef->nMethod; n++) {
|
||||
const ph7_method_def *pDefMeth = &pDef->aMethod[n];
|
||||
ph7_class_method *pMeth;
|
||||
if(!PH7_IsMethodName(pDefMeth->zName) || !pDefMeth->xFunc ||
|
||||
(pDefMeth->iFlags & ~PH7_MEMBER_STATIC) || (pDefMeth->nParam && !pDefMeth->aParam) ||
|
||||
pDefMeth->nParam > SXI32_HIGH || VmNativeMagicName(pDefMeth->zName)) {
|
||||
rc = PH7_CORRUPT;
|
||||
goto Fail;
|
||||
}
|
||||
SyStringInitFromBuf(&sName, pDefMeth->zName, SyStrlen(pDefMeth->zName));
|
||||
pEntry = SyHashGet(&pClass->hMethod, sName.zString, sName.nByte);
|
||||
pMeth = PH7_NewClassMethod(pVm, pClass, &sName, 0, PH7_CLASS_PROT_PUBLIC,
|
||||
pDefMeth->iFlags & PH7_MEMBER_STATIC ? PH7_CLASS_ATTR_STATIC : 0);
|
||||
if(pMeth == 0) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Fail;
|
||||
}
|
||||
/* Each prepared method owns its VM name, including overloads. */
|
||||
if(pEntry) {
|
||||
char *zName = SyMemBackendStrDup(&pVm->sAllocator, pMeth->sVmName.zString, pMeth->sVmName.nByte);
|
||||
sxu32 nOffset = (sxu32)(pMeth->sFunc.sName.zString - pMeth->sVmName.zString);
|
||||
if(zName == 0) {
|
||||
pMeth->sVmName.zString = 0;
|
||||
VmNativeMethodFree(pVm, pMeth);
|
||||
rc = PH7_NOMEM;
|
||||
goto Fail;
|
||||
}
|
||||
pMeth->sVmName.zString = zName;
|
||||
pMeth->sFunc.sName.zString = &zName[nOffset];
|
||||
}
|
||||
SySetInit(&pMeth->sFunc.sNative.aAux, &pVm->sAllocator, sizeof(void *));
|
||||
if(pMeth->sFunc.aByteCode.nSize == 0) {
|
||||
VmNativeMethodFree(pVm, pMeth);
|
||||
rc = PH7_NOMEM;
|
||||
goto Fail;
|
||||
}
|
||||
rc = PH7_ClassInstallMethod(pClass, pMeth);
|
||||
if(rc != SXRET_OK) {
|
||||
VmNativeMethodFree(pVm, pMeth);
|
||||
goto Fail;
|
||||
}
|
||||
pMeth->sFunc.sNative.sName.zString = SyMemBackendStrDup(&pVm->sAllocator, sName.zString, sName.nByte);
|
||||
pMeth->sFunc.sNative.sName.nByte = sName.nByte;
|
||||
if(pMeth->sFunc.sNative.sName.zString == 0) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Fail;
|
||||
}
|
||||
pMeth->sFunc.sNative.xFunc = pDefMeth->xFunc;
|
||||
pMeth->sFunc.sNative.pUserData = pDefMeth->pUserData;
|
||||
rc = VmNativeType(pVm, pClass, &pDefMeth->sResult, &pMeth->sFunc.nType);
|
||||
if(rc != SXRET_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
for(i = 0; i < pDefMeth->nParam; i++) {
|
||||
const ph7_param_def *pDefArg = &pDefMeth->aParam[i];
|
||||
ph7_vm_func_arg sArg;
|
||||
sxu32 j;
|
||||
char *zName;
|
||||
if(!PH7_IsIdentifier(pDefArg->zName, TRUE) ||
|
||||
SyStrncmp(pDefArg->zName, "this", sizeof("this")) == 0 ||
|
||||
SyStrncmp(pDefArg->zName, "parent", sizeof("parent")) == 0) {
|
||||
rc = PH7_CORRUPT;
|
||||
goto Fail;
|
||||
}
|
||||
for(j = 0; j < SySetUsed(&pMeth->sFunc.aArgs); j++) {
|
||||
ph7_vm_func_arg *pOther = (ph7_vm_func_arg *)SySetAt(&pMeth->sFunc.aArgs, j);
|
||||
if(pOther->sName.nByte == SyStrlen(pDefArg->zName) &&
|
||||
SyMemcmp(pOther->sName.zString, pDefArg->zName, pOther->sName.nByte) == 0) {
|
||||
rc = PH7_CORRUPT;
|
||||
goto Fail;
|
||||
}
|
||||
}
|
||||
SyZero(&sArg, sizeof(sArg));
|
||||
SySetInit(&sArg.aByteCode, &pVm->sAllocator, sizeof(VmInstr));
|
||||
rc = VmNativeType(pVm, pClass, &pDefArg->sType, &sArg.nType);
|
||||
if(rc != SXRET_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
if(sArg.nType & MEMOBJ_NAMED) {
|
||||
sArg.sClass = *(SyString *)SySetAt(&pVm->aTypeNames, ((sArg.nType & MEMOBJ_NAMED) >> 12) - 1);
|
||||
}
|
||||
zName = SyMemBackendStrDup(&pVm->sAllocator, pDefArg->zName, SyStrlen(pDefArg->zName));
|
||||
if(zName == 0) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Fail;
|
||||
}
|
||||
SyStringInitFromBuf(&sArg.sName, zName, SyStrlen(zName));
|
||||
rc = SySetPut(&pMeth->sFunc.aArgs, &sArg);
|
||||
if(rc != SXRET_OK) {
|
||||
SyMemBackendFree(&pVm->sAllocator, zName);
|
||||
goto Fail;
|
||||
}
|
||||
}
|
||||
SyHashResetLoopCursor(&pClass->hMethod);
|
||||
while((pEntry = SyHashGetNextEntry(&pClass->hMethod)) != 0) {
|
||||
ph7_class_method *pOther = (ph7_class_method *)pEntry->pUserData;
|
||||
if(pOther != pMeth && PH7_VmSameSignature(&pOther->sFunc, &pMeth->sFunc)) {
|
||||
rc = PH7_CORRUPT;
|
||||
goto Fail;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Storage is prepared before the class becomes visible to the program. */
|
||||
SyHashResetLoopCursor(&pClass->hAttr);
|
||||
while((pEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) {
|
||||
ph7_class_attr *pAttr = (ph7_class_attr *)pEntry->pUserData;
|
||||
if(pAttr->iFlags & (PH7_CLASS_ATTR_STATIC | PH7_CLASS_ATTR_CONSTANT)) {
|
||||
ph7_value *pValue = PH7_ReserveMemObj(pVm);
|
||||
if(pValue == 0) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Fail;
|
||||
}
|
||||
pAttr->nIdx = pValue->nIdx;
|
||||
if(pAttr->pNativeInitial) {
|
||||
rc = PH7_MemObjSnapshot(pAttr->pNativeInitial, pValue);
|
||||
if(rc != SXRET_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
pValue = (ph7_value *)SySetAt(&pVm->aMemObj, pAttr->nIdx);
|
||||
} else {
|
||||
MemObjSetType(pValue, (pAttr->nType & MEMOBJ_TYPEMASK));
|
||||
if(pAttr->nType & MEMOBJ_HASHMAP) {
|
||||
pValue->x.pOther = PH7_NewHashmap(pVm, 0, 0);
|
||||
if(pValue->x.pOther == 0) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Fail;
|
||||
}
|
||||
}
|
||||
}
|
||||
rc = PH7_MemObjDeclareType(pValue, pAttr->nType, FALSE);
|
||||
if(rc != SXRET_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
rc = PH7_VmRefObjInstall(pVm, pAttr->nIdx, 0, 0, VM_REF_IDX_KEEP);
|
||||
if(rc != SXRET_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
}
|
||||
}
|
||||
SyHashResetLoopCursor(&pClass->hMethod);
|
||||
while((pEntry = SyHashGetNextEntry(&pClass->hMethod)) != 0) {
|
||||
ph7_class_method *pMeth = (ph7_class_method *)pEntry->pUserData;
|
||||
rc = PH7_VmInstallUserFunction(pVm, &pMeth->sFunc, &pMeth->sVmName);
|
||||
if(rc != SXRET_OK) {
|
||||
goto Fail;
|
||||
}
|
||||
}
|
||||
rc = SyHashInsert(&pVm->hClass, pClass->sName.zString, pClass->sName.nByte, pClass);
|
||||
if(rc == SXRET_OK) {
|
||||
SyMemBackendFree(&pVm->sAllocator, aFree);
|
||||
return PH7_OK;
|
||||
}
|
||||
Fail:
|
||||
VmNativeClassFree(pVm, pClass);
|
||||
while(SySetUsed(&pVm->aTypeNames) > nTypes) {
|
||||
SyString *pName = (SyString *)SySetPop(&pVm->aTypeNames);
|
||||
SyMemBackendFree(&pVm->sAllocator, (void *)pName->zString);
|
||||
}
|
||||
/* Rollback must not allocate, including restoring reusable arena slots. */
|
||||
for(n = nMem; n < SySetUsed(&pVm->aMemObj); n++) {
|
||||
PH7_MemObjRelease((ph7_value *)SySetAt(&pVm->aMemObj, n));
|
||||
}
|
||||
pVm->aMemObj.nUsed = nMem;
|
||||
if(nFree) {
|
||||
SyMemcpy(aFree, SySetBasePtr(&pVm->aFreeObj), nFree * sizeof(VmSlot));
|
||||
}
|
||||
pVm->aFreeObj.nUsed = nFree;
|
||||
SyMemBackendFree(&pVm->sAllocator, aFree);
|
||||
return rc;
|
||||
}
|
||||
/*
|
||||
* Mount a compiled class into the freshly created virtual machine so that
|
||||
* it can be instanciated from the executed PHP script.
|
||||
@@ -1399,6 +1895,10 @@ static sxi32 VmMountUserClass(
|
||||
ph7_class_attr *pAttr;
|
||||
SyHashEntry *pEntry;
|
||||
sxi32 rc;
|
||||
if(pClass->bNative) {
|
||||
/* Registered classes already own their storage and overload chain. */
|
||||
return SXRET_OK;
|
||||
}
|
||||
/* Reset the loop cursor */
|
||||
SyHashResetLoopCursor(&pClass->hAttr);
|
||||
/* Process only static and constant attribute */
|
||||
@@ -1503,7 +2003,13 @@ PH7_PRIVATE sxi32 PH7_VmCreateClassInstanceFrame(
|
||||
MemObjSetType(pMemObj, (pAttr->nType & MEMOBJ_TYPEMASK));
|
||||
PH7_MemObjSetDeclaredType(pMemObj, pAttr->nType);
|
||||
PH7_MemObjInit(pVm, &sValue);
|
||||
if(SySetUsed(&pAttr->aByteCode) > 0) {
|
||||
if(pAttr->pNativeInitial) {
|
||||
rc = PH7_MemObjSafeStore(pAttr->pNativeInitial, pMemObj);
|
||||
pMemObj = (ph7_value *)SySetAt(&pVm->aMemObj, nObjIdx);
|
||||
if(rc != SXRET_OK) {
|
||||
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Cannot assign a value of incompatible type to variable '%z::$%z'", &pClass->sName, &pAttr->sName);
|
||||
}
|
||||
} else if(SySetUsed(&pAttr->aByteCode) > 0) {
|
||||
/* Initialize attribute default value (any complex expression) */
|
||||
rc = VmLocalExec(&(*pVm), &pAttr->aByteCode, &sValue);
|
||||
pMemObj = (ph7_value *)SySetAt(&pVm->aMemObj, nObjIdx);
|
||||
@@ -1952,6 +2458,36 @@ static ph7_value *VmNewOperandStack(
|
||||
}
|
||||
/* Forward declaration */
|
||||
static sxi32 VmRegisterSpecialFunction(ph7_vm *pVm);
|
||||
/* Shared by bytecode returns and native methods, after ordinary argument prep. */
|
||||
static void VmCheckMissingReturn(ph7_vm *pVm, ph7_vm_func *pFunc) {
|
||||
if(pFunc->nType && (pFunc->nType & MEMOBJ_VOID) == 0) {
|
||||
PH7_VmThrowError(pVm, PH7_CTX_WARNING, "Missing return value in closure/method returning non-void");
|
||||
}
|
||||
}
|
||||
static void VmCheckReturn(ph7_vm *pVm, ph7_vm_func *pFunc, ph7_value *pValue) {
|
||||
if(pFunc->nType & MEMOBJ_NAMED) {
|
||||
PH7_VmTypeClass(pVm, pFunc->nType, TRUE);
|
||||
if(PH7_VmTypeCheck(pValue, pFunc->nType) != SXRET_OK) {
|
||||
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
|
||||
}
|
||||
PH7_MemObjSetDeclaredType(pValue, pFunc->nType);
|
||||
} else if(pFunc->nType && (pFunc->nType & MEMOBJ_MIXED) == 0) {
|
||||
if(pFunc->nType & MEMOBJ_VOID) {
|
||||
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Return with a value in closure/method returning void");
|
||||
} else if(pFunc->nType != pValue->nType) {
|
||||
if(PH7_CheckVarCompat(pValue, pFunc->nType) == SXRET_OK) {
|
||||
ProcMemObjCast xCast = PH7_MemObjCastMethod(pFunc->nType);
|
||||
xCast(pValue);
|
||||
} else if((pFunc->iFlags & MEMOBJ_HASHMAP) && (pValue->nType & MEMOBJ_HASHMAP)) {
|
||||
if(PH7_HashmapCast(pValue, pFunc->iFlags ^ MEMOBJ_HASHMAP) != SXRET_OK) {
|
||||
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
|
||||
}
|
||||
} else {
|
||||
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Exception frames retain the enclosing method's lexical scope. */
|
||||
static ph7_class *VmMethodClass(ph7_vm *pVm) {
|
||||
VmFrame *pFrame = pVm->pFrame;
|
||||
@@ -1994,14 +2530,16 @@ PH7_PRIVATE sxi32 PH7_VmMakeReady(
|
||||
return SXERR_MEM;
|
||||
}
|
||||
/* Allocate the reference table */
|
||||
pVm->nRefSize = 0x10; /* Must be a power of two for fast arithemtic */
|
||||
pVm->apRefObj = (VmRefObj **)SyMemBackendAlloc(&pVm->sAllocator, sizeof(VmRefObj *) * pVm->nRefSize);
|
||||
if(pVm->apRefObj == 0) {
|
||||
/* Don't worry about freeing memory, everything will be released shortly */
|
||||
return SXERR_MEM;
|
||||
pVm->nRefSize = 0x10; /* Must be a power of two for fast arithemtic */
|
||||
pVm->apRefObj = (VmRefObj **)SyMemBackendAlloc(&pVm->sAllocator, sizeof(VmRefObj *) * pVm->nRefSize);
|
||||
if(pVm->apRefObj == 0) {
|
||||
/* Don't worry about freeing memory, everything will be released shortly */
|
||||
return SXERR_MEM;
|
||||
}
|
||||
/* Zero the reference table */
|
||||
SyZero(pVm->apRefObj, sizeof(VmRefObj *) * pVm->nRefSize);
|
||||
}
|
||||
/* Zero the reference table */
|
||||
SyZero(pVm->apRefObj, sizeof(VmRefObj *) * pVm->nRefSize);
|
||||
/* Register special functions first [i.e: print, json_encode(), func_get_args(), die, etc.] */
|
||||
rc = VmRegisterSpecialFunction(&(*pVm));
|
||||
if(rc != SXRET_OK) {
|
||||
@@ -2072,6 +2610,25 @@ static void VmFinalizeObjects(ph7_vm *pVm) {
|
||||
* stay installed; newly inserted closures are visited on the next pass. */
|
||||
pVm->hFunction.pCurrent = pNext;
|
||||
}
|
||||
/* Native declaration snapshots can be the last owners of objects.
|
||||
* Finalize them while callbacks, scope and indexed fields are live. */
|
||||
SyHashResetLoopCursor(&pVm->hClass);
|
||||
while((pFuncEntry = SyHashGetNextEntry(&pVm->hClass)) != 0) {
|
||||
ph7_class *pClass = (ph7_class *)pFuncEntry->pUserData;
|
||||
SyHashEntry_Pr *pNextClass = pVm->hClass.pCurrent;
|
||||
SyHashEntry *pAttrEntry;
|
||||
SyHashResetLoopCursor(&pClass->hAttr);
|
||||
while((pAttrEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) {
|
||||
ph7_class_attr *pAttr = (ph7_class_attr *)pAttrEntry->pUserData;
|
||||
SyHashEntry_Pr *pNextAttr = pClass->hAttr.pCurrent;
|
||||
ph7_value *pInitial = pAttr->pNativeInitial;
|
||||
if(pInitial && (pInitial->nType & (MEMOBJ_OBJ | MEMOBJ_HASHMAP)) == MEMOBJ_OBJ) {
|
||||
nFinalized |= PH7_ClassInstanceFinalize((ph7_class_instance *)pInitial->x.pOther);
|
||||
}
|
||||
pClass->hAttr.pCurrent = pNextAttr;
|
||||
}
|
||||
pVm->hClass.pCurrent = pNextClass;
|
||||
}
|
||||
/* Reacquire indexed values: a destructor may grow aMemObj. Arrays and
|
||||
* object attributes already store their contained values in this set.
|
||||
* Object-typed arrays carry MEMOBJ_OBJ too, but are not instances. */
|
||||
@@ -2096,6 +2653,20 @@ PH7_PRIVATE sxi32 PH7_VmRelease(ph7_vm *pVm) {
|
||||
/* No more user code can need reusable slots. Keep recycling enabled for
|
||||
* destructors above, but do not grow the free list during final reclaim. */
|
||||
pVm->nMagic = PH7_VM_STALE;
|
||||
/* Release declaration snapshots before indexed array storage and modules. */
|
||||
SyHashResetLoopCursor(&pVm->hClass);
|
||||
while((pFuncEntry = SyHashGetNextEntry(&pVm->hClass)) != 0) {
|
||||
ph7_class *pClass = (ph7_class *)pFuncEntry->pUserData;
|
||||
SyHashEntry *pAttrEntry;
|
||||
SyHashResetLoopCursor(&pClass->hAttr);
|
||||
while((pAttrEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) {
|
||||
ph7_class_attr *pAttr = (ph7_class_attr *)pAttrEntry->pUserData;
|
||||
if(pAttr->pNativeInitial) {
|
||||
PH7_MemObjRelease(pAttr->pNativeInitial);
|
||||
pAttr->pNativeInitial = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Captures have the same lifetime as installed runtime closures. Release
|
||||
* them while indexed storage and reference bookkeeping are still valid. */
|
||||
SyHashResetLoopCursor(&pVm->hFunction);
|
||||
@@ -2155,6 +2726,8 @@ static sxi32 VmInitCallContext(
|
||||
MemObjSetType(pRet, MEMOBJ_NULL);
|
||||
pOut->pRet = pRet;
|
||||
pOut->iFlags = iFlags;
|
||||
pOut->pThis = 0;
|
||||
pOut->bResultSet = FALSE;
|
||||
return SXRET_OK;
|
||||
}
|
||||
/*
|
||||
@@ -2192,6 +2765,51 @@ static void VmReleaseCallContext(ph7_context *pCtx) {
|
||||
SySetRelease(&pCtx->sChunk);
|
||||
}
|
||||
}
|
||||
/* Borrowed wrappers stay stable even if callback allocations grow aMemObj. */
|
||||
static sxi32 VmCallNativeMethod(ph7_vm *pVm, ph7_vm_func *pFunc,
|
||||
VmFrame *pFrame, ph7_class_instance *pThis, ph7_value *pResult) {
|
||||
ph7_context sCtx;
|
||||
ph7_value sThis;
|
||||
ph7_value *aValue, **apArg;
|
||||
sxu32 n, nArg = SySetUsed(&pFrame->sArg);
|
||||
sxi32 rc;
|
||||
aValue = (ph7_value *)SyMemBackendAlloc(&pVm->sAllocator,
|
||||
(nArg ? nArg : 1) * (sizeof(ph7_value) + sizeof(ph7_value *)));
|
||||
if(aValue == 0) {
|
||||
PH7_VmMemoryError(pVm);
|
||||
return PH7_ABORT;
|
||||
}
|
||||
apArg = (ph7_value **)&aValue[nArg];
|
||||
for(n = 0; n < nArg; n++) {
|
||||
VmSlot *pSlot = (VmSlot *)SySetAt(&pFrame->sArg, n);
|
||||
PH7_MemObjInit(pVm, &aValue[n]);
|
||||
PH7_MemObjLoad((ph7_value *)SySetAt(&pVm->aMemObj, pSlot->nIdx), &aValue[n]);
|
||||
apArg[n] = &aValue[n];
|
||||
}
|
||||
PH7_MemObjInit(pVm, &sThis);
|
||||
if(pThis) {
|
||||
sThis.x.pOther = pThis;
|
||||
MemObjSetType(&sThis, MEMOBJ_OBJ);
|
||||
/* Borrowed: the active frame already retains the instance. */
|
||||
}
|
||||
VmInitCallContext(&sCtx, pVm, &pFunc->sNative, pResult, 0);
|
||||
sCtx.pThis = pThis && (((ph7_class_method *)pFunc)->iFlags & PH7_CLASS_ATTR_STATIC) == 0 ? &sThis : 0;
|
||||
rc = pFunc->sNative.xFunc(&sCtx, (int)nArg, apArg);
|
||||
VmReleaseCallContext(&sCtx);
|
||||
for(n = 0; n < nArg; n++) {
|
||||
PH7_MemObjRelease(&aValue[n]);
|
||||
}
|
||||
SyMemBackendFree(&pVm->sAllocator, aValue);
|
||||
/* Do not enforce a result contract on an exception or an interrupted call. */
|
||||
if(rc == PH7_OK) {
|
||||
if(sCtx.bResultSet) {
|
||||
VmCheckReturn(pVm, pFunc, pResult);
|
||||
} else {
|
||||
VmCheckMissingReturn(pVm, pFunc);
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
/*
|
||||
* Release a ph7_value allocated from the body of a foreign function.
|
||||
* Refer to [ph7_context_release_value()] for additional information.
|
||||
@@ -2949,9 +3567,7 @@ Resume:
|
||||
* Keep the call's NULL result unconverted, unlike explicit return. */
|
||||
if(!pInstr->iP1 && pInstr->iP2 && (pEntryFrame->iFlags & VM_FRAME_ACTIVE) && (pEntryFrame->iFlags & VM_FRAME_THROW) == 0) {
|
||||
ph7_vm_func *pFunc = (ph7_vm_func *)pEntryFrame->pUserData;
|
||||
if(pFunc->nType && (pFunc->nType & MEMOBJ_VOID) == 0) {
|
||||
PH7_VmThrowError(&(*pVm), PH7_CTX_WARNING, "Missing return value in closure/method returning non-void");
|
||||
}
|
||||
VmCheckMissingReturn(pVm, pFunc);
|
||||
}
|
||||
if(pInstr->iP1) {
|
||||
if(pTos < pStack) {
|
||||
@@ -6201,8 +6817,9 @@ Resume:
|
||||
/* Mark current frame as active */
|
||||
pFrame->iFlags |= VM_FRAME_ACTIVE;
|
||||
/* Allocate a new operand stack and evaluate the function body */
|
||||
pFrameStack = VmNewOperandStack(&(*pVm), SySetUsed(&pVmFunc->aByteCode));
|
||||
if(pFrameStack == 0) {
|
||||
pFrameStack = pVmFunc->sNative.xFunc ? 0 :
|
||||
VmNewOperandStack(&(*pVm), SySetUsed(&pVmFunc->aByteCode));
|
||||
if(pFrameStack == 0 && pVmFunc->sNative.xFunc == 0) {
|
||||
/* Raise exception: Out of memory */
|
||||
PH7_VmMemoryError(&(*pVm));
|
||||
}
|
||||
@@ -6211,7 +6828,11 @@ Resume:
|
||||
SySetPut(&pVm->aSelf, (const void *)&pSelf);
|
||||
}
|
||||
/* Execute function body */
|
||||
rc = VmByteCodeExec(&(*pVm), (VmInstr *)SySetBasePtr(&pVmFunc->aByteCode), pFrameStack, -1, pTos, FALSE);
|
||||
if(pVmFunc->sNative.xFunc) {
|
||||
rc = VmCallNativeMethod(pVm, pVmFunc, pFrame, pThis, pTos);
|
||||
} else {
|
||||
rc = VmByteCodeExec(&(*pVm), (VmInstr *)SySetBasePtr(&pVmFunc->aByteCode), pFrameStack, -1, pTos, FALSE);
|
||||
}
|
||||
if(pSelf) {
|
||||
/* Pop class name */
|
||||
(void)SySetPop(&pVm->aSelf);
|
||||
@@ -6427,28 +7048,7 @@ Unwind:
|
||||
if(bReturnValue && (pEntryFrame->iFlags & VM_FRAME_ACTIVE) &&
|
||||
(iExecFlags & (VM_EXEC_HANDLER | VM_EXEC_EVAL)) == 0) {
|
||||
ph7_vm_func *pFunc = (ph7_vm_func *)pEntryFrame->pUserData;
|
||||
if(pFunc->nType & MEMOBJ_NAMED) {
|
||||
PH7_VmTypeClass(pVm, pFunc->nType, TRUE);
|
||||
if(PH7_VmTypeCheck(&sTransfer, pFunc->nType) != SXRET_OK) {
|
||||
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
|
||||
}
|
||||
PH7_MemObjSetDeclaredType(&sTransfer, pFunc->nType);
|
||||
} else if(pFunc->nType && (pFunc->nType & MEMOBJ_MIXED) == 0) {
|
||||
if(pFunc->nType & MEMOBJ_VOID) {
|
||||
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Return with a value in closure/method returning void");
|
||||
} else if(pFunc->nType != sTransfer.nType) {
|
||||
if(PH7_CheckVarCompat(&sTransfer, pFunc->nType) == SXRET_OK) {
|
||||
ProcMemObjCast xCast = PH7_MemObjCastMethod(pFunc->nType);
|
||||
xCast(&sTransfer);
|
||||
} else if((pFunc->iFlags & MEMOBJ_HASHMAP) && (sTransfer.nType & MEMOBJ_HASHMAP)) {
|
||||
if(PH7_HashmapCast(&sTransfer, pFunc->iFlags ^ MEMOBJ_HASHMAP) != SXRET_OK) {
|
||||
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
|
||||
}
|
||||
} else {
|
||||
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
|
||||
}
|
||||
}
|
||||
}
|
||||
VmCheckReturn(pVm, pFunc, &sTransfer);
|
||||
}
|
||||
rc = (iExecFlags & (VM_EXEC_INCLUDE | VM_EXEC_HANDLER)) ?
|
||||
(bReturnValue ? VM_INCLUDE_RETURN : VM_RETURN_VOID) : SXRET_OK;
|
||||
@@ -7579,45 +8179,58 @@ static int VmQueryInterfaceSet(ph7_class *pClass, SySet *pSet) {
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
/*
|
||||
* This function returns TRUE if the given class (first argument)
|
||||
* is an instance of the main class (second argument).
|
||||
* Otherwise FALSE is returned.
|
||||
*/
|
||||
static int VmInstanceOf(ph7_vm *pVm, ph7_class *pThis, ph7_class *pClass) {
|
||||
/* One nominal compatibility implementation: SXRET_OK matches, SXERR_NOMATCH
|
||||
* does not. Registration propagates allocation failures while ordinary
|
||||
* execution keeps its existing fatal OOM behavior. */
|
||||
static sxi32 VmInstanceOfResult(ph7_vm *pVm, ph7_class *pThis, ph7_class *pClass, sxi32 bRecoverable) {
|
||||
SySet sInterfaces;
|
||||
sxi32 rc;
|
||||
if(!pThis || !pClass) {
|
||||
return FALSE;
|
||||
return SXERR_NOMATCH;
|
||||
}
|
||||
if(pThis == pClass) {
|
||||
/* Instance of the same class */
|
||||
return TRUE;
|
||||
return SXRET_OK;
|
||||
}
|
||||
if(!(pThis->iFlags & PH7_CLASS_INTERFACE)) {
|
||||
sxu32 i;
|
||||
if(PH7_ClassBuildMro(pVm, pThis) != SXRET_OK) {
|
||||
rc = PH7_ClassBuildMro(pVm, pThis);
|
||||
if(rc != SXRET_OK) {
|
||||
if(bRecoverable) {
|
||||
return rc;
|
||||
}
|
||||
PH7_VmThrowError(pVm, PH7_CTX_ERR, "PH7 is running out-of-memory");
|
||||
}
|
||||
for(i = 1; i < SySetUsed(&pThis->aMro); i++) {
|
||||
if(*(ph7_class **)SySetAt(&pThis->aMro, i) == pClass) {
|
||||
return TRUE;
|
||||
return SXRET_OK;
|
||||
}
|
||||
}
|
||||
if(!(pClass->iFlags & PH7_CLASS_INTERFACE)) {
|
||||
return FALSE;
|
||||
return SXERR_NOMATCH;
|
||||
}
|
||||
}
|
||||
SySetInit(&sInterfaces, &pVm->sAllocator, sizeof(ph7_class *));
|
||||
rc = PH7_ClassCollectInterfaces(pVm, pThis, &sInterfaces);
|
||||
if(rc != SXRET_OK) {
|
||||
SySetRelease(&sInterfaces);
|
||||
if(bRecoverable) {
|
||||
return rc;
|
||||
}
|
||||
PH7_VmMemoryError(pVm);
|
||||
return FALSE;
|
||||
return SXERR_NOMATCH;
|
||||
}
|
||||
rc = VmQueryInterfaceSet(pClass, &sInterfaces);
|
||||
SySetRelease(&sInterfaces);
|
||||
return rc;
|
||||
return rc ? SXRET_OK : SXERR_NOMATCH;
|
||||
}
|
||||
/*
|
||||
* This function returns TRUE if the given class (first argument)
|
||||
* is an instance of the main class (second argument).
|
||||
* Otherwise FALSE is returned.
|
||||
*/
|
||||
static int VmInstanceOf(ph7_vm *pVm, ph7_class *pThis, ph7_class *pClass) {
|
||||
return VmInstanceOfResult(pVm, pThis, pClass, FALSE) == SXRET_OK;
|
||||
}
|
||||
/*
|
||||
* This function returns TRUE if the given class (first argument)
|
||||
@@ -11457,7 +12070,9 @@ PH7_PRIVATE sxi32 PH7_VmRefObjInstall(
|
||||
}
|
||||
if(pMapEntry) {
|
||||
/* Address of the hashmap node [i.e: Array entry] */
|
||||
SySetPut(&pRef->aArrEntries, (const void *)&pMapEntry);
|
||||
if(SySetPut(&pRef->aArrEntries, (const void *)&pMapEntry) != SXRET_OK) {
|
||||
return SXERR_MEM;
|
||||
}
|
||||
}
|
||||
return SXRET_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user