Preserve pending completions through finally and restore operand stack on unwind
Build / AerScript (push) Successful in 38s

This commit is contained in:
2026-10-26 20:43:57 +01:00
parent d912ddf4df
commit 3c83579fb6
4 changed files with 479 additions and 351 deletions
+48 -21
View File
@@ -1450,6 +1450,33 @@ static sxi32 PH7_CompileBreak(ph7_gen_state *pGen) {
/* Statement successfully compiled */
return SXRET_OK;
}
/* Out-of-stream handlers and includes share the ordinary loop cleanup and
* fixups through small bridges in their immediately enclosing bytecode. */
static void GenStateCompileLoopBridge(ph7_gen_state *pGen, ph7_include *pBridge,
GenBlock *pLoop, sxu32 nLine) {
sxu32 nSkip;
SyToken sToken;
SyToken *pSavedIn = pGen->pIn, *pSavedEnd = pGen->pEnd;
pBridge->nBreak = pBridge->nContinue = SXU32_HIGH;
if(pLoop == 0) {
return;
}
PH7_VmEmitInstr(pGen->pVm, 0, PH7_OP_JMP, 0, 0, 0, &nSkip);
pBridge->nBreak = PH7_VmInstrLength(pGen->pVm);
SyZero(&sToken, sizeof(sToken));
sToken.nLine = nLine;
pGen->pIn = &sToken;
pGen->pEnd = &sToken + 1;
PH7_CompileBreak(pGen);
if((pLoop->iFlags & GEN_BLOCK_SWITCH) == 0) {
pBridge->nContinue = PH7_VmInstrLength(pGen->pVm);
pGen->pIn = &sToken;
PH7_CompileContinue(pGen);
}
pGen->pIn = pSavedIn;
pGen->pEnd = pSavedEnd;
PH7_VmGetInstr(pGen->pVm, nSkip)->iP2 = PH7_VmInstrLength(pGen->pVm);
}
/*
* Compile or record a label.
* A label is a target point that is specified by an identifier followed by a colon.
@@ -2784,7 +2811,6 @@ static sxi32 PH7_CompileInclude(ph7_gen_state *pGen) {
ph7_include *pInclude = (ph7_include *)SyMemBackendAlloc(&pGen->pVm->sAllocator, sizeof(ph7_include));
GenBlock *pBlock = pGen->pCurrent;
GenBlock *pLoop = PH7_GenStateFetchBlock(pBlock, GEN_BLOCK_LOOP, 0);
sxu32 nSkip;
if(pInclude == 0) {
PH7_GenCompileError(pGen, E_ERROR, nLine, "PH7 is running out-of-memory");
}
@@ -2800,26 +2826,7 @@ static sxi32 PH7_CompileInclude(ph7_gen_state *pGen) {
}
pInclude->pFunc = pBlock ? (ph7_vm_func *)pBlock->pUserData : 0;
PH7_VmEmitInstr(pGen->pVm, nLine, PH7_OP_INCLUDE, 0, 1, pInclude, 0);
if(pLoop) {
PH7_VmEmitInstr(pGen->pVm, 0, PH7_OP_JMP, 0, 0, 0, &nSkip);
pInclude->nBreak = PH7_VmInstrLength(pGen->pVm);
/* Reuse the ordinary compiler's loop cleanup and forward fixups. */
SyToken sToken;
SyToken *pSavedIn = pGen->pIn, *pSavedEnd = pGen->pEnd;
SyZero(&sToken, sizeof(sToken));
sToken.nLine = nLine;
pGen->pIn = &sToken;
pGen->pEnd = &sToken + 1;
PH7_CompileBreak(pGen);
if((pLoop->iFlags & GEN_BLOCK_SWITCH) == 0) {
pInclude->nContinue = PH7_VmInstrLength(pGen->pVm);
pGen->pIn = &sToken;
PH7_CompileContinue(pGen);
}
pGen->pIn = pSavedIn;
pGen->pEnd = pSavedEnd;
PH7_VmGetInstr(pGen->pVm, nSkip)->iP2 = PH7_VmInstrLength(pGen->pVm);
}
GenStateCompileLoopBridge(pGen, pInclude, pLoop, nLine);
}
return SXRET_OK;
}
@@ -4275,6 +4282,8 @@ static sxi32 PH7_CompileThrow(ph7_gen_state *pGen) {
*/
static sxi32 PH7_CompileCatch(ph7_gen_state *pGen, ph7_exception *pException) {
ph7_exception_block sCatch;
ph7_include sLoopBridge;
GenBlock *pLoop = PH7_GenStateFetchBlock(pGen->pCurrent, GEN_BLOCK_LOOP, 0);
SySet *pInstrContainer;
GenBlock *pCatch;
SyToken *pToken;
@@ -4337,6 +4346,7 @@ static sxi32 PH7_CompileCatch(ph7_gen_state *pGen, ph7_exception *pException) {
/* Compile the block */
pGen->pIn++; /* Jump the right parenthesis */
/* Create the catch block */
GenStateCompileLoopBridge(pGen, &sLoopBridge, pLoop, pGen->pIn->nLine);
rc = PH7_GenStateEnterBlock(&(*pGen), GEN_BLOCK_EXCEPTION, PH7_VmInstrLength(pGen->pVm), 0, &pCatch);
if(rc != SXRET_OK) {
return SXERR_ABORT;
@@ -4344,8 +4354,15 @@ static sxi32 PH7_CompileCatch(ph7_gen_state *pGen, ph7_exception *pException) {
/* Swap bytecode container */
pInstrContainer = PH7_VmGetByteCodeContainer(pGen->pVm);
PH7_VmSetByteCodeContainer(pGen->pVm, &sCatch.sByteCode);
if(pLoop) {
PH7_GenStateEnterBlock(pGen, GEN_BLOCK_LOOP | GEN_BLOCK_INCLUDE |
(pLoop->iFlags & GEN_BLOCK_SWITCH), 0, &sLoopBridge, 0);
}
/* Compile the block */
PH7_CompileBlock(&(*pGen));
if(pLoop) {
PH7_GenStateLeaveBlock(pGen, 0);
}
/* Fix forward jumps now the destination is resolved */
PH7_GenStateFixJumps(pCatch, -1, PH7_VmInstrLength(pGen->pVm));
/* Emit the DONE instruction */
@@ -4371,8 +4388,11 @@ static sxi32 PH7_CompileCatch(ph7_gen_state *pGen, ph7_exception *pException) {
static sxi32 PH7_CompileFinally(ph7_gen_state *pGen, ph7_exception *pException) {
SySet *pInstrContainer;
GenBlock *pFinally;
ph7_include sLoopBridge;
GenBlock *pLoop = PH7_GenStateFetchBlock(pGen->pCurrent, GEN_BLOCK_LOOP, 0);
sxi32 rc;
pGen->pIn++; /* Jump the 'finally' keyword */
GenStateCompileLoopBridge(pGen, &sLoopBridge, pLoop, pGen->pIn->nLine);
rc = PH7_GenStateEnterBlock(&(*pGen), GEN_BLOCK_EXCEPTION, PH7_VmInstrLength(pGen->pVm), 0, &pFinally);
if(rc != SXRET_OK) {
return SXERR_ABORT;
@@ -4380,8 +4400,15 @@ static sxi32 PH7_CompileFinally(ph7_gen_state *pGen, ph7_exception *pException)
/* Swap bytecode container */
pInstrContainer = PH7_VmGetByteCodeContainer(pGen->pVm);
PH7_VmSetByteCodeContainer(pGen->pVm, &pException->sFinally);
if(pLoop) {
PH7_GenStateEnterBlock(pGen, GEN_BLOCK_LOOP | GEN_BLOCK_INCLUDE |
(pLoop->iFlags & GEN_BLOCK_SWITCH), 0, &sLoopBridge, 0);
}
/* Compile the block */
PH7_CompileBlock(&(*pGen));
if(pLoop) {
PH7_GenStateLeaveBlock(pGen, 0);
}
/* Fix forward jumps now the destination is resolved */
PH7_GenStateFixJumps(pFinally, -1, PH7_VmInstrLength(pGen->pVm));
/* Emit the DONE instruction */
+39 -16
View File
@@ -948,19 +948,29 @@ static ph7_class_instance *NewClassInstance(ph7_vm *pVm, ph7_class *pClass) {
* See the block comment above for more information.
*/
PH7_PRIVATE ph7_class_instance *PH7_NewClassInstance(ph7_vm *pVm, ph7_class *pClass) {
ph7_class_instance *pNew;
PH7_NewClassInstanceWithResult(pVm, pClass, &pNew, 0);
return pNew;
}
/* Source construction needs both the status and the initializer's exception,
* while existing internal callers retain the pointer-returning interface. */
PH7_PRIVATE sxi32 PH7_NewClassInstanceWithResult(ph7_vm *pVm, ph7_class *pClass,
ph7_class_instance **ppNew, ph7_value *pResult) {
ph7_class_instance *pNew;
sxi32 rc;
*ppNew = 0;
pNew = NewClassInstance(&(*pVm), &(*pClass));
if(pNew == 0) {
return 0;
return SXERR_MEM;
}
/* Associate a private VM frame with this class instance */
rc = PH7_VmCreateClassInstanceFrame(&(*pVm), pNew);
rc = PH7_VmCreateClassInstanceFrame(&(*pVm), pNew, pResult);
if(rc != SXRET_OK) {
SyMemBackendPoolFree(&pVm->sAllocator, pNew);
return 0;
PH7_ClassInstanceAbort(pNew);
return rc;
}
return pNew;
*ppNew = pNew;
return SXRET_OK;
}
/*
* Extract the value of a class instance [i.e: Object in the PHP jargon] attribute.
@@ -1057,23 +1067,25 @@ static ph7_value *ExtractClassAttrValue(ph7_vm *pVm, VmClassAttr *pAttr) {
* )
*/
PH7_PRIVATE ph7_class_instance *PH7_CloneClassInstance(ph7_class_instance *pSrc) {
ph7_class_instance *pClone;
PH7_CloneClassInstanceWithResult(pSrc, &pClone, 0);
return pClone;
}
/* Preserve the pointer interface while source clone can propagate completion. */
PH7_PRIVATE sxi32 PH7_CloneClassInstanceWithResult(ph7_class_instance *pSrc,
ph7_class_instance **ppClone, ph7_value *pResult) {
ph7_class_instance *pClone;
ph7_class_method *pMethod;
SyHashEntry *pEntry2;
SyHashEntry *pEntry;
ph7_vm *pVm;
sxi32 rc;
/* Allocate a new instance */
*ppClone = 0;
/* Reuse construction's initializer completion and partial-instance cleanup. */
pVm = pSrc->pVm;
pClone = NewClassInstance(pVm, pSrc->pClass);
if(pClone == 0) {
return 0;
}
/* Associate a private VM frame with this class instance */
rc = PH7_VmCreateClassInstanceFrame(pVm, pClone);
rc = PH7_NewClassInstanceWithResult(pVm, pSrc->pClass, &pClone, pResult);
if(rc != SXRET_OK) {
SyMemBackendPoolFree(&pVm->sAllocator, pClone);
return 0;
return rc;
}
/* Duplicate object values */
SyHashResetLoopCursor(&pSrc->hAttr);
@@ -1096,19 +1108,30 @@ PH7_PRIVATE ph7_class_instance *PH7_CloneClassInstance(ph7_class_instance *pSrc)
if(pMethod) {
if(pMethod->iCloneDepth < 16) {
pMethod->iCloneDepth++;
PH7_VmCallClassMethod(pVm, pClone, pMethod, 0, 0, 0);
rc = PH7_VmCallClassMethod(pVm, pClone, pMethod, pResult, 0, 0);
} else {
/* Nesting limit reached */
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Object clone limit reached");
}
/* Reset the cursor */
pMethod->iCloneDepth = 0;
if(rc != SXRET_OK) {
PH7_ClassInstanceUnref(pClone);
return rc;
}
}
/* Return the cloned object */
return pClone;
*ppClone = pClone;
return SXRET_OK;
}
#define CLASS_INSTANCE_DESTROYED 0x001 /* Instance is released */
#define CLASS_INSTANCE_FINALIZED 0x002 /* Destructor has already been invoked */
/* Failed construction releases installed fields, but must not invoke the
* destructor on an instance whose initialization did not finish. */
PH7_PRIVATE void PH7_ClassInstanceAbort(ph7_class_instance *pThis) {
pThis->iFlags |= CLASS_INSTANCE_FINALIZED;
PH7_ClassInstanceUnref(pThis);
}
/* Run user finalization separately from storage release so VM teardown can
* keep every closure environment valid until destructors have finished. */
PH7_PRIVATE sxi32 PH7_ClassInstanceFinalize(ph7_class_instance *pThis) {
+377 -302
View File
File diff suppressed because it is too large Load Diff
+4 -1
View File
@@ -1686,7 +1686,7 @@ PH7_PRIVATE sxi32 PH7_VmInitFuncState(ph7_vm *pVm, ph7_vm_func *pFunc, const cha
sxi32 iFlags, void *pUserData);
PH7_PRIVATE sxi32 PH7_VmInstallUserFunction(ph7_vm *pVm, ph7_vm_func *pFunc, SyString *pName);
PH7_PRIVATE sxi32 PH7_VmSameSignature(ph7_vm_func *pFirst, ph7_vm_func *pSecond);
PH7_PRIVATE sxi32 PH7_VmCreateClassInstanceFrame(ph7_vm *pVm, ph7_class_instance *pObj);
PH7_PRIVATE sxi32 PH7_VmCreateClassInstanceFrame(ph7_vm *pVm, ph7_class_instance *pObj, ph7_value *pResult);
PH7_PRIVATE sxi32 PH7_VmRefObjRemove(ph7_vm *pVm, sxu32 nIdx, SyHashEntry *pEntry, ph7_hashmap_node *pMapEntry);
PH7_PRIVATE sxi32 PH7_VmRefObjInstall(ph7_vm *pVm, sxu32 nIdx, SyHashEntry *pEntry, ph7_hashmap_node *pMapEntry, sxi32 iFlags);
PH7_PRIVATE sxi32 PH7_VmPushFilePath(ph7_vm *pVm, const char *zPath, int nLen, sxu8 bMain, sxi32 *pNew);
@@ -1803,7 +1803,10 @@ PH7_PRIVATE sxi32 PH7_ClassCollectInterfaces(ph7_vm *pVm, ph7_class *pClass, SyS
PH7_PRIVATE sxi32 PH7_ClassInterfaceInherit(ph7_class *pSub, ph7_class *pBase);
PH7_PRIVATE sxi32 PH7_ClassImplement(ph7_vm *pVm, ph7_class *pMain, ph7_class *pInterface);
PH7_PRIVATE ph7_class_instance *PH7_NewClassInstance(ph7_vm *pVm, ph7_class *pClass);
PH7_PRIVATE sxi32 PH7_NewClassInstanceWithResult(ph7_vm *pVm, ph7_class *pClass, ph7_class_instance **ppNew, ph7_value *pResult);
PH7_PRIVATE void PH7_ClassInstanceAbort(ph7_class_instance *pThis);
PH7_PRIVATE ph7_class_instance *PH7_CloneClassInstance(ph7_class_instance *pSrc);
PH7_PRIVATE sxi32 PH7_CloneClassInstanceWithResult(ph7_class_instance *pSrc, ph7_class_instance **ppClone, ph7_value *pResult);
PH7_PRIVATE sxi32 PH7_ClassInstanceCmp(ph7_class_instance *pLeft, ph7_class_instance *pRight, int bStrict, int iNest);
PH7_PRIVATE void PH7_ClassInstanceUnref(ph7_class_instance *pThis);
PH7_PRIVATE sxi32 PH7_ClassInstanceFinalize(ph7_class_instance *pThis);