Add system module with System class, core native exception helper and remove useless builtins
Build / AerScript (push) Successful in 50s

This commit is contained in:
2026-11-24 09:34:27 +01:00
parent cb894f66fd
commit 1db842fe0f
20 changed files with 1050 additions and 1145 deletions
+12 -127
View File
@@ -35,121 +35,6 @@ static const char *CharErrorDescription(int nErr) {
/* ------------------------------------------------------------------ */
/* CharException */
/* ------------------------------------------------------------------ */
static ph7_value *CharInstField(ph7_class_instance *pInst, const char *zName) {
SyString s;
if(pInst == 0) {
return 0;
}
SyStringInitFromBuf(&s, zName, (sxu32)SyStrlen(zName));
return PH7_ClassInstanceFetchAttr(pInst, &s);
}
/* Stamp the inherited Exception file/line with the first script frame. */
static void CharStampSite(ph7_vm *pVm, ph7_class_instance *pInst) {
SySet sTrace;
VmDebugTrace *pTrace;
ph7_value *pField;
if(VmExtractDebugTrace(pVm, &sTrace) != SXRET_OK) {
return;
}
while(SySetGetNextEntry(&sTrace, (void **)&pTrace) == SXRET_OK) {
if(pTrace->pFile == 0 || pTrace->nLine == 0) {
continue;
}
if(pTrace->pFile->nByte == sizeof("[NATIVE]") - 1 &&
SyMemcmp(pTrace->pFile->zString, "[NATIVE]", sizeof("[NATIVE]") - 1) == 0) {
continue;
}
pField = CharInstField(pInst, "file");
if(pField) {
PH7_MemObjRelease(pField);
PH7_MemObjInit(pVm, pField);
(void)ph7_value_string(pField, pTrace->pFile->zString, (int)pTrace->pFile->nByte);
}
pField = CharInstField(pInst, "line");
if(pField) {
(void)ph7_value_int64(pField, (ph7_int64)pTrace->nLine);
}
break;
}
SySetRelease(&sTrace);
}
/* Throw a real CharException instance; every temporary is released on all
* paths and the pending transfer is left for the VM unwinder. */
static int CharThrow(ph7_context *pCtx, int nErr, const char *zMsg) {
ph7_vm *pVm = pCtx->pVm;
ph7_class *pClass;
ph7_class_instance *pInst;
ph7_class_method *pCons;
ph7_value *pObj, *apArg[2], *aTmp[2];
sxi32 rc;
sxu32 n;
if(pVm->iPendingTransfer) {
return PH7_EXCEPTION;
}
pClass = PH7_VmExtractClass(pVm, CCLS_EXC, sizeof(CCLS_EXC) - 1, FALSE);
if(pClass == 0) {
return PH7_ABORT;
}
pInst = PH7_NewClassInstance(pVm, pClass);
if(pInst == 0) {
PH7_VmMemoryError(pVm);
return PH7_ABORT;
}
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
aTmp[n] = ph7_context_new_scalar(pCtx);
if(aTmp[n] == 0) {
while(n > 0) {
--n;
ph7_context_release_value(pCtx, aTmp[n]);
}
PH7_ClassInstanceUnref(pInst);
PH7_VmMemoryError(pVm);
return PH7_ABORT;
}
}
pObj = ph7_context_new_scalar(pCtx);
if(pObj == 0) {
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
ph7_context_release_value(pCtx, aTmp[n]);
}
PH7_ClassInstanceUnref(pInst);
PH7_VmMemoryError(pVm);
return PH7_ABORT;
}
ph7_value_string(aTmp[0], zMsg, -1);
ph7_value_int(aTmp[1], nErr);
apArg[0] = aTmp[0];
apArg[1] = aTmp[1];
pCons = PH7_ClassExtractMethod(pClass, "__construct", sizeof("__construct") - 1);
if(pCons) {
rc = PH7_VmCallClassMethod(pVm, pInst, pCons, 0, 2, apArg);
if(pInst->iRef < 1) {
pInst->iRef = 1;
}
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
ph7_context_release_value(pCtx, aTmp[n]);
}
if(rc != PH7_OK) {
ph7_context_release_value(pCtx, pObj);
PH7_ClassInstanceUnref(pInst);
return rc;
}
} else {
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
ph7_context_release_value(pCtx, aTmp[n]);
}
}
CharStampSite(pVm, pInst);
if(pInst->iRef < 1) {
pInst->iRef = 1;
}
PH7_MemObjRelease(pObj);
pObj->x.pOther = pInst;
MemObjSetType(pObj, MEMOBJ_OBJ);
rc = ph7_result_value(pCtx, pObj);
ph7_context_release_value(pCtx, pObj);
return rc == PH7_OK ? PH7_EXCEPTION : rc;
}
static int CharExceptionConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_value *pResult = ph7_context_new_scalar(pCtx);
sxi32 rc;
@@ -177,12 +62,12 @@ static int CharOperand(ph7_context *pCtx, ph7_value *pArg, ph7_int64 *pOut) {
sxu32 nByte;
ph7_int64 iVal;
if(pArg == 0) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Expected a char (0..255) or a single-byte string argument");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "Expected a char (0..255) or a single-byte string argument");
}
if(ph7_value_is_char(pArg)) {
iVal = ph7_value_to_int64(pArg);
if(iVal < 0 || iVal > 255) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Char argument is outside the 0..255 range");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "Char argument is outside the 0..255 range");
}
*pOut = iVal;
return PH7_OK;
@@ -190,13 +75,13 @@ static int CharOperand(ph7_context *pCtx, ph7_value *pArg, ph7_int64 *pOut) {
if(ph7_value_is_string(pArg)) {
nByte = SyBlobLength(&pArg->sBlob);
if(nByte != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Expected a string of exactly one byte");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "Expected a string of exactly one byte");
}
zData = (const char *)SyBlobData(&pArg->sBlob);
*pOut = (ph7_int64)(unsigned char)zData[0];
return PH7_OK;
}
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Expected a char (0..255) or a single-byte string argument");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "Expected a char (0..255) or a single-byte string argument");
}
/* Set a full-range char result; ph7_value_char() clamps to 0..255. */
static int CharResultChar(ph7_context *pCtx, ph7_int64 iVal) {
@@ -242,7 +127,7 @@ static int PredHexDigit(ph7_int64 c) { return PredDigit(c) || PredHexLower(c) ||
static int CharClassify(ph7_context *pCtx, int nArg, ph7_value **apArg, CharPred xPred) {
ph7_int64 c;
if(nArg != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Method expects exactly one argument");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "Method expects exactly one argument");
}
if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) {
return PH7_EXCEPTION;
@@ -279,7 +164,7 @@ CLS_FN(CharIsAsciiHexDigitUpper, PredHexUpper)
static int CharIsBetween(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_int64 c, zMin, zMax;
if(nArg != 3) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "IsBetween expects exactly three arguments");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "IsBetween expects exactly three arguments");
}
if(CharOperand(pCtx, apArg[0], &c) != PH7_OK ||
CharOperand(pCtx, apArg[1], &zMin) != PH7_OK ||
@@ -296,7 +181,7 @@ static int CharIsBetween(ph7_context *pCtx, int nArg, ph7_value **apArg) {
static int CharCaseConvert(ph7_context *pCtx, int nArg, ph7_value **apArg, int bUpper) {
ph7_int64 c;
if(nArg != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Method expects exactly one argument");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "Method expects exactly one argument");
}
if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) {
return PH7_EXCEPTION;
@@ -317,7 +202,7 @@ static int CharToLower(ph7_context *pCtx, int nArg, ph7_value **apArg) {
static int CharGetNumericValue(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_int64 c;
if(nArg != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "GetNumericValue expects exactly one argument");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "GetNumericValue expects exactly one argument");
}
if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) {
return PH7_EXCEPTION;
@@ -328,7 +213,7 @@ static int CharToString(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_int64 c;
char zByte[2];
if(nArg != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "ToString expects exactly one argument");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "ToString expects exactly one argument");
}
if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) {
return PH7_EXCEPTION;
@@ -341,14 +226,14 @@ static int CharParse(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zData;
sxu32 nByte;
if(nArg != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Parse expects exactly one argument");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "Parse expects exactly one argument");
}
if(apArg[0] == 0 || !ph7_value_is_string(apArg[0])) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Parse expects a single-byte string");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "Parse expects a single-byte string");
}
nByte = SyBlobLength(&apArg[0]->sBlob);
if(nByte != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Parse expects a single-byte string");
return PH7_VmThrowNativeException(pCtx, CCLS_EXC, CHAR_ERROR_ARGUMENT, "Parse expects a single-byte string");
}
zData = (const char *)SyBlobData(&apArg[0]->sBlob);
return CharResultChar(pCtx, (int)(unsigned char)zData[0]);