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;
char *pFileDir, fFilePath[PATH_MAX + 1];
char pFilePath[PATH_MAX + 1];
if(pVm->bNativeRegister) {
return SXERR_LOCKED;
}
/* Track the initialized VM before compilation can trigger library shutdown. */
MACRO_LD_PUSH(pEngine->pVms, pVm);
pEngine->iVm++;
@@ -611,6 +614,9 @@ int ph7_vm_exec(ph7_vm *pVm, int *pExitStatus) {
if(PH7_VM_MISUSE(pVm)) {
return PH7_CORRUPT;
}
if(pVm->bNativeRegister) {
return SXERR_LOCKED;
}
/* Execute PH7 byte-code */
rc = PH7_VmByteCodeExec(&(*pVm));
if(pExitStatus) {
@@ -630,6 +636,9 @@ int ph7_vm_reset(ph7_vm *pVm) {
if(PH7_VM_MISUSE(pVm)) {
return PH7_CORRUPT;
}
if(pVm->bNativeRegister) {
return SXERR_LOCKED;
}
rc = PH7_VmReset(&(*pVm));
return rc;
}
@@ -644,6 +653,9 @@ int ph7_vm_release(ph7_vm *pVm) {
if(PH7_VM_MISUSE(pVm)) {
return PH7_CORRUPT;
}
if(pVm->bNativeRegister) {
return SXERR_LOCKED;
}
pEngine = pVm->pEngine;
rc = PH7_VmRelease(&(*pVm));
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);
}
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()]
* 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 {
c = ph7_value_to_int(pArg);
}
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* NUL byte is an acceptable value */
zWorker[0] = (char)c;
length = (int)sizeof(char);
@@ -3446,6 +3449,9 @@ PH7_PRIVATE sxi32 PH7_InputFormat(
} else {
zBuf = (char *)ph7_value_to_string(pArg, &length);
}
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(length < 1) {
zBuf = " ";
length = (int)sizeof(char);
@@ -3467,6 +3473,9 @@ PH7_PRIVATE sxi32 PH7_InputFormat(
} else {
iVal = ph7_value_to_int64(pArg);
}
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Limit the precision to prevent overflowing buf[] during conversion */
if(precision > PH7_FMT_BUFSIZ - 40) {
precision = PH7_FMT_BUFSIZ - 40;
@@ -3560,6 +3569,9 @@ PH7_PRIVATE sxi32 PH7_InputFormat(
} else {
realvalue = ph7_value_to_double(pArg);
}
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(precision < 0) {
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 */
zString = ph7_value_to_string(apArg[0], &nLen);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nLen < 1) {
/* Nothing to shuffle */
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 */
zMask = ph7_value_to_string(apArg[0], &nMasklen);
}
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nMasklen < 1) {
/* Invalid mask, return FALSE */
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 */
zMask = ph7_value_to_string(apArg[1], &nMasklen);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nMasklen < 1) {
/* Set a default mask */
#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;
sxi32 rc;
/* Extract visibility level */
iProtection = PH7_GetProtectionLevel(iProtection);
iProtection = PH7_ClassConstantProtection(pClass, PH7_GetProtectionLevel(iProtection));
pGen->pIn++; /* Jump the 'const' keyword */
loop:
/* Mark as constant */
iFlags |= PH7_CLASS_ATTR_CONSTANT;
if(pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & PH7_TK_ID) == 0) {
/* 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 */
pName = &pGen->pIn->sData;
@@ -3964,26 +3965,17 @@ static sxi32 PH7_GenStateCompileClassMethod(
/* Peek method name */
pName = &pGen->pIn->sData;
nLine = pGen->pIn->nLine;
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) {
PH7_GenCompileError(pGen, E_ERROR, nLine, "Method '%z::%z()' must return void", &pClass->sName, pName);
{
const char *zError = PH7_ClassMethodDeclarationError(pClass, pName, nType,
iProtection, iFlags);
if(zError) {
PH7_GenCompileError(pGen, E_ERROR, nLine, zError, &pClass->sName, pName);
}
}
/* Jump the method name */
pGen->pIn++;
if(iFlags & PH7_CLASS_ATTR_VIRTUAL) {
/* 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 */
doBody = FALSE;
}
@@ -4165,7 +4157,7 @@ static sxi32 PH7_CompileClassInterface(ph7_gen_state *pGen) {
/* Preserve constant visibility recovery; non-public methods are fatal. */
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->nLine, "interface: Access type must be public");
pGen->pIn->nLine, PH7_INTERFACE_VISIBILITY_ERROR);
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 */
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) == 0) {
/* Force a string cast */
PH7_MemObjToString(&(*pKey));
}
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
if(SyBlobLength(&pKey->sBlob) > 0 && !HashmapIsIntKey(&pKey->sBlob)) {
/* Perform a blob lookup */
rc = HashmapLookupBlobKey(&(*pMap), SyBlobData(&pKey->sBlob), SyBlobLength(&pKey->sBlob), &pNode);
@@ -530,6 +536,9 @@ static sxi32 HashmapLookup(
PH7_MemObjToInteger(pKey);
}
/* Perform an int lookup */
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
rc = HashmapLookupIntKey(&(*pMap), pKey->x.iVal, &pNode);
result:
if(rc == SXRET_OK) {
@@ -555,11 +564,17 @@ static sxi32 HashmapInsert(
) {
ph7_hashmap_node *pNode = 0;
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->nType & MEMOBJ_STRING) == 0) {
/* Force a string cast */
PH7_MemObjToString(&(*pKey));
}
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
if(SyBlobLength(&pKey->sBlob) < 1 || HashmapIsIntKey(&pKey->sBlob)) {
if(SyBlobLength(&pKey->sBlob) < 1) {
/* Automatic index assign */
@@ -587,6 +602,9 @@ IntKey:
/* Force an integer cast */
PH7_MemObjToInteger(pKey);
}
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
if(SXRET_OK == HashmapLookupIntKey(&(*pMap), pKey->x.iVal, &pNode)) {
/* Overwrite the old value */
ph7_value *pElem;
@@ -882,6 +900,10 @@ static int HashmapFindValueByCallback(
/* Perform an int cast */
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
* would fold 2^32 or INT64_MIN to 0 and report a bogus match. */
rc = sResult.x.iVal != 0;
@@ -1441,8 +1463,9 @@ PH7_PRIVATE sxi32 PH7_HashmapRelease(ph7_hashmap *pMap, int FreeDS) {
pEntry = pNext;
n++;
}
if(pMap->nEntry > 0) {
/* Release the hash bucket */
if(pMap->apBucket) {
/* Snapshot failure may leave buckets allocated before the first node
* is linked (for example, when reference installation fails). */
SyMemBackendFree(&pVm->sAllocator, pMap->apBucket);
}
if(FreeDS) {
@@ -1823,7 +1846,7 @@ static sxi32 HashmapPtrMerge(
sxu32 i = 0, j = 0, k = 0;
while(i < nA && j < nB) {
sxi32 rc = xCmp(apA[i], apB[j], pCtx);
if(pCtx->bAbort) {
if(pCtx->bAbort || apA[i]->pMap->pVm->iPendingTransfer) {
return SXERR_ABORT;
}
if(rc <= 0) {
@@ -1891,6 +1914,9 @@ static sxi32 HashmapSortPointers(ph7_hashmap *pMap, ProcNodeCmp xCmp, HashmapSor
sxu32 n = pMap->nEntry;
sxu32 i;
sxi32 rc;
if(pVm->iPendingTransfer) {
return SXERR_ABORT;
}
/* Allocate the pointer arrays */
apNodes = (ph7_hashmap_node **)SyMemBackendAlloc(&pVm->sAllocator, n * sizeof(ph7_hashmap_node *));
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) {
ph7_hashmap_node *p, *pLast;
sxu32 i;
if(pMap->pVm->iPendingTransfer) {
return;
}
/* Rehash all entries */
pLast = p = pMap->pFirst;
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;
}
}
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Create a new array */
pArray = ph7_context_new_array(pCtx);
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);
}
PH7_MemObjRelease(&sResult);
if(pMap->pVm->iPendingTransfer) {
return pMap->pVm->iPendingTransfer;
}
} else {
/* No available callback */
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_class_method *pMethod = 0;
sxi32 rc;
if(pVm->iPendingTransfer) {
return pVm->iPendingTransfer;
}
/* Check if the method is available */
if(pThis) {
pMethod = PH7_ClassExtractMethod(pThis->pClass, zMethod, nLen);
@@ -110,9 +114,11 @@ static sxi32 MemObjCallClassCastMethod(
return SXERR_NOTFOUND;
}
/* Invoke the desired method */
PH7_VmCallClassMethod(&(*pVm), &(*pThis), pMethod, &(*pResult), 0, 0);
/* Method successfully called,pResult should hold the return value */
return SXRET_OK;
rc = PH7_VmCallClassMethod(&(*pVm), &(*pThis), pMethod, &(*pResult), 0, 0);
/* Scalar conversion APIs cannot carry an exception object. Preserve the
* 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
@@ -444,24 +450,33 @@ PH7_PRIVATE void PH7_MemObjSetDeclaredType(ph7_value *pValue, sxu32 nType) {
* destination are of the compatible data types.
*/
PH7_PRIVATE sxi32 PH7_MemObjSafeStore(ph7_value *pSrc, ph7_value *pDest) {
/* Scalar destinations need not have initialized arena metadata. */
ph7_vm *pVm = (pSrc->nType & MEMOBJ_HASHMAP) ? pDest->pVm : 0;
/* Both recursive array copies and object finalizers can relocate storage. */
ph7_vm *pVm = pDest->pVm;
sxu32 nDest = pVm ? pDest->nIdx : SXU32_HIGH;
sxi32 bArena = pVm && SySetAt(&pVm->aMemObj, nDest) == pDest;
if(pVm && pVm->iPendingTransfer) {
return pVm->iPendingTransfer;
}
if(pDest->nDeclaredType & MEMOBJ_NAMED) {
sxu32 nDeclared = pDest->nDeclaredType;
sxi32 iFlags = pDest->iFlags;
ph7_value sValue, sPrevious;
sxi32 rc;
if(PH7_VmTypeCheck(pSrc, nDeclared) != SXRET_OK) {
return SXERR_NOMATCH;
}
/* Retain both payloads until storage has its contract again. Releasing
* the old object/map may invoke a destructor which writes through an
* alias of this binding. The source can also be the destination itself. */
PH7_MemObjInit(pDest->pVm, &sValue);
PH7_MemObjInit(pDest->pVm, &sPrevious);
PH7_MemObjInit(pVm, &sValue);
PH7_MemObjInit(pVm, &sPrevious);
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_MemObjRelease(pDest);
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) {
ProcMemObjCast xCast = PH7_MemObjCastMethod(pDest->nType);
xCast(pSrc);
if(pVm && pVm->iPendingTransfer) {
return pVm->iPendingTransfer;
}
if(bArena) {
pDest = (ph7_value *)SySetAt(&pVm->aMemObj, nDest);
}
PH7_MemObjStore(pSrc, pDest);
} else {
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);
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.
*/
@@ -1039,7 +1085,7 @@ PH7_PRIVATE sxi32 PH7_MemObjStore(ph7_value *pSrc, ph7_value *pDest) {
pObj = (ph7_class_instance *)pDest->x.pOther;
}
pDest->x = pSrc->x;
pDest->iFlags = pSrc->iFlags;
pDest->iFlags = pSrc->iFlags & ~MEMOBJ_INDEX_HANDLE;
pDest->nType = pSrc->nType;
pDest->nDeclaredType = pSrc->nDeclaredType;
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) {
pDest->x = pSrc->x;
pDest->iFlags = pSrc->iFlags;
pDest->iFlags = pSrc->iFlags & ~MEMOBJ_INDEX_HANDLE;
pDest->nType = pSrc->nType;
pDest->nDeclaredType = pSrc->nDeclaredType;
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);
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.
* 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
* 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) {
SyHashEntry *pEntry, *pOtherEntry;
sxu32 i, j;
@@ -295,8 +331,9 @@ static sxi32 ClassValidateAttributes(ph7_vm *pVm, ph7_class *pClass) {
SyString sAttr, sOther;
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_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);
return SXERR_INVALID;
}
}
}
@@ -348,14 +385,18 @@ static sxi32 ClassValidateMethods(ph7_vm *pVm, ph7_class *pClass) {
continue;
}
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
* against the effective implementation below, not a hidden method. */
if(pMeth->iProtection > pOther->iProtection &&
(k < SySetUsed(&pMeth->sFunc.pClass->aMro) ||
!((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.
* 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->sFunc.nType != pOther->sFunc.nType ||
((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)) {
@@ -380,16 +423,22 @@ static sxi32 ClassValidateMethods(ph7_vm *pVm, ph7_class *pClass) {
}
}
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
* a later implementation selected for its compatible contract. */
if(pImpl && (pImpl->sFunc.nType != pMeth->sFunc.nType ||
((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)) {
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:
@@ -423,12 +472,12 @@ PH7_PRIVATE sxi32 PH7_ClassCollectInterfaces(ph7_vm *pVm, ph7_class *pClass, SyS
SyString *pName = (SyString *)SySetAt(pNames, j);
pInterface = PH7_VmExtractClass(pVm, pName->zString, pName->nByte, FALSE);
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);
return SXERR_INVALID;
}
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);
return SXERR_INVALID;
}
@@ -485,7 +534,9 @@ static sxi32 ClassValidateInterfaces(ph7_vm *pVm, ph7_class *pClass) {
pOther = *(ph7_class_method **)SySetAt(&sRequired, j);
if(PH7_VmSameSignature(&pMeth->sFunc, &pOther->sFunc) &&
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(pOther->iProtection != PH7_CLASS_PROT_PUBLIC ||
(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)) {
pImpl = pOther;
@@ -515,7 +568,9 @@ static sxi32 ClassValidateInterfaces(ph7_vm *pVm, ph7_class *pClass) {
}
}
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:
@@ -544,9 +599,19 @@ PH7_PRIVATE sxi32 PH7_ClassBuildMro(ph7_vm *pVm, ph7_class *pClass) {
return SXRET_OK;
}
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;
}
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. */
pClass->iMroState = 1;
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);
pBase = PH7_VmExtractClass(pVm, pName->zString, pName->nByte, FALSE);
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) {
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) {
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) {
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++) {
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);
@@ -620,7 +695,7 @@ PH7_PRIVATE sxi32 PH7_ClassBuildMro(ph7_vm *pVm, ph7_class *pClass) {
break;
}
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;
goto done;
}
@@ -636,8 +711,10 @@ PH7_PRIVATE sxi32 PH7_ClassBuildMro(ph7_vm *pVm, ph7_class *pClass) {
}
}
/* Publish only a complete linearization. */
SySetRelease(&pClass->aBase);
SySetRelease(&pClass->aMro);
if(pVm->pClassJournal == 0) {
SySetRelease(&pClass->aBase);
SySetRelease(&pClass->aMro);
}
pClass->aBase = sBase;
pClass->aMro = sMro;
done:
@@ -783,7 +860,10 @@ PH7_PRIVATE sxi32 PH7_ClassInterfaceInherit(ph7_class *pSub, ph7_class *pBase) {
SyString *pName;
sxi32 rc;
/* 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);
/* Copy constants */
while((pEntry = SyHashGetNextEntry(&pBase->hAttr)) != 0) {
@@ -824,6 +904,71 @@ PH7_PRIVATE sxi32 PH7_ClassInterfaceInherit(ph7_class *pSub, ph7_class *pBase) {
/* All done */
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.
* 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;
pDestr = PH7_ClassExtractMethod(pThis->pClass, "__destruct", sizeof("__destruct") - 1);
if(pDestr) {
ph7_value sResult;
sxi32 rc;
/* A destructor may remove its own last ordinary reference. */
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);
}
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 */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
rc = pVfs->xMkdir(zPath, iMode, iRecursive);
/* IO return value */
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);
/* Extract the mode */
iMode = ph7_value_to_int(apArg[1]);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Perform the requested operation */
rc = pVfs->xChmod(zPath, iMode);
/* 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);
/* Extract the user */
zUser = ph7_value_to_string(apArg[1], 0);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Perform the requested operation */
rc = pVfs->xChown(zPath, zUser);
/* 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);
/* Extract the user */
zGroup = ph7_value_to_string(apArg[1], 0);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Perform the requested operation */
rc = pVfs->xChgrp(zPath, zGroup);
/* IO return value */
@@ -1488,6 +1500,9 @@ static int PH7_vfs_touch(ph7_context *pCtx, int nArg, ph7_value **apArg) {
nAccess = nTime;
}
}
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
rc = pVfs->xTouch(zFile, nTime, nAccess);
/* IO result */
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) {
iNew = ph7_value_to_int(apArg[0]);
}
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Perform the requested operation */
iOld = pVfs->xUmask(iNew);
/* Old mask */
@@ -2527,6 +2545,7 @@ static int PH7_builtin_ftruncate(ph7_context *pCtx, int nArg, ph7_value **apArg)
const ph7_io_stream *pStream;
io_private *pDev;
int rc;
ph7_int64 nSize;
if(nArg < 2 || !ph7_value_is_resource(apArg[0])) {
/* Missing/Invalid arguments, return FALSE */
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;
}
/* 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) {
/* Discard buffered data */
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]);
}
/* Perform the requested operation */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
rc = pStream->xSeek(pDev->pHandle, iOfft, whence);
if(rc == PH7_OK) {
/* Ignore buffered data */
@@ -2973,6 +2999,11 @@ PH7_PRIVATE void *PH7_StreamOpenHandle(ph7_vm *pVm, const ph7_io_stream *pStream
SyString sFile;
char sFilePath[PATH_MAX + 1];
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) {
/* No such stream device */
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]);
}
/* Perform the requested operation */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
n = StreamReadLine(pDev, &zLine, nLen);
if(n < 1) {
/* 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 */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
pBuf = ph7_context_alloc_chunk(pCtx, (unsigned int)nLen, FALSE, FALSE);
if(pBuf == 0) {
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]);
}
/* Perform the requested operation */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
n = StreamReadLine(pDev, &zLine, nLen);
if(n < 1) {
/* 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]);
}
/* Perform the requested operation */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
n = StreamReadLine(pDev, &zLine, nLen);
if(n < 1) {
/* 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) {
/* Extract the offset */
n = ph7_value_to_int64(apArg[3]);
if(pCtx->pVm->iPendingTransfer) {
PH7_StreamCloseHandle(pStream, pHandle);
return pCtx->pVm->iPendingTransfer;
}
if(n > 0) {
if(pStream->xSeek) {
/* 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 */
if(pCtx->pVm->iPendingTransfer) {
PH7_StreamCloseHandle(pStream, pHandle);
return pCtx->pVm->iPendingTransfer;
}
nRead = 0;
for(;;) {
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 */
zData = ph7_value_to_string(apArg[1], &nLen);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nLen < 1) {
/* Nothing to write, return immediately */
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;
}
}
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
pHandle = PH7_StreamOpenHandle(pCtx->pVm, pStream, zFile, iOpenFlags, use_include,
nArg > 3 ? apArg[3] : 0, FALSE, FALSE);
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 */
zString = ph7_value_to_string(apArg[1], &nLen);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nArg > 2) {
/* Maximum data length to write */
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;
}
}
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
if(nLen < 1) {
/* Nothing to write */
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 */
nLock = ph7_value_to_int(apArg[1]);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Lock operation */
rc = pStream->xLock(pDev->pHandle, nLock);
/* IO result */
@@ -4363,6 +4429,9 @@ static int csv_write_callback(ph7_value *pKey, ph7_value *pValue, void *pUserDat
sxu32 n;
/* Point to the raw data */
zData = ph7_value_to_string(pValue, &nLen);
if(pValue->pVm->iPendingTransfer) {
return pValue->pVm->iPendingTransfer;
}
if(nLen < 1) {
/* Nothing to write */
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 */
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
ph7_array_walk(apArg[1], csv_write_callback, &sCsv);
if(pCtx->pVm->iPendingTransfer) {
return pCtx->pVm->iPendingTransfer;
}
/* Write a line ending */
#ifdef __WINNT__
zEol = "\r\n";
+981 -270
View File
File diff suppressed because it is too large Load Diff