Browse Source

Store variable type in dedicated field.

release/v0.1
Rafal Kupiec 1 year ago
parent
commit
3cca5faa76
Signed by: belliash GPG Key ID: 4E829243E0CFE6B4
6 changed files with 393 additions and 393 deletions
  1. +24
    -24
      engine/api.c
  2. +1
    -1
      engine/compiler.c
  3. +41
    -41
      engine/hashmap.c
  4. +172
    -172
      engine/memobj.c
  5. +153
    -153
      engine/vm.c
  6. +2
    -2
      include/ph7int.h

+ 24
- 24
engine/api.c View File

@ -1236,7 +1236,7 @@ const char *ph7_value_to_string(ph7_value *pValue, int *pLen) {
* Please refer to the official documentation for function purpose and expected parameters.
*/
void *ph7_value_to_resource(ph7_value *pValue) {
if((pValue->iFlags & MEMOBJ_RES) == 0) {
if((pValue->nType & MEMOBJ_RES) == 0) {
/* Not a resource,return NULL */
return 0;
}
@ -1310,7 +1310,7 @@ int ph7_result_string_format(ph7_context *pCtx, const char *zFormat, ...) {
va_list ap;
int rc;
p = pCtx->pRet;
if((p->iFlags & MEMOBJ_STRING) == 0) {
if((p->nType & MEMOBJ_STRING) == 0) {
/* Invalidate any prior representation */
PH7_MemObjRelease(p);
MemObjSetType(p, MEMOBJ_STRING);
@ -1469,7 +1469,7 @@ ph7_value *ph7_array_fetch(ph7_value *pArray, const char *zKey, int nByte) {
ph7_value skey;
int rc;
/* Make sure we are dealing with a valid hashmap */
if((pArray->iFlags & MEMOBJ_HASHMAP) == 0) {
if((pArray->nType & MEMOBJ_HASHMAP) == 0) {
return 0;
}
if(nByte < 0) {
@ -1499,7 +1499,7 @@ int ph7_array_walk(ph7_value *pArray, int (*xWalk)(ph7_value *pValue, ph7_value
return PH7_CORRUPT;
}
/* Make sure we are dealing with a valid hashmap */
if((pArray->iFlags & MEMOBJ_HASHMAP) == 0) {
if((pArray->nType & MEMOBJ_HASHMAP) == 0) {
return PH7_CORRUPT;
}
/* Start the walk process */
@ -1513,7 +1513,7 @@ int ph7_array_walk(ph7_value *pArray, int (*xWalk)(ph7_value *pValue, ph7_value
int ph7_array_add_elem(ph7_value *pArray, ph7_value *pKey, ph7_value *pValue) {
int rc;
/* Make sure we are dealing with a valid hashmap */
if((pArray->iFlags & MEMOBJ_HASHMAP) == 0) {
if((pArray->nType & MEMOBJ_HASHMAP) == 0) {
return PH7_CORRUPT;
}
/* Perform the insertion */
@ -1527,7 +1527,7 @@ int ph7_array_add_elem(ph7_value *pArray, ph7_value *pKey, ph7_value *pValue) {
int ph7_array_add_strkey_elem(ph7_value *pArray, const char *zKey, ph7_value *pValue) {
int rc;
/* Make sure we are dealing with a valid hashmap */
if((pArray->iFlags & MEMOBJ_HASHMAP) == 0) {
if((pArray->nType & MEMOBJ_HASHMAP) == 0) {
return PH7_CORRUPT;
}
/* Perform the insertion */
@ -1551,7 +1551,7 @@ int ph7_array_add_intkey_elem(ph7_value *pArray, int iKey, ph7_value *pValue) {
ph7_value sKey;
int rc;
/* Make sure we are dealing with a valid hashmap */
if((pArray->iFlags & MEMOBJ_HASHMAP) == 0) {
if((pArray->nType & MEMOBJ_HASHMAP) == 0) {
return PH7_CORRUPT;
}
PH7_MemObjInitFromInt(pArray->pVm, &sKey, iKey);
@ -1567,7 +1567,7 @@ int ph7_array_add_intkey_elem(ph7_value *pArray, int iKey, ph7_value *pValue) {
unsigned int ph7_array_count(ph7_value *pArray) {
ph7_hashmap *pMap;
/* Make sure we are dealing with a valid hashmap */
if((pArray->iFlags & MEMOBJ_HASHMAP) == 0) {
if((pArray->nType & MEMOBJ_HASHMAP) == 0) {
return 0;
}
/* Point to the internal representation of the hashmap */
@ -1584,7 +1584,7 @@ int ph7_object_walk(ph7_value *pObject, int (*xWalk)(const char *, ph7_value *,
return PH7_CORRUPT;
}
/* Make sure we are dealing with a valid class instance */
if((pObject->iFlags & MEMOBJ_OBJ) == 0) {
if((pObject->nType & MEMOBJ_OBJ) == 0) {
return PH7_CORRUPT;
}
/* Start the walk process */
@ -1599,7 +1599,7 @@ ph7_value *ph7_object_fetch_attr(ph7_value *pObject, const char *zAttr) {
ph7_value *pValue;
SyString sAttr;
/* Make sure we are dealing with a valid class instance */
if((pObject->iFlags & MEMOBJ_OBJ) == 0 || zAttr == 0) {
if((pObject->nType & MEMOBJ_OBJ) == 0 || zAttr == 0) {
return 0;
}
SyStringInitFromBuf(&sAttr, zAttr, SyStrlen(zAttr));
@ -1618,7 +1618,7 @@ const char *ph7_object_get_class_name(ph7_value *pObject, int *pLength) {
*pLength = 0;
}
/* Make sure we are dealing with a valid class instance */
if((pObject->iFlags & MEMOBJ_OBJ) == 0) {
if((pObject->nType & MEMOBJ_OBJ) == 0) {
return 0;
}
/* Point to the class */
@ -1809,7 +1809,7 @@ int ph7_value_void(ph7_value *pVal) {
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_string(ph7_value *pVal, const char *zString, int nLen) {
if((pVal->iFlags & MEMOBJ_STRING) == 0) {
if((pVal->nType & MEMOBJ_STRING) == 0) {
/* Invalidate any prior representation */
PH7_MemObjRelease(pVal);
MemObjSetType(pVal, MEMOBJ_STRING);
@ -1830,7 +1830,7 @@ int ph7_value_string(ph7_value *pVal, const char *zString, int nLen) {
int ph7_value_string_format(ph7_value *pVal, const char *zFormat, ...) {
va_list ap;
int rc;
if((pVal->iFlags & MEMOBJ_STRING) == 0) {
if((pVal->nType & MEMOBJ_STRING) == 0) {
/* Invalidate any prior representation */
PH7_MemObjRelease(pVal);
MemObjSetType(pVal, MEMOBJ_STRING);
@ -1874,42 +1874,42 @@ int ph7_value_release(ph7_value *pVal) {
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_is_int(ph7_value *pVal) {
return (pVal->iFlags & MEMOBJ_INT) ? TRUE : FALSE;
return (pVal->nType & MEMOBJ_INT) ? TRUE : FALSE;
}
/*
* [CAPIREF: ph7_value_is_float()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_is_float(ph7_value *pVal) {
return (pVal->iFlags & MEMOBJ_REAL) ? TRUE : FALSE;
return (pVal->nType & MEMOBJ_REAL) ? TRUE : FALSE;
}
/*
* [CAPIREF: ph7_value_is_bool()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_is_bool(ph7_value *pVal) {
return (pVal->iFlags & MEMOBJ_BOOL) ? TRUE : FALSE;
return (pVal->nType & MEMOBJ_BOOL) ? TRUE : FALSE;
}
/*
* [CAPIREF: ph7_value_is_char()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_is_char(ph7_value *pVal) {
return (pVal->iFlags & MEMOBJ_CHAR) ? TRUE : FALSE;
return (pVal->nType & MEMOBJ_CHAR) ? TRUE : FALSE;
}
/*
* [CAPIREF: ph7_value_is_void()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_is_void(ph7_value *pVal) {
return (pVal->iFlags & MEMOBJ_VOID) ? TRUE : FALSE;
return (pVal->nType & MEMOBJ_VOID) ? TRUE : FALSE;
}
/*
* [CAPIREF: ph7_value_is_string()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_is_string(ph7_value *pVal) {
return (pVal->iFlags & MEMOBJ_STRING) ? TRUE : FALSE;
return (pVal->nType & MEMOBJ_STRING) ? TRUE : FALSE;
}
/*
* [CAPIREF: ph7_value_is_numeric()]
@ -1934,33 +1934,33 @@ int ph7_value_is_callable(ph7_value *pVal) {
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_is_callback(ph7_value *pVal) {
return (pVal->iFlags & MEMOBJ_CALL) ? TRUE : FALSE;
return (pVal->nType & MEMOBJ_CALL) ? TRUE : FALSE;
}
/*
* [CAPIREF: ph7_value_is_scalar()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_is_scalar(ph7_value *pVal) {
return (pVal->iFlags & MEMOBJ_SCALAR) ? TRUE : FALSE;
return (pVal->nType & MEMOBJ_SCALAR) ? TRUE : FALSE;
}
/*
* [CAPIREF: ph7_value_is_array()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_is_array(ph7_value *pVal) {
return (pVal->iFlags & MEMOBJ_HASHMAP) ? TRUE : FALSE;
return (pVal->nType & MEMOBJ_HASHMAP) ? TRUE : FALSE;
}
/*
* [CAPIREF: ph7_value_is_object()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_is_object(ph7_value *pVal) {
return (pVal->iFlags & MEMOBJ_OBJ) ? TRUE : FALSE;
return (pVal->nType & MEMOBJ_OBJ) ? TRUE : FALSE;
}
/*
* [CAPIREF: ph7_value_is_resource()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int ph7_value_is_resource(ph7_value *pVal) {
return (pVal->iFlags & MEMOBJ_RES) ? TRUE : FALSE;
return (pVal->nType & MEMOBJ_RES) ? TRUE : FALSE;
}

+ 1
- 1
engine/compiler.c View File

@ -947,7 +947,7 @@ PH7_PRIVATE sxi32 PH7_CompileClosure(ph7_gen_state *pGen, sxi32 iCompileFlag) {
}
SyStringInitFromBuf(&sName, zName, nLen);
PH7_MemObjInitFromString(pGen->pVm, pObj, &sName);
pObj->iFlags = MEMOBJ_CALL;
pObj->nType = MEMOBJ_CALL;
/* Compile the closure body */
rc = PH7_GenStateCompileFunc(&(*pGen), &sName, 0, TRUE, &pAnonFunc);
if(rc == SXERR_ABORT) {

+ 41
- 41
engine/hashmap.c View File

@ -69,7 +69,7 @@ static sxi64 HashmapCount(ph7_hashmap *pMap, int bRecursive, int iRecCount) {
/* Point to the element value */
pElem = (ph7_value *)SySetAt(&pMap->pVm->aMemObj, pEntry->nValIdx);
if(pElem) {
if(pElem->iFlags & MEMOBJ_HASHMAP) {
if(pElem->nType & MEMOBJ_HASHMAP) {
if(iRecCount > 31) {
/* Nesting limit reached */
return iCount;
@ -455,8 +455,8 @@ static sxi32 HashmapLookup(
) {
ph7_hashmap_node *pNode = 0; /* cc -O6 warning */
sxi32 rc;
if(pKey->iFlags & (MEMOBJ_STRING | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) {
if((pKey->iFlags & MEMOBJ_STRING) == 0) {
if(pKey->nType & (MEMOBJ_STRING | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) {
if((pKey->nType & MEMOBJ_STRING) == 0) {
/* Force a string cast */
PH7_MemObjToString(&(*pKey));
}
@ -467,7 +467,7 @@ static sxi32 HashmapLookup(
}
}
/* Perform an int lookup */
if((pKey->iFlags & MEMOBJ_INT) == 0) {
if((pKey->nType & MEMOBJ_INT) == 0) {
/* Force an integer cast */
PH7_MemObjToInteger(pKey);
}
@ -497,8 +497,8 @@ static sxi32 HashmapInsert(
) {
ph7_hashmap_node *pNode = 0;
sxi32 rc = SXRET_OK;
if(pKey && pKey->iFlags & (MEMOBJ_STRING | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) {
if((pKey->iFlags & MEMOBJ_STRING) == 0) {
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));
}
@ -530,7 +530,7 @@ static sxi32 HashmapInsert(
}
IntKey:
if(pKey) {
if((pKey->iFlags & MEMOBJ_INT) == 0) {
if((pKey->nType & MEMOBJ_INT) == 0) {
/* Force an integer cast */
PH7_MemObjToInteger(pKey);
}
@ -698,9 +698,9 @@ static int HashmapFindValue(
/* Extract node value */
pVal = HashmapExtractNodeValue(pEntry);
if(pVal) {
if((pVal->iFlags | pNeedle->iFlags) & MEMOBJ_NULL) {
sxi32 iF1 = pVal->iFlags;
sxi32 iF2 = pNeedle->iFlags;
if((pVal->nType | pNeedle->nType) & MEMOBJ_NULL) {
sxi32 iF1 = pVal->nType;
sxi32 iF2 = pNeedle->nType;
if(iF1 == iF2) {
/* NULL values are equals */
if(ppNode) {
@ -769,7 +769,7 @@ static int HashmapFindValueByCallback(
rc = PH7_VmCallUserFunction(pMap->pVm, pCallback, 2, apArg, &sResult);
if(rc == SXRET_OK) {
/* Extract callback result */
if((sResult.iFlags & MEMOBJ_INT) == 0) {
if((sResult.nType & MEMOBJ_INT) == 0) {
/* Perform an int cast */
PH7_MemObjToInteger(&sResult);
}
@ -1490,10 +1490,10 @@ static sxi32 HashmapCmpCallback1(ph7_hashmap_node *pA, ph7_hashmap_node *pB, voi
PH7_HashmapExtractNodeValue(pB, &sB, FALSE);
if(iFlags == 5) {
/* String cast */
if((sA.iFlags & MEMOBJ_STRING) == 0) {
if((sA.nType & MEMOBJ_STRING) == 0) {
PH7_MemObjToString(&sA);
}
if((sB.iFlags & MEMOBJ_STRING) == 0) {
if((sB.nType & MEMOBJ_STRING) == 0) {
PH7_MemObjToString(&sB);
}
} else {
@ -1569,10 +1569,10 @@ static sxi32 HashmapCmpCallback3(ph7_hashmap_node *pA, ph7_hashmap_node *pB, voi
PH7_HashmapExtractNodeValue(pB, &sB, FALSE);
if(iFlags == 5) {
/* String cast */
if((sA.iFlags & MEMOBJ_STRING) == 0) {
if((sA.nType & MEMOBJ_STRING) == 0) {
PH7_MemObjToString(&sA);
}
if((sB.iFlags & MEMOBJ_STRING) == 0) {
if((sB.nType & MEMOBJ_STRING) == 0) {
PH7_MemObjToString(&sB);
}
} else {
@ -1611,7 +1611,7 @@ static sxi32 HashmapCmpCallback4(ph7_hashmap_node *pA, ph7_hashmap_node *pB, voi
rc = -1; /* Set a dummy result */
} else {
/* Extract callback result */
if((sResult.iFlags & MEMOBJ_INT) == 0) {
if((sResult.nType & MEMOBJ_INT) == 0) {
/* Perform an int cast */
PH7_MemObjToInteger(&sResult);
}
@ -1691,7 +1691,7 @@ static sxi32 HashmapCmpCallback6(ph7_hashmap_node *pA, ph7_hashmap_node *pB, voi
rc = -1; /* Set a dummy result */
} else {
/* Extract callback result */
if((sResult.iFlags & MEMOBJ_INT) == 0) {
if((sResult.nType & MEMOBJ_INT) == 0) {
/* Perform an int cast */
PH7_MemObjToInteger(&sResult);
}
@ -4333,7 +4333,7 @@ static int ph7_hashmap_flip(ph7_context *pCtx, int nArg, ph7_value **apArg) {
for(n = 0 ; n < pSrc->nEntry ; n++) {
/* Extract the node value */
pKey = HashmapExtractNodeValue(pEntry);
if(pKey && (pKey->iFlags & MEMOBJ_NULL) == 0) {
if(pKey && (pKey->nType & MEMOBJ_NULL) == 0) {
/* Prepare the value for insertion */
if(pEntry->iType == HASHMAP_INT_NODE) {
PH7_MemObjInitFromInt(pSrc->pVm, &sVal, pEntry->xKey.iKey);
@ -4372,12 +4372,12 @@ static void DoubleSum(ph7_context *pCtx, ph7_hashmap *pMap) {
pEntry = pMap->pFirst;
for(n = 0 ; n < pMap->nEntry ; n++) {
pObj = HashmapExtractNodeValue(pEntry);
if(pObj && (pObj->iFlags & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->iFlags & MEMOBJ_REAL) {
if(pObj && (pObj->nType & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->nType & MEMOBJ_REAL) {
dSum += pObj->x.rVal;
} else if(pObj->iFlags & (MEMOBJ_INT | MEMOBJ_BOOL)) {
} else if(pObj->nType & (MEMOBJ_INT | MEMOBJ_BOOL)) {
dSum += (double)pObj->x.iVal;
} else if(pObj->iFlags & MEMOBJ_STRING) {
} else if(pObj->nType & MEMOBJ_STRING) {
if(SyBlobLength(&pObj->sBlob) > 0) {
double dv = 0;
SyStrToReal((const char *)SyBlobData(&pObj->sBlob), SyBlobLength(&pObj->sBlob), (void *)&dv, 0);
@ -4399,12 +4399,12 @@ static void Int64Sum(ph7_context *pCtx, ph7_hashmap *pMap) {
pEntry = pMap->pFirst;
for(n = 0 ; n < pMap->nEntry ; n++) {
pObj = HashmapExtractNodeValue(pEntry);
if(pObj && (pObj->iFlags & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->iFlags & MEMOBJ_REAL) {
if(pObj && (pObj->nType & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->nType & MEMOBJ_REAL) {
nSum += (sxi64)pObj->x.rVal;
} else if(pObj->iFlags & (MEMOBJ_INT | MEMOBJ_BOOL)) {
} else if(pObj->nType & (MEMOBJ_INT | MEMOBJ_BOOL)) {
nSum += pObj->x.iVal;
} else if(pObj->iFlags & MEMOBJ_STRING) {
} else if(pObj->nType & MEMOBJ_STRING) {
if(SyBlobLength(&pObj->sBlob) > 0) {
sxi64 nv = 0;
SyStrToInt64((const char *)SyBlobData(&pObj->sBlob), SyBlobLength(&pObj->sBlob), (void *)&nv, 0);
@ -4449,7 +4449,7 @@ static int ph7_hashmap_sum(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_result_int(pCtx, 0);
return PH7_OK;
}
if(pObj->iFlags & MEMOBJ_REAL) {
if(pObj->nType & MEMOBJ_REAL) {
DoubleSum(pCtx, pMap);
} else {
Int64Sum(pCtx, pMap);
@ -4473,12 +4473,12 @@ static void DoubleProd(ph7_context *pCtx, ph7_hashmap *pMap) {
dProd = 1;
for(n = 0 ; n < pMap->nEntry ; n++) {
pObj = HashmapExtractNodeValue(pEntry);
if(pObj && (pObj->iFlags & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->iFlags & MEMOBJ_REAL) {
if(pObj && (pObj->nType & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->nType & MEMOBJ_REAL) {
dProd *= pObj->x.rVal;
} else if(pObj->iFlags & (MEMOBJ_INT | MEMOBJ_BOOL)) {
} else if(pObj->nType & (MEMOBJ_INT | MEMOBJ_BOOL)) {
dProd *= (double)pObj->x.iVal;
} else if(pObj->iFlags & MEMOBJ_STRING) {
} else if(pObj->nType & MEMOBJ_STRING) {
if(SyBlobLength(&pObj->sBlob) > 0) {
double dv = 0;
SyStrToReal((const char *)SyBlobData(&pObj->sBlob), SyBlobLength(&pObj->sBlob), (void *)&dv, 0);
@ -4501,12 +4501,12 @@ static void Int64Prod(ph7_context *pCtx, ph7_hashmap *pMap) {
nProd = 1;
for(n = 0 ; n < pMap->nEntry ; n++) {
pObj = HashmapExtractNodeValue(pEntry);
if(pObj && (pObj->iFlags & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->iFlags & MEMOBJ_REAL) {
if(pObj && (pObj->nType & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->nType & MEMOBJ_REAL) {
nProd *= (sxi64)pObj->x.rVal;
} else if(pObj->iFlags & (MEMOBJ_INT | MEMOBJ_BOOL)) {
} else if(pObj->nType & (MEMOBJ_INT | MEMOBJ_BOOL)) {
nProd *= pObj->x.iVal;
} else if(pObj->iFlags & MEMOBJ_STRING) {
} else if(pObj->nType & MEMOBJ_STRING) {
if(SyBlobLength(&pObj->sBlob) > 0) {
sxi64 nv = 0;
SyStrToInt64((const char *)SyBlobData(&pObj->sBlob), SyBlobLength(&pObj->sBlob), (void *)&nv, 0);
@ -4551,7 +4551,7 @@ static int ph7_hashmap_product(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_result_int(pCtx, 0);
return PH7_OK;
}
if(pObj->iFlags & MEMOBJ_REAL) {
if(pObj->nType & MEMOBJ_REAL) {
DoubleProd(pCtx, pMap);
} else {
Int64Prod(pCtx, pMap);
@ -5124,7 +5124,7 @@ static int HashmapWalkRecursive(
/* Extract the node value */
pValue = HashmapExtractNodeValue(pEntry);
if(pValue) {
if(pValue->iFlags & MEMOBJ_HASHMAP) {
if(pValue->nType & MEMOBJ_HASHMAP) {
if(iNest < 32) {
/* Recurse */
iNest++;
@ -5392,8 +5392,8 @@ PH7_PRIVATE sxi32 PH7_HashmapWalk(
*/
PH7_PRIVATE sxi32 PH7_HashmapCast(ph7_value *pObj, sxi32 nType) {
sxi32 rc;
if((pObj->iFlags & MEMOBJ_HASHMAP)) {
if((pObj->iFlags & nType) == 0) {
if((pObj->nType & MEMOBJ_HASHMAP)) {
if((pObj->nType & nType) == 0) {
ph7_hashmap *pMap;
ph7_hashmap_node *pNode;
ph7_value pValue, pKey;
@ -5413,10 +5413,10 @@ PH7_PRIVATE sxi32 PH7_HashmapCast(ph7_value *pObj, sxi32 nType) {
}
PH7_HashmapInsert(pMap, &pKey, &pValue);
}
pObj->iFlags = MEMOBJ_HASHMAP | nType;
pObj->nType = MEMOBJ_HASHMAP | nType;
}
} else {
if(pObj->iFlags != nType && PH7_CheckVarCompat(pObj, nType) != SXRET_OK) {
if(pObj->nType != nType && PH7_CheckVarCompat(pObj, nType) != SXRET_OK) {
return SXERR_NOMATCH;
}
ProcMemObjCast xCast = PH7_MemObjCastMethod(nType);

+ 172
- 172
engine/memobj.c View File

@ -124,23 +124,23 @@ static sxi32 MemObjCallClassCastMethod(
* If pObj represents a NULL value, return 0.
*/
static sxi64 MemObjIntValue(ph7_value *pObj) {
sxi32 iFlags;
iFlags = pObj->iFlags;
if(iFlags & MEMOBJ_REAL) {
sxi32 nType;
nType = pObj->nType;
if(nType & MEMOBJ_REAL) {
return MemObjRealToInt(&(*pObj));
} else if(iFlags & (MEMOBJ_INT | MEMOBJ_BOOL | MEMOBJ_CHAR)) {
} else if(nType & (MEMOBJ_INT | MEMOBJ_BOOL | MEMOBJ_CHAR)) {
return pObj->x.iVal;
} else if(iFlags & MEMOBJ_STRING) {
} else if(nType & MEMOBJ_STRING) {
return MemObjStringToInt(&(*pObj));
} else if(iFlags & (MEMOBJ_CALL | MEMOBJ_NULL | MEMOBJ_VOID)) {
} else if(nType & (MEMOBJ_CALL | MEMOBJ_NULL | MEMOBJ_VOID)) {
return 0;
} else if(iFlags & MEMOBJ_HASHMAP) {
} else if(nType & MEMOBJ_HASHMAP) {
ph7_hashmap *pMap = (ph7_hashmap *)pObj->x.pOther;
sxu32 n = pMap->nEntry;
PH7_HashmapUnref(pMap);
/* Return total number of entries in the hashmap */
return n;
} else if(iFlags & MEMOBJ_OBJ) {
} else if(nType & MEMOBJ_OBJ) {
ph7_value sResult;
sxi64 iVal = 1;
sxi32 rc;
@ -148,14 +148,14 @@ static sxi64 MemObjIntValue(ph7_value *pObj) {
PH7_MemObjInit(pObj->pVm, &sResult);
rc = MemObjCallClassCastMethod(pObj->pVm, (ph7_class_instance *)pObj->x.pOther,
"__toInt", sizeof("__toInt") - 1, &sResult);
if(rc == SXRET_OK && (sResult.iFlags & MEMOBJ_INT)) {
if(rc == SXRET_OK && (sResult.nType & MEMOBJ_INT)) {
/* Extract method return value */
iVal = sResult.x.iVal;
}
PH7_ClassInstanceUnref((ph7_class_instance *)pObj->x.pOther);
PH7_MemObjRelease(&sResult);
return iVal;
} else if(iFlags & MEMOBJ_RES) {
} else if(nType & MEMOBJ_RES) {
return pObj->x.pOther != 0;
}
/* CANT HAPPEN */
@ -172,13 +172,13 @@ static sxi64 MemObjIntValue(ph7_value *pObj) {
* If pObj represents a NULL value, return 0.0
*/
static ph7_real MemObjRealValue(ph7_value *pObj) {
sxi32 iFlags;
iFlags = pObj->iFlags;
if(iFlags & MEMOBJ_REAL) {
sxi32 nType;
nType = pObj->nType;
if(nType & MEMOBJ_REAL) {
return pObj->x.rVal;
} else if(iFlags & (MEMOBJ_INT | MEMOBJ_BOOL | MEMOBJ_CHAR)) {
} else if(nType & (MEMOBJ_INT | MEMOBJ_BOOL | MEMOBJ_CHAR)) {
return (ph7_real)pObj->x.iVal;
} else if(iFlags & MEMOBJ_STRING) {
} else if(nType & MEMOBJ_STRING) {
SyString sString;
ph7_real rVal = 0.0;
SyStringInitFromBuf(&sString, SyBlobData(&pObj->sBlob), SyBlobLength(&pObj->sBlob));
@ -187,15 +187,15 @@ static ph7_real MemObjRealValue(ph7_value *pObj) {
SyStrToReal(sString.zString, sString.nByte, (void *)&rVal, 0);
}
return rVal;
} else if(iFlags & (MEMOBJ_CALL | MEMOBJ_NULL | MEMOBJ_VOID)) {
} else if(nType & (MEMOBJ_CALL | MEMOBJ_NULL | MEMOBJ_VOID)) {
return 0.0;
} else if(iFlags & MEMOBJ_HASHMAP) {
} else if(nType & MEMOBJ_HASHMAP) {
/* Return the total number of entries in the hashmap */
ph7_hashmap *pMap = (ph7_hashmap *)pObj->x.pOther;
ph7_real n = (ph7_real)pMap->nEntry;
PH7_HashmapUnref(pMap);
return n;
} else if(iFlags & MEMOBJ_OBJ) {
} else if(nType & MEMOBJ_OBJ) {
ph7_value sResult;
ph7_real rVal = 1;
sxi32 rc;
@ -203,14 +203,14 @@ static ph7_real MemObjRealValue(ph7_value *pObj) {
PH7_MemObjInit(pObj->pVm, &sResult);
rc = MemObjCallClassCastMethod(pObj->pVm, (ph7_class_instance *)pObj->x.pOther,
"__toFloat", sizeof("__toFloat") - 1, &sResult);
if(rc == SXRET_OK && (sResult.iFlags & MEMOBJ_REAL)) {
if(rc == SXRET_OK && (sResult.nType & MEMOBJ_REAL)) {
/* Extract method return value */
rVal = sResult.x.rVal;
}
PH7_ClassInstanceUnref((ph7_class_instance *)pObj->x.pOther);
PH7_MemObjRelease(&sResult);
return rVal;
} else if(iFlags & MEMOBJ_RES) {
} else if(nType & MEMOBJ_RES) {
return (ph7_real)(pObj->x.pOther != 0);
}
/* NOT REACHED */
@ -221,12 +221,12 @@ static ph7_real MemObjRealValue(ph7_value *pObj) {
* This function never fail and always return SXRET_OK.
*/
static sxi32 MemObjStringValue(SyBlob *pOut, ph7_value *pObj, sxu8 bStrictBool) {
if(pObj->iFlags & MEMOBJ_REAL) {
if(pObj->nType & MEMOBJ_REAL) {
SyBlobFormat(&(*pOut), "%.15g", pObj->x.rVal);
} else if(pObj->iFlags & MEMOBJ_INT) {
} else if(pObj->nType & MEMOBJ_INT) {
SyBlobFormat(&(*pOut), "%qd", pObj->x.iVal);
/* %qd (BSD quad) is equivalent to %lld in the libc printf */
} else if(pObj->iFlags & MEMOBJ_BOOL) {
} else if(pObj->nType & MEMOBJ_BOOL) {
if(pObj->x.iVal) {
SyBlobAppend(&(*pOut), "TRUE", sizeof("TRUE") - 1);
} else {
@ -234,21 +234,21 @@ static sxi32 MemObjStringValue(SyBlob *pOut, ph7_value *pObj, sxu8 bStrictBool)
SyBlobAppend(&(*pOut), "FALSE", sizeof("FALSE") - 1);
}
}
} else if(pObj->iFlags & MEMOBJ_CHAR) {
} else if(pObj->nType & MEMOBJ_CHAR) {
if(pObj->x.iVal > 0) {
SyBlobFormat(&(*pOut), "%c", pObj->x.iVal);
}
} else if(pObj->iFlags & MEMOBJ_HASHMAP) {
} else if(pObj->nType & MEMOBJ_HASHMAP) {
SyBlobAppend(&(*pOut), "Array", sizeof("Array") - 1);
PH7_HashmapUnref((ph7_hashmap *)pObj->x.pOther);
} else if(pObj->iFlags & MEMOBJ_OBJ) {
} else if(pObj->nType & MEMOBJ_OBJ) {
ph7_value sResult;
sxi32 rc;
/* Invoke the __toString() method if available */
PH7_MemObjInit(pObj->pVm, &sResult);
rc = MemObjCallClassCastMethod(pObj->pVm, (ph7_class_instance *)pObj->x.pOther,
"__toString", sizeof("__toString") - 1, &sResult);
if(rc == SXRET_OK && (sResult.iFlags & MEMOBJ_STRING) && SyBlobLength(&sResult.sBlob) > 0) {
if(rc == SXRET_OK && (sResult.nType & MEMOBJ_STRING) && SyBlobLength(&sResult.sBlob) > 0) {
/* Expand method return value */
SyBlobDup(&sResult.sBlob, pOut);
} else {
@ -257,7 +257,7 @@ static sxi32 MemObjStringValue(SyBlob *pOut, ph7_value *pObj, sxu8 bStrictBool)
}
PH7_ClassInstanceUnref((ph7_class_instance *)pObj->x.pOther);
PH7_MemObjRelease(&sResult);
} else if(pObj->iFlags & MEMOBJ_RES) {
} else if(pObj->nType & MEMOBJ_RES) {
SyBlobFormat(&(*pOut), "ResourceID_%#x", pObj->x.pOther);
}
return SXRET_OK;
@ -275,13 +275,13 @@ static sxi32 MemObjStringValue(SyBlob *pOut, ph7_value *pObj, sxu8 bStrictBool)
* an array with zero elements.
*/
static sxi32 MemObjBooleanValue(ph7_value *pObj) {
sxi32 iFlags;
iFlags = pObj->iFlags;
if(iFlags & MEMOBJ_REAL) {
sxi32 nType;
nType = pObj->nType;
if(nType & MEMOBJ_REAL) {
return pObj->x.rVal != 0.0 ? 1 : 0;
} else if(iFlags & (MEMOBJ_INT | MEMOBJ_CHAR)) {
} else if(nType & (MEMOBJ_INT | MEMOBJ_CHAR)) {
return pObj->x.iVal ? 1 : 0;
} else if(iFlags & MEMOBJ_STRING) {
} else if(nType & MEMOBJ_STRING) {
SyString sString;
SyStringInitFromBuf(&sString, SyBlobData(&pObj->sBlob), SyBlobLength(&pObj->sBlob));
if(sString.nByte == 0) {
@ -302,7 +302,7 @@ static sxi32 MemObjBooleanValue(ph7_value *pObj) {
}
return zIn >= zEnd ? 0 : 1;
}
} else if(iFlags & MEMOBJ_CALL) {
} else if(nType & MEMOBJ_CALL) {
SyString sString;
SyStringInitFromBuf(&sString, SyBlobData(&pObj->sBlob), SyBlobLength(&pObj->sBlob));
if(sString.nByte == 0) {
@ -312,14 +312,14 @@ static sxi32 MemObjBooleanValue(ph7_value *pObj) {
/* Something set, return true */
return 1;
}
} else if(iFlags & (MEMOBJ_NULL | MEMOBJ_VOID)) {
} else if(nType & (MEMOBJ_NULL | MEMOBJ_VOID)) {
return 0;
} else if(iFlags & MEMOBJ_HASHMAP) {
} else if(nType & MEMOBJ_HASHMAP) {
ph7_hashmap *pMap = (ph7_hashmap *)pObj->x.pOther;
sxu32 n = pMap->nEntry;
PH7_HashmapUnref(pMap);
return n > 0 ? TRUE : FALSE;
} else if(iFlags & MEMOBJ_OBJ) {
} else if(nType & MEMOBJ_OBJ) {
ph7_value sResult;
sxi32 iVal = 1;
sxi32 rc;
@ -330,14 +330,14 @@ static sxi32 MemObjBooleanValue(ph7_value *pObj) {
PH7_MemObjInit(pObj->pVm, &sResult);
rc = MemObjCallClassCastMethod(pObj->pVm, (ph7_class_instance *)pObj->x.pOther,
"__toBool", sizeof("__toBool") - 1, &sResult);
if(rc == SXRET_OK && (sResult.iFlags & (MEMOBJ_INT | MEMOBJ_BOOL))) {
if(rc == SXRET_OK && (sResult.nType & (MEMOBJ_INT | MEMOBJ_BOOL))) {
/* Extract method return value */
iVal = (sxi32)(sResult.x.iVal != 0); /* Stupid cc warning -W -Wall -O6 */
}
PH7_ClassInstanceUnref((ph7_class_instance *)pObj->x.pOther);
PH7_MemObjRelease(&sResult);
return iVal;
} else if(iFlags & MEMOBJ_RES) {
} else if(nType & MEMOBJ_RES) {
return pObj->x.pOther != 0;
}
/* NOT REACHED */
@ -348,13 +348,13 @@ static sxi32 MemObjBooleanValue(ph7_value *pObj) {
* This function never fail and always return SXRET_OK.
*/
static ph7_real MemObjCharValue(ph7_value *pObj) {
sxi32 iFlags;
iFlags = pObj->iFlags;
if(iFlags & (MEMOBJ_CALL | MEMOBJ_REAL | MEMOBJ_HASHMAP | MEMOBJ_RES | MEMOBJ_NULL | MEMOBJ_VOID)) {
sxi32 nType;
nType = pObj->nType;
if(nType & (MEMOBJ_CALL | MEMOBJ_REAL | MEMOBJ_HASHMAP | MEMOBJ_RES | MEMOBJ_NULL | MEMOBJ_VOID)) {
return 0;
} else if(iFlags & MEMOBJ_INT) {
} else if(nType & MEMOBJ_INT) {
return pObj->x.iVal;
} else if(iFlags & MEMOBJ_STRING) {
} else if(nType & MEMOBJ_STRING) {
SyString sString;
SyStringInitFromBuf(&sString, SyBlobData(&pObj->sBlob), SyBlobLength(&pObj->sBlob));
if(sString.nByte == 0) {
@ -370,26 +370,26 @@ static ph7_real MemObjCharValue(ph7_value *pObj) {
* Checks a ph7_value variable compatibility with nType data type.
*/
PH7_PRIVATE sxi32 PH7_CheckVarCompat(ph7_value *pObj, int nType) {
if(((nType & MEMOBJ_HASHMAP) == 0) && ((pObj->iFlags & MEMOBJ_HASHMAP) == 0)) {
if(pObj->iFlags & MEMOBJ_NULL) {
if(((nType & MEMOBJ_HASHMAP) == 0) && ((pObj->nType & MEMOBJ_HASHMAP) == 0)) {
if(pObj->nType & MEMOBJ_NULL) {
/* Always allow to store a NULL */
return SXRET_OK;
} else if((nType & MEMOBJ_REAL) && (pObj->iFlags & MEMOBJ_INT)) {
} else if((nType & MEMOBJ_REAL) && (pObj->nType & MEMOBJ_INT)) {
/* Allow to store INT to FLOAT variable */
return SXRET_OK;
} else if((nType & MEMOBJ_CHAR) && (pObj->iFlags & MEMOBJ_INT)) {
} else if((nType & MEMOBJ_CHAR) && (pObj->nType & MEMOBJ_INT)) {
/* Allow to store INT to CHAR variable */
return SXRET_OK;
} else if((nType & MEMOBJ_STRING) && (pObj->iFlags & MEMOBJ_CHAR)) {
} else if((nType & MEMOBJ_STRING) && (pObj->nType & MEMOBJ_CHAR)) {
/* Allow to store CHAR to STRING variable */
return SXRET_OK;
} else if((nType & MEMOBJ_CHAR) && (pObj->iFlags & MEMOBJ_STRING)) {
} else if((nType & MEMOBJ_CHAR) && (pObj->nType & MEMOBJ_STRING)) {
/* Allow to store STRING to CHAR variable (if not too long) */
int len = SyBlobLength(&pObj->sBlob);
if(len == 0 || len == 1) {
return SXRET_OK;
}
} else if((nType & MEMOBJ_CALL) && (pObj->iFlags & MEMOBJ_STRING)) {
} else if((nType & MEMOBJ_CALL) && (pObj->nType & MEMOBJ_STRING)) {
/* Allow to store STRING to CALLBACK variable */
return SXRET_OK;
}
@ -401,48 +401,48 @@ PH7_PRIVATE sxi32 PH7_CheckVarCompat(ph7_value *pObj, int nType) {
* destination are of the compatible data types.
*/
PH7_PRIVATE sxi32 PH7_MemObjSafeStore(ph7_value *pSrc, ph7_value *pDest) {
if(pDest->iFlags == 0 || ((pDest->iFlags | MEMOBJ_FIXEDVAL | MEMOBJ_PARENTOBJ) == (pSrc->iFlags | MEMOBJ_FIXEDVAL | MEMOBJ_PARENTOBJ))) {
if(pDest->nType == 0 || ((pDest->nType | MEMOBJ_FIXEDVAL | MEMOBJ_PARENTOBJ) == (pSrc->nType | MEMOBJ_FIXEDVAL | MEMOBJ_PARENTOBJ))) {
PH7_MemObjStore(pSrc, pDest);
} else if(pDest->iFlags & MEMOBJ_MIXED) {
if(pDest->iFlags & MEMOBJ_HASHMAP) {
} else if(pDest->nType & MEMOBJ_MIXED) {
if(pDest->nType & MEMOBJ_HASHMAP) {
/* mixed[] */
if(pSrc->iFlags & MEMOBJ_HASHMAP) {
if(pSrc->nType & MEMOBJ_HASHMAP) {
PH7_MemObjStore(pSrc, pDest);
pDest->iFlags |= MEMOBJ_MIXED;
} else if(pSrc->iFlags & MEMOBJ_NULL) {
pDest->nType |= MEMOBJ_MIXED;
} else if(pSrc->nType & MEMOBJ_NULL) {
PH7_MemObjToHashmap(pSrc);
MemObjSetType(pSrc, pDest->iFlags);
MemObjSetType(pSrc, pDest->nType);
PH7_MemObjStore(pSrc, pDest);
} else {
return SXERR_NOMATCH;
}
} else {
/* mixed */
if(pSrc->iFlags == MEMOBJ_NULL) {
if(pSrc->nType == MEMOBJ_NULL) {
MemObjSetType(pDest, MEMOBJ_MIXED | MEMOBJ_VOID);
} else if((pSrc->iFlags & MEMOBJ_HASHMAP) == 0) {
} else if((pSrc->nType & MEMOBJ_HASHMAP) == 0) {
PH7_MemObjStore(pSrc, pDest);
pDest->iFlags |= MEMOBJ_MIXED;
pDest->nType |= MEMOBJ_MIXED;
} else {
return SXERR_NOMATCH;
}
}
} else if((pDest->iFlags & MEMOBJ_HASHMAP)) {
} else if((pDest->nType & MEMOBJ_HASHMAP)) {
/* [] */
if(pSrc->iFlags & MEMOBJ_NULL) {
if(pSrc->nType & MEMOBJ_NULL) {
PH7_MemObjToHashmap(pSrc);
MemObjSetType(pSrc, pDest->iFlags);
MemObjSetType(pSrc, pDest->nType);
PH7_MemObjStore(pSrc, pDest);
} else if(pSrc->iFlags & MEMOBJ_HASHMAP) {
if(PH7_HashmapCast(pSrc, pDest->iFlags ^ MEMOBJ_HASHMAP) != SXRET_OK) {
} else if(pSrc->nType & MEMOBJ_HASHMAP) {
if(PH7_HashmapCast(pSrc, pDest->nType ^ MEMOBJ_HASHMAP) != SXRET_OK) {
return SXERR_NOMATCH;
}
PH7_MemObjStore(pSrc, pDest);
} else {
return SXERR_NOMATCH;
}
} else if(PH7_CheckVarCompat(pSrc, pDest->iFlags) == SXRET_OK) {
ProcMemObjCast xCast = PH7_MemObjCastMethod(pDest->iFlags);
} else if(PH7_CheckVarCompat(pSrc, pDest->nType) == SXRET_OK) {
ProcMemObjCast xCast = PH7_MemObjCastMethod(pDest->nType);
xCast(pSrc);
PH7_MemObjStore(pSrc, pDest);
} else {
@ -454,7 +454,7 @@ PH7_PRIVATE sxi32 PH7_MemObjSafeStore(ph7_value *pSrc, ph7_value *pDest) {
* Convert a ph7_value to type integer.Invalidate any prior representations.
*/
PH7_PRIVATE sxi32 PH7_MemObjToInteger(ph7_value *pObj) {
if((pObj->iFlags & MEMOBJ_INT) == 0) {
if((pObj->nType & MEMOBJ_INT) == 0) {
/* Preform the conversion */
pObj->x.iVal = MemObjIntValue(&(*pObj));
/* Invalidate any prior representations */
@ -468,7 +468,7 @@ PH7_PRIVATE sxi32 PH7_MemObjToInteger(ph7_value *pObj) {
* Invalidate any prior representations
*/
PH7_PRIVATE sxi32 PH7_MemObjToReal(ph7_value *pObj) {
if((pObj->iFlags & MEMOBJ_REAL) == 0) {
if((pObj->nType & MEMOBJ_REAL) == 0) {
/* Preform the conversion */
pObj->x.rVal = MemObjRealValue(&(*pObj));
/* Invalidate any prior representations */
@ -481,7 +481,7 @@ PH7_PRIVATE sxi32 PH7_MemObjToReal(ph7_value *pObj) {
* Convert a ph7_value to type boolean.Invalidate any prior representations.
*/
PH7_PRIVATE sxi32 PH7_MemObjToBool(ph7_value *pObj) {
if((pObj->iFlags & MEMOBJ_BOOL) == 0) {
if((pObj->nType & MEMOBJ_BOOL) == 0) {
/* Preform the conversion */
pObj->x.iVal = MemObjBooleanValue(&(*pObj));
/* Invalidate any prior representations */
@ -491,7 +491,7 @@ PH7_PRIVATE sxi32 PH7_MemObjToBool(ph7_value *pObj) {
return SXRET_OK;
}
PH7_PRIVATE sxi32 PH7_MemObjToChar(ph7_value *pObj) {
if((pObj->iFlags & MEMOBJ_CHAR) == 0) {
if((pObj->nType & MEMOBJ_CHAR) == 0) {
/* Preform the conversion */
pObj->x.iVal = MemObjCharValue(&(*pObj));
/* Invalidate any prior representations */
@ -504,7 +504,7 @@ PH7_PRIVATE sxi32 PH7_MemObjToChar(ph7_value *pObj) {
* Convert a ph7_value to type void.Invalidate any prior representations.
*/
PH7_PRIVATE sxi32 PH7_MemObjToVoid(ph7_value *pObj) {
if((pObj->iFlags & MEMOBJ_VOID) == 0) {
if((pObj->nType & MEMOBJ_VOID) == 0) {
PH7_MemObjRelease(pObj);
MemObjSetType(pObj, MEMOBJ_VOID);
}
@ -512,7 +512,7 @@ PH7_PRIVATE sxi32 PH7_MemObjToVoid(ph7_value *pObj) {
}
PH7_PRIVATE sxi32 PH7_MemObjToCallback(ph7_value *pObj) {
sxi32 rc = SXRET_OK;
if((pObj->iFlags & (MEMOBJ_CALL | MEMOBJ_STRING)) == 0) {
if((pObj->nType & (MEMOBJ_CALL | MEMOBJ_STRING)) == 0) {
SyBlobReset(&pObj->sBlob); /* Reset the internal buffer */
rc = MemObjStringValue(&pObj->sBlob, &(*pObj), TRUE);
}
@ -521,10 +521,10 @@ PH7_PRIVATE sxi32 PH7_MemObjToCallback(ph7_value *pObj) {
}
PH7_PRIVATE sxi32 PH7_MemObjToResource(ph7_value *pObj) {
sxi32 rc = SXRET_OK;
if((pObj->iFlags & MEMOBJ_NULL) == 0) {
if((pObj->nType & MEMOBJ_NULL) == 0) {
PH7_VmThrowError(&(*pObj->pVm), PH7_CTX_WARNING, "Unsafe type casting condition, assuming default value");
}
if((pObj->iFlags & MEMOBJ_RES) == 0) {
if((pObj->nType & MEMOBJ_RES) == 0) {
pObj->x.iVal = 0;
}
MemObjSetType(pObj, MEMOBJ_RES);
@ -535,8 +535,8 @@ PH7_PRIVATE sxi32 PH7_MemObjToResource(ph7_value *pObj) {
*/
PH7_PRIVATE sxi32 PH7_MemObjToString(ph7_value *pObj) {
sxi32 rc = SXRET_OK;
if((pObj->iFlags & MEMOBJ_STRING) == 0) {
if((pObj->iFlags & MEMOBJ_CALL) == 0) {
if((pObj->nType & MEMOBJ_STRING) == 0) {
if((pObj->nType & MEMOBJ_CALL) == 0) {
/* Perform the conversion */
SyBlobReset(&pObj->sBlob); /* Reset the internal buffer */
rc = MemObjStringValue(&pObj->sBlob, &(*pObj), TRUE);
@ -553,21 +553,21 @@ PH7_PRIVATE sxi32 PH7_MemObjToString(ph7_value *pObj) {
* and the value of the scalar which was converted.
*/
PH7_PRIVATE sxi32 PH7_MemObjToHashmap(ph7_value *pObj) {
if((pObj->iFlags & MEMOBJ_HASHMAP) == 0) {
if((pObj->nType & MEMOBJ_HASHMAP) == 0) {
ph7_hashmap *pMap;
/* Allocate a new hashmap instance */
pMap = PH7_NewHashmap(pObj->pVm, 0, 0);
if(pMap == 0) {
return SXERR_MEM;
}
if((pObj->iFlags & (MEMOBJ_NULL | MEMOBJ_RES)) == 0) {
if((pObj->nType & (MEMOBJ_NULL | MEMOBJ_RES)) == 0) {
/*
* According to the PHP language reference manual.
* For any of the types: integer, float, string, boolean converting a value
* to an array results in an array with a single element with index zero
* and the value of the scalar which was converted.
*/
if(pObj->iFlags & MEMOBJ_OBJ) {
if(pObj->nType & MEMOBJ_OBJ) {
/* Object cast */
PH7_ClassInstanceToHashmap((ph7_class_instance *)pObj->x.pOther, pMap);
} else {
@ -599,12 +599,12 @@ PH7_PRIVATE sxi32 PH7_MemObjToHashmap(ph7_value *pObj) {
* Refer to the official documentation for more information.
*/
PH7_PRIVATE sxi32 PH7_MemObjToObject(ph7_value *pObj) {
if(pObj->iFlags & MEMOBJ_NULL) {
if(pObj->nType & MEMOBJ_NULL) {
/* Invalidate any prior representation */
PH7_MemObjRelease(pObj);
/* Typecast the value */
MemObjSetType(pObj, MEMOBJ_OBJ);
} else if((pObj->iFlags & MEMOBJ_OBJ) == 0) {
} else if((pObj->nType & MEMOBJ_OBJ) == 0) {
ph7_class_instance *pStd;
ph7_class_method *pCons;
ph7_class *pClass;
@ -655,24 +655,24 @@ PH7_PRIVATE sxi32 PH7_MemObjToObject(ph7_value *pObj) {
* to variable $var, $var becomes a string. If an integer value is then
* assigned to $var, it becomes an integer.
*/
PH7_PRIVATE ProcMemObjCast PH7_MemObjCastMethod(sxi32 iFlags) {
if(iFlags & MEMOBJ_STRING) {
PH7_PRIVATE ProcMemObjCast PH7_MemObjCastMethod(sxi32 nType) {
if(nType & MEMOBJ_STRING) {
return PH7_MemObjToString;
} else if(iFlags & MEMOBJ_INT) {
} else if(nType & MEMOBJ_INT) {
return PH7_MemObjToInteger;
} else if(iFlags & MEMOBJ_REAL) {
} else if(nType & MEMOBJ_REAL) {
return PH7_MemObjToReal;
} else if(iFlags & MEMOBJ_BOOL) {
} else if(nType & MEMOBJ_BOOL) {
return PH7_MemObjToBool;
} else if(iFlags & MEMOBJ_CHAR) {
} else if(nType & MEMOBJ_CHAR) {
return PH7_MemObjToChar;
} else if(iFlags & MEMOBJ_OBJ) {
} else if(nType & MEMOBJ_OBJ) {
return PH7_MemObjToObject;
} else if(iFlags & MEMOBJ_CALL) {
} else if(nType & MEMOBJ_CALL) {
return PH7_MemObjToCallback;
} else if(iFlags & MEMOBJ_RES) {
} else if(nType & MEMOBJ_RES) {
return PH7_MemObjToResource;
} else if(iFlags & MEMOBJ_VOID) {
} else if(nType & MEMOBJ_VOID) {
return PH7_MemObjToVoid;
}
/* Release the variable */
@ -684,7 +684,7 @@ PH7_PRIVATE ProcMemObjCast PH7_MemObjCastMethod(sxi32 iFlags) {
* Return TRUE if numeric.FALSE otherwise.
*/
PH7_PRIVATE sxi32 PH7_MemObjIsNumeric(ph7_value *pObj) {
if(pObj->iFlags & (MEMOBJ_INT | MEMOBJ_REAL)) {
if(pObj->nType & (MEMOBJ_INT | MEMOBJ_REAL)) {
return TRUE;
}
return FALSE;
@ -698,9 +698,9 @@ PH7_PRIVATE sxi32 PH7_MemObjIsNumeric(ph7_value *pObj) {
* and ignore the rest.
*/
PH7_PRIVATE sxi32 PH7_MemObjToNumeric(ph7_value *pObj) {
if(pObj->iFlags & (MEMOBJ_INT | MEMOBJ_REAL | MEMOBJ_BOOL | MEMOBJ_NULL)) {
if(pObj->iFlags & (MEMOBJ_BOOL | MEMOBJ_NULL | MEMOBJ_VOID)) {
if(pObj->iFlags & (MEMOBJ_CALL | MEMOBJ_NULL | MEMOBJ_VOID)) {
if(pObj->nType & (MEMOBJ_INT | MEMOBJ_REAL | MEMOBJ_BOOL | MEMOBJ_NULL)) {
if(pObj->nType & (MEMOBJ_BOOL | MEMOBJ_NULL | MEMOBJ_VOID)) {
if(pObj->nType & (MEMOBJ_CALL | MEMOBJ_NULL | MEMOBJ_VOID)) {
pObj->x.iVal = 0;
}
MemObjSetType(pObj, MEMOBJ_INT);
@ -708,7 +708,7 @@ PH7_PRIVATE sxi32 PH7_MemObjToNumeric(ph7_value *pObj) {
/* Already numeric */
return SXRET_OK;
}
if(pObj->iFlags & MEMOBJ_STRING) {
if(pObj->nType & MEMOBJ_STRING) {
sxi32 rc = SXERR_INVALID;
sxu8 bReal = FALSE;
SyString sString;
@ -730,7 +730,7 @@ PH7_PRIVATE sxi32 PH7_MemObjToNumeric(ph7_value *pObj) {
MemObjSetType(pObj, MEMOBJ_INT);
SyBlobRelease(&pObj->sBlob);
}
} else if(pObj->iFlags & (MEMOBJ_OBJ | MEMOBJ_HASHMAP | MEMOBJ_RES)) {
} else if(pObj->nType & (MEMOBJ_OBJ | MEMOBJ_HASHMAP | MEMOBJ_RES)) {
PH7_MemObjToInteger(pObj);
} else {
/* Perform a blind cast */
@ -748,7 +748,7 @@ PH7_PRIVATE sxi32 PH7_MemObjInit(ph7_vm *pVm, ph7_value *pObj) {
pObj->pVm = pVm;
SyBlobInit(&pObj->sBlob, &pVm->sAllocator);
/* Set the NULL type */
pObj->iFlags = MEMOBJ_NULL;
pObj->nType = MEMOBJ_NULL;
return SXRET_OK;
}
/*
@ -762,7 +762,7 @@ PH7_PRIVATE sxi32 PH7_MemObjInitFromInt(ph7_vm *pVm, ph7_value *pObj, sxi64 iVal
SyBlobInit(&pObj->sBlob, &pVm->sAllocator);
/* Set the desired type */
pObj->x.iVal = iVal;
pObj->iFlags = MEMOBJ_INT;
pObj->nType = MEMOBJ_INT;
return SXRET_OK;
}
/*
@ -776,7 +776,7 @@ PH7_PRIVATE sxi32 PH7_MemObjInitFromBool(ph7_vm *pVm, ph7_value *pObj, sxi32 iVa
SyBlobInit(&pObj->sBlob, &pVm->sAllocator);
/* Set the desired type */
pObj->x.iVal = iVal ? 1 : 0;
pObj->iFlags = MEMOBJ_BOOL;
pObj->nType = MEMOBJ_BOOL;
return SXRET_OK;
}
/*
@ -790,7 +790,7 @@ PH7_PRIVATE sxi32 PH7_MemObjInitFromReal(ph7_vm *pVm, ph7_value *pObj, ph7_real
SyBlobInit(&pObj->sBlob, &pVm->sAllocator);
/* Set the desired type */
pObj->x.rVal = rVal;
pObj->iFlags = MEMOBJ_REAL;
pObj->nType = MEMOBJ_REAL;
return SXRET_OK;
}
/*
@ -803,7 +803,7 @@ PH7_PRIVATE sxi32 PH7_MemObjInitFromVoid(ph7_vm *pVm, ph7_value *pObj, ph7_real
pObj->pVm = pVm;
SyBlobInit(&pObj->sBlob, &pVm->sAllocator);
/* Set the desired type */
pObj->iFlags = MEMOBJ_VOID;
pObj->nType = MEMOBJ_VOID;
return SXRET_OK;
}/*
* Initialize a ph7_value to the array type.
@ -815,7 +815,7 @@ PH7_PRIVATE sxi32 PH7_MemObjInitFromArray(ph7_vm *pVm, ph7_value *pObj, ph7_hash
pObj->pVm = pVm;
SyBlobInit(&pObj->sBlob, &pVm->sAllocator);
/* Set the desired type */
pObj->iFlags = MEMOBJ_HASHMAP;
pObj->nType = MEMOBJ_HASHMAP;
pObj->x.pOther = pArray;
return SXRET_OK;
}
@ -833,7 +833,7 @@ PH7_PRIVATE sxi32 PH7_MemObjInitFromString(ph7_vm *pVm, ph7_value *pObj, const S
SyBlobAppend(&pObj->sBlob, (const void *)pVal->zString, pVal->nByte);
}
/* Set the desired type */
pObj->iFlags = MEMOBJ_STRING;
pObj->nType = MEMOBJ_STRING;
return SXRET_OK;
}
/*
@ -844,7 +844,7 @@ PH7_PRIVATE sxi32 PH7_MemObjInitFromString(ph7_vm *pVm, ph7_value *pObj, const S
*/
PH7_PRIVATE sxi32 PH7_MemObjStringAppend(ph7_value *pObj, const char *zData, sxu32 nLen) {
sxi32 rc;
if((pObj->iFlags & MEMOBJ_STRING) == 0) {
if((pObj->nType & MEMOBJ_STRING) == 0) {
/* Invalidate any prior representation */
PH7_MemObjRelease(pObj);
MemObjSetType(pObj, MEMOBJ_STRING);
@ -861,7 +861,7 @@ PH7_PRIVATE sxi32 PH7_MemObjStringAppend(ph7_value *pObj, const char *zData, sxu
*/
PH7_PRIVATE sxi32 PH7_MemObjStringFormat(ph7_value *pObj, const char *zFormat, va_list ap) {
sxi32 rc;
if((pObj->iFlags & MEMOBJ_STRING) == 0) {
if((pObj->nType & MEMOBJ_STRING) == 0) {
/* Invalidate any prior representation */
PH7_MemObjRelease(pObj);
MemObjSetType(pObj, MEMOBJ_STRING);
@ -878,15 +878,15 @@ PH7_PRIVATE sxi32 PH7_MemObjStore(ph7_value *pSrc, ph7_value *pDest) {
ph7_hashmap *pSrcMap = 0;
sxi32 rc;
if(pSrc->x.pOther) {
if(pSrc->iFlags & MEMOBJ_HASHMAP) {
if(pSrc->nType & MEMOBJ_HASHMAP) {
/* Dump source hashmap */
pSrcMap = (ph7_hashmap *)pSrc->x.pOther;
} else if(pSrc->iFlags & MEMOBJ_OBJ) {
} else if(pSrc->nType & MEMOBJ_OBJ) {
/* Increment reference count */
((ph7_class_instance *)pSrc->x.pOther)->iRef++;
}
}
if(pDest->iFlags & MEMOBJ_OBJ) {
if(pDest->nType & MEMOBJ_OBJ) {
pObj = (ph7_class_instance *)pDest->x.pOther;
}
SyMemcpy((const void *) & (*pSrc), &(*pDest), sizeof(ph7_value) - (sizeof(ph7_vm *) + sizeof(SyBlob) + sizeof(sxu32)));
@ -918,10 +918,10 @@ PH7_PRIVATE sxi32 PH7_MemObjLoad(ph7_value *pSrc, ph7_value *pDest) {
SyMemcpy((const void *) & (*pSrc), &(*pDest),
sizeof(ph7_value) - (sizeof(ph7_vm *) + sizeof(SyBlob) + sizeof(sxu32)));
if(pSrc->x.pOther) {
if(pSrc->iFlags & MEMOBJ_HASHMAP) {
if(pSrc->nType & MEMOBJ_HASHMAP) {
/* Increment reference count */
((ph7_hashmap *)pSrc->x.pOther)->iRef++;
} else if(pSrc->iFlags & MEMOBJ_OBJ) {
} else if(pSrc->nType & MEMOBJ_OBJ) {
/* Increment reference count */
((ph7_class_instance *)pSrc->x.pOther)->iRef++;
}
@ -938,18 +938,18 @@ PH7_PRIVATE sxi32 PH7_MemObjLoad(ph7_value *pSrc, ph7_value *pDest) {
* Invalidate any prior representation of a given ph7_value.
*/
PH7_PRIVATE sxi32 PH7_MemObjRelease(ph7_value *pObj) {
if((pObj->iFlags & MEMOBJ_NULL) == 0) {
if((pObj->nType & MEMOBJ_NULL) == 0) {
if(pObj->x.pOther) {
if(pObj->iFlags & MEMOBJ_HASHMAP) {
if(pObj->nType & MEMOBJ_HASHMAP) {
PH7_HashmapUnref((ph7_hashmap *)pObj->x.pOther);
} else if(pObj->iFlags & MEMOBJ_OBJ) {
} else if(pObj->nType & MEMOBJ_OBJ) {
PH7_ClassInstanceUnref((ph7_class_instance *)pObj->x.pOther);
}
}
/* Release the internal buffer */
SyBlobRelease(&pObj->sBlob);
/* Invalidate any prior representation */
pObj->iFlags = MEMOBJ_NULL;
pObj->nType = MEMOBJ_NULL;
}
return SXRET_OK;
}
@ -1012,31 +1012,31 @@ PH7_PRIVATE sxi32 PH7_MemObjCmp(ph7_value *pObj1, ph7_value *pObj2, int bStrict,
if(bStrict) {
sxi32 iF1, iF2;
/* Strict comparisons with === */
iF1 = pObj1->iFlags;
iF2 = pObj2->iFlags;
iF1 = pObj1->nType;
iF2 = pObj2->nType;
if(iF1 != iF2) {
/* Not of the same type */
return 1;
}
}
/* Combine flag together */
iComb = pObj1->iFlags | pObj2->iFlags;
iComb = pObj1->nType | pObj2->nType;
if(iComb & (MEMOBJ_NULL | MEMOBJ_RES | MEMOBJ_BOOL)) {
/* Convert to boolean: Keep in mind FALSE < TRUE */
if((pObj1->iFlags & MEMOBJ_BOOL) == 0) {
if((pObj1->nType & MEMOBJ_BOOL) == 0) {
PH7_MemObjToBool(pObj1);
}
if((pObj2->iFlags & MEMOBJ_BOOL) == 0) {
if((pObj2->nType & MEMOBJ_BOOL) == 0) {
PH7_MemObjToBool(pObj2);
}
return (sxi32)((pObj1->x.iVal != 0) - (pObj2->x.iVal != 0));
} else if(iComb & MEMOBJ_HASHMAP) {
/* Hashmap aka 'array' comparison */
if((pObj1->iFlags & MEMOBJ_HASHMAP) == 0) {
if((pObj1->nType & MEMOBJ_HASHMAP) == 0) {
/* Array is always greater */
return -1;
}
if((pObj2->iFlags & MEMOBJ_HASHMAP) == 0) {
if((pObj2->nType & MEMOBJ_HASHMAP) == 0) {
/* Array is always greater */
return 1;
}
@ -1045,11 +1045,11 @@ PH7_PRIVATE sxi32 PH7_MemObjCmp(ph7_value *pObj1, ph7_value *pObj2, int bStrict,
return rc;
} else if(iComb & MEMOBJ_OBJ) {
/* Object comparison */
if((pObj1->iFlags & MEMOBJ_OBJ) == 0) {
if((pObj1->nType & MEMOBJ_OBJ) == 0) {
/* Object is always greater */
return -1;
}
if((pObj2->iFlags & MEMOBJ_OBJ) == 0) {
if((pObj2->nType & MEMOBJ_OBJ) == 0) {
/* Object is always greater */
return 1;
}
@ -1076,10 +1076,10 @@ PH7_PRIVATE sxi32 PH7_MemObjCmp(ph7_value *pObj1, ph7_value *pObj2, int bStrict,
}
}
/* Perform a strict string comparison.*/
if((pObj1->iFlags & MEMOBJ_STRING) == 0) {
if((pObj1->nType & MEMOBJ_STRING) == 0) {
PH7_MemObjToString(pObj1);
}
if((pObj2->iFlags & MEMOBJ_STRING) == 0) {
if((pObj2->nType & MEMOBJ_STRING) == 0) {
PH7_MemObjToString(pObj2);
}
SyStringInitFromBuf(&s1, SyBlobData(&pObj1->sBlob), SyBlobLength(&pObj1->sBlob));
@ -1098,20 +1098,20 @@ PH7_PRIVATE sxi32 PH7_MemObjCmp(ph7_value *pObj1, ph7_value *pObj2, int bStrict,
} else if(iComb & (MEMOBJ_INT | MEMOBJ_REAL)) {
Numeric:
/* Perform a numeric comparison if one of the operand is numeric(integer or real) */
if((pObj1->iFlags & (MEMOBJ_INT | MEMOBJ_REAL)) == 0) {
if((pObj1->nType & (MEMOBJ_INT | MEMOBJ_REAL)) == 0) {
PH7_MemObjToNumeric(pObj1);
}
if((pObj2->iFlags & (MEMOBJ_INT | MEMOBJ_REAL)) == 0) {
if((pObj2->nType & (MEMOBJ_INT | MEMOBJ_REAL)) == 0) {
PH7_MemObjToNumeric(pObj2);
}
if((pObj1->iFlags & pObj2->iFlags & MEMOBJ_INT) == 0) {
if((pObj1->nType & pObj2->nType & MEMOBJ_INT) == 0) {
ph7_real r1, r2;
/* Compare as reals */
if((pObj1->iFlags & MEMOBJ_REAL) == 0) {
if((pObj1->nType & MEMOBJ_REAL) == 0) {
PH7_MemObjToReal(pObj1);
}
r1 = pObj1->x.rVal;
if((pObj2->iFlags & MEMOBJ_REAL) == 0) {
if((pObj2->nType & MEMOBJ_REAL) == 0) {
PH7_MemObjToReal(pObj2);
}
r2 = pObj2->x.rVal;
@ -1147,17 +1147,17 @@ Numeric:
* This function take care of handling all the scenarios.
*/
PH7_PRIVATE sxi32 PH7_MemObjAdd(ph7_value *pObj1, ph7_value *pObj2, int bAddStore) {
if(((pObj1->iFlags | pObj2->iFlags) & MEMOBJ_HASHMAP) == 0) {
if(((pObj1->nType | pObj2->nType) & MEMOBJ_HASHMAP) == 0) {
/* Arithemtic operation */
PH7_MemObjToNumeric(pObj1);
PH7_MemObjToNumeric(pObj2);
if((pObj1->iFlags | pObj2->iFlags) & MEMOBJ_REAL) {
if((pObj1->nType | pObj2->nType) & MEMOBJ_REAL) {
/* Floating point arithmetic */
ph7_real a, b;
if((pObj1->iFlags & MEMOBJ_REAL) == 0) {
if((pObj1->nType & MEMOBJ_REAL) == 0) {
PH7_MemObjToReal(pObj1);
}
if((pObj2->iFlags & MEMOBJ_REAL) == 0) {
if((pObj2->nType & MEMOBJ_REAL) == 0) {
PH7_MemObjToReal(pObj2);
}
a = pObj1->x.rVal;
@ -1173,13 +1173,13 @@ PH7_PRIVATE sxi32 PH7_MemObjAdd(ph7_value *pObj1, ph7_value *pObj2, int bAddStor
MemObjSetType(pObj1, MEMOBJ_INT);
}
} else {
if((pObj1->iFlags | pObj2->iFlags) & MEMOBJ_HASHMAP) {
if((pObj1->nType | pObj2->nType) & MEMOBJ_HASHMAP) {
ph7_hashmap *pMap;
sxi32 rc;
if(bAddStore) {
/* Do not duplicate the hashmap,use the left one since its an add&store operation.
*/
if((pObj1->iFlags & MEMOBJ_HASHMAP) == 0) {
if((pObj1->nType & MEMOBJ_HASHMAP) == 0) {
/* Force a hashmap cast */
rc = PH7_MemObjToHashmap(pObj1);
if(rc != SXRET_OK) {
@ -1196,27 +1196,27 @@ PH7_PRIVATE sxi32 PH7_MemObjAdd(ph7_value *pObj1, ph7_value *pObj2, int bAddStor
}
}
if(!bAddStore) {
if(pObj1->iFlags & MEMOBJ_HASHMAP) {
if(pObj1->nType & MEMOBJ_HASHMAP) {
/* Perform a hashmap duplication */
PH7_HashmapDup((ph7_hashmap *)pObj1->x.pOther, pMap);
} else {
if((pObj1->iFlags & MEMOBJ_NULL) == 0) {
if((pObj1->nType & MEMOBJ_NULL) == 0) {
/* Simple insertion */
PH7_HashmapInsert(pMap, 0, pObj1);