Add TextWriter, IOException, per-VM module data and pre-load imports; Add console module with Console class

This commit is contained in:
2026-11-23 18:36:46 +01:00
parent 2243557bf5
commit 4d06edd045
55 changed files with 1679 additions and 675 deletions
+182
View File
@@ -302,6 +302,136 @@ static sxi32 StdConvert(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
return rc == PH7_OK ? ph7_result_value(pCtx, pValue) : rc;
}
/* ------------------------------------------------------------------ */
/* IOException: a core Exception subclass carrying an error code. */
/* ------------------------------------------------------------------ */
static const char *IoErrorDescription(int nErr) {
switch(nErr) {
case 0: return "No error";
case 1: return "Invalid argument";
case 2: return "I/O error";
default: return "Unknown error code";
}
}
/* Stamp file/line with the first script frame of the active call stack.
* Native frames ([NATIVE], line 0) are skipped so a catch(Exception $e)
* observes the script location that raised the error. */
static ph7_value *IoInstField(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);
}
static void IOExceptionStamp(ph7_vm *pVm, ph7_class_instance *pInst) {
ph7_value *pField;
SySet sTrace;
VmDebugTrace *pTrace;
if(pInst == 0 || 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);
}
static sxi32 IOExceptionConstruct(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
ph7_value *pResult = ph7_context_new_scalar(pCtx);
ph7_value *pThis = ph7_context_this(pCtx);
ph7_class_instance *pInst;
sxi32 rc;
if(pResult == 0) {
return PH7_NOMEM;
}
/* Let the base Exception constructor own message/code/file/line/trace.
* parent:: is required: a plain dispatch would resolve back here. */
rc = ph7_context_parent(pCtx, "__construct", nArg > 2 ? 3 : 2, apArg, pResult);
if(rc != PH7_OK) {
return rc;
}
if(pThis && pThis->x.pOther) {
pInst = (ph7_class_instance *)pThis->x.pOther;
IOExceptionStamp(pCtx->pVm, pInst);
}
return PH7_OK;
}
static sxi32 IOExceptionDescription(ph7_context *pCtx, sxi32 nArg, ph7_value **apArg) {
int nErr = nArg > 0 ? ph7_value_to_int(apArg[0]) : -1;
return ph7_result_string(pCtx, IoErrorDescription(nErr), -1);
}
/* Core-owned IOException throw used by both the Console module and TextWriter
* so the same validation produces the same exception on every call path.
* The constructor stamps file/line; the pending transfer is left for the VM
* unwinder. Full cleanup on every failure path. */
PH7_PRIVATE sxi32 PH7_VmThrowIoException(ph7_context *pCtx, int nCode, const char *zMsg) {
ph7_vm *pVm = pCtx->pVm;
ph7_class *pClass;
ph7_class_instance *pInst;
ph7_class_method *pCons;
ph7_value *pResult, *pObj, *apArg[2];
sxi32 rc;
if(pVm->iPendingTransfer) {
return PH7_EXCEPTION;
}
pClass = PH7_VmExtractClass(pVm, "IOException", sizeof("IOException") - 1, TRUE);
if(pClass == 0) {
return PH7_ABORT;
}
pInst = PH7_NewClassInstance(pVm, pClass);
if(pInst == 0) {
PH7_VmMemoryError(pVm);
return PH7_ABORT;
}
pResult = ph7_context_new_scalar(pCtx);
apArg[0] = ph7_context_new_scalar(pCtx);
apArg[1] = ph7_context_new_scalar(pCtx);
pObj = ph7_context_new_scalar(pCtx);
if(pResult == 0 || apArg[0] == 0 || apArg[1] == 0 || pObj == 0) {
PH7_ClassInstanceUnref(pInst);
PH7_VmMemoryError(pVm);
return PH7_ABORT;
}
ph7_value_string(apArg[0], zMsg, -1);
ph7_value_int(apArg[1], nCode);
pCons = PH7_ClassExtractMethod(pClass, "__construct", sizeof("__construct") - 1);
if(pCons) {
rc = PH7_VmCallClassMethod(pVm, pInst, pCons, pResult, 2, apArg);
if(pInst->iRef < 1) {
pInst->iRef = 1;
}
if(rc != PH7_OK) {
PH7_ClassInstanceUnref(pInst);
return rc;
}
}
if(pInst->iRef < 1) {
pInst->iRef = 1;
}
PH7_MemObjRelease(pObj);
pObj->x.pOther = pInst;
MemObjSetType(pObj, MEMOBJ_OBJ);
rc = ph7_result_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 };
static const ph7_type_def aConversion[] = {
@@ -376,14 +506,49 @@ PH7_PRIVATE sxi32 PH7_VmInstallBuiltinClasses(ph7_vm *pVm) {
{ .zName = "__toString", .sResult = { PH7_TYPE_STRING, 0, 0 }, .xFunc = StdConvert, .pUserData = (void *)&aConversion[3] },
{ .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = StdConstruct, .iFlags = PH7_MEMBER_PRIVATE }
};
static ph7_constant_def aIoConst[] = {
{ .zName = "ERROR_NONE", .iFlags = PH7_MEMBER_PUBLIC },
{ .zName = "ERROR_ARGUMENT", .iFlags = PH7_MEMBER_PUBLIC },
{ .zName = "ERROR_IO", .iFlags = PH7_MEMBER_PUBLIC }
};
static ph7_param_def aIoParam[] = {
{ .zName = "message", .sType = { PH7_TYPE_STRING, 0, 0 } },
{ .zName = "code", .sType = { PH7_TYPE_INT, 0, 0 } }
};
static const ph7_param_def aIoDescParam[] = {
{ .zName = "code", .sType = { PH7_TYPE_INT, 0, 0 } }
};
static ph7_param_def aPrintLineParam[] = {
{ .zName = "value", .sType = { PH7_TYPE_MIXED, 0, 0 } }
};
static const ph7_method_def aIoMethod[] = {
{ .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aIoParam, .nParam = 2, .xFunc = IOExceptionConstruct },
{ .zName = "GetDescription", .sResult = { PH7_TYPE_STRING, 0, 0 }, .aParam = aIoDescParam, .nParam = 1, .xFunc = IOExceptionDescription,
.iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
};
static const ph7_field_def aTextWriterField[] = {
{ .zName = "handle", .sType = { PH7_TYPE_RESOURCE, 0, 0 }, .iFlags = PH7_MEMBER_PRIVATE }
};
static const ph7_method_def aTextWriterMethod[] = {
{ .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = TextWriterConstruct, .iFlags = PH7_MEMBER_PRIVATE },
{ .zName = "__destruct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterDestruct },
{ .zName = "__clone", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterClone, .iFlags = PH7_MEMBER_PRIVATE },
{ .zName = "Print", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = TextWriterPrint, .iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_VARIADIC },
{ .zName = "PrintLine", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aPrintLineParam, .nParam = 1, .xFunc = TextWriterPrintLine, .iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_VARIADIC },
{ .zName = "Flush", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterFlush }
};
static const char *const aIoBase[] = { "Exception" };
ph7_class_def aClass[] = {
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "Exception", .aField = aExceptionField, .nField = SX_ARRAYSIZE(aExceptionField), .aMethod = aExceptionMethod, .nMethod = SX_ARRAYSIZE(aExceptionMethod) },
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "IOException", .aConstant = aIoConst, .nConstant = SX_ARRAYSIZE(aIoConst), .aMethod = aIoMethod, .nMethod = SX_ARRAYSIZE(aIoMethod), .azBase = aIoBase, .nBase = 1 },
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "TextWriter", .aField = aTextWriterField, .nField = SX_ARRAYSIZE(aTextWriterField), .aMethod = aTextWriterMethod, .nMethod = SX_ARRAYSIZE(aTextWriterMethod) },
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "Iterator", .aMethod = aIteratorMethod, .nMethod = SX_ARRAYSIZE(aIteratorMethod), .iFlags = PH7_DECL_INTERFACE },
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "IteratorAggregate", .aMethod = aAggregateMethod, .nMethod = SX_ARRAYSIZE(aAggregateMethod), .iFlags = PH7_DECL_INTERFACE },
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "Serializable", .aMethod = aSerializableMethod, .nMethod = SX_ARRAYSIZE(aSerializableMethod), .iFlags = PH7_DECL_INTERFACE },
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "Directory", .aField = aDirectoryField, .nField = SX_ARRAYSIZE(aDirectoryField), .aMethod = aDirectoryMethod, .nMethod = SX_ARRAYSIZE(aDirectoryMethod) },
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "stdClass", .aField = aStdField, .nField = SX_ARRAYSIZE(aStdField), .aMethod = aStdMethod, .nMethod = SX_ARRAYSIZE(aStdMethod) }
};
ph7_value *apIoValue[SX_ARRAYSIZE(aIoConst)] = { 0 };
sxi32 rc = PH7_NOMEM;
sxu32 n;
for(n = 0; n < SX_ARRAYSIZE(apValue); n++) {
@@ -392,6 +557,14 @@ PH7_PRIVATE sxi32 PH7_VmInstallBuiltinClasses(ph7_vm *pVm) {
goto Cleanup;
}
}
for(n = 0; n < SX_ARRAYSIZE(apIoValue); n++) {
apIoValue[n] = ph7_new_scalar(pVm);
if(apIoValue[n] == 0) {
goto Cleanup;
}
ph7_value_int(apIoValue[n], (int)n);
aIoConst[n].pValue = apIoValue[n];
}
rc = ph7_value_string(apValue[0], "Unknown exception", -1);
if(rc == PH7_OK) {
rc = ph7_value_string(apValue[1], "", 0);
@@ -407,6 +580,9 @@ PH7_PRIVATE sxi32 PH7_VmInstallBuiltinClasses(ph7_vm *pVm) {
aExceptionParam[0].pDefault = apValue[1];
aExceptionParam[1].pDefault = apValue[2];
aExceptionParam[2].pDefault = apValue[3];
aIoParam[0].pDefault = apValue[1];
aIoParam[1].pDefault = apValue[2];
aPrintLineParam[0].pDefault = apValue[1];
for(n = 0; n < SX_ARRAYSIZE(aClass); n++) {
rc = ph7_create_class(pVm, &aClass[n]);
if(rc != PH7_OK) {
@@ -414,6 +590,12 @@ PH7_PRIVATE sxi32 PH7_VmInstallBuiltinClasses(ph7_vm *pVm) {
}
}
Cleanup:
for(n = 0; n < SX_ARRAYSIZE(apIoValue); n++) {
if(apIoValue[n]) {
ph7_release_value(pVm, apIoValue[n]);
aIoConst[n].pValue = 0;
}
}
for(n = 0; n < SX_ARRAYSIZE(apValue); n++) {
if(apValue[n]) {
ph7_release_value(pVm, apValue[n]);