Extend native class API to support full AerScript declarations
Build / AerScript (push) Successful in 18s

This commit is contained in:
2026-11-18 20:57:39 +01:00
parent 61006fe848
commit 58f604aea5
10 changed files with 1556 additions and 328 deletions
+108
View File
@@ -463,6 +463,9 @@ static sxi32 ProcessSourceFile(
int iFileDir, rc; int iFileDir, rc;
char *pFileDir, fFilePath[PATH_MAX + 1]; char *pFileDir, fFilePath[PATH_MAX + 1];
char pFilePath[PATH_MAX + 1]; char pFilePath[PATH_MAX + 1];
if(pVm->bNativeRegister) {
return SXERR_LOCKED;
}
/* Track the initialized VM before compilation can trigger library shutdown. */ /* Track the initialized VM before compilation can trigger library shutdown. */
MACRO_LD_PUSH(pEngine->pVms, pVm); MACRO_LD_PUSH(pEngine->pVms, pVm);
pEngine->iVm++; pEngine->iVm++;
@@ -611,6 +614,9 @@ int ph7_vm_exec(ph7_vm *pVm, int *pExitStatus) {
if(PH7_VM_MISUSE(pVm)) { if(PH7_VM_MISUSE(pVm)) {
return PH7_CORRUPT; return PH7_CORRUPT;
} }
if(pVm->bNativeRegister) {
return SXERR_LOCKED;
}
/* Execute PH7 byte-code */ /* Execute PH7 byte-code */
rc = PH7_VmByteCodeExec(&(*pVm)); rc = PH7_VmByteCodeExec(&(*pVm));
if(pExitStatus) { if(pExitStatus) {
@@ -630,6 +636,9 @@ int ph7_vm_reset(ph7_vm *pVm) {
if(PH7_VM_MISUSE(pVm)) { if(PH7_VM_MISUSE(pVm)) {
return PH7_CORRUPT; return PH7_CORRUPT;
} }
if(pVm->bNativeRegister) {
return SXERR_LOCKED;
}
rc = PH7_VmReset(&(*pVm)); rc = PH7_VmReset(&(*pVm));
return rc; return rc;
} }
@@ -644,6 +653,9 @@ int ph7_vm_release(ph7_vm *pVm) {
if(PH7_VM_MISUSE(pVm)) { if(PH7_VM_MISUSE(pVm)) {
return PH7_CORRUPT; return PH7_CORRUPT;
} }
if(pVm->bNativeRegister) {
return SXERR_LOCKED;
}
pEngine = pVm->pEngine; pEngine = pVm->pEngine;
rc = PH7_VmRelease(&(*pVm)); rc = PH7_VmRelease(&(*pVm));
if(rc == PH7_OK) { if(rc == PH7_OK) {
@@ -686,6 +698,102 @@ int ph7_create_class(ph7_vm *pVm, const ph7_class_def *pDef) {
} }
return PH7_VmCreateNativeClass(pVm, pDef); return PH7_VmCreateNativeClass(pVm, pDef);
} }
int ph7_context_arg_value(ph7_context *pCtx, unsigned int nArg, ph7_value *pOut) {
VmFrame *pFrame = pCtx ? (VmFrame *)pCtx->pNativeFrame : 0;
VmSlot *pSlot;
if(pFrame == 0 || pOut == 0 || pOut->pVm != pCtx->pVm ||
nArg >= SySetUsed(&pFrame->sArg)) {
return PH7_CORRUPT;
}
pSlot = (VmSlot *)SySetAt(&pFrame->sArg, nArg);
return PH7_MemObjCopy((ph7_value *)SySetAt(&pCtx->pVm->aMemObj, pSlot->nIdx), pOut);
}
int ph7_context_arg_store(ph7_context *pCtx, unsigned int nArg, ph7_value *pValue) {
VmFrame *pFrame = pCtx ? (VmFrame *)pCtx->pNativeFrame : 0;
VmSlot *pSlot;
ph7_vm_func *pFunc;
ph7_value sChecked;
sxi32 rc;
if(pFrame == 0 || pValue == 0 || pValue->pVm != pCtx->pVm ||
nArg >= SySetUsed(&pFrame->sArg)) {
return PH7_CORRUPT;
}
pFunc = (ph7_vm_func *)pFrame->pUserData;
PH7_MemObjInit(pCtx->pVm, &sChecked);
if(pFunc && nArg < SySetUsed(&pFunc->aArgs)) {
ph7_vm_func_arg *pFormal = (ph7_vm_func_arg *)SySetAt(&pFunc->aArgs, nArg);
MemObjSetType(&sChecked, pFormal->nType & MEMOBJ_TYPEMASK);
PH7_MemObjSetDeclaredType(&sChecked, pFormal->nType);
}
rc = PH7_MemObjSafeStore(pValue, &sChecked);
if(rc == PH7_OK && pCtx->pVm->iPendingTransfer == 0) {
pSlot = (VmSlot *)SySetAt(&pFrame->sArg, nArg);
rc = PH7_MemObjSafeStore(&sChecked,
(ph7_value *)SySetAt(&pCtx->pVm->aMemObj, pSlot->nIdx));
}
PH7_MemObjRelease(&sChecked);
return pCtx->pVm->iPendingTransfer ? pCtx->pVm->iPendingTransfer : rc;
}
int ph7_context_arg_bind(ph7_context *pCtx, unsigned int nArg, ph7_value *pOut) {
int rc = ph7_context_arg_value(pCtx, nArg, pOut);
if(rc == PH7_OK) {
VmFrame *pFrame = (VmFrame *)pCtx->pNativeFrame;
VmSlot *pSlot = (VmSlot *)SySetAt(&pFrame->sArg, nArg);
pOut->nIdx = pSlot->nIdx;
pOut->iFlags |= MEMOBJ_INDEX_HANDLE;
}
return rc;
}
int ph7_context_attr_value(ph7_context *pCtx, ph7_value *pObject,
const char *zClass, const char *zName, ph7_value *pOut) {
return PH7_VmContextAttr(pCtx, pObject, zClass, zName, pOut, FALSE);
}
int ph7_context_attr_store(ph7_context *pCtx, ph7_value *pObject,
const char *zClass, const char *zName, ph7_value *pValue) {
return PH7_VmContextAttr(pCtx, pObject, zClass, zName, pValue, TRUE);
}
int ph7_context_call(ph7_context *pCtx, ph7_value *pCallable,
int nArg, ph7_value **apArg, ph7_value *pResult) {
ph7_value sResult;
int rc;
if(pCtx == 0 || pCallable == 0 || pCallable->pVm != pCtx->pVm ||
pResult == 0 || pResult->pVm != pCtx->pVm || nArg < 0 || (nArg && apArg == 0)) {
return PH7_CORRUPT;
}
PH7_MemObjInit(pCtx->pVm, &sResult);
rc = PH7_VmCallUserFunction(pCtx->pVm, pCallable, nArg, apArg, &sResult);
if(rc == PH7_OK || rc == PH7_EXCEPTION) {
int copy_rc = PH7_MemObjCopy(&sResult, pResult);
if(copy_rc != PH7_OK) rc = copy_rc;
}
PH7_MemObjRelease(&sResult);
return rc;
}
int ph7_context_parent(ph7_context *pCtx, const char *zMethod,
int nArg, ph7_value **apArg, ph7_value *pResult) {
ph7_value sResult;
int rc;
if(pCtx == 0 || pResult == 0 || pResult->pVm != pCtx->pVm) {
return PH7_CORRUPT;
}
PH7_MemObjInit(pCtx->pVm, &sResult);
rc = PH7_VmContextParent(pCtx, zMethod, nArg, apArg, &sResult);
if(rc == PH7_OK || rc == PH7_EXCEPTION) {
int copy_rc = PH7_MemObjCopy(&sResult, pResult);
if(copy_rc != PH7_OK) rc = copy_rc;
}
PH7_MemObjRelease(&sResult);
return rc;
}
int ph7_context_var_value(ph7_context *pCtx, const char *zName, ph7_value *pOut) {
return PH7_VmContextVar(pCtx, zName, pOut, FALSE);
}
int ph7_context_var_store(ph7_context *pCtx, const char *zName, ph7_value *pValue) {
return PH7_VmContextVar(pCtx, zName, pValue, TRUE);
}
int ph7_context_status(ph7_context *pCtx) {
return pCtx ? (pCtx->pVm->iPendingTransfer ? pCtx->pVm->iPendingTransfer : PH7_OK) : PH7_CORRUPT;
}
/* /*
* [CAPIREF: ph7_delete_function()] * [CAPIREF: ph7_delete_function()]
* Please refer to the official documentation for function purpose and expected parameters. * Please refer to the official documentation for function purpose and expected parameters.
+21
View File
@@ -3434,6 +3434,9 @@ PH7_PRIVATE sxi32 PH7_InputFormat(
} else { } else {
c = ph7_value_to_int(pArg); c = ph7_value_to_int(pArg);
} }
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* NUL byte is an acceptable value */ /* NUL byte is an acceptable value */
zWorker[0] = (char)c; zWorker[0] = (char)c;
length = (int)sizeof(char); length = (int)sizeof(char);
@@ -3446,6 +3449,9 @@ PH7_PRIVATE sxi32 PH7_InputFormat(
} else { } else {
zBuf = (char *)ph7_value_to_string(pArg, &length); zBuf = (char *)ph7_value_to_string(pArg, &length);
} }
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(length < 1) { if(length < 1) {
zBuf = " "; zBuf = " ";
length = (int)sizeof(char); length = (int)sizeof(char);
@@ -3467,6 +3473,9 @@ PH7_PRIVATE sxi32 PH7_InputFormat(
} else { } else {
iVal = ph7_value_to_int64(pArg); iVal = ph7_value_to_int64(pArg);
} }
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Limit the precision to prevent overflowing buf[] during conversion */ /* Limit the precision to prevent overflowing buf[] during conversion */
if(precision > PH7_FMT_BUFSIZ - 40) { if(precision > PH7_FMT_BUFSIZ - 40) {
precision = PH7_FMT_BUFSIZ - 40; precision = PH7_FMT_BUFSIZ - 40;
@@ -3560,6 +3569,9 @@ PH7_PRIVATE sxi32 PH7_InputFormat(
} else { } else {
realvalue = ph7_value_to_double(pArg); realvalue = ph7_value_to_double(pArg);
} }
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(precision < 0) { if(precision < 0) {
precision = 6; /* Set default precision */ precision = 6; /* Set default precision */
} }
@@ -4468,6 +4480,9 @@ static int PH7_builtin_str_shuffle(ph7_context *pCtx, int nArg, ph7_value **apAr
} }
/* Extract the target string */ /* Extract the target string */
zString = ph7_value_to_string(apArg[0], &nLen); zString = ph7_value_to_string(apArg[0], &nLen);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nLen < 1) { if(nLen < 1) {
/* Nothing to shuffle */ /* Nothing to shuffle */
ph7_result_string(pCtx, "", 0); ph7_result_string(pCtx, "", 0);
@@ -5124,6 +5139,9 @@ static int PH7_builtin_strtok(ph7_context *pCtx, int nArg, ph7_value **apArg) {
/* Extract the mask */ /* Extract the mask */
zMask = ph7_value_to_string(apArg[0], &nMasklen); zMask = ph7_value_to_string(apArg[0], &nMasklen);
} }
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nMasklen < 1) { if(nMasklen < 1) {
/* Invalid mask, return FALSE */ /* Invalid mask, return FALSE */
ph7_context_free_chunk(pCtx, (void *)pAux->zDup); ph7_context_free_chunk(pCtx, (void *)pAux->zDup);
@@ -5157,6 +5175,9 @@ static int PH7_builtin_strtok(ph7_context *pCtx, int nArg, ph7_value **apArg) {
} }
/* Extract the mask */ /* Extract the mask */
zMask = ph7_value_to_string(apArg[1], &nMasklen); zMask = ph7_value_to_string(apArg[1], &nMasklen);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nMasklen < 1) { if(nMasklen < 1) {
/* Set a default mask */ /* Set a default mask */
#define TOK_MASK " \n\t\r\f" #define TOK_MASK " \n\t\r\f"
+10 -18
View File
@@ -3765,14 +3765,15 @@ static sxi32 PH7_GenStateCompileClassConstant(ph7_gen_state *pGen, sxi32 iProtec
SyString *pName; SyString *pName;
sxi32 rc; sxi32 rc;
/* Extract visibility level */ /* Extract visibility level */
iProtection = PH7_GetProtectionLevel(iProtection); iProtection = PH7_ClassConstantProtection(pClass, PH7_GetProtectionLevel(iProtection));
pGen->pIn++; /* Jump the 'const' keyword */ pGen->pIn++; /* Jump the 'const' keyword */
loop: loop:
/* Mark as constant */ /* Mark as constant */
iFlags |= PH7_CLASS_ATTR_CONSTANT; iFlags |= PH7_CLASS_ATTR_CONSTANT;
if(pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & PH7_TK_ID) == 0) { if(pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & PH7_TK_ID) == 0) {
/* Invalid constant name */ /* Invalid constant name */
PH7_GenCompileError(pGen, E_ERROR, nLine, "Invalid constant name '%z' in class '%z'", &pGen->pIn->sData, pClass->sName); PH7_GenCompileError(pGen, E_ERROR, nLine, "Invalid constant name '%z' in class '%z'",
pGen->pIn < pGen->pEnd ? &pGen->pIn->sData : &pClass->sName, &pClass->sName);
} }
/* Peek constant name */ /* Peek constant name */
pName = &pGen->pIn->sData; pName = &pGen->pIn->sData;
@@ -3964,26 +3965,17 @@ static sxi32 PH7_GenStateCompileClassMethod(
/* Peek method name */ /* Peek method name */
pName = &pGen->pIn->sData; pName = &pGen->pIn->sData;
nLine = pGen->pIn->nLine; nLine = pGen->pIn->nLine;
if(((pName->nByte == sizeof("__construct") - 1 && {
SyMemcmp(pName->zString, "__construct", sizeof("__construct") - 1) == 0) || const char *zError = PH7_ClassMethodDeclarationError(pClass, pName, nType,
(pName->nByte == sizeof("__destruct") - 1 && iProtection, iFlags);
SyMemcmp(pName->zString, "__destruct", sizeof("__destruct") - 1) == 0)) && nType != MEMOBJ_VOID) { if(zError) {
PH7_GenCompileError(pGen, E_ERROR, nLine, "Method '%z::%z()' must return void", &pClass->sName, pName); PH7_GenCompileError(pGen, E_ERROR, nLine, zError, &pClass->sName, pName);
}
} }
/* Jump the method name */ /* Jump the method name */
pGen->pIn++; pGen->pIn++;
if(iFlags & PH7_CLASS_ATTR_VIRTUAL) { if(iFlags & PH7_CLASS_ATTR_VIRTUAL) {
/* Virtual method */ /* Virtual method */
if(iProtection == PH7_CLASS_PROT_PRIVATE) {
PH7_GenCompileError(pGen, E_ERROR, nLine,
"Virtual method '%z::%z()' cannot be declared private",
&pClass->sName, pName);
}
if((pClass->iFlags & PH7_CLASS_VIRTUAL) == 0) {
PH7_GenCompileError(pGen, E_ERROR, nLine,
"A method '%z::%z()' cannot be declared as virtual in non-virtual class",
&pClass->sName, pName);
}
/* Assemble method signature only */ /* Assemble method signature only */
doBody = FALSE; doBody = FALSE;
} }
@@ -4165,7 +4157,7 @@ static sxi32 PH7_CompileClassInterface(ph7_gen_state *pGen) {
/* Preserve constant visibility recovery; non-public methods are fatal. */ /* Preserve constant visibility recovery; non-public methods are fatal. */
PH7_GenCompileError(&(*pGen), PH7_GenCompileError(&(*pGen),
pGen->pIn + 1 < pGen->pEnd && SX_PTR_TO_INT(pGen->pIn[1].pUserData) == PH7_KEYWORD_CONST ? E_WARNING : E_ERROR, pGen->pIn + 1 < pGen->pEnd && SX_PTR_TO_INT(pGen->pIn[1].pUserData) == PH7_KEYWORD_CONST ? E_WARNING : E_ERROR,
pGen->pIn->nLine, "interface: Access type must be public"); pGen->pIn->nLine, PH7_INTERFACE_VISIBILITY_ERROR);
nKwrd = PH7_KEYWORD_PUBLIC; nKwrd = PH7_KEYWORD_PUBLIC;
} }
if(nKwrd == PH7_KEYWORD_PUBLIC) { if(nKwrd == PH7_KEYWORD_PUBLIC) {
+38 -3
View File
@@ -513,11 +513,17 @@ static sxi32 HashmapLookup(
) { ) {
ph7_hashmap_node *pNode = 0; /* cc -O6 warning */ ph7_hashmap_node *pNode = 0; /* cc -O6 warning */
sxi32 rc; sxi32 rc;
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
if(pKey->nType & (MEMOBJ_STRING | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) { if(pKey->nType & (MEMOBJ_STRING | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) {
if((pKey->nType & MEMOBJ_STRING) == 0) { if((pKey->nType & MEMOBJ_STRING) == 0) {
/* Force a string cast */ /* Force a string cast */
PH7_MemObjToString(&(*pKey)); PH7_MemObjToString(&(*pKey));
} }
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
if(SyBlobLength(&pKey->sBlob) > 0 && !HashmapIsIntKey(&pKey->sBlob)) { if(SyBlobLength(&pKey->sBlob) > 0 && !HashmapIsIntKey(&pKey->sBlob)) {
/* Perform a blob lookup */ /* Perform a blob lookup */
rc = HashmapLookupBlobKey(&(*pMap), SyBlobData(&pKey->sBlob), SyBlobLength(&pKey->sBlob), &pNode); rc = HashmapLookupBlobKey(&(*pMap), SyBlobData(&pKey->sBlob), SyBlobLength(&pKey->sBlob), &pNode);
@@ -530,6 +536,9 @@ static sxi32 HashmapLookup(
PH7_MemObjToInteger(pKey); PH7_MemObjToInteger(pKey);
} }
/* Perform an int lookup */ /* Perform an int lookup */
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
rc = HashmapLookupIntKey(&(*pMap), pKey->x.iVal, &pNode); rc = HashmapLookupIntKey(&(*pMap), pKey->x.iVal, &pNode);
result: result:
if(rc == SXRET_OK) { if(rc == SXRET_OK) {
@@ -555,11 +564,17 @@ static sxi32 HashmapInsert(
) { ) {
ph7_hashmap_node *pNode = 0; ph7_hashmap_node *pNode = 0;
sxi32 rc = SXRET_OK; sxi32 rc = SXRET_OK;
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
if(pKey && pKey->nType & (MEMOBJ_STRING | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) { if(pKey && pKey->nType & (MEMOBJ_STRING | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) {
if((pKey->nType & MEMOBJ_STRING) == 0) { if((pKey->nType & MEMOBJ_STRING) == 0) {
/* Force a string cast */ /* Force a string cast */
PH7_MemObjToString(&(*pKey)); PH7_MemObjToString(&(*pKey));
} }
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
if(SyBlobLength(&pKey->sBlob) < 1 || HashmapIsIntKey(&pKey->sBlob)) { if(SyBlobLength(&pKey->sBlob) < 1 || HashmapIsIntKey(&pKey->sBlob)) {
if(SyBlobLength(&pKey->sBlob) < 1) { if(SyBlobLength(&pKey->sBlob) < 1) {
/* Automatic index assign */ /* Automatic index assign */
@@ -587,6 +602,9 @@ IntKey:
/* Force an integer cast */ /* Force an integer cast */
PH7_MemObjToInteger(pKey); PH7_MemObjToInteger(pKey);
} }
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
if(SXRET_OK == HashmapLookupIntKey(&(*pMap), pKey->x.iVal, &pNode)) { if(SXRET_OK == HashmapLookupIntKey(&(*pMap), pKey->x.iVal, &pNode)) {
/* Overwrite the old value */ /* Overwrite the old value */
ph7_value *pElem; ph7_value *pElem;
@@ -882,6 +900,10 @@ static int HashmapFindValueByCallback(
/* Perform an int cast */ /* Perform an int cast */
PH7_MemObjToInteger(&sResult); PH7_MemObjToInteger(&sResult);
} }
if(pMap->pVm->iPendingTransfer) {
PH7_MemObjRelease(&sResult);
return pMap->pVm->iPendingTransfer;
}
/* Classify against zero on the full int64: a raw (sxi32) cast /* Classify against zero on the full int64: a raw (sxi32) cast
* would fold 2^32 or INT64_MIN to 0 and report a bogus match. */ * would fold 2^32 or INT64_MIN to 0 and report a bogus match. */
rc = sResult.x.iVal != 0; rc = sResult.x.iVal != 0;
@@ -1441,8 +1463,9 @@ PH7_PRIVATE sxi32 PH7_HashmapRelease(ph7_hashmap *pMap, int FreeDS) {
pEntry = pNext; pEntry = pNext;
n++; n++;
} }
if(pMap->nEntry > 0) { if(pMap->apBucket) {
/* Release the hash bucket */ /* Snapshot failure may leave buckets allocated before the first node
* is linked (for example, when reference installation fails). */
SyMemBackendFree(&pVm->sAllocator, pMap->apBucket); SyMemBackendFree(&pVm->sAllocator, pMap->apBucket);
} }
if(FreeDS) { if(FreeDS) {
@@ -1823,7 +1846,7 @@ static sxi32 HashmapPtrMerge(
sxu32 i = 0, j = 0, k = 0; sxu32 i = 0, j = 0, k = 0;
while(i < nA && j < nB) { while(i < nA && j < nB) {
sxi32 rc = xCmp(apA[i], apB[j], pCtx); sxi32 rc = xCmp(apA[i], apB[j], pCtx);
if(pCtx->bAbort) { if(pCtx->bAbort || apA[i]->pMap->pVm->iPendingTransfer) {
return SXERR_ABORT; return SXERR_ABORT;
} }
if(rc <= 0) { if(rc <= 0) {
@@ -1891,6 +1914,9 @@ static sxi32 HashmapSortPointers(ph7_hashmap *pMap, ProcNodeCmp xCmp, HashmapSor
sxu32 n = pMap->nEntry; sxu32 n = pMap->nEntry;
sxu32 i; sxu32 i;
sxi32 rc; sxi32 rc;
if(pVm->iPendingTransfer) {
return SXERR_ABORT;
}
/* Allocate the pointer arrays */ /* Allocate the pointer arrays */
apNodes = (ph7_hashmap_node **)SyMemBackendAlloc(&pVm->sAllocator, n * sizeof(ph7_hashmap_node *)); apNodes = (ph7_hashmap_node **)SyMemBackendAlloc(&pVm->sAllocator, n * sizeof(ph7_hashmap_node *));
if(apNodes == 0) { if(apNodes == 0) {
@@ -2337,6 +2363,9 @@ static sxi32 HashmapCmpCallback7(ph7_hashmap_node *pA, ph7_hashmap_node *pB, voi
static void HashmapSortRehash(ph7_hashmap *pMap) { static void HashmapSortRehash(ph7_hashmap *pMap) {
ph7_hashmap_node *p, *pLast; ph7_hashmap_node *p, *pLast;
sxu32 i; sxu32 i;
if(pMap->pVm->iPendingTransfer) {
return;
}
/* Rehash all entries */ /* Rehash all entries */
pLast = p = pMap->pFirst; pLast = p = pMap->pFirst;
pMap->iNextIdx = 0; /* Reset the automatic index */ pMap->iNextIdx = 0; /* Reset the automatic index */
@@ -3815,6 +3844,9 @@ static int ph7_hashmap_splice(ph7_context *pCtx, int nArg, ph7_value **apArg) {
iLength = (int)pSrc->nEntry - iOfft; iLength = (int)pSrc->nEntry - iOfft;
} }
} }
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Create a new array */ /* Create a new array */
pArray = ph7_context_new_array(pCtx); pArray = ph7_context_new_array(pCtx);
if(pArray == 0) { if(pArray == 0) {
@@ -5668,6 +5700,9 @@ static int ph7_hashmap_filter(ph7_context *pCtx, int nArg, ph7_value **apArg) {
keep = ph7_value_to_bool(&sResult); keep = ph7_value_to_bool(&sResult);
} }
PH7_MemObjRelease(&sResult); PH7_MemObjRelease(&sResult);
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
} else { } else {
/* No available callback */ /* No available callback */
keep = FALSE; keep = FALSE;
+58 -12
View File
@@ -101,6 +101,10 @@ static sxi32 MemObjCallClassCastMethod(
ph7_value *pResult /* OUT: Store the return value of the magic method here */ ph7_value *pResult /* OUT: Store the return value of the magic method here */
) { ) {
ph7_class_method *pMethod = 0; ph7_class_method *pMethod = 0;
sxi32 rc;
if(pVm->iPendingTransfer) {
return pVm->iPendingTransfer;
}
/* Check if the method is available */ /* Check if the method is available */
if(pThis) { if(pThis) {
pMethod = PH7_ClassExtractMethod(pThis->pClass, zMethod, nLen); pMethod = PH7_ClassExtractMethod(pThis->pClass, zMethod, nLen);
@@ -110,9 +114,11 @@ static sxi32 MemObjCallClassCastMethod(
return SXERR_NOTFOUND; return SXERR_NOTFOUND;
} }
/* Invoke the desired method */ /* Invoke the desired method */
PH7_VmCallClassMethod(&(*pVm), &(*pThis), pMethod, &(*pResult), 0, 0); rc = PH7_VmCallClassMethod(&(*pVm), &(*pThis), pMethod, &(*pResult), 0, 0);
/* Method successfully called,pResult should hold the return value */ /* Scalar conversion APIs cannot carry an exception object. Preserve the
return SXRET_OK; * completion for the enclosing evaluator instead of reporting success. */
PH7_VmDeferTransfer(pVm, rc, pResult);
return rc;
} }
/* /*
* Return some kind of integer value which is the best we can * Return some kind of integer value which is the best we can
@@ -444,24 +450,33 @@ PH7_PRIVATE void PH7_MemObjSetDeclaredType(ph7_value *pValue, sxu32 nType) {
* destination are of the compatible data types. * destination are of the compatible data types.
*/ */
PH7_PRIVATE sxi32 PH7_MemObjSafeStore(ph7_value *pSrc, ph7_value *pDest) { PH7_PRIVATE sxi32 PH7_MemObjSafeStore(ph7_value *pSrc, ph7_value *pDest) {
/* Scalar destinations need not have initialized arena metadata. */ /* Both recursive array copies and object finalizers can relocate storage. */
ph7_vm *pVm = (pSrc->nType & MEMOBJ_HASHMAP) ? pDest->pVm : 0; ph7_vm *pVm = pDest->pVm;
sxu32 nDest = pVm ? pDest->nIdx : SXU32_HIGH; sxu32 nDest = pVm ? pDest->nIdx : SXU32_HIGH;
sxi32 bArena = pVm && SySetAt(&pVm->aMemObj, nDest) == pDest; sxi32 bArena = pVm && SySetAt(&pVm->aMemObj, nDest) == pDest;
if(pVm && pVm->iPendingTransfer) {
return pVm->iPendingTransfer;
}
if(pDest->nDeclaredType & MEMOBJ_NAMED) { if(pDest->nDeclaredType & MEMOBJ_NAMED) {
sxu32 nDeclared = pDest->nDeclaredType; sxu32 nDeclared = pDest->nDeclaredType;
sxi32 iFlags = pDest->iFlags; sxi32 iFlags = pDest->iFlags;
ph7_value sValue, sPrevious; ph7_value sValue, sPrevious;
sxi32 rc; sxi32 rc;
if(PH7_VmTypeCheck(pSrc, nDeclared) != SXRET_OK) {
return SXERR_NOMATCH;
}
/* Retain both payloads until storage has its contract again. Releasing /* Retain both payloads until storage has its contract again. Releasing
* the old object/map may invoke a destructor which writes through an * the old object/map may invoke a destructor which writes through an
* alias of this binding. The source can also be the destination itself. */ * alias of this binding. The source can also be the destination itself. */
PH7_MemObjInit(pDest->pVm, &sValue); PH7_MemObjInit(pVm, &sValue);
PH7_MemObjInit(pDest->pVm, &sPrevious); PH7_MemObjInit(pVm, &sPrevious);
PH7_MemObjLoad(pSrc, &sValue); PH7_MemObjLoad(pSrc, &sValue);
/* Nominal resolution may autoload and reenter the VM. Keep no arena
* address or borrowed source payload across that resolution. */
if(PH7_VmTypeCheck(&sValue, nDeclared) != SXRET_OK) {
PH7_MemObjRelease(&sValue);
return SXERR_NOMATCH;
}
if(bArena) {
pDest = (ph7_value *)SySetAt(&pVm->aMemObj, nDest);
}
PH7_MemObjLoad(pDest, &sPrevious); PH7_MemObjLoad(pDest, &sPrevious);
PH7_MemObjRelease(pDest); PH7_MemObjRelease(pDest);
pDest->x.pOther = 0; pDest->x.pOther = 0;
@@ -538,6 +553,12 @@ PH7_PRIVATE sxi32 PH7_MemObjSafeStore(ph7_value *pSrc, ph7_value *pDest) {
} else if(PH7_CheckVarCompat(pSrc, pDest->nType) == SXRET_OK) { } else if(PH7_CheckVarCompat(pSrc, pDest->nType) == SXRET_OK) {
ProcMemObjCast xCast = PH7_MemObjCastMethod(pDest->nType); ProcMemObjCast xCast = PH7_MemObjCastMethod(pDest->nType);
xCast(pSrc); xCast(pSrc);
if(pVm && pVm->iPendingTransfer) {
return pVm->iPendingTransfer;
}
if(bArena) {
pDest = (ph7_value *)SySetAt(&pVm->aMemObj, nDest);
}
PH7_MemObjStore(pSrc, pDest); PH7_MemObjStore(pSrc, pDest);
} else { } else {
return SXERR_NOMATCH; return SXERR_NOMATCH;
@@ -1019,6 +1040,31 @@ PH7_PRIVATE sxi32 PH7_MemObjStringFormat(ph7_value *pObj, const char *zFormat, v
rc = SyBlobFormatAp(&pObj->sBlob, zFormat, ap); rc = SyBlobFormatAp(&pObj->sBlob, zFormat, ap);
return rc; return rc;
} }
/* Owned context output values may be reused. Copy before releasing the old
* payload, then transfer ownership, so reentrant finalizers and aliases are safe. */
PH7_PRIVATE sxi32 PH7_MemObjCopy(ph7_value *pSrc, ph7_value *pDest) {
ph7_vm *pVm = pDest->pVm;
sxu32 nDest = pDest->nIdx;
sxi32 bArena = SySetAt(&pVm->aMemObj, nDest) == pDest;
ph7_value sCopy;
sxi32 rc;
if(pSrc == pDest) {
return SXRET_OK;
}
PH7_MemObjInit(pVm, &sCopy);
rc = PH7_MemObjStore(pSrc, &sCopy);
if(rc != SXRET_OK) {
PH7_MemObjRelease(&sCopy);
return rc;
}
PH7_MemObjRelease(pDest);
if(bArena) {
pDest = (ph7_value *)SySetAt(&pVm->aMemObj, nDest);
}
*pDest = sCopy;
pDest->nIdx = bArena ? nDest : SXU32_HIGH;
return SXRET_OK;
}
/* /*
* Duplicate the contents of a ph7_value. * Duplicate the contents of a ph7_value.
*/ */
@@ -1039,7 +1085,7 @@ PH7_PRIVATE sxi32 PH7_MemObjStore(ph7_value *pSrc, ph7_value *pDest) {
pObj = (ph7_class_instance *)pDest->x.pOther; pObj = (ph7_class_instance *)pDest->x.pOther;
} }
pDest->x = pSrc->x; pDest->x = pSrc->x;
pDest->iFlags = pSrc->iFlags; pDest->iFlags = pSrc->iFlags & ~MEMOBJ_INDEX_HANDLE;
pDest->nType = pSrc->nType; pDest->nType = pSrc->nType;
pDest->nDeclaredType = pSrc->nDeclaredType; pDest->nDeclaredType = pSrc->nDeclaredType;
rc = SXRET_OK; rc = SXRET_OK;
@@ -1077,7 +1123,7 @@ PH7_PRIVATE sxi32 PH7_MemObjStore(ph7_value *pSrc, ph7_value *pDest) {
*/ */
PH7_PRIVATE sxi32 PH7_MemObjLoad(ph7_value *pSrc, ph7_value *pDest) { PH7_PRIVATE sxi32 PH7_MemObjLoad(ph7_value *pSrc, ph7_value *pDest) {
pDest->x = pSrc->x; pDest->x = pSrc->x;
pDest->iFlags = pSrc->iFlags; pDest->iFlags = pSrc->iFlags & ~MEMOBJ_INDEX_HANDLE;
pDest->nType = pSrc->nType; pDest->nType = pSrc->nType;
pDest->nDeclaredType = pSrc->nDeclaredType; pDest->nDeclaredType = pSrc->nDeclaredType;
if(pSrc->x.pOther) { if(pSrc->x.pOther) {
+173 -23
View File
@@ -147,6 +147,33 @@ PH7_PRIVATE ph7_class_method *PH7_NewClassMethod(ph7_vm *pVm, ph7_class *pClass,
pName->nByte, VM_FUNC_CLASS_METHOD, pClass); pName->nByte, VM_FUNC_CLASS_METHOD, pClass);
return pMeth; return pMeth;
} }
/* Declaration rules shared by the compiler and native metadata preparation. */
PH7_PRIVATE sxi32 PH7_ClassConstantProtection(ph7_class *pClass, sxi32 iProtection) {
return pClass->iFlags & PH7_CLASS_INTERFACE ? PH7_CLASS_PROT_PUBLIC : iProtection;
}
PH7_PRIVATE const char *PH7_ClassMethodDeclarationError(ph7_class *pClass,
const SyString *pName, sxu32 nType, sxi32 iProtection, sxi32 iFlags) {
if((pClass->iFlags & PH7_CLASS_INTERFACE) &&
(iProtection != PH7_CLASS_PROT_PUBLIC || iFlags != 0)) {
return "Invalid interface method declaration '%z::%z()'";
}
if(((pName->nByte == sizeof("__construct") - 1 &&
SyMemcmp(pName->zString, "__construct", sizeof("__construct") - 1) == 0) ||
(pName->nByte == sizeof("__destruct") - 1 &&
SyMemcmp(pName->zString, "__destruct", sizeof("__destruct") - 1) == 0)) &&
nType != MEMOBJ_VOID) {
return "Method '%z::%z()' must return void";
}
if(iFlags & PH7_CLASS_ATTR_VIRTUAL) {
if(iProtection == PH7_CLASS_PROT_PRIVATE) {
return "Virtual method '%z::%z()' cannot be declared private";
}
if((pClass->iFlags & PH7_CLASS_VIRTUAL) == 0) {
return "A method '%z::%z()' cannot be declared as virtual in non-virtual class";
}
}
return 0;
}
/* /*
* Check if the given name have a class method associated with it. * Check if the given name have a class method associated with it.
* Return the desired method [i.e: ph7_class_method instance] on success. NULL otherwise. * Return the desired method [i.e: ph7_class_method instance] on success. NULL otherwise.
@@ -258,6 +285,15 @@ static sxi32 ClassIsDescendant(ph7_class *pClass, ph7_class *pAncestor) {
* inheritance chain, but a private declaration cannot narrow a visible member * inheritance chain, but a private declaration cannot narrow a visible member
* inherited from a base. * inherited from a base.
*/ */
/* Preparation never runs user code or emits fatal language diagnostics.
* Callers return/clean up after every diagnostic in both modes. */
#define ClassError(VM, ...) ((VM)->pClassJournal ? SXERR_INVALID : \
PH7_VmThrowError((VM), PH7_CTX_ERR, __VA_ARGS__))
typedef struct ClassJournalEntry {
ph7_class *pClass;
SySet aBase, aMro;
sxi32 iState;
} ClassJournalEntry;
static sxi32 ClassValidateAttributes(ph7_vm *pVm, ph7_class *pClass) { static sxi32 ClassValidateAttributes(ph7_vm *pVm, ph7_class *pClass) {
SyHashEntry *pEntry, *pOtherEntry; SyHashEntry *pEntry, *pOtherEntry;
sxu32 i, j; sxu32 i, j;
@@ -295,8 +331,9 @@ static sxi32 ClassValidateAttributes(ph7_vm *pVm, ph7_class *pClass) {
SyString sAttr, sOther; SyString sAttr, sOther;
PH7_ClassAttrDiagnosticName(pAttr->pClass, pAttr->iFlags, &pAttr->sName, zAttr, sizeof(zAttr), &sAttr); PH7_ClassAttrDiagnosticName(pAttr->pClass, pAttr->iFlags, &pAttr->sName, zAttr, sizeof(zAttr), &sAttr);
PH7_ClassAttrDiagnosticName(pOther->pClass, pOther->iFlags, &pOther->sName, zOther, sizeof(zOther), &sOther); PH7_ClassAttrDiagnosticName(pOther->pClass, pOther->iFlags, &pOther->sName, zOther, sizeof(zOther), &sOther);
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Class attribute '%z' conflicts with '%z' in class '%z'", ClassError(pVm, "Class attribute '%z' conflicts with '%z' in class '%z'",
&sAttr, &sOther, &pClass->sName); &sAttr, &sOther, &pClass->sName);
return SXERR_INVALID;
} }
} }
} }
@@ -348,14 +385,18 @@ static sxi32 ClassValidateMethods(ph7_vm *pVm, ph7_class *pClass) {
continue; continue;
} }
if(pOther->iFlags & PH7_CLASS_ATTR_FINAL) { if(pOther->iFlags & PH7_CLASS_ATTR_FINAL) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Cannot overwrite final method '%z:%z()' inside child class '%z'", &pOther->sFunc.pClass->sName, &pOther->sFunc.sName, &pClass->sName); ClassError(pVm, "Cannot overwrite final method '%z:%z()' inside child class '%z'", &pOther->sFunc.pClass->sName, &pOther->sFunc.sName, &pClass->sName);
rc = SXERR_INVALID;
goto done;
} }
/* Visibility involving an unrelated virtual requirement is checked /* Visibility involving an unrelated virtual requirement is checked
* against the effective implementation below, not a hidden method. */ * against the effective implementation below, not a hidden method. */
if(pMeth->iProtection > pOther->iProtection && if(pMeth->iProtection > pOther->iProtection &&
(k < SySetUsed(&pMeth->sFunc.pClass->aMro) || (k < SySetUsed(&pMeth->sFunc.pClass->aMro) ||
!((pMeth->iFlags | pOther->iFlags) & PH7_CLASS_ATTR_VIRTUAL))) { !((pMeth->iFlags | pOther->iFlags) & PH7_CLASS_ATTR_VIRTUAL))) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Cannot narrow visibility of method '%z::%z()' inside class '%z'", &pOther->sFunc.pClass->sName, &pOther->sFunc.sName, &pClass->sName); ClassError(pVm, "Cannot narrow visibility of method '%z::%z()' inside class '%z'", &pOther->sFunc.pClass->sName, &pOther->sFunc.sName, &pClass->sName);
rc = SXERR_INVALID;
goto done;
} }
/* Check real overrides and consistency between requirements here. /* Check real overrides and consistency between requirements here.
* Hidden unrelated implementations do not impose contracts. */ * Hidden unrelated implementations do not impose contracts. */
@@ -363,7 +404,9 @@ static sxi32 ClassValidateMethods(ph7_vm *pVm, ph7_class *pClass) {
((pMeth->iFlags & pOther->iFlags) & PH7_CLASS_ATTR_VIRTUAL)) && ((pMeth->iFlags & pOther->iFlags) & PH7_CLASS_ATTR_VIRTUAL)) &&
(pMeth->sFunc.nType != pOther->sFunc.nType || (pMeth->sFunc.nType != pOther->sFunc.nType ||
((pMeth->iFlags ^ pOther->iFlags) & PH7_CLASS_ATTR_STATIC))) { ((pMeth->iFlags ^ pOther->iFlags) & PH7_CLASS_ATTR_STATIC))) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible method contract '%z::%z()' inside class '%z'", &pOther->sFunc.pClass->sName, &pOther->sFunc.sName, &pClass->sName); ClassError(pVm, "Incompatible method contract '%z::%z()' inside class '%z'", &pOther->sFunc.pClass->sName, &pOther->sFunc.sName, &pClass->sName);
rc = SXERR_INVALID;
goto done;
} }
} }
if(!(pMeth->iFlags & PH7_CLASS_ATTR_VIRTUAL)) { if(!(pMeth->iFlags & PH7_CLASS_ATTR_VIRTUAL)) {
@@ -380,16 +423,22 @@ static sxi32 ClassValidateMethods(ph7_vm *pVm, ph7_class *pClass) {
} }
} }
if(pImpl && pImpl->iProtection > pMeth->iProtection) { if(pImpl && pImpl->iProtection > pMeth->iProtection) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Cannot narrow visibility of method '%z::%z()' inside class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName); ClassError(pVm, "Cannot narrow visibility of method '%z::%z()' inside class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName);
rc = SXERR_INVALID;
goto done;
} }
/* Every requirement uses the same first concrete C3 declaration, not /* Every requirement uses the same first concrete C3 declaration, not
* a later implementation selected for its compatible contract. */ * a later implementation selected for its compatible contract. */
if(pImpl && (pImpl->sFunc.nType != pMeth->sFunc.nType || if(pImpl && (pImpl->sFunc.nType != pMeth->sFunc.nType ||
((pImpl->iFlags ^ pMeth->iFlags) & PH7_CLASS_ATTR_STATIC))) { ((pImpl->iFlags ^ pMeth->iFlags) & PH7_CLASS_ATTR_STATIC))) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible method contract '%z::%z()' inside class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName); ClassError(pVm, "Incompatible method contract '%z::%z()' inside class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName);
rc = SXERR_INVALID;
goto done;
} }
if(!pImpl && !(pClass->iFlags & PH7_CLASS_VIRTUAL)) { if(!pImpl && !(pClass->iFlags & PH7_CLASS_VIRTUAL)) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Virtual method '%z::%z()' must be defined inside child class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName); ClassError(pVm, "Virtual method '%z::%z()' must be defined inside child class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName);
rc = SXERR_INVALID;
goto done;
} }
} }
done: done:
@@ -423,12 +472,12 @@ PH7_PRIVATE sxi32 PH7_ClassCollectInterfaces(ph7_vm *pVm, ph7_class *pClass, SyS
SyString *pName = (SyString *)SySetAt(pNames, j); SyString *pName = (SyString *)SySetAt(pNames, j);
pInterface = PH7_VmExtractClass(pVm, pName->zString, pName->nByte, FALSE); pInterface = PH7_VmExtractClass(pVm, pName->zString, pName->nByte, FALSE);
if(pInterface == 0) { if(pInterface == 0) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, (pOwner->iFlags & PH7_CLASS_INTERFACE) ? ClassError(pVm, (pOwner->iFlags & PH7_CLASS_INTERFACE) ?
"Call to non-existent base interface '%z'" : "Call to non-existent interface '%z'", pName); "Call to non-existent base interface '%z'" : "Call to non-existent interface '%z'", pName);
return SXERR_INVALID; return SXERR_INVALID;
} }
if(!(pInterface->iFlags & PH7_CLASS_INTERFACE)) { if(!(pInterface->iFlags & PH7_CLASS_INTERFACE)) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, (pOwner->iFlags & PH7_CLASS_INTERFACE) ? ClassError(pVm, (pOwner->iFlags & PH7_CLASS_INTERFACE) ?
"Interface '%z' cannot inherit from class '%z'" : "Class '%z' cannot implement a class '%z'", &pOwner->sName, pName); "Interface '%z' cannot inherit from class '%z'" : "Class '%z' cannot implement a class '%z'", &pOwner->sName, pName);
return SXERR_INVALID; return SXERR_INVALID;
} }
@@ -485,7 +534,9 @@ static sxi32 ClassValidateInterfaces(ph7_vm *pVm, ph7_class *pClass) {
pOther = *(ph7_class_method **)SySetAt(&sRequired, j); pOther = *(ph7_class_method **)SySetAt(&sRequired, j);
if(PH7_VmSameSignature(&pMeth->sFunc, &pOther->sFunc) && if(PH7_VmSameSignature(&pMeth->sFunc, &pOther->sFunc) &&
pMeth->sFunc.nType != pOther->sFunc.nType) { pMeth->sFunc.nType != pOther->sFunc.nType) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Conflicting interface contract '%z::%z()' inside class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName); ClassError(pVm, "Conflicting interface contract '%z::%z()' inside class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName);
rc = SXERR_INVALID;
goto done;
} }
} }
} }
@@ -505,7 +556,9 @@ static sxi32 ClassValidateInterfaces(ph7_vm *pVm, ph7_class *pClass) {
if(!pImpl || (pOther->iFlags & PH7_CLASS_ATTR_VIRTUAL)) { if(!pImpl || (pOther->iFlags & PH7_CLASS_ATTR_VIRTUAL)) {
if(pOther->iProtection != PH7_CLASS_PROT_PUBLIC || if(pOther->iProtection != PH7_CLASS_PROT_PUBLIC ||
(pOther->iFlags & PH7_CLASS_ATTR_STATIC) || pOther->sFunc.nType != pMeth->sFunc.nType) { (pOther->iFlags & PH7_CLASS_ATTR_STATIC) || pOther->sFunc.nType != pMeth->sFunc.nType) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible interface implementation '%z::%z()' inside class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName); ClassError(pVm, "Incompatible interface implementation '%z::%z()' inside class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName);
rc = SXERR_INVALID;
goto done;
} }
if(!(pOther->iFlags & PH7_CLASS_ATTR_VIRTUAL)) { if(!(pOther->iFlags & PH7_CLASS_ATTR_VIRTUAL)) {
pImpl = pOther; pImpl = pOther;
@@ -515,7 +568,9 @@ static sxi32 ClassValidateInterfaces(ph7_vm *pVm, ph7_class *pClass) {
} }
} }
if(!pImpl && !(pClass->iFlags & PH7_CLASS_VIRTUAL)) { if(!pImpl && !(pClass->iFlags & PH7_CLASS_VIRTUAL)) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Method '%z:%z()' must be defined inside class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName); ClassError(pVm, "Method '%z:%z()' must be defined inside class '%z'", &pMeth->sFunc.pClass->sName, &pMeth->sFunc.sName, &pClass->sName);
rc = SXERR_INVALID;
goto done;
} }
} }
done: done:
@@ -544,9 +599,19 @@ PH7_PRIVATE sxi32 PH7_ClassBuildMro(ph7_vm *pVm, ph7_class *pClass) {
return SXRET_OK; return SXRET_OK;
} }
if(pClass->iMroState == 1) { if(pClass->iMroState == 1) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Inheritance cycle involving class '%z'", &pClass->sName); ClassError(pVm, "Inheritance cycle involving class '%z'", &pClass->sName);
return SXERR_INVALID; return SXERR_INVALID;
} }
if(pVm->pClassJournal) {
ClassJournalEntry sEntry;
sEntry.pClass = pClass;
sEntry.aBase = pClass->aBase;
sEntry.aMro = pClass->aMro;
sEntry.iState = pClass->iMroState;
if(SySetPut(pVm->pClassJournal, &sEntry) != SXRET_OK) {
return SXERR_MEM;
}
}
/* Set before resolution, which can reenter through an autoloader. */ /* Set before resolution, which can reenter through an autoloader. */
pClass->iMroState = 1; pClass->iMroState = 1;
SySetInit(&sBase, &pVm->sAllocator, sizeof(ph7_class *)); SySetInit(&sBase, &pVm->sAllocator, sizeof(ph7_class *));
@@ -556,17 +621,27 @@ PH7_PRIVATE sxi32 PH7_ClassBuildMro(ph7_vm *pVm, ph7_class *pClass) {
SyString *pName = (SyString *)SySetAt(&pClass->pInfo->sExtends, i); SyString *pName = (SyString *)SySetAt(&pClass->pInfo->sExtends, i);
pBase = PH7_VmExtractClass(pVm, pName->zString, pName->nByte, FALSE); pBase = PH7_VmExtractClass(pVm, pName->zString, pName->nByte, FALSE);
if(pBase == 0) { if(pBase == 0) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Call to non-existent base class '%z'", pName); ClassError(pVm, "Call to non-existent base class '%z'", pName);
rc = SXERR_INVALID;
goto done;
} else if(pBase == pClass) { } else if(pBase == pClass) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Class '%z' cannot inherit from itself", &pClass->sName); ClassError(pVm, "Class '%z' cannot inherit from itself", &pClass->sName);
rc = SXERR_INVALID;
goto done;
} else if(pBase->iFlags & PH7_CLASS_INTERFACE) { } else if(pBase->iFlags & PH7_CLASS_INTERFACE) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Class '%z' cannot inherit from interface '%z'", &pClass->sName, pName); ClassError(pVm, "Class '%z' cannot inherit from interface '%z'", &pClass->sName, pName);
rc = SXERR_INVALID;
goto done;
} else if(pBase->iFlags & PH7_CLASS_FINAL) { } else if(pBase->iFlags & PH7_CLASS_FINAL) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Class '%z' cannot inherit from final class '%z'", &pClass->sName, pName); ClassError(pVm, "Class '%z' cannot inherit from final class '%z'", &pClass->sName, pName);
rc = SXERR_INVALID;
goto done;
} }
for(j = 0; j < SySetUsed(&sBase); j++) { for(j = 0; j < SySetUsed(&sBase); j++) {
if(*(ph7_class **)SySetAt(&sBase, j) == pBase) { if(*(ph7_class **)SySetAt(&sBase, j) == pBase) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Duplicate base class '%z' in class '%z'", &pBase->sName, &pClass->sName); ClassError(pVm, "Duplicate base class '%z' in class '%z'", &pBase->sName, &pClass->sName);
rc = SXERR_INVALID;
goto done;
} }
} }
rc = SySetPut(&sBase, &pBase); rc = SySetPut(&sBase, &pBase);
@@ -620,7 +695,7 @@ PH7_PRIVATE sxi32 PH7_ClassBuildMro(ph7_vm *pVm, ph7_class *pClass) {
break; break;
} }
if(pCandidate == 0) { if(pCandidate == 0) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Inconsistent C3 hierarchy for class '%z'", &pClass->sName); ClassError(pVm, "Inconsistent C3 hierarchy for class '%z'", &pClass->sName);
rc = SXERR_INVALID; rc = SXERR_INVALID;
goto done; goto done;
} }
@@ -636,8 +711,10 @@ PH7_PRIVATE sxi32 PH7_ClassBuildMro(ph7_vm *pVm, ph7_class *pClass) {
} }
} }
/* Publish only a complete linearization. */ /* Publish only a complete linearization. */
SySetRelease(&pClass->aBase); if(pVm->pClassJournal == 0) {
SySetRelease(&pClass->aMro); SySetRelease(&pClass->aBase);
SySetRelease(&pClass->aMro);
}
pClass->aBase = sBase; pClass->aBase = sBase;
pClass->aMro = sMro; pClass->aMro = sMro;
done: done:
@@ -783,7 +860,10 @@ PH7_PRIVATE sxi32 PH7_ClassInterfaceInherit(ph7_class *pSub, ph7_class *pBase) {
SyString *pName; SyString *pName;
sxi32 rc; sxi32 rc;
/* Install in the derived hashtable */ /* Install in the derived hashtable */
SyHashInsert(&pSub->hDerived, (const void *)SyStringData(&pBase->sName), SyStringLength(&pBase->sName), pBase); rc = SyHashInsert(&pSub->hDerived, (const void *)SyStringData(&pBase->sName), SyStringLength(&pBase->sName), pBase);
if(rc != SXRET_OK) {
return rc;
}
SyHashResetLoopCursor(&pBase->hAttr); SyHashResetLoopCursor(&pBase->hAttr);
/* Copy constants */ /* Copy constants */
while((pEntry = SyHashGetNextEntry(&pBase->hAttr)) != 0) { while((pEntry = SyHashGetNextEntry(&pBase->hAttr)) != 0) {
@@ -824,6 +904,71 @@ PH7_PRIVATE sxi32 PH7_ClassInterfaceInherit(ph7_class *pSub, ph7_class *pBase) {
/* All done */ /* All done */
return SXRET_OK; return SXRET_OK;
} }
/* Validate privately with the ordinary graph algorithms, restoring every
* borrowed base before returning. Only the new declaration retains state. */
PH7_PRIVATE sxi32 PH7_ClassPrepareNative(ph7_vm *pVm, ph7_class *pClass) {
SySet sJournal, sInterfaces;
ClassJournalEntry *pEntry;
sxi32 rc, bDeferred;
sxu32 i;
SySetInit(&sJournal, &pVm->sAllocator, sizeof(ClassJournalEntry));
SySetInit(&sInterfaces, &pVm->sAllocator, sizeof(ph7_class *));
pVm->pClassJournal = &sJournal;
pVm->pStagedClass = pClass;
pVm->bClassDeferred = FALSE;
rc = PH7_ClassBuildMro(pVm, pClass);
if(rc == SXRET_OK) {
rc = PH7_ClassCollectInterfaces(pVm, pClass, &sInterfaces);
}
bDeferred = pVm->bClassDeferred;
/* Only the unpublished declaration may retain prepared state. Every
* borrowed base is restored exactly, even on success, without allocation.
* No autoload, initializer or other callback runs inside this journal. */
while((pEntry = (ClassJournalEntry *)SySetPop(&sJournal)) != 0) {
ph7_class *pOwner = pEntry->pClass;
if(pOwner == pClass && rc == SXRET_OK && !bDeferred) {
SySetRelease(&pEntry->aBase);
SySetRelease(&pEntry->aMro);
} else {
if(SySetBasePtr(&pOwner->aBase) != SySetBasePtr(&pEntry->aBase)) {
SySetRelease(&pOwner->aBase);
}
if(SySetBasePtr(&pOwner->aMro) != SySetBasePtr(&pEntry->aMro)) {
SySetRelease(&pOwner->aMro);
}
pOwner->aBase = pEntry->aBase;
pOwner->aMro = pEntry->aMro;
pOwner->iMroState = pEntry->iState;
}
}
pVm->pClassJournal = 0;
pVm->pStagedClass = 0;
pVm->bClassDeferred = FALSE;
SySetRelease(&sJournal);
if(bDeferred && rc == SXERR_INVALID) {
/* Missing names resolve at first language use, after compilation/import.
* A malformed complete graph is instead a recoverable registration error. */
rc = SXRET_OK;
} else if(rc == SXRET_OK) {
if(pClass->iFlags & PH7_CLASS_INTERFACE) {
for(i = 0; i < SySetUsed(&sInterfaces) && rc == SXRET_OK; i++) {
ph7_class *pBase = *(ph7_class **)SySetAt(&sInterfaces, i);
if(pBase != pClass) {
rc = PH7_ClassInterfaceInherit(pClass, pBase);
}
}
} else {
for(i = 1; i < SySetUsed(&pClass->aMro) && rc == SXRET_OK; i++) {
rc = PH7_ClassInherit(pVm, pClass, *(ph7_class **)SySetAt(&pClass->aMro, i));
}
for(i = 0; i < SySetUsed(&sInterfaces) && rc == SXRET_OK; i++) {
rc = PH7_ClassImplement(pVm, pClass, *(ph7_class **)SySetAt(&sInterfaces, i));
}
}
}
SySetRelease(&sInterfaces);
return rc;
}
/* /*
* Implements an object interface in the given main class. * Implements an object interface in the given main class.
* According to the PHP language reference manual. * According to the PHP language reference manual.
@@ -1138,9 +1283,14 @@ PH7_PRIVATE sxi32 PH7_ClassInstanceFinalize(ph7_class_instance *pThis) {
pThis->iFlags |= CLASS_INSTANCE_FINALIZED; pThis->iFlags |= CLASS_INSTANCE_FINALIZED;
pDestr = PH7_ClassExtractMethod(pThis->pClass, "__destruct", sizeof("__destruct") - 1); pDestr = PH7_ClassExtractMethod(pThis->pClass, "__destruct", sizeof("__destruct") - 1);
if(pDestr) { if(pDestr) {
ph7_value sResult;
sxi32 rc;
/* A destructor may remove its own last ordinary reference. */ /* A destructor may remove its own last ordinary reference. */
pThis->iRef++; pThis->iRef++;
PH7_VmCallClassMethod(pThis->pVm, pThis, pDestr, 0, 0, 0); PH7_MemObjInit(pThis->pVm, &sResult);
rc = PH7_VmCallClassMethod(pThis->pVm, pThis, pDestr, &sResult, 0, 0);
PH7_VmDeferTransfer(pThis->pVm, rc, &sResult);
PH7_MemObjRelease(&sResult);
PH7_ClassInstanceUnref(pThis); PH7_ClassInstanceUnref(pThis);
} }
return 1; return 1;
+76 -1
View File
@@ -280,6 +280,9 @@ static int PH7_vfs_mkdir(ph7_context *pCtx, int nArg, ph7_value **apArg) {
} }
} }
/* Perform the requested operation */ /* Perform the requested operation */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
rc = pVfs->xMkdir(zPath, iMode, iRecursive); rc = pVfs->xMkdir(zPath, iMode, iRecursive);
/* IO return value */ /* IO return value */
ph7_result_bool(pCtx, rc == PH7_OK); ph7_result_bool(pCtx, rc == PH7_OK);
@@ -518,6 +521,9 @@ static int PH7_vfs_chmod(ph7_context *pCtx, int nArg, ph7_value **apArg) {
zPath = ph7_value_to_string(apArg[0], 0); zPath = ph7_value_to_string(apArg[0], 0);
/* Extract the mode */ /* Extract the mode */
iMode = ph7_value_to_int(apArg[1]); iMode = ph7_value_to_int(apArg[1]);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Perform the requested operation */ /* Perform the requested operation */
rc = pVfs->xChmod(zPath, iMode); rc = pVfs->xChmod(zPath, iMode);
/* IO return value */ /* IO return value */
@@ -559,6 +565,9 @@ static int PH7_vfs_chown(ph7_context *pCtx, int nArg, ph7_value **apArg) {
zPath = ph7_value_to_string(apArg[0], 0); zPath = ph7_value_to_string(apArg[0], 0);
/* Extract the user */ /* Extract the user */
zUser = ph7_value_to_string(apArg[1], 0); zUser = ph7_value_to_string(apArg[1], 0);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Perform the requested operation */ /* Perform the requested operation */
rc = pVfs->xChown(zPath, zUser); rc = pVfs->xChown(zPath, zUser);
/* IO return value */ /* IO return value */
@@ -600,6 +609,9 @@ static int PH7_vfs_chgrp(ph7_context *pCtx, int nArg, ph7_value **apArg) {
zPath = ph7_value_to_string(apArg[0], 0); zPath = ph7_value_to_string(apArg[0], 0);
/* Extract the user */ /* Extract the user */
zGroup = ph7_value_to_string(apArg[1], 0); zGroup = ph7_value_to_string(apArg[1], 0);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Perform the requested operation */ /* Perform the requested operation */
rc = pVfs->xChgrp(zPath, zGroup); rc = pVfs->xChgrp(zPath, zGroup);
/* IO return value */ /* IO return value */
@@ -1488,6 +1500,9 @@ static int PH7_vfs_touch(ph7_context *pCtx, int nArg, ph7_value **apArg) {
nAccess = nTime; nAccess = nTime;
} }
} }
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
rc = pVfs->xTouch(zFile, nTime, nAccess); rc = pVfs->xTouch(zFile, nTime, nAccess);
/* IO result */ /* IO result */
ph7_result_bool(pCtx, rc == PH7_OK); ph7_result_bool(pCtx, rc == PH7_OK);
@@ -2186,6 +2201,9 @@ static int PH7_vfs_umask(ph7_context *pCtx, int nArg, ph7_value **apArg) {
if(nArg > 0) { if(nArg > 0) {
iNew = ph7_value_to_int(apArg[0]); iNew = ph7_value_to_int(apArg[0]);
} }
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Perform the requested operation */ /* Perform the requested operation */
iOld = pVfs->xUmask(iNew); iOld = pVfs->xUmask(iNew);
/* Old mask */ /* Old mask */
@@ -2527,6 +2545,7 @@ static int PH7_builtin_ftruncate(ph7_context *pCtx, int nArg, ph7_value **apArg)
const ph7_io_stream *pStream; const ph7_io_stream *pStream;
io_private *pDev; io_private *pDev;
int rc; int rc;
ph7_int64 nSize;
if(nArg < 2 || !ph7_value_is_resource(apArg[0])) { if(nArg < 2 || !ph7_value_is_resource(apArg[0])) {
/* Missing/Invalid arguments, return FALSE */ /* Missing/Invalid arguments, return FALSE */
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle"); PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING, "Expecting an IO handle");
@@ -2553,7 +2572,11 @@ static int PH7_builtin_ftruncate(ph7_context *pCtx, int nArg, ph7_value **apArg)
return PH7_OK; return PH7_OK;
} }
/* Perform the requested operation */ /* Perform the requested operation */
rc = pStream->xTrunc(pDev->pHandle, ph7_value_to_int64(apArg[1])); nSize = ph7_value_to_int64(apArg[1]);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
rc = pStream->xTrunc(pDev->pHandle, nSize);
if(rc == PH7_OK) { if(rc == PH7_OK) {
/* Discard buffered data */ /* Discard buffered data */
ResetIOPrivate(pDev); ResetIOPrivate(pDev);
@@ -2618,6 +2641,9 @@ static int PH7_builtin_fseek(ph7_context *pCtx, int nArg, ph7_value **apArg) {
whence = ph7_value_to_int(apArg[2]); whence = ph7_value_to_int(apArg[2]);
} }
/* Perform the requested operation */ /* Perform the requested operation */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
rc = pStream->xSeek(pDev->pHandle, iOfft, whence); rc = pStream->xSeek(pDev->pHandle, iOfft, whence);
if(rc == PH7_OK) { if(rc == PH7_OK) {
/* Ignore buffered data */ /* Ignore buffered data */
@@ -2973,6 +2999,11 @@ PH7_PRIVATE void *PH7_StreamOpenHandle(ph7_vm *pVm, const ph7_io_stream *pStream
SyString sFile; SyString sFile;
char sFilePath[PATH_MAX + 1]; char sFilePath[PATH_MAX + 1];
int rc; int rc;
/* Open callers may have just converted flags/mode through a magic hook.
* A failed conversion must never open (and possibly truncate) the URI. */
if(pVm->iPendingTransfer) {
return 0;
}
if(pStream == 0) { if(pStream == 0) {
/* No such stream device */ /* No such stream device */
return 0; return 0;
@@ -3179,6 +3210,9 @@ static int PH7_builtin_fgets(ph7_context *pCtx, int nArg, ph7_value **apArg) {
nLen = ph7_value_to_int64(apArg[1]); nLen = ph7_value_to_int64(apArg[1]);
} }
/* Perform the requested operation */ /* Perform the requested operation */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
n = StreamReadLine(pDev, &zLine, nLen); n = StreamReadLine(pDev, &zLine, nLen);
if(n < 1) { if(n < 1) {
/* EOF or IO error, return FALSE */ /* EOF or IO error, return FALSE */
@@ -3240,6 +3274,9 @@ static int PH7_builtin_fread(ph7_context *pCtx, int nArg, ph7_value **apArg) {
} }
} }
/* Allocate enough buffer */ /* Allocate enough buffer */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
pBuf = ph7_context_alloc_chunk(pCtx, (unsigned int)nLen, FALSE, FALSE); pBuf = ph7_context_alloc_chunk(pCtx, (unsigned int)nLen, FALSE, FALSE);
if(pBuf == 0) { if(pBuf == 0) {
PH7_VmMemoryError(pCtx->pVm); PH7_VmMemoryError(pCtx->pVm);
@@ -3316,6 +3353,9 @@ static int PH7_builtin_fgetcsv(ph7_context *pCtx, int nArg, ph7_value **apArg) {
nLen = ph7_value_to_int64(apArg[1]); nLen = ph7_value_to_int64(apArg[1]);
} }
/* Perform the requested operation */ /* Perform the requested operation */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
n = StreamReadLine(pDev, &zLine, nLen); n = StreamReadLine(pDev, &zLine, nLen);
if(n < 1) { if(n < 1) {
/* EOF or IO error, return FALSE */ /* EOF or IO error, return FALSE */
@@ -3419,6 +3459,9 @@ static int PH7_builtin_fgetss(ph7_context *pCtx, int nArg, ph7_value **apArg) {
nLen = ph7_value_to_int64(apArg[1]); nLen = ph7_value_to_int64(apArg[1]);
} }
/* Perform the requested operation */ /* Perform the requested operation */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
n = StreamReadLine(pDev, &zLine, nLen); n = StreamReadLine(pDev, &zLine, nLen);
if(n < 1) { if(n < 1) {
/* EOF or IO error, return FALSE */ /* EOF or IO error, return FALSE */
@@ -3756,6 +3799,10 @@ static int PH7_builtin_file_get_contents(ph7_context *pCtx, int nArg, ph7_value
if(nArg > 3) { if(nArg > 3) {
/* Extract the offset */ /* Extract the offset */
n = ph7_value_to_int64(apArg[3]); n = ph7_value_to_int64(apArg[3]);
if(pCtx->pVm->iPendingTransfer) {
PH7_StreamCloseHandle(pStream, pHandle);
return pCtx->pVm->iPendingTransfer;
}
if(n > 0) { if(n > 0) {
if(pStream->xSeek) { if(pStream->xSeek) {
/* Seek to the desired offset */ /* Seek to the desired offset */
@@ -3768,6 +3815,10 @@ static int PH7_builtin_file_get_contents(ph7_context *pCtx, int nArg, ph7_value
} }
} }
/* Perform the requested operation */ /* Perform the requested operation */
if(pCtx->pVm->iPendingTransfer) {
PH7_StreamCloseHandle(pStream, pHandle);
return pCtx->pVm->iPendingTransfer;
}
nRead = 0; nRead = 0;
for(;;) { for(;;) {
n = pStream->xRead(pHandle, zBuf, n = pStream->xRead(pHandle, zBuf,
@@ -3839,6 +3890,9 @@ static int PH7_builtin_file_put_contents(ph7_context *pCtx, int nArg, ph7_value
} }
/* Data to write */ /* Data to write */
zData = ph7_value_to_string(apArg[1], &nLen); zData = ph7_value_to_string(apArg[1], &nLen);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nLen < 1) { if(nLen < 1) {
/* Nothing to write, return immediately */ /* Nothing to write, return immediately */
ph7_result_bool(pCtx, 0); ph7_result_bool(pCtx, 0);
@@ -3862,6 +3916,9 @@ static int PH7_builtin_file_put_contents(ph7_context *pCtx, int nArg, ph7_value
iOpenFlags |= PH7_IO_OPEN_APPEND; iOpenFlags |= PH7_IO_OPEN_APPEND;
} }
} }
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
pHandle = PH7_StreamOpenHandle(pCtx->pVm, pStream, zFile, iOpenFlags, use_include, pHandle = PH7_StreamOpenHandle(pCtx->pVm, pStream, zFile, iOpenFlags, use_include,
nArg > 3 ? apArg[3] : 0, FALSE, FALSE); nArg > 3 ? apArg[3] : 0, FALSE, FALSE);
if(pHandle == 0) { if(pHandle == 0) {
@@ -4207,6 +4264,9 @@ static int PH7_builtin_fwrite(ph7_context *pCtx, int nArg, ph7_value **apArg) {
} }
/* Extract the data to write */ /* Extract the data to write */
zString = ph7_value_to_string(apArg[1], &nLen); zString = ph7_value_to_string(apArg[1], &nLen);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nArg > 2) { if(nArg > 2) {
/* Maximum data length to write */ /* Maximum data length to write */
n = ph7_value_to_int(apArg[2]); n = ph7_value_to_int(apArg[2]);
@@ -4214,6 +4274,9 @@ static int PH7_builtin_fwrite(ph7_context *pCtx, int nArg, ph7_value **apArg) {
nLen = n; nLen = n;
} }
} }
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nLen < 1) { if(nLen < 1) {
/* Nothing to write */ /* Nothing to write */
ph7_result_int(pCtx, 0); ph7_result_int(pCtx, 0);
@@ -4276,6 +4339,9 @@ static int PH7_builtin_flock(ph7_context *pCtx, int nArg, ph7_value **apArg) {
} }
/* Requested lock operation */ /* Requested lock operation */
nLock = ph7_value_to_int(apArg[1]); nLock = ph7_value_to_int(apArg[1]);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Lock operation */ /* Lock operation */
rc = pStream->xLock(pDev->pHandle, nLock); rc = pStream->xLock(pDev->pHandle, nLock);
/* IO result */ /* IO result */
@@ -4363,6 +4429,9 @@ static int csv_write_callback(ph7_value *pKey, ph7_value *pValue, void *pUserDat
sxu32 n; sxu32 n;
/* Point to the raw data */ /* Point to the raw data */
zData = ph7_value_to_string(pValue, &nLen); zData = ph7_value_to_string(pValue, &nLen);
if(pValue->pVm->iPendingTransfer) {
return pValue->pVm->iPendingTransfer;
}
if(nLen < 1) { if(nLen < 1) {
/* Nothing to write */ /* Nothing to write */
return PH7_OK; return PH7_OK;
@@ -4465,7 +4534,13 @@ static int PH7_builtin_fputcsv(ph7_context *pCtx, int nArg, ph7_value **apArg) {
} }
} }
/* Iterate throw array entries and write csv data */ /* Iterate throw array entries and write csv data */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
ph7_array_walk(apArg[1], csv_write_callback, &sCsv); ph7_array_walk(apArg[1], csv_write_callback, &sCsv);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Write a line ending */ /* Write a line ending */
#ifdef __WINNT__ #ifdef __WINNT__
zEol = "\r\n"; zEol = "\r\n";
+981 -270
View File
File diff suppressed because it is too large Load Diff
+57 -1
View File
@@ -546,19 +546,31 @@ typedef struct ph7_type_def {
const char *zClass; /* Required only for PH7_TYPE_NAMED; self is allowed. */ const char *zClass; /* Required only for PH7_TYPE_NAMED; self is allowed. */
int bArray; /* Zero or one: the language's T[] annotation. */ int bArray; /* Zero or one: the language's T[] annotation. */
} ph7_type_def; } ph7_type_def;
/* Evaluators use ph7_result_* and return PH7_OK, PH7_ABORT or PH7_EXCEPTION.
* They run at the same evaluation point as the corresponding source expression. */
typedef int (*ph7_initializer_func)(ph7_context *);
typedef struct ph7_field_def { typedef struct ph7_field_def {
const char *zName; const char *zName;
ph7_type_def sType; ph7_type_def sType;
const ph7_value *pInitial; const ph7_value *pInitial;
unsigned int iFlags; unsigned int iFlags;
ph7_initializer_func xInitial;
void *pInitialData;
} ph7_field_def; } ph7_field_def;
typedef struct ph7_constant_def { typedef struct ph7_constant_def {
const char *zName; const char *zName;
const ph7_value *pValue; const ph7_value *pValue;
unsigned int iFlags;
ph7_initializer_func xInitial;
void *pInitialData;
} ph7_constant_def; } ph7_constant_def;
typedef struct ph7_param_def { typedef struct ph7_param_def {
const char *zName; /* Exact lowercase this/parent are reserved, even for static methods. */ const char *zName; /* Explicit parameters shadow implicit this/parent bindings. */
ph7_type_def sType; ph7_type_def sType;
unsigned int iFlags;
const ph7_value *pDefault;
ph7_initializer_func xDefault;
void *pDefaultData;
} ph7_param_def; } ph7_param_def;
typedef struct ph7_method_def { typedef struct ph7_method_def {
const char *zName; const char *zName;
@@ -570,6 +582,8 @@ typedef struct ph7_method_def {
unsigned int iFlags; unsigned int iFlags;
} ph7_method_def; } ph7_method_def;
typedef struct ph7_class_def { typedef struct ph7_class_def {
unsigned int nSize; /* sizeof(ph7_class_def), checked before any later field */
unsigned int nVersion; /* PH7_CLASS_DEF_VERSION; covers all nested record layouts */
const char *zName; const char *zName;
const ph7_field_def *aField; const ph7_field_def *aField;
unsigned int nField; unsigned int nField;
@@ -577,13 +591,55 @@ typedef struct ph7_class_def {
unsigned int nConstant; unsigned int nConstant;
const ph7_method_def *aMethod; const ph7_method_def *aMethod;
unsigned int nMethod; unsigned int nMethod;
unsigned int iFlags;
const char *const *azBase;
unsigned int nBase;
const char *const *azInterface;
unsigned int nInterface;
/* Optional declaration order: indices into fields followed by constants.
* NULL/0 keeps the default: all fields, then all constants. Otherwise
* supply every index exactly once, in source declaration order. */
const unsigned int *aMemberOrder;
unsigned int nMemberOrder;
} ph7_class_def; } ph7_class_def;
#define PH7_CLASS_DEF_VERSION 2
#define PH7_MEMBER_STATIC 0x01 #define PH7_MEMBER_STATIC 0x01
#define PH7_MEMBER_VIRTUAL 0x04
#define PH7_MEMBER_FINAL 0x08
#define PH7_MEMBER_PUBLIC 0x10
#define PH7_MEMBER_PROTECTED 0x20
#define PH7_MEMBER_PRIVATE 0x40
#define PH7_PARAM_REF 0x01
#define PH7_DECL_FINAL 0x01
#define PH7_DECL_INTERFACE 0x02
#define PH7_DECL_VIRTUAL 0x04
/* Return an exception object using ph7_result_value(), then this status. */ /* Return an exception object using ph7_result_value(), then this status. */
#define PH7_EXCEPTION (-255) #define PH7_EXCEPTION (-255)
PH7_APIEXPORT int ph7_create_class(ph7_vm *pVm, const ph7_class_def *pDef); PH7_APIEXPORT int ph7_create_class(ph7_vm *pVm, const ph7_class_def *pDef);
/* Borrowed receiver, valid only during this callback; NULL for static calls. */ /* Borrowed receiver, valid only during this callback; NULL for static calls. */
PH7_APIEXPORT ph7_value *ph7_context_this(ph7_context *pCtx); PH7_APIEXPORT ph7_value *ph7_context_this(ph7_context *pCtx);
/* Indexed operations reacquire moving VM storage on every call. argv contains
* by-value snapshots; use these operations for immediate reference semantics. */
PH7_APIEXPORT int ph7_context_arg_value(ph7_context *pCtx, unsigned int nArg, ph7_value *pOut);
PH7_APIEXPORT int ph7_context_arg_store(ph7_context *pCtx, unsigned int nArg, ph7_value *pValue);
/* A forwarding handle is borrowed until this callback returns. Calls reacquire
* its live indexed binding; ordinary value setters still mutate only its copy. */
PH7_APIEXPORT int ph7_context_arg_bind(ph7_context *pCtx, unsigned int nArg, ph7_value *pOut);
/* pObject selects instance storage; otherwise zClass selects static/constant
* storage. Access is checked in the callback's declaring language scope. */
PH7_APIEXPORT int ph7_context_attr_value(ph7_context *pCtx, ph7_value *pObject,
const char *zClass, const char *zName, ph7_value *pOut);
PH7_APIEXPORT int ph7_context_attr_store(ph7_context *pCtx, ph7_value *pObject,
const char *zClass, const char *zName, ph7_value *pValue);
PH7_APIEXPORT int ph7_context_call(ph7_context *pCtx, ph7_value *pCallable,
int nArg, ph7_value **apArg, ph7_value *pResult);
PH7_APIEXPORT int ph7_context_parent(ph7_context *pCtx, const char *zMethod,
int nArg, ph7_value **apArg, ph7_value *pResult);
PH7_APIEXPORT int ph7_context_var_value(ph7_context *pCtx, const char *zName, ph7_value *pOut);
PH7_APIEXPORT int ph7_context_var_store(ph7_context *pCtx, const char *zName, ph7_value *pValue);
/* Legacy scalar conversion APIs cannot return an exception status. Query this
* before further native side effects; returning it propagates the completion. */
PH7_APIEXPORT int ph7_context_status(ph7_context *pCtx);
PH7_APIEXPORT int ph7_create_function(ph7_vm *pVm, const char *zName, int (*xFunc)(ph7_context *, int, ph7_value **), void *pUserData); PH7_APIEXPORT int ph7_create_function(ph7_vm *pVm, const char *zName, int (*xFunc)(ph7_context *, int, ph7_value **), void *pUserData);
PH7_APIEXPORT int ph7_delete_function(ph7_vm *pVm, const char *zName); PH7_APIEXPORT int ph7_delete_function(ph7_vm *pVm, const char *zName);
PH7_APIEXPORT int ph7_create_constant(ph7_vm *pVm, const char *zName, void (*xExpand)(ph7_value *, void *), void *pUserData); PH7_APIEXPORT int ph7_create_constant(ph7_vm *pVm, const char *zName, void (*xExpand)(ph7_value *, void *), void *pUserData);
+34
View File
@@ -627,6 +627,7 @@ struct ph7_value {
#define MEMOBJ_VARIABLE 0 /* Memory value is variable */ #define MEMOBJ_VARIABLE 0 /* Memory value is variable */
#define MEMOBJ_PARENTOBJ 1 /* Memory value is 'parent' object */ #define MEMOBJ_PARENTOBJ 1 /* Memory value is 'parent' object */
#define MEMOBJ_THISOBJ 2 /* Memory value is 'this' object */ #define MEMOBJ_THISOBJ 2 /* Memory value is 'this' object */
#define MEMOBJ_INDEX_HANDLE 4 /* Native context forwarding handle, not a value flag */
/* Allowed variable data types. /* Allowed variable data types.
*/ */
#define MEMOBJ_BOOL 0x0001 /* Memory value is a boolean */ #define MEMOBJ_BOOL 0x0001 /* Memory value is a boolean */
@@ -758,6 +759,7 @@ struct ph7_context {
sxi32 iFlags; /* Call flags */ sxi32 iFlags; /* Call flags */
ph7_value *pThis; /* Stable borrowed native method receiver */ ph7_value *pThis; /* Stable borrowed native method receiver */
sxi32 bResultSet; /* Distinguish explicit NULL from a bare return */ sxi32 bResultSet; /* Distinguish explicit NULL from a bare return */
void *pNativeFrame; /* Borrowed argument frame for indexed context operations */
}; };
/* /*
* Each hashmap entry [i.e: array(4,5,6)] is recorded in an instance * Each hashmap entry [i.e: array(4,5,6)] is recorded in an instance
@@ -951,6 +953,9 @@ struct ph7_vm_func_arg {
sxu32 nType; /* Type of this argument [i.e: array, int, string, float, object, etc.] */ sxu32 nType; /* Type of this argument [i.e: array, int, string, float, object, etc.] */
SyString sClass; /* Class name if the argument expect a class instance [i.e: function foo(BaseClass $bar){} ] */ SyString sClass; /* Class name if the argument expect a class instance [i.e: function foo(BaseClass $bar){} ] */
sxi32 iFlags; /* Configuration flags */ sxi32 iFlags; /* Configuration flags */
ph7_value *pNativeDefault; /* Owned snapshot, distinct from an absent default */
ph7_initializer_func xNativeDefault;
void *pNativeData;
}; };
/* /*
* Each static variable is parsed out and remembered in an instance * Each static variable is parsed out and remembered in an instance
@@ -1071,6 +1076,10 @@ struct ph7_class {
SySet aMro; /* C3 linearization, including this class first */ SySet aMro; /* C3 linearization, including this class first */
sxi32 iMroState; /* 0: unbuilt, 1: building, 2: ready */ sxi32 iMroState; /* 0: unbuilt, 1: building, 2: ready */
sxi32 bNative; /* Public native declaration */ sxi32 bNative; /* Public native declaration */
sxi32 iNativeState; /* 0: pending, 1: initializing, 2: usable */
sxi32 bMounting; /* Reentrant static initializer guard */
sxi32 bMethodsMounted; /* Concrete declarations enter the function table once */
sxu32 nNativeWarnings; /* Accepted declaration diagnostics, delivered after commit */
}; };
/* Class configuration flags */ /* Class configuration flags */
#define PH7_CLASS_FINAL 0x001 /* Class is final [cannot be extended] */ #define PH7_CLASS_FINAL 0x001 /* Class is final [cannot be extended] */
@@ -1094,6 +1103,8 @@ struct ph7_class_attr {
sxu32 nIdx; /* Attribute index */ sxu32 nIdx; /* Attribute index */
sxu32 nLine; /* Line number on which this attribute was defined */ sxu32 nLine; /* Line number on which this attribute was defined */
ph7_value *pNativeInitial; /* Owned native declaration snapshot, if present */ ph7_value *pNativeInitial; /* Owned native declaration snapshot, if present */
ph7_initializer_func xNativeInitial;
void *pNativeData;
}; };
/* Attribute configuration */ /* Attribute configuration */
#define PH7_CLASS_ATTR_STATIC 0x001 /* Static attribute */ #define PH7_CLASS_ATTR_STATIC 0x001 /* Static attribute */
@@ -1256,6 +1267,13 @@ struct ph7_vm {
void *pStderr; /* STDERR IO stream */ void *pStderr; /* STDERR IO stream */
sxbool bDebug; /* TRUE to enable debugging */ sxbool bDebug; /* TRUE to enable debugging */
sxbool bErrReport; /* TRUE to report all runtime Error/Warning/Notice/Deprecated */ sxbool bErrReport; /* TRUE to report all runtime Error/Warning/Notice/Deprecated */
SySet *pClassJournal; /* Non-reentrant, callback-free metadata preparation */
ph7_class *pStagedClass; /* Unpublished declaration visible only to preparation */
sxi32 bClassDeferred; /* A forward hierarchy name is not installed yet */
sxi32 bNativeRegister; /* Callback-free declaration transaction is active */
sxu32 nNativeReserve; /* Conservative rollback free-slot capacity bound */
sxi32 iPendingTransfer; /* Conversion/readiness completion crossing scalar APIs */
ph7_value sPendingTransfer;
int nExceptDepth; /* Exception depth */ int nExceptDepth; /* Exception depth */
int closure_cnt; /* Loaded closures counter */ int closure_cnt; /* Loaded closures counter */
int json_rc; /* JSON return status [refer to json_encode()/json_decode()] */ int json_rc; /* JSON return status [refer to json_encode()/json_decode()] */
@@ -1286,6 +1304,7 @@ struct VmFrame {
SyHash hConst; /* Constant hashtable for fast lookup */ SyHash hConst; /* Constant hashtable for fast lookup */
SyHash hVar; /* Variable hashtable for fast lookup */ SyHash hVar; /* Variable hashtable for fast lookup */
SySet sArg; /* Function arguments container */ SySet sArg; /* Function arguments container */
SySet sArgPin; /* Indexed reference arguments retained through reentry */
SySet sRef; /* Local reference table (VmSlot instance) */ SySet sRef; /* Local reference table (VmSlot instance) */
SySet sConstDecl; /* Constants already declared during this invocation */ SySet sConstDecl; /* Constants already declared during this invocation */
sxi32 iFlags; /* Frame configuration flags (See below) */ sxi32 iFlags; /* Frame configuration flags (See below) */
@@ -1336,6 +1355,7 @@ struct VmRefObj {
SySet aArrEntries; /* Foreign hashmap entries [i.e: array(&$a) ] */ SySet aArrEntries; /* Foreign hashmap entries [i.e: array(&$a) ] */
sxu32 nIdx; /* Referenced object index */ sxu32 nIdx; /* Referenced object index */
sxu32 nCapture; /* Closure owners: keep contents and index alive */ sxu32 nCapture; /* Closure owners: keep contents and index alive */
sxu32 nBorrow; /* Active reference argument handles retain the same index */
sxi32 iFlags; /* Configuration flags */ sxi32 iFlags; /* Configuration flags */
VmRefObj *pNextCollide, *pPrevCollide; /* Collision link */ VmRefObj *pNextCollide, *pPrevCollide; /* Collision link */
VmRefObj *pNext, *pPrev; /* List of all referenced objects */ VmRefObj *pNext, *pPrev; /* List of all referenced objects */
@@ -1745,6 +1765,20 @@ PH7_PRIVATE sxi32 PH7_IsIdentifier(const char *zName, sxi32 bKeyword);
PH7_PRIVATE sxi32 PH7_IsMethodName(const char *zName); PH7_PRIVATE sxi32 PH7_IsMethodName(const char *zName);
PH7_PRIVATE sxi32 PH7_IsReservedConstant(const SyString *pName); PH7_PRIVATE sxi32 PH7_IsReservedConstant(const SyString *pName);
PH7_PRIVATE sxi32 PH7_VmCreateNativeClass(ph7_vm *pVm, const ph7_class_def *pDef); PH7_PRIVATE sxi32 PH7_VmCreateNativeClass(ph7_vm *pVm, const ph7_class_def *pDef);
PH7_PRIVATE const char *PH7_ClassMethodDeclarationError(ph7_class *pClass,
const SyString *pName, sxu32 nType, sxi32 iProtection, sxi32 iFlags);
#define PH7_INTERFACE_VISIBILITY_ERROR "interface: Access type must be public"
PH7_PRIVATE sxi32 PH7_ClassConstantProtection(ph7_class *pClass, sxi32 iProtection);
PH7_PRIVATE sxi32 PH7_VmContextAttr(ph7_context *pCtx, ph7_value *pObject,
const char *zClass, const char *zName, ph7_value *pValue, sxi32 bStore);
PH7_PRIVATE sxi32 PH7_ClassPrepareNative(ph7_vm *pVm, ph7_class *pClass);
PH7_PRIVATE sxi32 PH7_VmContextParent(ph7_context *pCtx, const char *zMethod,
int nArg, ph7_value **apArg, ph7_value *pResult);
PH7_PRIVATE void PH7_VmDeferTransfer(ph7_vm *pVm, sxi32 rc, ph7_value *pValue);
PH7_PRIVATE sxi32 PH7_VmTakeTransfer(ph7_vm *pVm, ph7_value *pResult);
PH7_PRIVATE sxi32 PH7_VmContextVar(ph7_context *pCtx, const char *zName,
ph7_value *pValue, sxi32 bStore);
PH7_PRIVATE sxi32 PH7_MemObjCopy(ph7_value *pSrc, ph7_value *pDest);
PH7_PRIVATE ph7_value *PH7_ReserveConstObj(ph7_vm *pVm, sxu32 *pIndex); PH7_PRIVATE ph7_value *PH7_ReserveConstObj(ph7_vm *pVm, sxu32 *pIndex);
PH7_PRIVATE sxi32 PH7_VmOutputConsume(ph7_vm *pVm, SyString *pString); PH7_PRIVATE sxi32 PH7_VmOutputConsume(ph7_vm *pVm, SyString *pString);
PH7_PRIVATE sxi32 PH7_VmOutputConsumeAp(ph7_vm *pVm, const char *zFormat, va_list ap); PH7_PRIVATE sxi32 PH7_VmOutputConsumeAp(ph7_vm *pVm, const char *zFormat, va_list ap);