Add C API for registering native AerScript classes
Build / AerScript (push) Successful in 24s

This commit is contained in:
2026-11-14 23:26:28 +01:00
parent d98f6a8e91
commit 61006fe848
9 changed files with 896 additions and 87 deletions
+18
View File
@@ -677,6 +677,15 @@ int ph7_create_function(ph7_vm *pVm, const char *zName, int (*xFunc)(ph7_context
rc = PH7_VmInstallForeignFunction(&(*pVm), &sName, xFunc, pUserData); rc = PH7_VmInstallForeignFunction(&(*pVm), &sName, xFunc, pUserData);
return rc; return rc;
} }
ph7_value *ph7_context_this(ph7_context *pCtx) {
return pCtx ? pCtx->pThis : 0;
}
int ph7_create_class(ph7_vm *pVm, const ph7_class_def *pDef) {
if(PH7_VM_MISUSE(pVm) || pDef == 0) {
return PH7_CORRUPT;
}
return PH7_VmCreateNativeClass(pVm, pDef);
}
/* /*
* [CAPIREF: ph7_delete_function()] * [CAPIREF: ph7_delete_function()]
* Please refer to the official documentation for function purpose and expected parameters. * Please refer to the official documentation for function purpose and expected parameters.
@@ -923,6 +932,7 @@ int ph7_value_compare(ph7_value *pLeft, ph7_value *pRight, int bStrict) {
* Please refer to the official documentation for function purpose and expected parameters. * Please refer to the official documentation for function purpose and expected parameters.
*/ */
int ph7_result_int(ph7_context *pCtx, int iValue) { int ph7_result_int(ph7_context *pCtx, int iValue) {
pCtx->bResultSet = TRUE;
return ph7_value_int(pCtx->pRet, iValue); return ph7_value_int(pCtx->pRet, iValue);
} }
/* /*
@@ -930,6 +940,7 @@ int ph7_result_int(ph7_context *pCtx, int iValue) {
* Please refer to the official documentation for function purpose and expected parameters. * Please refer to the official documentation for function purpose and expected parameters.
*/ */
int ph7_result_int64(ph7_context *pCtx, ph7_int64 iValue) { int ph7_result_int64(ph7_context *pCtx, ph7_int64 iValue) {
pCtx->bResultSet = TRUE;
return ph7_value_int64(pCtx->pRet, iValue); return ph7_value_int64(pCtx->pRet, iValue);
} }
/* /*
@@ -937,6 +948,7 @@ int ph7_result_int64(ph7_context *pCtx, ph7_int64 iValue) {
* Please refer to the official documentation for function purpose and expected parameters. * Please refer to the official documentation for function purpose and expected parameters.
*/ */
int ph7_result_bool(ph7_context *pCtx, int iBool) { int ph7_result_bool(ph7_context *pCtx, int iBool) {
pCtx->bResultSet = TRUE;
return ph7_value_bool(pCtx->pRet, iBool); return ph7_value_bool(pCtx->pRet, iBool);
} }
/* /*
@@ -944,6 +956,7 @@ int ph7_result_bool(ph7_context *pCtx, int iBool) {
* Please refer to the official documentation for function purpose and expected parameters. * Please refer to the official documentation for function purpose and expected parameters.
*/ */
int ph7_result_double(ph7_context *pCtx, double Value) { int ph7_result_double(ph7_context *pCtx, double Value) {
pCtx->bResultSet = TRUE;
return ph7_value_double(pCtx->pRet, Value); return ph7_value_double(pCtx->pRet, Value);
} }
/* /*
@@ -951,6 +964,7 @@ int ph7_result_double(ph7_context *pCtx, double Value) {
* Please refer to the official documentation for function purpose and expected parameters. * Please refer to the official documentation for function purpose and expected parameters.
*/ */
int ph7_result_null(ph7_context *pCtx) { int ph7_result_null(ph7_context *pCtx) {
pCtx->bResultSet = TRUE;
/* Invalidate any prior representation and set the NULL flag */ /* Invalidate any prior representation and set the NULL flag */
PH7_MemObjRelease(pCtx->pRet); PH7_MemObjRelease(pCtx->pRet);
return PH7_OK; return PH7_OK;
@@ -960,6 +974,7 @@ int ph7_result_null(ph7_context *pCtx) {
* Please refer to the official documentation for function purpose and expected parameters. * Please refer to the official documentation for function purpose and expected parameters.
*/ */
int ph7_result_string(ph7_context *pCtx, const char *zString, int nLen) { int ph7_result_string(ph7_context *pCtx, const char *zString, int nLen) {
pCtx->bResultSet = TRUE;
return ph7_value_string(pCtx->pRet, zString, nLen); return ph7_value_string(pCtx->pRet, zString, nLen);
} }
/* /*
@@ -971,6 +986,7 @@ int ph7_result_string_format(ph7_context *pCtx, const char *zFormat, ...) {
va_list ap; va_list ap;
int rc; int rc;
p = pCtx->pRet; p = pCtx->pRet;
pCtx->bResultSet = TRUE;
if((p->nType & MEMOBJ_STRING) == 0) { if((p->nType & MEMOBJ_STRING) == 0) {
/* Invalidate any prior representation */ /* Invalidate any prior representation */
PH7_MemObjRelease(p); PH7_MemObjRelease(p);
@@ -988,6 +1004,7 @@ int ph7_result_string_format(ph7_context *pCtx, const char *zFormat, ...) {
*/ */
int ph7_result_value(ph7_context *pCtx, ph7_value *pValue) { int ph7_result_value(ph7_context *pCtx, ph7_value *pValue) {
int rc = PH7_OK; int rc = PH7_OK;
pCtx->bResultSet = TRUE;
if(pValue == 0) { if(pValue == 0) {
PH7_MemObjRelease(pCtx->pRet); PH7_MemObjRelease(pCtx->pRet);
} else { } else {
@@ -1000,6 +1017,7 @@ int ph7_result_value(ph7_context *pCtx, ph7_value *pValue) {
* Please refer to the official documentation for function purpose and expected parameters. * Please refer to the official documentation for function purpose and expected parameters.
*/ */
int ph7_result_resource(ph7_context *pCtx, void *pUserData) { int ph7_result_resource(ph7_context *pCtx, void *pUserData) {
pCtx->bResultSet = TRUE;
return ph7_value_resource(pCtx->pRet, pUserData); return ph7_value_resource(pCtx->pRet, pUserData);
} }
/* /*
+4 -28
View File
@@ -1003,23 +1003,11 @@ PH7_PRIVATE sxi32 PH7_CompileArray(ph7_gen_state *pGen, sxi32 iCompileFlag) {
} }
/* Names, not class pointers, survive forward declarations and arena growth. */ /* Names, not class pointers, survive forward declarations and arena growth. */
static sxu32 GenStateNamedType(ph7_gen_state *pGen, SyString *pName) { static sxu32 GenStateNamedType(ph7_gen_state *pGen, SyString *pName) {
SyString sName; sxu32 nType = 0;
sxu32 i; if(PH7_VmInternType(pGen->pVm, pName, &nType) != SXRET_OK) {
char *zName;
for(i = 0; i < SySetUsed(&pGen->pVm->aTypeNames); i++) {
if(SyStringCmp(pName, (SyString *)SySetAt(&pGen->pVm->aTypeNames, i), SyMemcmp) == 0) {
return ((i + 1) << 12) | MEMOBJ_OBJ;
}
}
zName = SyMemBackendStrDup(&pGen->pVm->sAllocator, pName->zString, pName->nByte);
if(zName == 0 || i >= (MEMOBJ_NAMED >> 12)) {
PH7_GenCompileError(pGen, E_ERROR, pGen->pIn->nLine, "PH7 engine is running out-of-memory"); PH7_GenCompileError(pGen, E_ERROR, pGen->pIn->nLine, "PH7 engine is running out-of-memory");
} }
SyStringInitFromBuf(&sName, zName, pName->nByte); return nType;
if(SySetPut(&pGen->pVm->aTypeNames, &sName) != SXRET_OK) {
PH7_GenCompileError(pGen, E_ERROR, pGen->pIn->nLine, "PH7 engine is running out-of-memory");
}
return ((i + 1) << 12) | MEMOBJ_OBJ;
} }
/* /*
* Classify a literal expression node for the static comparison checks. * Classify a literal expression node for the static comparison checks.
@@ -1636,19 +1624,7 @@ PH7_PRIVATE sxi32 PH7_CompileLiteral(ph7_gen_state *pGen, sxi32 iCompileFlag) {
* Return TRUE if reserved. FALSE otherwise. * Return TRUE if reserved. FALSE otherwise.
*/ */
static int PH7_GenStateIsReservedConstant(SyString *pName) { static int PH7_GenStateIsReservedConstant(SyString *pName) {
if(pName->nByte == sizeof("null") - 1) { return PH7_IsReservedConstant(pName);
if(SyStrnicmp(pName->zString, "null", sizeof("null") - 1) == 0) {
return TRUE;
} else if(SyStrnicmp(pName->zString, "true", sizeof("true") - 1) == 0) {
return TRUE;
}
} else if(pName->nByte == sizeof("false") - 1) {
if(SyStrnicmp(pName->zString, "false", sizeof("false") - 1) == 0) {
return TRUE;
}
}
/* Not a reserved constant */
return FALSE;
} }
/* /*
* Compile the 'const' statement. * Compile the 'const' statement.
+5
View File
@@ -26,6 +26,11 @@ PH7_PRIVATE sxi32 VmExtractDebugTrace(ph7_vm *pVm, SySet *pDebugTrace) {
if(pFunc && (pVm->pFrame->iFlags & VM_FRAME_EXCEPTION) == 0) { if(pFunc && (pVm->pFrame->iFlags & VM_FRAME_EXCEPTION) == 0) {
VmDebugTrace aTrace; VmDebugTrace aTrace;
SySet *aByteCode = &pFunc->aByteCode; SySet *aByteCode = &pFunc->aByteCode;
static SyString sNative = { "[NATIVE]", sizeof("[NATIVE]") - 1 };
SyZero(&aTrace, sizeof(aTrace));
if(pFunc->sNative.xFunc) {
aTrace.pFile = &sNative;
}
/* Extract closure/method name and passed arguments */ /* Extract closure/method name and passed arguments */
aTrace.pFuncName = &pFunc->sName; aTrace.pFuncName = &pFunc->sName;
aTrace.pArg = &pVm->pFrame->sArg; aTrace.pArg = &pVm->pFrame->sArg;
+79
View File
@@ -1154,6 +1154,85 @@ PH7_PRIVATE sxi32 PH7_HashmapDup(ph7_hashmap *pSrc, ph7_hashmap *pDest) {
pDest->bIdxExhausted = pSrc->bIdxExhausted; pDest->bIdxExhausted = pSrc->bIdxExhausted;
return SXRET_OK; return SXRET_OK;
} }
/*
* Recoverable declaration snapshots. Ordinary array stores recursively copy
* through the execution allocator; registration must also handle a failed
* allocation before a node has been linked, without invoking the VM fatal path.
*/
PH7_PRIVATE sxi32 PH7_MemObjSnapshot(ph7_value *pSrc, ph7_value *pDest) {
ph7_vm *pVm = pDest->pVm;
ph7_hashmap *pSrcMap, *pMap;
ph7_hashmap_node *pEntry;
sxi32 rc;
if((pSrc->nType & MEMOBJ_HASHMAP) == 0 || pSrc->x.pOther == 0) {
return PH7_MemObjStore(pSrc, pDest);
}
pSrcMap = (ph7_hashmap *)pSrc->x.pOther;
pMap = PH7_NewHashmap(pVm, 0, 0);
if(pMap == 0) {
return SXERR_MEM;
}
pDest->x.pOther = pMap;
pDest->nType = pSrc->nType;
pDest->nDeclaredType = pSrc->nDeclaredType;
for(pEntry = pSrcMap->pFirst; pEntry; pEntry = pEntry->pPrev) {
ph7_value sCopy, *pValue;
ph7_hashmap_node *pNode;
sxu32 nIdx, nHash;
PH7_MemObjInit(pVm, &sCopy);
rc = PH7_MemObjSnapshot(HashmapExtractNodeValue(pEntry), &sCopy);
if(rc != SXRET_OK) {
PH7_MemObjRelease(&sCopy);
return rc;
}
pValue = PH7_ReserveMemObj(pVm);
if(pValue == 0) {
PH7_MemObjRelease(&sCopy);
return SXERR_MEM;
}
nIdx = pValue->nIdx;
/* Transfer the owned snapshot; do not trigger another recursive copy. */
PH7_MemObjLoad(&sCopy, pValue);
/* Strings loaded read-only must outlive sCopy. */
if(SyBlobLength(&sCopy.sBlob)) {
SyBlobRelease(&pValue->sBlob);
rc = SyBlobDup(&sCopy.sBlob, &pValue->sBlob);
}
PH7_MemObjRelease(&sCopy);
if(rc != SXRET_OK) {
PH7_VmDestroyMemObj(pVm, pValue);
return rc;
}
if(pEntry->iType == HASHMAP_BLOB_NODE) {
nHash = pMap->xBlobHash(SyBlobData(&pEntry->xKey.sKey), SyBlobLength(&pEntry->xKey.sKey));
pNode = HashmapNewBlobNode(pMap, SyBlobData(&pEntry->xKey.sKey), SyBlobLength(&pEntry->xKey.sKey), nHash, nIdx);
} else {
nHash = pMap->xIntHash(pEntry->xKey.iKey);
pNode = HashmapNewIntNode(pMap, pEntry->xKey.iKey, nHash, nIdx);
}
rc = pNode && (pNode->iType != HASHMAP_BLOB_NODE ||
SyBlobLength(&pNode->xKey.sKey) == SyBlobLength(&pEntry->xKey.sKey)) ?
HashmapGrowBucket(pMap) : SXERR_MEM;
if(rc == SXRET_OK) {
rc = PH7_VmRefObjInstall(pVm, nIdx, 0, pNode, 0);
}
if(rc != SXRET_OK) {
if(pNode) {
if(pNode->iType == HASHMAP_BLOB_NODE) {
SyBlobRelease(&pNode->xKey.sKey);
}
SyMemBackendPoolFree(&pVm->sAllocator, pNode);
}
PH7_VmDestroyMemObj(pVm, (ph7_value *)SySetAt(&pVm->aMemObj, nIdx));
return rc;
}
HashmapNodeLink(pMap, pNode, nHash & (pMap->nSize - 1));
}
pMap->iNextIdx = pSrcMap->iNextIdx;
pMap->bIdxExhausted = pSrcMap->bIdxExhausted;
pMap->nDeclaredType = pSrcMap->nDeclaredType;
return SXRET_OK;
}
/* /*
* Perform the union of two hashmaps. * Perform the union of two hashmaps.
* This operation is performed only if the user uses the '+' operator * This operation is performed only if the user uses the '+' operator
+40
View File
@@ -546,6 +546,46 @@ static sxi32 TokenizeAerScript(SyStream *pStream, SyToken *pToken, void *pUserDa
return SXRET_OK; return SXRET_OK;
} }
PH7_PRIVATE sxi32 PH7_IsIdentifier(const char *zName, sxi32 bKeyword) {
const unsigned char *z = (const unsigned char *)zName;
if(z == 0 || (!SyisAlpha(z[0]) && z[0] != '_' && z[0] < 0xc0)) {
return FALSE;
}
for(z++; *z; z++) {
if(!SyisAlphaNum(*z) && *z != '_' && *z < 0x80) {
return FALSE;
}
}
return bKeyword || KeywordCode(zName, (sxu32)(z - (const unsigned char *)zName)) == PH7_TK_ID;
}
/* Match the parser's method declaration token, including ID|OP keywords. */
PH7_PRIVATE sxi32 PH7_IsMethodName(const char *zName) {
SyStream sStream;
SyToken sToken;
if(!PH7_IsIdentifier(zName, TRUE)) {
return FALSE;
}
SyZero(&sStream, sizeof(sStream));
sStream.zText = (const unsigned char *)zName;
sStream.zEnd = sStream.zText + SyStrlen(zName);
return TokenizeAerScript(&sStream, &sToken, 0, 0) == SXRET_OK &&
(sToken.nType & PH7_TK_ID) && sToken.sData.zString == zName &&
sStream.zText == sStream.zEnd;
}
PH7_PRIVATE sxi32 PH7_IsReservedConstant(const SyString *pName) {
if(pName->nByte == sizeof("null") - 1) {
if(SyStrnicmp(pName->zString, "null", sizeof("null") - 1) == 0) {
return TRUE;
} else if(SyStrnicmp(pName->zString, "true", sizeof("true") - 1) == 0) {
return TRUE;
}
} else if(pName->nByte == sizeof("false") - 1) {
if(SyStrnicmp(pName->zString, "false", sizeof("false") - 1) == 0) {
return TRUE;
}
}
return FALSE;
}
static sxu32 KeywordCode(const char *z, sxu32 n) { static sxu32 KeywordCode(const char *z, sxu32 n) {
typedef struct { typedef struct {
char *token; char *token;
+15 -4
View File
@@ -396,11 +396,13 @@ PH7_PRIVATE sxi32 PH7_CheckVarCompat(ph7_value *pObj, int nType) {
} }
/* Apply only to owned storage. Array copies are made before this walk, so a /* Apply only to owned storage. Array copies are made before this walk, so a
* wider destination never weakens the source array's leaf contracts. */ * wider destination never weakens the source array's leaf contracts. */
PH7_PRIVATE void PH7_MemObjSetDeclaredType(ph7_value *pValue, sxu32 nType) { PH7_PRIVATE sxi32 PH7_MemObjDeclareType(ph7_value *pValue, sxu32 nType, sxi32 bResolve) {
ph7_hashmap_node *pNode; ph7_hashmap_node *pNode;
ph7_hashmap *pMap; ph7_hashmap *pMap;
if(nType & MEMOBJ_NAMED) { if(nType & MEMOBJ_NAMED) {
if(bResolve) {
PH7_VmTypeClass(pValue->pVm, nType, TRUE); PH7_VmTypeClass(pValue->pVm, nType, TRUE);
}
} else { } else {
nType = 0; nType = 0;
} }
@@ -414,18 +416,27 @@ PH7_PRIVATE void PH7_MemObjSetDeclaredType(ph7_value *pValue, sxu32 nType) {
if(nType & MEMOBJ_HASHMAP) { if(nType & MEMOBJ_HASHMAP) {
pValue->x.pOther = PH7_NewHashmap(pValue->pVm, 0, 0); pValue->x.pOther = PH7_NewHashmap(pValue->pVm, 0, 0);
if(pValue->x.pOther == 0) { if(pValue->x.pOther == 0) {
PH7_VmMemoryError(pValue->pVm); return SXERR_MEM;
} }
} }
} }
if((pValue->nType & MEMOBJ_HASHMAP) == 0 || pValue->x.pOther == 0) { if((pValue->nType & MEMOBJ_HASHMAP) == 0 || pValue->x.pOther == 0) {
return; return SXRET_OK;
} }
pMap = (ph7_hashmap *)pValue->x.pOther; pMap = (ph7_hashmap *)pValue->x.pOther;
pMap->nDeclaredType = nType & ~MEMOBJ_HASHMAP; pMap->nDeclaredType = nType & ~MEMOBJ_HASHMAP;
for(pNode = pMap->pFirst; pNode; pNode = pNode->pPrev) { for(pNode = pMap->pFirst; pNode; pNode = pNode->pPrev) {
ph7_value *pLeaf = (ph7_value *)SySetAt(&pValue->pVm->aMemObj, pNode->nValIdx); ph7_value *pLeaf = (ph7_value *)SySetAt(&pValue->pVm->aMemObj, pNode->nValIdx);
PH7_MemObjSetDeclaredType(pLeaf, nType ? pMap->nDeclaredType | (pLeaf->nType & MEMOBJ_HASHMAP) : 0); sxi32 rc = PH7_MemObjDeclareType(pLeaf, nType ? pMap->nDeclaredType | (pLeaf->nType & MEMOBJ_HASHMAP) : 0, bResolve);
if(rc != SXRET_OK) {
return rc;
}
}
return SXRET_OK;
}
PH7_PRIVATE void PH7_MemObjSetDeclaredType(ph7_value *pValue, sxu32 nType) {
if(PH7_MemObjDeclareType(pValue, nType, TRUE) != SXRET_OK) {
PH7_VmMemoryError(pValue->pVm);
} }
} }
/* /*
+661 -46
View File
@@ -618,6 +618,33 @@ static void VmLeaveFrame(ph7_vm *pVm) {
} }
/* Forward declaration */ /* Forward declaration */
static int VmInstanceOf(ph7_vm *pVm, ph7_class *pThis, ph7_class *pClass); static int VmInstanceOf(ph7_vm *pVm, ph7_class *pThis, ph7_class *pClass);
static sxi32 VmInstanceOfResult(ph7_vm *pVm, ph7_class *pThis, ph7_class *pClass, sxi32 bRecoverable);
/* Names, not class pointers, survive forward declarations and autoloading. */
PH7_PRIVATE sxi32 PH7_VmInternType(ph7_vm *pVm, const SyString *pName, sxu32 *pType) {
SyString sName;
sxu32 i;
char *zName;
for(i = 0; i < SySetUsed(&pVm->aTypeNames); i++) {
if(SyStringCmp(pName, (SyString *)SySetAt(&pVm->aTypeNames, i), SyMemcmp) == 0) {
*pType = ((i + 1) << 12) | MEMOBJ_OBJ;
return SXRET_OK;
}
}
if(i >= (MEMOBJ_NAMED >> 12)) {
return SXERR_MEM;
}
zName = SyMemBackendStrDup(&pVm->sAllocator, pName->zString, pName->nByte);
if(zName == 0) {
return SXERR_MEM;
}
SyStringInitFromBuf(&sName, zName, pName->nByte);
if(SySetPut(&pVm->aTypeNames, &sName) != SXRET_OK) {
SyMemBackendFree(&pVm->sAllocator, zName);
return SXERR_MEM;
}
*pType = ((i + 1) << 12) | MEMOBJ_OBJ;
return SXRET_OK;
}
PH7_PRIVATE ph7_class *PH7_VmTypeClass(ph7_vm *pVm, sxu32 nType, sxi32 bResolve) { PH7_PRIVATE ph7_class *PH7_VmTypeClass(ph7_vm *pVm, sxu32 nType, sxi32 bResolve) {
SyString *pName; SyString *pName;
SyHashEntry *pEntry; SyHashEntry *pEntry;
@@ -645,7 +672,7 @@ PH7_PRIVATE ph7_class *PH7_VmTypeClass(ph7_vm *pVm, sxu32 nType, sxi32 bResolve)
return pClass; return pClass;
} }
/* Read-only validation of all leaves; never cast or manufacture an instance. */ /* Read-only validation of all leaves; never cast or manufacture an instance. */
PH7_PRIVATE sxi32 PH7_VmTypeCheck(ph7_value *pValue, sxu32 nType) { static sxi32 VmTypeCheck(ph7_value *pValue, sxu32 nType, sxi32 bRecoverable) {
ph7_class *pClass = PH7_VmTypeClass(pValue->pVm, nType, FALSE); ph7_class *pClass = PH7_VmTypeClass(pValue->pVm, nType, FALSE);
ph7_class_instance *pInstance; ph7_class_instance *pInstance;
if(pClass == 0) { if(pClass == 0) {
@@ -664,8 +691,9 @@ PH7_PRIVATE sxi32 PH7_VmTypeCheck(ph7_value *pValue, sxu32 nType) {
for(pNode = pMap ? pMap->pFirst : 0; pNode; pNode = pNode->pPrev) { for(pNode = pMap ? pMap->pFirst : 0; pNode; pNode = pNode->pPrev) {
ph7_value *pLeaf = (ph7_value *)SySetAt(&pValue->pVm->aMemObj, pNode->nValIdx); ph7_value *pLeaf = (ph7_value *)SySetAt(&pValue->pVm->aMemObj, pNode->nValIdx);
sxu32 nLeafType = (nType & ~MEMOBJ_HASHMAP) | (pLeaf->nType & MEMOBJ_HASHMAP); sxu32 nLeafType = (nType & ~MEMOBJ_HASHMAP) | (pLeaf->nType & MEMOBJ_HASHMAP);
if(PH7_VmTypeCheck(pLeaf, nLeafType) != SXRET_OK) { sxi32 rc = VmTypeCheck(pLeaf, nLeafType, bRecoverable);
return SXERR_NOMATCH; if(rc != SXRET_OK) {
return rc;
} }
} }
return SXRET_OK; return SXRET_OK;
@@ -674,7 +702,10 @@ PH7_PRIVATE sxi32 PH7_VmTypeCheck(ph7_value *pValue, sxu32 nType) {
return SXERR_NOMATCH; return SXERR_NOMATCH;
} }
pInstance = (ph7_class_instance *)pValue->x.pOther; pInstance = (ph7_class_instance *)pValue->x.pOther;
return !pInstance || VmInstanceOf(pValue->pVm, pInstance->pClass, pClass) ? SXRET_OK : SXERR_NOMATCH; return pInstance ? VmInstanceOfResult(pValue->pVm, pInstance->pClass, pClass, bRecoverable) : SXRET_OK;
}
PH7_PRIVATE sxi32 PH7_VmTypeCheck(ph7_value *pValue, sxu32 nType) {
return VmTypeCheck(pValue, nType, FALSE);
} }
/* /*
* Ranks for general parameter types. Nominal specificity is compared by * Ranks for general parameter types. Nominal specificity is compared by
@@ -1387,6 +1418,471 @@ static ph7_vm_func *VmOverload(
/* Appropriate function for the current call context */ /* Appropriate function for the current call context */
return pLink; return pLink;
} }
/* Map public descriptors to the existing language type representation. */
static sxi32 VmNativeType(ph7_vm *pVm, ph7_class *pClass,
const ph7_type_def *pDef, sxu32 *pType) {
static const sxu32 aType[] = {
0, MEMOBJ_BOOL, MEMOBJ_CALL, MEMOBJ_CHAR, MEMOBJ_REAL, MEMOBJ_INT,
MEMOBJ_MIXED, MEMOBJ_OBJ, MEMOBJ_RES, MEMOBJ_STRING, MEMOBJ_VOID
};
sxi32 rc = SXRET_OK;
if(pDef->bArray != 0 && pDef->bArray != 1) {
return PH7_CORRUPT;
}
if(pDef->iType == PH7_TYPE_NAMED) {
SyString sName;
if(!PH7_IsIdentifier(pDef->zClass, FALSE)) {
return PH7_CORRUPT;
}
SyStringInitFromBuf(&sName, pDef->zClass, SyStrlen(pDef->zClass));
if(sName.nByte == 4 && SyMemcmp(sName.zString, "self", 4) == 0) {
sName = pClass->sName;
}
rc = PH7_VmInternType(pVm, &sName, pType);
} else {
if(pDef->iType < PH7_TYPE_BOOL || pDef->iType > PH7_TYPE_VOID || pDef->zClass) {
return PH7_CORRUPT;
}
*pType = aType[pDef->iType];
}
if(rc == SXRET_OK && pDef->bArray) {
*pType |= MEMOBJ_HASHMAP;
}
return rc;
}
static void VmNativeMethodFree(ph7_vm *pVm, ph7_class_method *pMeth) {
sxu32 n;
for(n = 0; n < SySetUsed(&pMeth->sFunc.aArgs); n++) {
ph7_vm_func_arg *pArg = (ph7_vm_func_arg *)SySetAt(&pMeth->sFunc.aArgs, n);
SyMemBackendFree(&pVm->sAllocator, (void *)pArg->sName.zString);
SySetRelease(&pArg->aByteCode);
}
SySetRelease(&pMeth->sFunc.aArgs);
SySetRelease(&pMeth->sFunc.aByteCode);
SySetRelease(&pMeth->sFunc.aStatic);
SySetRelease(&pMeth->sFunc.aClosureEnv);
SySetRelease(&pMeth->sFunc.sNative.aAux);
SyMemBackendFree(&pVm->sAllocator, (void *)pMeth->sFunc.sNative.sName.zString);
SyMemBackendFree(&pVm->sAllocator, (void *)pMeth->sVmName.zString);
SyMemBackendPoolFree(&pVm->sAllocator, pMeth);
}
static sxi32 VmNativeInitialType(ph7_value *pValue, sxu32 nType) {
if(nType == 0) {
return SXRET_OK; /* Constants have no annotation in source either. */
}
if(nType & MEMOBJ_NAMED) {
/* NULL and empty arrays do not need the late class declaration yet. */
if(MemObjIsNullReference(pValue)) {
return SXRET_OK;
}
if((nType & MEMOBJ_HASHMAP) && (pValue->nType & MEMOBJ_HASHMAP) &&
((ph7_hashmap *)pValue->x.pOther)->nEntry == 0) {
return SXRET_OK;
}
return VmTypeCheck(pValue, nType, TRUE);
}
if(nType & MEMOBJ_HASHMAP) {
ph7_hashmap *pMap;
ph7_hashmap_node *pNode;
if(pValue->nType & MEMOBJ_NULL) {
ph7_hashmap *pMap = PH7_NewHashmap(pValue->pVm, 0, 0);
if(pMap == 0) {
return PH7_NOMEM;
}
pValue->x.pOther = pMap;
MemObjSetType(pValue, nType);
} else if((pValue->nType & MEMOBJ_HASHMAP) == 0) {
return PH7_CORRUPT;
}
if(nType & MEMOBJ_MIXED) {
/* Safe-store mixed[] keeps the source leaves' concrete types. */
pValue->nType |= MEMOBJ_MIXED;
return SXRET_OK;
}
pMap = (ph7_hashmap *)pValue->x.pOther;
for(pNode = pMap ? pMap->pFirst : 0; pNode; pNode = pNode->pPrev) {
ph7_value *pLeaf = (ph7_value *)SySetAt(&pValue->pVm->aMemObj, pNode->nValIdx);
sxi32 rc = VmNativeInitialType(pLeaf, (nType & ~MEMOBJ_HASHMAP) | (pLeaf->nType & MEMOBJ_HASHMAP));
if(rc != SXRET_OK) {
return rc;
}
}
pValue->nType = nType;
return SXRET_OK;
}
if(nType & MEMOBJ_MIXED) {
if(pValue->nType & MEMOBJ_HASHMAP) {
return PH7_CORRUPT;
}
/* Preserve the same scalar mixed contract as PH7_MemObjSafeStore,
* including explicit NULL, when the snapshot becomes static storage. */
if(pValue->nType == MEMOBJ_NULL) {
MemObjSetType(pValue, (MEMOBJ_MIXED | MEMOBJ_VOID));
} else {
pValue->nType |= MEMOBJ_MIXED;
}
return SXRET_OK;
}
if(pValue->nType != nType) {
ProcMemObjCast xCast;
if(PH7_CheckVarCompat(pValue, nType) != SXRET_OK) {
return PH7_CORRUPT;
}
xCast = PH7_MemObjCastMethod(nType);
if(xCast) {
return xCast(pValue);
}
}
return SXRET_OK;
}
static void VmNativeClassFree(ph7_vm *pVm, ph7_class *pClass) {
SyHashEntry *pEntry;
SyHashResetLoopCursor(&pClass->hMethod);
while((pEntry = SyHashGetNextEntry(&pClass->hMethod)) != 0) {
ph7_class_method *pMeth = (ph7_class_method *)pEntry->pUserData;
SyHashEntry *pInstalled = SyHashGet(&pVm->hFunction, pMeth->sVmName.zString, pMeth->sVmName.nByte);
if(pInstalled && ((ph7_vm_func *)pInstalled->pUserData)->pClass == pClass) {
SyHashDeleteEntry2(pInstalled);
}
}
SyHashResetLoopCursor(&pClass->hMethod);
while((pEntry = SyHashGetNextEntry(&pClass->hMethod)) != 0) {
VmNativeMethodFree(pVm, (ph7_class_method *)pEntry->pUserData);
}
SyHashResetLoopCursor(&pClass->hAttr);
while((pEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) {
ph7_class_attr *pAttr = (ph7_class_attr *)pEntry->pUserData;
if(pAttr->nIdx != SXU32_HIGH) {
PH7_VmDestroyMemObj(pVm, (ph7_value *)SySetAt(&pVm->aMemObj, pAttr->nIdx));
}
if(pAttr->pNativeInitial) {
PH7_MemObjRelease(pAttr->pNativeInitial);
SyMemBackendPoolFree(&pVm->sAllocator, pAttr->pNativeInitial);
}
SySetRelease(&pAttr->aByteCode);
SyMemBackendFree(&pVm->sAllocator, (void *)pAttr->sName.zString);
SyMemBackendPoolFree(&pVm->sAllocator, pAttr);
}
SyHashRelease(&pClass->hMethod);
SyHashRelease(&pClass->hAttr);
SyHashRelease(&pClass->hDerived);
SySetRelease(&pClass->aInterface);
SySetRelease(&pClass->aBase);
SySetRelease(&pClass->aMro);
SyMemBackendFree(&pVm->sAllocator, (void *)pClass->sName.zString);
SyMemBackendPoolFree(&pVm->sAllocator, pClass);
}
static sxi32 VmNativeMagicName(const char *zName) {
static const char *azName[] = {
"__construct", "__destruct", "__clone", "__invoke",
"__toInt", "__toFloat", "__toString", "__toBool"
};
sxu32 n;
for(n = 0; n < SX_ARRAYSIZE(azName); n++) {
if(SyStrncmp(zName, azName[n], SyStrlen(azName[n]) + 1) == 0) {
return TRUE;
}
}
return FALSE;
}
/*
* Prepare privately, then publish. No compiler diagnostics or VM fatal OOM
* paths are used here: a host may correct a declaration and retry.
*/
PH7_PRIVATE sxi32 PH7_VmCreateNativeClass(ph7_vm *pVm, const ph7_class_def *pDef) {
ph7_class *pClass;
SyString sName;
SyHashEntry *pEntry;
sxu32 n, i, nTypes = SySetUsed(&pVm->aTypeNames);
sxu32 nMem = SySetUsed(&pVm->aMemObj), nFree = SySetUsed(&pVm->aFreeObj);
VmSlot *aFree = 0;
sxi32 rc = PH7_CORRUPT;
if((pVm->nMagic != PH7_VM_INIT && pVm->nMagic != PH7_VM_RUN &&
pVm->nMagic != PH7_VM_EXEC && pVm->nMagic != PH7_VM_INCL) ||
!PH7_IsIdentifier(pDef->zName, FALSE) ||
(pDef->nField && !pDef->aField) || (pDef->nConstant && !pDef->aConstant) ||
(pDef->nMethod && !pDef->aMethod) || pDef->nField > SXI32_HIGH ||
pDef->nConstant > SXI32_HIGH - pDef->nField || pDef->nMethod > SXI32_HIGH) {
return PH7_CORRUPT;
}
SyStringInitFromBuf(&sName, pDef->zName, SyStrlen(pDef->zName));
if(SyHashGet(&pVm->hClass, sName.zString, sName.nByte)) {
return PH7_CORRUPT;
}
/* Hosts may register after vm_init, before compiling their program. */
if(pVm->apRefObj == 0) {
VmRefObj **apRef = (VmRefObj **)SyMemBackendAlloc(&pVm->sAllocator, 16 * sizeof(VmRefObj *));
if(apRef == 0) {
return PH7_NOMEM;
}
SyZero(apRef, 16 * sizeof(VmRefObj *));
pVm->apRefObj = apRef;
pVm->nRefSize = 16;
}
/* SySetPut's first failed growth leaves a nonzero capacity without
* storage. Preallocate the VM sets that must survive a failed transaction. */
if((pVm->aFreeObj.nSize == 0 && SySetAlloc(&pVm->aFreeObj, 8) != SXRET_OK) ||
(pVm->aTypeNames.nSize == 0 && SySetAlloc(&pVm->aTypeNames, 8) != SXRET_OK)) {
return PH7_NOMEM;
}
if(nFree) {
aFree = (VmSlot *)SyMemBackendAlloc(&pVm->sAllocator, nFree * sizeof(VmSlot));
if(aFree == 0) {
return PH7_NOMEM;
}
SyMemcpy(SySetBasePtr(&pVm->aFreeObj), aFree, nFree * sizeof(VmSlot));
}
pClass = PH7_NewRawClass(pVm, &sName);
if(pClass == 0) {
SyMemBackendFree(&pVm->sAllocator, aFree);
return PH7_NOMEM;
}
pClass->bNative = TRUE;
if(!pClass->hAttr.apBucket || !pClass->hMethod.apBucket || !pClass->hDerived.apBucket) {
rc = PH7_NOMEM;
goto Fail;
}
for(n = 0; n < pDef->nField + pDef->nConstant; n++) {
ph7_class_attr *pAttr;
const ph7_value *pInitial;
sxu32 nType = 0;
sxi32 iFlags;
if(n < pDef->nField) {
const ph7_field_def *pField = &pDef->aField[n];
if(pField->iFlags & ~PH7_MEMBER_STATIC) {
rc = PH7_CORRUPT;
goto Fail;
}
rc = VmNativeType(pVm, pClass, &pField->sType, &nType);
if(rc != SXRET_OK) {
goto Fail;
}
sName.zString = pField->zName;
pInitial = pField->pInitial;
iFlags = pField->iFlags & PH7_MEMBER_STATIC ? PH7_CLASS_ATTR_STATIC : 0;
} else {
const ph7_constant_def *pConst = &pDef->aConstant[n - pDef->nField];
sName.zString = pConst->zName;
pInitial = pConst->pValue;
iFlags = PH7_CLASS_ATTR_CONSTANT;
if(pInitial == 0) {
rc = PH7_CORRUPT;
goto Fail;
}
}
if(!PH7_IsIdentifier(sName.zString, (iFlags & PH7_CLASS_ATTR_CONSTANT) == 0) ||
(pInitial && pInitial->pVm != pVm)) {
rc = PH7_CORRUPT;
goto Fail;
}
sName.nByte = SyStrlen(sName.zString);
if((iFlags & PH7_CLASS_ATTR_CONSTANT) && PH7_IsReservedConstant(&sName)) {
rc = PH7_CORRUPT;
goto Fail;
}
if(SyHashGet(&pClass->hAttr, sName.zString, sName.nByte)) {
rc = PH7_CORRUPT;
goto Fail;
}
pAttr = PH7_NewClassAttr(pVm, pClass, &sName, 0, PH7_CLASS_PROT_PUBLIC, iFlags, nType);
if(pAttr == 0) {
rc = PH7_NOMEM;
goto Fail;
}
pAttr->pClass = pClass;
rc = SyHashInsert(&pClass->hAttr, pAttr->sName.zString, pAttr->sName.nByte, pAttr);
if(rc != SXRET_OK) {
SyMemBackendFree(&pVm->sAllocator, (void *)pAttr->sName.zString);
SyMemBackendPoolFree(&pVm->sAllocator, pAttr);
goto Fail;
}
if(pInitial) {
pAttr->pNativeInitial = (ph7_value *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(ph7_value));
if(pAttr->pNativeInitial == 0) {
rc = PH7_NOMEM;
goto Fail;
}
PH7_MemObjInit(pVm, pAttr->pNativeInitial);
rc = PH7_MemObjSnapshot((ph7_value *)pInitial, pAttr->pNativeInitial);
if(rc != SXRET_OK) {
goto Fail;
}
rc = VmNativeInitialType(pAttr->pNativeInitial, nType);
if(rc != SXRET_OK) {
goto Fail;
}
}
}
for(n = 0; n < pDef->nMethod; n++) {
const ph7_method_def *pDefMeth = &pDef->aMethod[n];
ph7_class_method *pMeth;
if(!PH7_IsMethodName(pDefMeth->zName) || !pDefMeth->xFunc ||
(pDefMeth->iFlags & ~PH7_MEMBER_STATIC) || (pDefMeth->nParam && !pDefMeth->aParam) ||
pDefMeth->nParam > SXI32_HIGH || VmNativeMagicName(pDefMeth->zName)) {
rc = PH7_CORRUPT;
goto Fail;
}
SyStringInitFromBuf(&sName, pDefMeth->zName, SyStrlen(pDefMeth->zName));
pEntry = SyHashGet(&pClass->hMethod, sName.zString, sName.nByte);
pMeth = PH7_NewClassMethod(pVm, pClass, &sName, 0, PH7_CLASS_PROT_PUBLIC,
pDefMeth->iFlags & PH7_MEMBER_STATIC ? PH7_CLASS_ATTR_STATIC : 0);
if(pMeth == 0) {
rc = PH7_NOMEM;
goto Fail;
}
/* Each prepared method owns its VM name, including overloads. */
if(pEntry) {
char *zName = SyMemBackendStrDup(&pVm->sAllocator, pMeth->sVmName.zString, pMeth->sVmName.nByte);
sxu32 nOffset = (sxu32)(pMeth->sFunc.sName.zString - pMeth->sVmName.zString);
if(zName == 0) {
pMeth->sVmName.zString = 0;
VmNativeMethodFree(pVm, pMeth);
rc = PH7_NOMEM;
goto Fail;
}
pMeth->sVmName.zString = zName;
pMeth->sFunc.sName.zString = &zName[nOffset];
}
SySetInit(&pMeth->sFunc.sNative.aAux, &pVm->sAllocator, sizeof(void *));
if(pMeth->sFunc.aByteCode.nSize == 0) {
VmNativeMethodFree(pVm, pMeth);
rc = PH7_NOMEM;
goto Fail;
}
rc = PH7_ClassInstallMethod(pClass, pMeth);
if(rc != SXRET_OK) {
VmNativeMethodFree(pVm, pMeth);
goto Fail;
}
pMeth->sFunc.sNative.sName.zString = SyMemBackendStrDup(&pVm->sAllocator, sName.zString, sName.nByte);
pMeth->sFunc.sNative.sName.nByte = sName.nByte;
if(pMeth->sFunc.sNative.sName.zString == 0) {
rc = PH7_NOMEM;
goto Fail;
}
pMeth->sFunc.sNative.xFunc = pDefMeth->xFunc;
pMeth->sFunc.sNative.pUserData = pDefMeth->pUserData;
rc = VmNativeType(pVm, pClass, &pDefMeth->sResult, &pMeth->sFunc.nType);
if(rc != SXRET_OK) {
goto Fail;
}
for(i = 0; i < pDefMeth->nParam; i++) {
const ph7_param_def *pDefArg = &pDefMeth->aParam[i];
ph7_vm_func_arg sArg;
sxu32 j;
char *zName;
if(!PH7_IsIdentifier(pDefArg->zName, TRUE) ||
SyStrncmp(pDefArg->zName, "this", sizeof("this")) == 0 ||
SyStrncmp(pDefArg->zName, "parent", sizeof("parent")) == 0) {
rc = PH7_CORRUPT;
goto Fail;
}
for(j = 0; j < SySetUsed(&pMeth->sFunc.aArgs); j++) {
ph7_vm_func_arg *pOther = (ph7_vm_func_arg *)SySetAt(&pMeth->sFunc.aArgs, j);
if(pOther->sName.nByte == SyStrlen(pDefArg->zName) &&
SyMemcmp(pOther->sName.zString, pDefArg->zName, pOther->sName.nByte) == 0) {
rc = PH7_CORRUPT;
goto Fail;
}
}
SyZero(&sArg, sizeof(sArg));
SySetInit(&sArg.aByteCode, &pVm->sAllocator, sizeof(VmInstr));
rc = VmNativeType(pVm, pClass, &pDefArg->sType, &sArg.nType);
if(rc != SXRET_OK) {
goto Fail;
}
if(sArg.nType & MEMOBJ_NAMED) {
sArg.sClass = *(SyString *)SySetAt(&pVm->aTypeNames, ((sArg.nType & MEMOBJ_NAMED) >> 12) - 1);
}
zName = SyMemBackendStrDup(&pVm->sAllocator, pDefArg->zName, SyStrlen(pDefArg->zName));
if(zName == 0) {
rc = PH7_NOMEM;
goto Fail;
}
SyStringInitFromBuf(&sArg.sName, zName, SyStrlen(zName));
rc = SySetPut(&pMeth->sFunc.aArgs, &sArg);
if(rc != SXRET_OK) {
SyMemBackendFree(&pVm->sAllocator, zName);
goto Fail;
}
}
SyHashResetLoopCursor(&pClass->hMethod);
while((pEntry = SyHashGetNextEntry(&pClass->hMethod)) != 0) {
ph7_class_method *pOther = (ph7_class_method *)pEntry->pUserData;
if(pOther != pMeth && PH7_VmSameSignature(&pOther->sFunc, &pMeth->sFunc)) {
rc = PH7_CORRUPT;
goto Fail;
}
}
}
/* Storage is prepared before the class becomes visible to the program. */
SyHashResetLoopCursor(&pClass->hAttr);
while((pEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) {
ph7_class_attr *pAttr = (ph7_class_attr *)pEntry->pUserData;
if(pAttr->iFlags & (PH7_CLASS_ATTR_STATIC | PH7_CLASS_ATTR_CONSTANT)) {
ph7_value *pValue = PH7_ReserveMemObj(pVm);
if(pValue == 0) {
rc = PH7_NOMEM;
goto Fail;
}
pAttr->nIdx = pValue->nIdx;
if(pAttr->pNativeInitial) {
rc = PH7_MemObjSnapshot(pAttr->pNativeInitial, pValue);
if(rc != SXRET_OK) {
goto Fail;
}
pValue = (ph7_value *)SySetAt(&pVm->aMemObj, pAttr->nIdx);
} else {
MemObjSetType(pValue, (pAttr->nType & MEMOBJ_TYPEMASK));
if(pAttr->nType & MEMOBJ_HASHMAP) {
pValue->x.pOther = PH7_NewHashmap(pVm, 0, 0);
if(pValue->x.pOther == 0) {
rc = PH7_NOMEM;
goto Fail;
}
}
}
rc = PH7_MemObjDeclareType(pValue, pAttr->nType, FALSE);
if(rc != SXRET_OK) {
goto Fail;
}
rc = PH7_VmRefObjInstall(pVm, pAttr->nIdx, 0, 0, VM_REF_IDX_KEEP);
if(rc != SXRET_OK) {
goto Fail;
}
}
}
SyHashResetLoopCursor(&pClass->hMethod);
while((pEntry = SyHashGetNextEntry(&pClass->hMethod)) != 0) {
ph7_class_method *pMeth = (ph7_class_method *)pEntry->pUserData;
rc = PH7_VmInstallUserFunction(pVm, &pMeth->sFunc, &pMeth->sVmName);
if(rc != SXRET_OK) {
goto Fail;
}
}
rc = SyHashInsert(&pVm->hClass, pClass->sName.zString, pClass->sName.nByte, pClass);
if(rc == SXRET_OK) {
SyMemBackendFree(&pVm->sAllocator, aFree);
return PH7_OK;
}
Fail:
VmNativeClassFree(pVm, pClass);
while(SySetUsed(&pVm->aTypeNames) > nTypes) {
SyString *pName = (SyString *)SySetPop(&pVm->aTypeNames);
SyMemBackendFree(&pVm->sAllocator, (void *)pName->zString);
}
/* Rollback must not allocate, including restoring reusable arena slots. */
for(n = nMem; n < SySetUsed(&pVm->aMemObj); n++) {
PH7_MemObjRelease((ph7_value *)SySetAt(&pVm->aMemObj, n));
}
pVm->aMemObj.nUsed = nMem;
if(nFree) {
SyMemcpy(aFree, SySetBasePtr(&pVm->aFreeObj), nFree * sizeof(VmSlot));
}
pVm->aFreeObj.nUsed = nFree;
SyMemBackendFree(&pVm->sAllocator, aFree);
return rc;
}
/* /*
* Mount a compiled class into the freshly created virtual machine so that * Mount a compiled class into the freshly created virtual machine so that
* it can be instanciated from the executed PHP script. * it can be instanciated from the executed PHP script.
@@ -1399,6 +1895,10 @@ static sxi32 VmMountUserClass(
ph7_class_attr *pAttr; ph7_class_attr *pAttr;
SyHashEntry *pEntry; SyHashEntry *pEntry;
sxi32 rc; sxi32 rc;
if(pClass->bNative) {
/* Registered classes already own their storage and overload chain. */
return SXRET_OK;
}
/* Reset the loop cursor */ /* Reset the loop cursor */
SyHashResetLoopCursor(&pClass->hAttr); SyHashResetLoopCursor(&pClass->hAttr);
/* Process only static and constant attribute */ /* Process only static and constant attribute */
@@ -1503,7 +2003,13 @@ PH7_PRIVATE sxi32 PH7_VmCreateClassInstanceFrame(
MemObjSetType(pMemObj, (pAttr->nType & MEMOBJ_TYPEMASK)); MemObjSetType(pMemObj, (pAttr->nType & MEMOBJ_TYPEMASK));
PH7_MemObjSetDeclaredType(pMemObj, pAttr->nType); PH7_MemObjSetDeclaredType(pMemObj, pAttr->nType);
PH7_MemObjInit(pVm, &sValue); PH7_MemObjInit(pVm, &sValue);
if(SySetUsed(&pAttr->aByteCode) > 0) { if(pAttr->pNativeInitial) {
rc = PH7_MemObjSafeStore(pAttr->pNativeInitial, pMemObj);
pMemObj = (ph7_value *)SySetAt(&pVm->aMemObj, nObjIdx);
if(rc != SXRET_OK) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Cannot assign a value of incompatible type to variable '%z::$%z'", &pClass->sName, &pAttr->sName);
}
} else if(SySetUsed(&pAttr->aByteCode) > 0) {
/* Initialize attribute default value (any complex expression) */ /* Initialize attribute default value (any complex expression) */
rc = VmLocalExec(&(*pVm), &pAttr->aByteCode, &sValue); rc = VmLocalExec(&(*pVm), &pAttr->aByteCode, &sValue);
pMemObj = (ph7_value *)SySetAt(&pVm->aMemObj, nObjIdx); pMemObj = (ph7_value *)SySetAt(&pVm->aMemObj, nObjIdx);
@@ -1952,6 +2458,36 @@ static ph7_value *VmNewOperandStack(
} }
/* Forward declaration */ /* Forward declaration */
static sxi32 VmRegisterSpecialFunction(ph7_vm *pVm); static sxi32 VmRegisterSpecialFunction(ph7_vm *pVm);
/* Shared by bytecode returns and native methods, after ordinary argument prep. */
static void VmCheckMissingReturn(ph7_vm *pVm, ph7_vm_func *pFunc) {
if(pFunc->nType && (pFunc->nType & MEMOBJ_VOID) == 0) {
PH7_VmThrowError(pVm, PH7_CTX_WARNING, "Missing return value in closure/method returning non-void");
}
}
static void VmCheckReturn(ph7_vm *pVm, ph7_vm_func *pFunc, ph7_value *pValue) {
if(pFunc->nType & MEMOBJ_NAMED) {
PH7_VmTypeClass(pVm, pFunc->nType, TRUE);
if(PH7_VmTypeCheck(pValue, pFunc->nType) != SXRET_OK) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
}
PH7_MemObjSetDeclaredType(pValue, pFunc->nType);
} else if(pFunc->nType && (pFunc->nType & MEMOBJ_MIXED) == 0) {
if(pFunc->nType & MEMOBJ_VOID) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Return with a value in closure/method returning void");
} else if(pFunc->nType != pValue->nType) {
if(PH7_CheckVarCompat(pValue, pFunc->nType) == SXRET_OK) {
ProcMemObjCast xCast = PH7_MemObjCastMethod(pFunc->nType);
xCast(pValue);
} else if((pFunc->iFlags & MEMOBJ_HASHMAP) && (pValue->nType & MEMOBJ_HASHMAP)) {
if(PH7_HashmapCast(pValue, pFunc->iFlags ^ MEMOBJ_HASHMAP) != SXRET_OK) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
}
} else {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
}
}
}
}
/* Exception frames retain the enclosing method's lexical scope. */ /* Exception frames retain the enclosing method's lexical scope. */
static ph7_class *VmMethodClass(ph7_vm *pVm) { static ph7_class *VmMethodClass(ph7_vm *pVm) {
VmFrame *pFrame = pVm->pFrame; VmFrame *pFrame = pVm->pFrame;
@@ -1994,6 +2530,7 @@ PH7_PRIVATE sxi32 PH7_VmMakeReady(
return SXERR_MEM; return SXERR_MEM;
} }
/* Allocate the reference table */ /* Allocate the reference table */
if(pVm->apRefObj == 0) {
pVm->nRefSize = 0x10; /* Must be a power of two for fast arithemtic */ pVm->nRefSize = 0x10; /* Must be a power of two for fast arithemtic */
pVm->apRefObj = (VmRefObj **)SyMemBackendAlloc(&pVm->sAllocator, sizeof(VmRefObj *) * pVm->nRefSize); pVm->apRefObj = (VmRefObj **)SyMemBackendAlloc(&pVm->sAllocator, sizeof(VmRefObj *) * pVm->nRefSize);
if(pVm->apRefObj == 0) { if(pVm->apRefObj == 0) {
@@ -2002,6 +2539,7 @@ PH7_PRIVATE sxi32 PH7_VmMakeReady(
} }
/* Zero the reference table */ /* Zero the reference table */
SyZero(pVm->apRefObj, sizeof(VmRefObj *) * pVm->nRefSize); SyZero(pVm->apRefObj, sizeof(VmRefObj *) * pVm->nRefSize);
}
/* Register special functions first [i.e: print, json_encode(), func_get_args(), die, etc.] */ /* Register special functions first [i.e: print, json_encode(), func_get_args(), die, etc.] */
rc = VmRegisterSpecialFunction(&(*pVm)); rc = VmRegisterSpecialFunction(&(*pVm));
if(rc != SXRET_OK) { if(rc != SXRET_OK) {
@@ -2072,6 +2610,25 @@ static void VmFinalizeObjects(ph7_vm *pVm) {
* stay installed; newly inserted closures are visited on the next pass. */ * stay installed; newly inserted closures are visited on the next pass. */
pVm->hFunction.pCurrent = pNext; pVm->hFunction.pCurrent = pNext;
} }
/* Native declaration snapshots can be the last owners of objects.
* Finalize them while callbacks, scope and indexed fields are live. */
SyHashResetLoopCursor(&pVm->hClass);
while((pFuncEntry = SyHashGetNextEntry(&pVm->hClass)) != 0) {
ph7_class *pClass = (ph7_class *)pFuncEntry->pUserData;
SyHashEntry_Pr *pNextClass = pVm->hClass.pCurrent;
SyHashEntry *pAttrEntry;
SyHashResetLoopCursor(&pClass->hAttr);
while((pAttrEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) {
ph7_class_attr *pAttr = (ph7_class_attr *)pAttrEntry->pUserData;
SyHashEntry_Pr *pNextAttr = pClass->hAttr.pCurrent;
ph7_value *pInitial = pAttr->pNativeInitial;
if(pInitial && (pInitial->nType & (MEMOBJ_OBJ | MEMOBJ_HASHMAP)) == MEMOBJ_OBJ) {
nFinalized |= PH7_ClassInstanceFinalize((ph7_class_instance *)pInitial->x.pOther);
}
pClass->hAttr.pCurrent = pNextAttr;
}
pVm->hClass.pCurrent = pNextClass;
}
/* Reacquire indexed values: a destructor may grow aMemObj. Arrays and /* Reacquire indexed values: a destructor may grow aMemObj. Arrays and
* object attributes already store their contained values in this set. * object attributes already store their contained values in this set.
* Object-typed arrays carry MEMOBJ_OBJ too, but are not instances. */ * Object-typed arrays carry MEMOBJ_OBJ too, but are not instances. */
@@ -2096,6 +2653,20 @@ PH7_PRIVATE sxi32 PH7_VmRelease(ph7_vm *pVm) {
/* No more user code can need reusable slots. Keep recycling enabled for /* No more user code can need reusable slots. Keep recycling enabled for
* destructors above, but do not grow the free list during final reclaim. */ * destructors above, but do not grow the free list during final reclaim. */
pVm->nMagic = PH7_VM_STALE; pVm->nMagic = PH7_VM_STALE;
/* Release declaration snapshots before indexed array storage and modules. */
SyHashResetLoopCursor(&pVm->hClass);
while((pFuncEntry = SyHashGetNextEntry(&pVm->hClass)) != 0) {
ph7_class *pClass = (ph7_class *)pFuncEntry->pUserData;
SyHashEntry *pAttrEntry;
SyHashResetLoopCursor(&pClass->hAttr);
while((pAttrEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) {
ph7_class_attr *pAttr = (ph7_class_attr *)pAttrEntry->pUserData;
if(pAttr->pNativeInitial) {
PH7_MemObjRelease(pAttr->pNativeInitial);
pAttr->pNativeInitial = 0;
}
}
}
/* Captures have the same lifetime as installed runtime closures. Release /* Captures have the same lifetime as installed runtime closures. Release
* them while indexed storage and reference bookkeeping are still valid. */ * them while indexed storage and reference bookkeeping are still valid. */
SyHashResetLoopCursor(&pVm->hFunction); SyHashResetLoopCursor(&pVm->hFunction);
@@ -2155,6 +2726,8 @@ static sxi32 VmInitCallContext(
MemObjSetType(pRet, MEMOBJ_NULL); MemObjSetType(pRet, MEMOBJ_NULL);
pOut->pRet = pRet; pOut->pRet = pRet;
pOut->iFlags = iFlags; pOut->iFlags = iFlags;
pOut->pThis = 0;
pOut->bResultSet = FALSE;
return SXRET_OK; return SXRET_OK;
} }
/* /*
@@ -2192,6 +2765,51 @@ static void VmReleaseCallContext(ph7_context *pCtx) {
SySetRelease(&pCtx->sChunk); SySetRelease(&pCtx->sChunk);
} }
} }
/* Borrowed wrappers stay stable even if callback allocations grow aMemObj. */
static sxi32 VmCallNativeMethod(ph7_vm *pVm, ph7_vm_func *pFunc,
VmFrame *pFrame, ph7_class_instance *pThis, ph7_value *pResult) {
ph7_context sCtx;
ph7_value sThis;
ph7_value *aValue, **apArg;
sxu32 n, nArg = SySetUsed(&pFrame->sArg);
sxi32 rc;
aValue = (ph7_value *)SyMemBackendAlloc(&pVm->sAllocator,
(nArg ? nArg : 1) * (sizeof(ph7_value) + sizeof(ph7_value *)));
if(aValue == 0) {
PH7_VmMemoryError(pVm);
return PH7_ABORT;
}
apArg = (ph7_value **)&aValue[nArg];
for(n = 0; n < nArg; n++) {
VmSlot *pSlot = (VmSlot *)SySetAt(&pFrame->sArg, n);
PH7_MemObjInit(pVm, &aValue[n]);
PH7_MemObjLoad((ph7_value *)SySetAt(&pVm->aMemObj, pSlot->nIdx), &aValue[n]);
apArg[n] = &aValue[n];
}
PH7_MemObjInit(pVm, &sThis);
if(pThis) {
sThis.x.pOther = pThis;
MemObjSetType(&sThis, MEMOBJ_OBJ);
/* Borrowed: the active frame already retains the instance. */
}
VmInitCallContext(&sCtx, pVm, &pFunc->sNative, pResult, 0);
sCtx.pThis = pThis && (((ph7_class_method *)pFunc)->iFlags & PH7_CLASS_ATTR_STATIC) == 0 ? &sThis : 0;
rc = pFunc->sNative.xFunc(&sCtx, (int)nArg, apArg);
VmReleaseCallContext(&sCtx);
for(n = 0; n < nArg; n++) {
PH7_MemObjRelease(&aValue[n]);
}
SyMemBackendFree(&pVm->sAllocator, aValue);
/* Do not enforce a result contract on an exception or an interrupted call. */
if(rc == PH7_OK) {
if(sCtx.bResultSet) {
VmCheckReturn(pVm, pFunc, pResult);
} else {
VmCheckMissingReturn(pVm, pFunc);
}
}
return rc;
}
/* /*
* Release a ph7_value allocated from the body of a foreign function. * Release a ph7_value allocated from the body of a foreign function.
* Refer to [ph7_context_release_value()] for additional information. * Refer to [ph7_context_release_value()] for additional information.
@@ -2949,9 +3567,7 @@ Resume:
* Keep the call's NULL result unconverted, unlike explicit return. */ * Keep the call's NULL result unconverted, unlike explicit return. */
if(!pInstr->iP1 && pInstr->iP2 && (pEntryFrame->iFlags & VM_FRAME_ACTIVE) && (pEntryFrame->iFlags & VM_FRAME_THROW) == 0) { if(!pInstr->iP1 && pInstr->iP2 && (pEntryFrame->iFlags & VM_FRAME_ACTIVE) && (pEntryFrame->iFlags & VM_FRAME_THROW) == 0) {
ph7_vm_func *pFunc = (ph7_vm_func *)pEntryFrame->pUserData; ph7_vm_func *pFunc = (ph7_vm_func *)pEntryFrame->pUserData;
if(pFunc->nType && (pFunc->nType & MEMOBJ_VOID) == 0) { VmCheckMissingReturn(pVm, pFunc);
PH7_VmThrowError(&(*pVm), PH7_CTX_WARNING, "Missing return value in closure/method returning non-void");
}
} }
if(pInstr->iP1) { if(pInstr->iP1) {
if(pTos < pStack) { if(pTos < pStack) {
@@ -6201,8 +6817,9 @@ Resume:
/* Mark current frame as active */ /* Mark current frame as active */
pFrame->iFlags |= VM_FRAME_ACTIVE; pFrame->iFlags |= VM_FRAME_ACTIVE;
/* Allocate a new operand stack and evaluate the function body */ /* Allocate a new operand stack and evaluate the function body */
pFrameStack = VmNewOperandStack(&(*pVm), SySetUsed(&pVmFunc->aByteCode)); pFrameStack = pVmFunc->sNative.xFunc ? 0 :
if(pFrameStack == 0) { VmNewOperandStack(&(*pVm), SySetUsed(&pVmFunc->aByteCode));
if(pFrameStack == 0 && pVmFunc->sNative.xFunc == 0) {
/* Raise exception: Out of memory */ /* Raise exception: Out of memory */
PH7_VmMemoryError(&(*pVm)); PH7_VmMemoryError(&(*pVm));
} }
@@ -6211,7 +6828,11 @@ Resume:
SySetPut(&pVm->aSelf, (const void *)&pSelf); SySetPut(&pVm->aSelf, (const void *)&pSelf);
} }
/* Execute function body */ /* Execute function body */
if(pVmFunc->sNative.xFunc) {
rc = VmCallNativeMethod(pVm, pVmFunc, pFrame, pThis, pTos);
} else {
rc = VmByteCodeExec(&(*pVm), (VmInstr *)SySetBasePtr(&pVmFunc->aByteCode), pFrameStack, -1, pTos, FALSE); rc = VmByteCodeExec(&(*pVm), (VmInstr *)SySetBasePtr(&pVmFunc->aByteCode), pFrameStack, -1, pTos, FALSE);
}
if(pSelf) { if(pSelf) {
/* Pop class name */ /* Pop class name */
(void)SySetPop(&pVm->aSelf); (void)SySetPop(&pVm->aSelf);
@@ -6427,28 +7048,7 @@ Unwind:
if(bReturnValue && (pEntryFrame->iFlags & VM_FRAME_ACTIVE) && if(bReturnValue && (pEntryFrame->iFlags & VM_FRAME_ACTIVE) &&
(iExecFlags & (VM_EXEC_HANDLER | VM_EXEC_EVAL)) == 0) { (iExecFlags & (VM_EXEC_HANDLER | VM_EXEC_EVAL)) == 0) {
ph7_vm_func *pFunc = (ph7_vm_func *)pEntryFrame->pUserData; ph7_vm_func *pFunc = (ph7_vm_func *)pEntryFrame->pUserData;
if(pFunc->nType & MEMOBJ_NAMED) { VmCheckReturn(pVm, pFunc, &sTransfer);
PH7_VmTypeClass(pVm, pFunc->nType, TRUE);
if(PH7_VmTypeCheck(&sTransfer, pFunc->nType) != SXRET_OK) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
}
PH7_MemObjSetDeclaredType(&sTransfer, pFunc->nType);
} else if(pFunc->nType && (pFunc->nType & MEMOBJ_MIXED) == 0) {
if(pFunc->nType & MEMOBJ_VOID) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Return with a value in closure/method returning void");
} else if(pFunc->nType != sTransfer.nType) {
if(PH7_CheckVarCompat(&sTransfer, pFunc->nType) == SXRET_OK) {
ProcMemObjCast xCast = PH7_MemObjCastMethod(pFunc->nType);
xCast(&sTransfer);
} else if((pFunc->iFlags & MEMOBJ_HASHMAP) && (sTransfer.nType & MEMOBJ_HASHMAP)) {
if(PH7_HashmapCast(&sTransfer, pFunc->iFlags ^ MEMOBJ_HASHMAP) != SXRET_OK) {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
}
} else {
PH7_VmThrowError(pVm, PH7_CTX_ERR, "Incompatible type when returning data by closure/method");
}
}
}
} }
rc = (iExecFlags & (VM_EXEC_INCLUDE | VM_EXEC_HANDLER)) ? rc = (iExecFlags & (VM_EXEC_INCLUDE | VM_EXEC_HANDLER)) ?
(bReturnValue ? VM_INCLUDE_RETURN : VM_RETURN_VOID) : SXRET_OK; (bReturnValue ? VM_INCLUDE_RETURN : VM_RETURN_VOID) : SXRET_OK;
@@ -7579,45 +8179,58 @@ static int VmQueryInterfaceSet(ph7_class *pClass, SySet *pSet) {
} }
return FALSE; return FALSE;
} }
/* /* One nominal compatibility implementation: SXRET_OK matches, SXERR_NOMATCH
* This function returns TRUE if the given class (first argument) * does not. Registration propagates allocation failures while ordinary
* is an instance of the main class (second argument). * execution keeps its existing fatal OOM behavior. */
* Otherwise FALSE is returned. static sxi32 VmInstanceOfResult(ph7_vm *pVm, ph7_class *pThis, ph7_class *pClass, sxi32 bRecoverable) {
*/
static int VmInstanceOf(ph7_vm *pVm, ph7_class *pThis, ph7_class *pClass) {
SySet sInterfaces; SySet sInterfaces;
sxi32 rc; sxi32 rc;
if(!pThis || !pClass) { if(!pThis || !pClass) {
return FALSE; return SXERR_NOMATCH;
} }
if(pThis == pClass) { if(pThis == pClass) {
/* Instance of the same class */ /* Instance of the same class */
return TRUE; return SXRET_OK;
} }
if(!(pThis->iFlags & PH7_CLASS_INTERFACE)) { if(!(pThis->iFlags & PH7_CLASS_INTERFACE)) {
sxu32 i; sxu32 i;
if(PH7_ClassBuildMro(pVm, pThis) != SXRET_OK) { rc = PH7_ClassBuildMro(pVm, pThis);
if(rc != SXRET_OK) {
if(bRecoverable) {
return rc;
}
PH7_VmThrowError(pVm, PH7_CTX_ERR, "PH7 is running out-of-memory"); PH7_VmThrowError(pVm, PH7_CTX_ERR, "PH7 is running out-of-memory");
} }
for(i = 1; i < SySetUsed(&pThis->aMro); i++) { for(i = 1; i < SySetUsed(&pThis->aMro); i++) {
if(*(ph7_class **)SySetAt(&pThis->aMro, i) == pClass) { if(*(ph7_class **)SySetAt(&pThis->aMro, i) == pClass) {
return TRUE; return SXRET_OK;
} }
} }
if(!(pClass->iFlags & PH7_CLASS_INTERFACE)) { if(!(pClass->iFlags & PH7_CLASS_INTERFACE)) {
return FALSE; return SXERR_NOMATCH;
} }
} }
SySetInit(&sInterfaces, &pVm->sAllocator, sizeof(ph7_class *)); SySetInit(&sInterfaces, &pVm->sAllocator, sizeof(ph7_class *));
rc = PH7_ClassCollectInterfaces(pVm, pThis, &sInterfaces); rc = PH7_ClassCollectInterfaces(pVm, pThis, &sInterfaces);
if(rc != SXRET_OK) { if(rc != SXRET_OK) {
SySetRelease(&sInterfaces); SySetRelease(&sInterfaces);
if(bRecoverable) {
return rc;
}
PH7_VmMemoryError(pVm); PH7_VmMemoryError(pVm);
return FALSE; return SXERR_NOMATCH;
} }
rc = VmQueryInterfaceSet(pClass, &sInterfaces); rc = VmQueryInterfaceSet(pClass, &sInterfaces);
SySetRelease(&sInterfaces); SySetRelease(&sInterfaces);
return rc; return rc ? SXRET_OK : SXERR_NOMATCH;
}
/*
* This function returns TRUE if the given class (first argument)
* is an instance of the main class (second argument).
* Otherwise FALSE is returned.
*/
static int VmInstanceOf(ph7_vm *pVm, ph7_class *pThis, ph7_class *pClass) {
return VmInstanceOfResult(pVm, pThis, pClass, FALSE) == SXRET_OK;
} }
/* /*
* This function returns TRUE if the given class (first argument) * This function returns TRUE if the given class (first argument)
@@ -11457,7 +12070,9 @@ PH7_PRIVATE sxi32 PH7_VmRefObjInstall(
} }
if(pMapEntry) { if(pMapEntry) {
/* Address of the hashmap node [i.e: Array entry] */ /* Address of the hashmap node [i.e: Array entry] */
SySetPut(&pRef->aArrEntries, (const void *)&pMapEntry); if(SySetPut(&pRef->aArrEntries, (const void *)&pMapEntry) != SXRET_OK) {
return SXERR_MEM;
}
} }
return SXRET_OK; return SXRET_OK;
} }
+54
View File
@@ -530,6 +530,60 @@ PH7_APIEXPORT int ph7_vm_reset(ph7_vm *pVm);
PH7_APIEXPORT int ph7_vm_release(ph7_vm *pVm); PH7_APIEXPORT int ph7_vm_release(ph7_vm *pVm);
PH7_APIEXPORT int ph7_vm_dump(ph7_vm *pVm, int (*xConsumer)(const void *, unsigned int, void *), void *pUserData); PH7_APIEXPORT int ph7_vm_dump(ph7_vm *pVm, int (*xConsumer)(const void *, unsigned int, void *), void *pUserData);
/* In-process Extending Interfaces */ /* In-process Extending Interfaces */
/*
* Public native class declarations. Names are NUL-terminated identifiers.
* A NULL initializer pointer means no initializer; a value containing NULL is
* an explicit initializer. Descriptors and values are copied by registration.
* Callback code and user data must remain valid until the VM is released.
*/
enum ph7_type {
PH7_TYPE_BOOL = 1, PH7_TYPE_CALLBACK, PH7_TYPE_CHAR, PH7_TYPE_FLOAT,
PH7_TYPE_INT, PH7_TYPE_MIXED, PH7_TYPE_OBJECT, PH7_TYPE_RESOURCE,
PH7_TYPE_STRING, PH7_TYPE_VOID, PH7_TYPE_NAMED
};
typedef struct ph7_type_def {
int iType;
const char *zClass; /* Required only for PH7_TYPE_NAMED; self is allowed. */
int bArray; /* Zero or one: the language's T[] annotation. */
} ph7_type_def;
typedef struct ph7_field_def {
const char *zName;
ph7_type_def sType;
const ph7_value *pInitial;
unsigned int iFlags;
} ph7_field_def;
typedef struct ph7_constant_def {
const char *zName;
const ph7_value *pValue;
} ph7_constant_def;
typedef struct ph7_param_def {
const char *zName; /* Exact lowercase this/parent are reserved, even for static methods. */
ph7_type_def sType;
} ph7_param_def;
typedef struct ph7_method_def {
const char *zName;
ph7_type_def sResult;
const ph7_param_def *aParam;
unsigned int nParam;
int (*xFunc)(ph7_context *, int, ph7_value **);
void *pUserData;
unsigned int iFlags;
} ph7_method_def;
typedef struct ph7_class_def {
const char *zName;
const ph7_field_def *aField;
unsigned int nField;
const ph7_constant_def *aConstant;
unsigned int nConstant;
const ph7_method_def *aMethod;
unsigned int nMethod;
} ph7_class_def;
#define PH7_MEMBER_STATIC 0x01
/* Return an exception object using ph7_result_value(), then this status. */
#define PH7_EXCEPTION (-255)
PH7_APIEXPORT int ph7_create_class(ph7_vm *pVm, const ph7_class_def *pDef);
/* Borrowed receiver, valid only during this callback; NULL for static calls. */
PH7_APIEXPORT ph7_value *ph7_context_this(ph7_context *pCtx);
PH7_APIEXPORT int ph7_create_function(ph7_vm *pVm, const char *zName, int (*xFunc)(ph7_context *, int, ph7_value **), void *pUserData); PH7_APIEXPORT int ph7_create_function(ph7_vm *pVm, const char *zName, int (*xFunc)(ph7_context *, int, ph7_value **), void *pUserData);
PH7_APIEXPORT int ph7_delete_function(ph7_vm *pVm, const char *zName); PH7_APIEXPORT int ph7_delete_function(ph7_vm *pVm, const char *zName);
PH7_APIEXPORT int ph7_create_constant(ph7_vm *pVm, const char *zName, void (*xExpand)(ph7_value *, void *), void *pUserData); PH7_APIEXPORT int ph7_create_constant(ph7_vm *pVm, const char *zName, void (*xExpand)(ph7_value *, void *), void *pUserData);
+12 -1
View File
@@ -756,6 +756,8 @@ struct ph7_context {
*/ */
ph7_vm *pVm; /* Virtual machine that own this context */ ph7_vm *pVm; /* Virtual machine that own this context */
sxi32 iFlags; /* Call flags */ sxi32 iFlags; /* Call flags */
ph7_value *pThis; /* Stable borrowed native method receiver */
sxi32 bResultSet; /* Distinguish explicit NULL from a bare return */
}; };
/* /*
* Each hashmap entry [i.e: array(4,5,6)] is recorded in an instance * Each hashmap entry [i.e: array(4,5,6)] is recorded in an instance
@@ -1014,6 +1016,7 @@ struct ph7_vm_func {
*/ */
void *pUserData; /* Upper layer private data associated with this instance */ void *pUserData; /* Upper layer private data associated with this instance */
ph7_vm_func *pNextName; /* Next VM function with the same name as this one */ ph7_vm_func *pNextName; /* Next VM function with the same name as this one */
ph7_user_func sNative; /* Native method body and overload-specific data */
}; };
/* Forward reference */ /* Forward reference */
typedef struct ph7_builtin_constant ph7_builtin_constant; typedef struct ph7_builtin_constant ph7_builtin_constant;
@@ -1067,6 +1070,7 @@ struct ph7_class {
SySet aBase; /* Direct class bases in extends order */ SySet aBase; /* Direct class bases in extends order */
SySet aMro; /* C3 linearization, including this class first */ SySet aMro; /* C3 linearization, including this class first */
sxi32 iMroState; /* 0: unbuilt, 1: building, 2: ready */ sxi32 iMroState; /* 0: unbuilt, 1: building, 2: ready */
sxi32 bNative; /* Public native declaration */
}; };
/* Class configuration flags */ /* Class configuration flags */
#define PH7_CLASS_FINAL 0x001 /* Class is final [cannot be extended] */ #define PH7_CLASS_FINAL 0x001 /* Class is final [cannot be extended] */
@@ -1089,6 +1093,7 @@ struct ph7_class_attr {
sxu32 nType; /* Class attribute data type */ sxu32 nType; /* Class attribute data type */
sxu32 nIdx; /* Attribute index */ sxu32 nIdx; /* Attribute index */
sxu32 nLine; /* Line number on which this attribute was defined */ sxu32 nLine; /* Line number on which this attribute was defined */
ph7_value *pNativeInitial; /* Owned native declaration snapshot, if present */
}; };
/* Attribute configuration */ /* Attribute configuration */
#define PH7_CLASS_ATTR_STATIC 0x001 /* Static attribute */ #define PH7_CLASS_ATTR_STATIC 0x001 /* Static attribute */
@@ -1383,7 +1388,6 @@ struct VmShutdownCB {
int nArg; /* Total number of given arguments */ int nArg; /* Total number of given arguments */
}; };
/* Uncaught exception code value */ /* Uncaught exception code value */
#define PH7_EXCEPTION -255
/* /*
* Allowed value for ph7_vm.nMagic * Allowed value for ph7_vm.nMagic
@@ -1734,6 +1738,13 @@ PH7_PRIVATE sxi32 PH7_VmInstallForeignFunction(ph7_vm *pVm, const SyString *pNam
PH7_PRIVATE sxi32 PH7_VmInstallClass(ph7_vm *pVm, ph7_class *pClass); PH7_PRIVATE sxi32 PH7_VmInstallClass(ph7_vm *pVm, ph7_class *pClass);
PH7_PRIVATE sxi32 PH7_VmBlobConsumer(const void *pSrc, unsigned int nLen, void *pUserData); PH7_PRIVATE sxi32 PH7_VmBlobConsumer(const void *pSrc, unsigned int nLen, void *pUserData);
PH7_PRIVATE ph7_value *PH7_ReserveMemObj(ph7_vm *pVm); PH7_PRIVATE ph7_value *PH7_ReserveMemObj(ph7_vm *pVm);
PH7_PRIVATE sxi32 PH7_VmInternType(ph7_vm *pVm, const SyString *pName, sxu32 *pType);
PH7_PRIVATE sxi32 PH7_MemObjSnapshot(ph7_value *pSrc, ph7_value *pDest);
PH7_PRIVATE sxi32 PH7_MemObjDeclareType(ph7_value *pValue, sxu32 nType, sxi32 bResolve);
PH7_PRIVATE sxi32 PH7_IsIdentifier(const char *zName, sxi32 bKeyword);
PH7_PRIVATE sxi32 PH7_IsMethodName(const char *zName);
PH7_PRIVATE sxi32 PH7_IsReservedConstant(const SyString *pName);
PH7_PRIVATE sxi32 PH7_VmCreateNativeClass(ph7_vm *pVm, const ph7_class_def *pDef);
PH7_PRIVATE ph7_value *PH7_ReserveConstObj(ph7_vm *pVm, sxu32 *pIndex); PH7_PRIVATE ph7_value *PH7_ReserveConstObj(ph7_vm *pVm, sxu32 *pIndex);
PH7_PRIVATE sxi32 PH7_VmOutputConsume(ph7_vm *pVm, SyString *pString); PH7_PRIVATE sxi32 PH7_VmOutputConsume(ph7_vm *pVm, SyString *pString);
PH7_PRIVATE sxi32 PH7_VmOutputConsumeAp(ph7_vm *pVm, const char *zFormat, va_list ap); PH7_PRIVATE sxi32 PH7_VmOutputConsumeAp(ph7_vm *pVm, const char *zFormat, va_list ap);