From 58b7ceeae9c33304625db545864e88aea3764e7b Mon Sep 17 00:00:00 2001 From: belliash Date: Tue, 11 Sep 2018 17:49:20 +0200 Subject: [PATCH] Get rid of $GLOBALS completely. --- engine/hashmap.c | 71 ++++-------------------------------------------- engine/vm.c | 54 ++++++++++++------------------------ 2 files changed, 23 insertions(+), 102 deletions(-) diff --git a/engine/hashmap.c b/engine/hashmap.c index 777aa94..dff4957 100644 --- a/engine/hashmap.c +++ b/engine/hashmap.c @@ -202,7 +202,7 @@ PH7_PRIVATE void PH7_HashmapUnlinkNode(ph7_hashmap_node *pNode, int bRestore) { } SyMemBackendPoolFree(&pVm->sAllocator, pNode); pMap->nEntry--; - if(pMap->nEntry < 1 && pMap != pVm->pGlobal) { + if(pMap->nEntry < 1) { /* Free the hash-bucket */ SyMemBackendFree(&pVm->sAllocator, pMap->apBucket); pMap->apBucket = 0; @@ -552,11 +552,6 @@ static sxi32 HashmapInsert( } return SXRET_OK; } - if(pMap == pMap->pVm->pGlobal) { - /* Forbidden */ - PH7_VmThrowError(pMap->pVm, PH7_CTX_NOTICE, "$GLOBALS is a read-only array, insertion is forbidden"); - return SXRET_OK; - } /* Perform a blob-key insertion */ rc = HashmapInsertBlobKey(&(*pMap), SyBlobData(&pKey->sBlob), SyBlobLength(&pKey->sBlob), &(*pVal), 0, FALSE); return rc; @@ -581,11 +576,6 @@ IntKey: } return SXRET_OK; } - if(pMap == pMap->pVm->pGlobal) { - /* Forbidden */ - PH7_VmThrowError(pMap->pVm, PH7_CTX_NOTICE, "$GLOBALS is a read-only array, insertion is forbidden"); - return SXRET_OK; - } /* Perform a 64-bit-int-key insertion */ rc = HashmapInsertIntKey(&(*pMap), pKey->x.iVal, &(*pVal), 0, FALSE); if(rc == SXRET_OK) { @@ -599,11 +589,6 @@ IntKey: } } } else { - if(pMap == pMap->pVm->pGlobal) { - /* Forbidden */ - PH7_VmThrowError(pMap->pVm, PH7_CTX_NOTICE, "$GLOBALS is a read-only array, insertion is forbidden"); - return SXRET_OK; - } /* Assign an automatic index */ rc = HashmapInsertIntKey(&(*pMap), pMap->iNextIdx, &(*pVal), 0, FALSE); if(rc == SXRET_OK) { @@ -1310,31 +1295,10 @@ PH7_PRIVATE sxi32 PH7_HashmapCreateSuper(ph7_vm *pVm) { "_ENV", /* $_ENV */ "_HEADER" /* $_HEADER */ }; - ph7_hashmap *pMap; - ph7_value *pObj; SyString *pFile; sxi32 rc; sxu32 n; - /* Allocate a new hashmap for the $GLOBALS array */ - pMap = PH7_NewHashmap(&(*pVm), 0, 0); - if(pMap == 0) { - return SXERR_MEM; - } - pVm->pGlobal = pMap; - /* Reserve a ph7_value for the $GLOBALS array*/ - pObj = PH7_ReserveMemObj(&(*pVm)); - if(pObj == 0) { - return SXERR_MEM; - } - PH7_MemObjInitFromArray(&(*pVm), pObj, pMap); - /* Record object index */ - pVm->nGlobalIdx = pObj->nIdx; - /* Install the special $GLOBALS array */ - rc = SyHashInsert(&pVm->hSuper, (const void *)"GLOBALS", sizeof("GLOBALS") - 1, SX_INT_TO_PTR(pVm->nGlobalIdx)); - if(rc != SXRET_OK) { - return rc; - } - /* Install superglobals now */ + /* Install superglobals */ for(n = 0 ; n < SX_ARRAYSIZE(azSuper) ; n++) { ph7_value *pSuper; /* Request an empty array */ @@ -1371,11 +1335,6 @@ PH7_PRIVATE sxi32 PH7_HashmapRelease(ph7_hashmap *pMap, int FreeDS) { ph7_hashmap_node *pEntry, *pNext; ph7_vm *pVm = pMap->pVm; sxu32 n; - if(pMap == pVm->pGlobal) { - /* Cannot delete the $GLOBALS array */ - PH7_VmThrowError(pMap->pVm, PH7_CTX_NOTICE, "$GLOBALS is a read-only array, deletion is forbidden"); - return SXRET_OK; - } /* Start the release process */ n = 0; pEntry = pMap->pFirst; @@ -1424,7 +1383,7 @@ PH7_PRIVATE void PH7_HashmapUnref(ph7_hashmap *pMap) { ph7_vm *pVm = pMap->pVm; /* TICKET 1432-49: $GLOBALS is not subject to garbage collection */ pMap->iRef--; - if(pMap->iRef < 1 && pMap != pVm->pGlobal) { + if(pMap->iRef < 1) { PH7_HashmapRelease(pMap, TRUE); } } @@ -1456,18 +1415,9 @@ PH7_PRIVATE sxi32 PH7_HashmapLookup( PH7_PRIVATE sxi32 PH7_HashmapInsert( ph7_hashmap *pMap, /* Target hashmap */ ph7_value *pKey, /* Lookup key */ - ph7_value *pVal /* Node value.NULL otherwise */ + ph7_value *pVal /* Node value, NULL otherwise */ ) { - sxi32 rc; - if(pVal && (pVal->iFlags & MEMOBJ_HASHMAP) && (ph7_hashmap *)pVal->x.pOther == pMap->pVm->pGlobal) { - /* - * TICKET 1433-35: Insertion in the $GLOBALS array is forbidden. - */ - PH7_VmThrowError(pMap->pVm, PH7_CTX_ERR, "$GLOBALS is a read-only array, insertion is forbidden"); - return SXRET_OK; - } - rc = HashmapInsert(&(*pMap), &(*pKey), &(*pVal)); - return rc; + return HashmapInsert(&(*pMap), &(*pKey), &(*pVal)); } /* * Insert a given key and it's associated value (foreign index) in the given @@ -1501,16 +1451,7 @@ PH7_PRIVATE sxi32 PH7_HashmapInsertByRef( ph7_value *pKey, /* Lookup key */ sxu32 nRefIdx /* Foreign ph7_value index */ ) { - sxi32 rc; - if(nRefIdx == pMap->pVm->nGlobalIdx) { - /* - * TICKET 1433-35: Insertion in the $GLOBALS array is forbidden. - */ - PH7_VmThrowError(pMap->pVm, PH7_CTX_ERR, "$GLOBALS is a read-only array, insertion is forbidden"); - return SXRET_OK; - } - rc = HashmapInsertByRef(&(*pMap), &(*pKey), nRefIdx); - return rc; + return HashmapInsertByRef(&(*pMap), &(*pKey), nRefIdx); } /* * Reset the node cursor of a given hashmap. diff --git a/engine/vm.c b/engine/vm.c index 7c6903e..0de3ca5 100644 --- a/engine/vm.c +++ b/engine/vm.c @@ -1371,14 +1371,9 @@ static ph7_value *VmExtractMemObj( SySetPut(&pVm->aFreeObj, (const void *)&sLocal); return 0; } - if(pFrame->pParent != 0) { - /* Local variable */ - sLocal.nIdx = nIdx; - SySetPut(&pFrame->sLocal, (const void *)&sLocal); - } else { - /* Register in the $GLOBALS array */ - VmHashmapRefInsert(pVm->pGlobal, pName->zString, pName->nByte, nIdx); - } + /* Register local variable */ + sLocal.nIdx = nIdx; + SySetPut(&pFrame->sLocal, (const void *)&sLocal); /* Install in the reference table */ PH7_VmRefObjInstall(&(*pVm), nIdx, SyHashLastEntry(&pFrame->hVar), 0, 0); /* Save object index */ @@ -1588,10 +1583,6 @@ PH7_PRIVATE sxi32 PH7_VmConfigure( } /* Install in the reference table */ PH7_VmRefObjInstall(&(*pVm), nIdx, pRef, 0, 0); - if(nOp == PH7_VM_CONFIG_CREATE_SUPER || pVm->pFrame->pParent == 0) { - /* Register in the $GLOBALS array */ - VmHashmapRefInsert(pVm->pGlobal, zName, nByte, nIdx); - } } } break; @@ -3954,27 +3945,19 @@ static sxi32 VmByteCodeExec( pTos->nIdx = pObj->nIdx; } } else if(sName.nByte > 0) { - if((pTos->iFlags & MEMOBJ_HASHMAP) && (pVm->pGlobal == (ph7_hashmap *)pTos->x.pOther)) { - PH7_VmThrowError(&(*pVm), PH7_CTX_ERR, "$GLOBALS is a read-only array and therefore cannot be referenced"); + VmFrame *pFrame = pVm->pFrame; + while(pFrame->pParent && (pFrame->iFlags & VM_FRAME_EXCEPTION)) { + /* Safely ignore the exception frame */ + pFrame = pFrame->pParent; + } + /* Query the local frame */ + pEntry = SyHashGet(&pFrame->hVar, (const void *)sName.zString, sName.nByte); + if(pEntry) { + PH7_VmThrowError(&(*pVm), PH7_CTX_ERR, "Referenced variable name '%z' already exists", &sName); } else { - VmFrame *pFrame = pVm->pFrame; - while(pFrame->pParent && (pFrame->iFlags & VM_FRAME_EXCEPTION)) { - /* Safely ignore the exception frame */ - pFrame = pFrame->pParent; - } - /* Query the local frame */ - pEntry = SyHashGet(&pFrame->hVar, (const void *)sName.zString, sName.nByte); - if(pEntry) { - PH7_VmThrowError(&(*pVm), PH7_CTX_ERR, "Referenced variable name '%z' already exists", &sName); - } else { - rc = SyHashInsert(&pFrame->hVar, (const void *)sName.zString, sName.nByte, SX_INT_TO_PTR(nIdx)); - if(pFrame->pParent == 0) { - /* Insert in the $GLOBALS array */ - VmHashmapRefInsert(pVm->pGlobal, sName.zString, sName.nByte, nIdx); - } - if(rc == SXRET_OK) { - PH7_VmRefObjInstall(&(*pVm), nIdx, SyHashLastEntry(&pFrame->hVar), 0, 0); - } + rc = SyHashInsert(&pFrame->hVar, (const void *)sName.zString, sName.nByte, SX_INT_TO_PTR(nIdx)); + if(rc == SXRET_OK) { + PH7_VmRefObjInstall(&(*pVm), nIdx, SyHashLastEntry(&pFrame->hVar), 0, 0); } } } @@ -8359,10 +8342,7 @@ static int vm_builtin_unset(ph7_context *pCtx, int nArg, ph7_value **apArg) { } } else { sxu32 nIdx = pObj->nIdx; - /* TICKET 1433-35: Protect the $GLOBALS array from deletion */ - if(nIdx != pVm->nGlobalIdx) { - PH7_VmUnsetMemObj(&(*pVm), nIdx, FALSE); - } + PH7_VmUnsetMemObj(&(*pVm), nIdx, FALSE); } } return SXRET_OK; @@ -8379,7 +8359,7 @@ static sxi32 VmHashVarWalker(SyHashEntry *pEntry, void *pUserData) { nIdx = SX_PTR_TO_INT(pEntry->pUserData); pObj = (ph7_value *)SySetAt(&pVm->aMemObj, nIdx); if(pObj) { - if((pObj->iFlags & MEMOBJ_HASHMAP) == 0 || (ph7_hashmap *)pObj->x.pOther != pVm->pGlobal) { + if((pObj->iFlags & MEMOBJ_HASHMAP) == 0) { if(pEntry->nKeyLen > 0) { SyString sName; ph7_value sKey;