Add variadic support for native class methods
Build / AerScript (push) Successful in 47s

This commit is contained in:
2026-11-21 17:48:41 +01:00
parent bcfa4c6163
commit 1d9bf543b6
3 changed files with 84 additions and 13 deletions
+79 -10
View File
@@ -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,13 +1448,17 @@ static ph7_vm_func *VmOverload(
if(k == j || aRank[k * nCols + nPassed] == 0) {
continue;
}
{
sxu32 nFirstArgs = SySetUsed(&apSet[k]->aArgs);
sxu32 nSecondArgs = SySetUsed(&apSet[j]->aArgs);
if(VmOverloadDominates(pVm, aArg, apSet[k], apSet[j], nPassed,
SySetUsed(&apSet[k]->aArgs) - nPassed,
SySetUsed(&apSet[j]->aArgs) - nPassed)) {
nFirstArgs > nPassed ? nFirstArgs - nPassed : 0,
nSecondArgs > nPassed ? nSecondArgs - nPassed : 0)) {
bDominated = 1;
break;
}
}
}
if(!bDominated) {
if(iTarget != SXU32_HIGH) {
/* More than one candidate is equally good */
@@ -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;
+1
View File
@@ -609,6 +609,7 @@ typedef struct ph7_class_def {
#define PH7_MEMBER_PUBLIC 0x10
#define PH7_MEMBER_PROTECTED 0x20
#define PH7_MEMBER_PRIVATE 0x40
#define PH7_MEMBER_VARIADIC 0x80 /* Method accepts a variadic argument tail past aParam */
#define PH7_PARAM_REF 0x01
#define PH7_DECL_FINAL 0x01
#define PH7_DECL_INTERFACE 0x02
+1
View File
@@ -998,6 +998,7 @@ struct ph7_vm_func_closure_env {
#define VM_FUNC_CLASS_METHOD 0x008 /* VM function is in fact a class method */
#define VM_FUNC_CLOSURE 0x010 /* VM function is a closure */
#define VM_FUNC_ARG_IGNORE 0x020 /* Do not install argument in the current frame */
#define VM_FUNC_VARIADIC 0x040 /* Native method accepts a variadic argument tail */
/*
* Each user defined function is parsed out and stored in an instance
* of the following structure.