This commit is contained in:
+82
-13
@@ -955,6 +955,11 @@ static sxi32 VmOverloadRankArgs(ph7_vm *pVm, ph7_vm_func *pFunc, ph7_value *aArg
|
||||
sxi32 nRank;
|
||||
aFormal = (ph7_vm_func_arg *)SySetBasePtr(&pFunc->aArgs);
|
||||
for(i = 0 ; i < nArg ; i++) {
|
||||
if(i >= SySetUsed(&pFunc->aArgs)) {
|
||||
/* A variadic tail has no formal to validate against. */
|
||||
aRank[i] = 0;
|
||||
continue;
|
||||
}
|
||||
if((aFormal[i].iFlags & VM_FUNC_ARG_BY_REF) && aArg[i].nIdx == SXU32_HIGH) {
|
||||
return FALSE;
|
||||
}
|
||||
@@ -1032,15 +1037,33 @@ static sxi32 VmOverloadArgCompare(ph7_vm *pVm, ph7_value *pArg, ph7_vm_func_arg
|
||||
}
|
||||
/*
|
||||
* Return TRUE if the first candidate is a strictly better overload than the
|
||||
* second one. When all argument ranks are equal, prefer fewer omitted defaults.
|
||||
* second one. Argument types are compared first; a formal that types an
|
||||
* argument outranks a variadic tail that merely accepts it. Only when every
|
||||
* argument is a tie do arity and variadic status break the tie, keeping the
|
||||
* relation a strict order so it cannot create dominance cycles.
|
||||
*/
|
||||
static sxi32 VmOverloadDominates(ph7_vm *pVm, ph7_value *aArg, ph7_vm_func *pFirst, ph7_vm_func *pSecond, sxu32 nArg, sxu32 nFirstDefaults, sxu32 nSecondDefaults) {
|
||||
sxu32 i;
|
||||
sxu32 i, nFirstArgs = SySetUsed(&pFirst->aArgs), nSecondArgs = SySetUsed(&pSecond->aArgs);
|
||||
sxi32 bBetter = 0;
|
||||
ph7_vm_func_arg *aFirst = (ph7_vm_func_arg *)SySetBasePtr(&pFirst->aArgs);
|
||||
ph7_vm_func_arg *aSecond = (ph7_vm_func_arg *)SySetBasePtr(&pSecond->aArgs);
|
||||
for(i = 0 ; i < nArg ; i++) {
|
||||
sxi32 nOrder = VmOverloadArgCompare(pVm, &aArg[i], &aFirst[i], &aSecond[i]);
|
||||
sxi32 nOrder;
|
||||
if(i >= nFirstArgs) {
|
||||
/* First has no formal here: it can only dominate when the second
|
||||
* has none either (both variadic tails), never when the second
|
||||
* types the argument. */
|
||||
if(i >= nSecondArgs) {
|
||||
continue;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
if(i >= nSecondArgs) {
|
||||
/* First types the argument, the second only catches it in its tail. */
|
||||
bBetter = 1;
|
||||
continue;
|
||||
}
|
||||
nOrder = VmOverloadArgCompare(pVm, &aArg[i], &aFirst[i], &aSecond[i]);
|
||||
if(nOrder > 0) {
|
||||
/* Worse or incomparable on any argument prevents dominance. */
|
||||
return FALSE;
|
||||
@@ -1052,6 +1075,10 @@ static sxi32 VmOverloadDominates(ph7_vm *pVm, ph7_value *aArg, ph7_vm_func *pFir
|
||||
if(bBetter) {
|
||||
return TRUE;
|
||||
}
|
||||
/* Non-variadic matches outrank variadic ones on an otherwise equal input. */
|
||||
if(((pFirst->iFlags ^ pSecond->iFlags) & VM_FUNC_VARIADIC) != 0) {
|
||||
return (pSecond->iFlags & VM_FUNC_VARIADIC) != 0;
|
||||
}
|
||||
return nFirstDefaults < nSecondDefaults;
|
||||
}
|
||||
/* Forward declaration */
|
||||
@@ -1313,7 +1340,21 @@ static ph7_vm_func *VmOverload(
|
||||
for(iMethod = 0; iMethod < SySetUsed(&sMethods); iMethod++) {
|
||||
pLink = *(ph7_vm_func **)SySetAt(&sMethods, iMethod);
|
||||
iArgs = SySetUsed(&pLink->aArgs);
|
||||
if(nPassed == iArgs) {
|
||||
if(pLink->iFlags & VM_FUNC_VARIADIC) {
|
||||
/* Variadic: the required prefix must be present, the tail is unbounded.
|
||||
* Registration guarantees defaults follow required formals, so
|
||||
* counting formals without a default yields the prefix length. */
|
||||
sxu32 nRequired = 0;
|
||||
pFuncArg = (ph7_vm_func_arg *)SySetBasePtr(&pLink->aArgs);
|
||||
for(j = 0 ; j < iArgs ; ++j) {
|
||||
if(!VmArgHasDefault(&pFuncArg[j])) {
|
||||
++nRequired;
|
||||
}
|
||||
}
|
||||
if(nPassed < nRequired) {
|
||||
continue;
|
||||
}
|
||||
} else if(nPassed == iArgs) {
|
||||
/* Exact amount of parameters, a candidate to call */
|
||||
} else if(nPassed < iArgs) {
|
||||
/* Fewer parameters passed, check that every missing one has a default */
|
||||
@@ -1407,11 +1448,15 @@ static ph7_vm_func *VmOverload(
|
||||
if(k == j || aRank[k * nCols + nPassed] == 0) {
|
||||
continue;
|
||||
}
|
||||
if(VmOverloadDominates(pVm, aArg, apSet[k], apSet[j], nPassed,
|
||||
SySetUsed(&apSet[k]->aArgs) - nPassed,
|
||||
SySetUsed(&apSet[j]->aArgs) - nPassed)) {
|
||||
bDominated = 1;
|
||||
break;
|
||||
{
|
||||
sxu32 nFirstArgs = SySetUsed(&apSet[k]->aArgs);
|
||||
sxu32 nSecondArgs = SySetUsed(&apSet[j]->aArgs);
|
||||
if(VmOverloadDominates(pVm, aArg, apSet[k], apSet[j], nPassed,
|
||||
nFirstArgs > nPassed ? nFirstArgs - nPassed : 0,
|
||||
nSecondArgs > nPassed ? nSecondArgs - nPassed : 0)) {
|
||||
bDominated = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!bDominated) {
|
||||
@@ -1621,7 +1666,8 @@ static void VmNativeClassFree(ph7_vm *pVm, ph7_class *pClass) {
|
||||
static sxi32 VmNativeProtection(sxu32 iFlags) {
|
||||
sxu32 iProtection = iFlags & (PH7_MEMBER_PUBLIC | PH7_MEMBER_PROTECTED | PH7_MEMBER_PRIVATE);
|
||||
if(iFlags & ~(PH7_MEMBER_STATIC | PH7_MEMBER_FINAL | PH7_MEMBER_VIRTUAL |
|
||||
PH7_MEMBER_PUBLIC | PH7_MEMBER_PROTECTED | PH7_MEMBER_PRIVATE)) {
|
||||
PH7_MEMBER_PUBLIC | PH7_MEMBER_PROTECTED | PH7_MEMBER_PRIVATE |
|
||||
PH7_MEMBER_VARIADIC)) {
|
||||
return 0;
|
||||
}
|
||||
if((iFlags & (PH7_MEMBER_FINAL | PH7_MEMBER_VIRTUAL)) ==
|
||||
@@ -1858,6 +1904,11 @@ static sxi32 VmPrepareNativeClass(ph7_vm *pVm, const ph7_class_def *pDef) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Fail;
|
||||
}
|
||||
/* The variadic bit is private to the VM function: it must not be
|
||||
* confused with PH7_CLASS_ATTR_* on the method itself. */
|
||||
if(pDefMeth->iFlags & PH7_MEMBER_VARIADIC) {
|
||||
pMeth->sFunc.iFlags |= VM_FUNC_VARIADIC;
|
||||
}
|
||||
/* Each prepared method owns its VM name, including overloads. */
|
||||
if(pEntry) {
|
||||
char *zName = SyMemBackendStrDup(&pVm->sAllocator, pMeth->sVmName.zString, pMeth->sVmName.nByte);
|
||||
@@ -1899,6 +1950,23 @@ static sxi32 VmPrepareNativeClass(ph7_vm *pVm, const ph7_class_def *pDef) {
|
||||
rc = PH7_CORRUPT;
|
||||
goto Fail;
|
||||
}
|
||||
/* Every native declaration must put required formals before defaulted
|
||||
* ones. Validate before any default snapshot is taken so a rejected
|
||||
* descriptor stays atomic (Fail releases the partially built class). */
|
||||
{
|
||||
sxu32 bSeenDefault = 0;
|
||||
for(i = 0; i < pDefMeth->nParam; i++) {
|
||||
const ph7_param_def *pDefArg = &pDefMeth->aParam[i];
|
||||
sxi32 bHasDefault = (pDefArg->pDefault || pDefArg->xDefault) ? TRUE : FALSE;
|
||||
if(bSeenDefault && !bHasDefault) {
|
||||
rc = PH7_CORRUPT;
|
||||
goto Fail;
|
||||
}
|
||||
if(bHasDefault) {
|
||||
bSeenDefault = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(i = 0; i < pDefMeth->nParam; i++) {
|
||||
const ph7_param_def *pDefArg = &pDefMeth->aParam[i];
|
||||
ph7_vm_func_arg sArg;
|
||||
@@ -7031,11 +7099,12 @@ Resume:
|
||||
} else {
|
||||
char zName[32];
|
||||
SyString sName;
|
||||
/* Set a dummy name */
|
||||
/* Set a dummy name. A variadic tail slot does not
|
||||
* exist yet: allocate it rather than only looking
|
||||
* it up, and duplicate the name for frame storage. */
|
||||
sName.nByte = SyBufferFormat(zName, sizeof(zName), "[%u]apArg", n);
|
||||
sName.zString = zName;
|
||||
/* Anonymous argument */
|
||||
pObj = VmExtractMemObj(&(*pVm), &sName, TRUE);
|
||||
pObj = VmCreateMemObj(&(*pVm), &sName, TRUE);
|
||||
}
|
||||
if(pObj) {
|
||||
sxu32 nObjIdx = pObj->nIdx;
|
||||
|
||||
Reference in New Issue
Block a user