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
+125 -5
View File
@@ -7799,6 +7799,12 @@ struct ph7_stream_data {
void *pHandle; /* Stream handle */
ph7_output_consumer sConsumer; /* VM output consumer */
} x;
/* One-byte-at-a-time look-ahead from Console::Read() must be able to give
* an unused byte back to the shared stdin stream, otherwise fgets()/fread()
* would silently lose data. Served before any real read. */
int nUnread; /* Number of pending bytes */
int iUnread; /* Index of the next pending byte */
unsigned char aUnread[4]; /* Pending bytes, in stream order */
};
/*
* Allocate a new instance of the ph7_stream_data structure.
@@ -7894,6 +7900,7 @@ static int PHPStreamData_Open(const char *zName, int iMode, ph7_value *pResource
/* ph7_int64 (*xRead)(void *,void *,ph7_int64) */
static ph7_int64 PHPStreamData_Read(void *pHandle, void *pBuffer, ph7_int64 nDatatoRead) {
ph7_stream_data *pData = (ph7_stream_data *)pHandle;
ph7_int64 nServed = 0;
if(pData == 0) {
return -1;
}
@@ -7901,6 +7908,22 @@ static ph7_int64 PHPStreamData_Read(void *pHandle, void *pBuffer, ph7_int64 nDat
/* Forbidden */
return -1;
}
/* Give back bytes that Console::Read() looked ahead but did not consume. */
while(pData->nUnread > 0 && nServed < nDatatoRead) {
/* Both sides must use the same ring arithmetic (C2): the writer uses
* (iUnread + nUnread) % 4, so a bare iUnread++ would read past the
* array after four wrap-around cycles. */
((unsigned char *)pBuffer)[nServed++] = pData->aUnread[pData->iUnread];
pData->iUnread = (pData->iUnread + 1) % SX_ARRAYSIZE(pData->aUnread);
if(--pData->nUnread == 0) {
pData->iUnread = 0;
}
}
if(nServed >= nDatatoRead) {
return nServed;
}
nDatatoRead -= nServed;
pBuffer = (void *)(((unsigned char *)pBuffer) + nServed);
#ifdef __WINNT__
{
DWORD nRd;
@@ -7908,9 +7931,9 @@ static ph7_int64 PHPStreamData_Read(void *pHandle, void *pBuffer, ph7_int64 nDat
rc = ReadFile(pData->x.pHandle, pBuffer, (DWORD)nDatatoRead, &nRd, 0);
if(!rc) {
/* IO error */
return -1;
return nServed > 0 ? nServed : -1;
}
return (ph7_int64)nRd;
return nServed + (ph7_int64)nRd;
}
#elif defined(__UNIXES__)
{
@@ -7919,12 +7942,12 @@ static ph7_int64 PHPStreamData_Read(void *pHandle, void *pBuffer, ph7_int64 nDat
fd = SX_PTR_TO_INT(pData->x.pHandle);
nRd = read(fd, pBuffer, (size_t)nDatatoRead);
if(nRd < 1) {
return -1;
return nServed > 0 ? nServed : -1;
}
return (ph7_int64)nRd;
return nServed + (ph7_int64)nRd;
}
#else
return -1;
return nServed > 0 ? nServed : -1;
#endif
}
/* ph7_int64 (*xWrite)(void *,const void *,ph7_int64) */
@@ -8160,6 +8183,103 @@ PH7_PRIVATE void *PH7_ExportStdin(ph7_vm *pVm) {
return pVm->pStdin;
}
}
/* Give one byte back to the shared stdin stream. When the byte still lives in
* io_private's working buffer we simply rewind the offset; otherwise it goes
* into php://stdin's low-level pending queue so fgets()/fread() see it next. */
PH7_PRIVATE void PH7_StdinUnread(ph7_vm *pVm, unsigned char c) {
io_private *pIn = (io_private *)pVm->pStdin;
ph7_stream_data *pData;
if(pIn == 0 || IO_PRIVATE_INVALID(pIn)) {
return;
}
if(pIn->nOfft > 0 && pIn->nOfft <= SyBlobLength(&pIn->sBuffer)) {
pIn->nOfft--;
return;
}
pData = (ph7_stream_data *)pIn->pHandle;
if(pData != 0 && pData->iType == PH7_IO_STREAM_STDIN && pData->nUnread < (int)SX_ARRAYSIZE(pData->aUnread)) {
pData->aUnread[(pData->iUnread + pData->nUnread) % SX_ARRAYSIZE(pData->aUnread)] = c;
pData->nUnread++;
}
}
/* Read one line from the shared stdin handle. StreamReadLine() consults the
* same xRead (and therefore the same pending-unread queue) as Console::Read,
* fgets() and fread(), so mixing the readers never loses a byte. The returned
* buffer is a VM-owned copy: StreamReadLine() may reset its working buffer as
* soon as it returns. NULL means EOF without data. */
PH7_PRIVATE char *PH7_StdinReadLine(ph7_vm *pVm, int *pLen) {
io_private *pIn = (io_private *)PH7_ExportStdin(pVm);
const char *zLine;
ph7_int64 n;
char *pOut;
if(pLen == 0) {
return 0;
}
*pLen = 0;
if(pIn == 0) {
return 0;
}
n = StreamReadLine(pIn, &zLine, -1);
if(n < 1 || n > 0x7FFFFFFF) {
return 0;
}
pOut = (char *)SyMemBackendAlloc(&pVm->sAllocator, (sxu32)(n + 1));
if(pOut == 0) {
return 0;
}
SyMemcpy(zLine, pOut, (sxu32)n);
pOut[n] = 0;
*pLen = (int)n;
return pOut;
}
/* Decode one Unicode code point from the shared stdin stream. Invalid UTF-8
* yields 0xFFFD, end of input -1. Bytes inspected but not consumed are
* returned with PH7_StdinUnread() so no other reader loses data. Unlike
* PH7_Utf8Read(), overlong forms, surrogates and values above U+10FFFF are
* rejected here. */
PH7_PRIVATE int PH7_StdinReadCodepoint(ph7_vm *pVm) {
io_private *pIn = (io_private *)PH7_ExportStdin(pVm);
unsigned char c;
ph7_int64 n;
int i, nWant, nGot = 0, cp, nMin;
if(pIn == 0) {
return -1;
}
n = StreamRead(pIn, &c, 1);
if(n < 1) {
return -1;
}
if(c < 0x80) {
return (int)c;
}
if(c >= 0xC2 && c <= 0xDF) {
nWant = 2; cp = c & 0x1F; nMin = 0x80;
} else if(c >= 0xE0 && c <= 0xEF) {
nWant = 3; cp = c & 0x0F; nMin = 0x800;
} else if(c >= 0xF0 && c <= 0xF4) {
nWant = 4; cp = c & 0x07; nMin = 0x10000;
} else {
return 0xFFFD; /* Continuation byte or invalid lead. */
}
nGot = 1;
for(i = 1 ; i < nWant ; ++i) {
n = StreamRead(pIn, &c, 1);
if(n < 1) {
break; /* Truncated sequence: return replacement for what we got. */
}
if((c & 0xC0) != 0x80) {
PH7_StdinUnread(pVm, c);
return 0xFFFD;
}
cp = (cp << 6) | (c & 0x3F);
++nGot;
}
if(nGot < nWant || cp < nMin || cp > 0x10FFFF ||
(cp & 0xFFFFF800) == 0xD800 || (cp & 0xFFFFFFFE) == 0xFFFE) {
return 0xFFFD;
}
return cp;
}
/*
* Export the STDOUT handle.
*/