From 0bef2482980a27a15f76442ee6c748a76a6d174d Mon Sep 17 00:00:00 2001 From: belliash Date: Fri, 14 Jun 2019 17:11:34 +0200 Subject: [PATCH] Implement PH7_VmDestroyMemObj() to forcibly destroy a memory object. --- engine/vm.c | 24 ++++++++++++++++++++++++ include/ph7int.h | 1 + 2 files changed, 25 insertions(+) diff --git a/engine/vm.c b/engine/vm.c index 8c20bf6..7655f82 100644 --- a/engine/vm.c +++ b/engine/vm.c @@ -7507,6 +7507,30 @@ PH7_PRIVATE sxi32 PH7_VmUnsetMemObj(ph7_vm *pVm, sxu32 nObjIdx, int bForce) { } return SXRET_OK; } +/* + * Forcibly destroy a memory object [i.e: a ph7_value], remove it from + * the current frame, the reference table and discard it's contents. + * This function never fail and always return SXRET_OK. + */ +PH7_PRIVATE sxi32 PH7_VmDestroyMemObj(ph7_vm *pVm, ph7_value *pObj) { + VmRefObj *pRef; + if(pObj) { + /* Release the object */ + PH7_MemObjRelease(pObj); + /* Remove old reference links if available */ + pRef = VmRefObjExtract(&(*pVm), pObj->nIdx); + if(pRef) { + /* Unlink from the reference table */ + VmRefObjUnlink(&(*pVm), pRef); + } + VmSlot sFree; + /* Restore to the free list */ + sFree.nIdx = pObj->nIdx; + sFree.pUserData = 0; + SySetPut(&pVm->aFreeObj, (const void *)&sFree); + } + return SXRET_OK; +} /* * void unset($var,...) * Unset one or more given variable. diff --git a/include/ph7int.h b/include/ph7int.h index 1587da9..1f37459 100644 --- a/include/ph7int.h +++ b/include/ph7int.h @@ -1697,6 +1697,7 @@ PH7_PRIVATE sxi32 PH7_VmCallClassMethod(ph7_vm *pVm, ph7_class_instance *pThis, PH7_PRIVATE sxi32 PH7_VmCallUserFunction(ph7_vm *pVm, ph7_value *pFunc, int nArg, ph7_value **apArg, ph7_value *pResult); PH7_PRIVATE sxi32 PH7_VmCallUserFunctionAp(ph7_vm *pVm, ph7_value *pFunc, ph7_value *pResult, ...); PH7_PRIVATE sxi32 PH7_VmUnsetMemObj(ph7_vm *pVm, sxu32 nObjIdx, int bForce); +PH7_PRIVATE sxi32 PH7_VmDestroyMemObj(ph7_vm *pVm, ph7_value *pObj); PH7_PRIVATE void PH7_VmRandomString(ph7_vm *pVm, char *zBuf, int nLen); PH7_PRIVATE ph7_class *PH7_VmExtractActiveClass(ph7_vm *pVm, sxi32 iDepth); PH7_PRIVATE int PH7_VmIsCallable(ph7_vm *pVm, ph7_value *pValue, int CallInvoke);