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
+115
View File
@@ -431,6 +431,121 @@ PH7_PRIVATE sxi32 PH7_VmThrowIoException(ph7_context *pCtx, int nCode, const cha
rc = ph7_result_value(pCtx, pObj);
return rc == PH7_OK ? PH7_EXCEPTION : rc;
}
/* Stamp file/line with the first script frame of the active call stack for the
* generic native-exception thrower. Native frames ([NATIVE], line 0) are
* skipped so a catch(Exception $e) observes the script location that raised
* the error. IOException keeps its own stamp because its constructor already
* performs one. */
static void NativeExceptionStampSite(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 = IoInstField(pInst, "file");
if(pField) {
PH7_MemObjRelease(pField);
PH7_MemObjInit(pVm, pField);
(void)ph7_value_string(pField, pTrace->pFile->zString, (int)pTrace->pFile->nByte);
}
pField = IoInstField(pInst, "line");
if(pField) {
(void)ph7_value_int64(pField, (ph7_int64)pTrace->nLine);
}
break;
}
SySetRelease(&sTrace);
}
/* Core-owned throw used by native modules that publish a custom exception
* class (MathException, CharException, ...). The named class constructor is
* invoked with (message, code), the instance is stamped and published as the
* context result; every temporary is released on all paths. */
PH7_PRIVATE sxi32 PH7_VmThrowNativeException(ph7_context *pCtx, const char *zClass,
int nCode, const char *zMessage) {
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; /* a handler exception takes precedence */
}
pClass = PH7_VmExtractClass(pVm, zClass, (sxu32)SyStrlen(zClass), 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], zMessage, -1);
ph7_value_int(aTmp[1], nCode);
for(n = 0 ; n < SX_ARRAYSIZE(apArg) ; ++n) {
apArg[n] = aTmp[n];
}
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]);
}
}
NativeExceptionStampSite(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;
}
PH7_PRIVATE sxi32 PH7_VmInstallBuiltinClasses(ph7_vm *pVm) {
ph7_value *apValue[4] = { 0, 0, 0, 0 };