Add system module with System class, core native exception helper and remove useless builtins
Build / AerScript (push) Successful in 50s
Build / AerScript (push) Successful in 50s
This commit is contained in:
+12
-127
@@ -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]);
|
||||
|
||||
+25
-146
@@ -53,127 +53,6 @@ static const char *MathErrorDescription(int nErr) {
|
||||
default: return "Unknown error code";
|
||||
}
|
||||
}
|
||||
/* Fetch an inherited (protected) attribute slot straight off the instance.
|
||||
* PH7_ClassInstanceFetchAttr walks the attribute table without a scope check,
|
||||
* so the base Exception fields are reachable from this module. */
|
||||
static ph7_value *MathInstField(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 fields with the first script frame
|
||||
* of the active call stack. Native frames ([NATIVE], line 0) are skipped so
|
||||
* catch(Exception $e) observes the place in the script that raised the error. */
|
||||
static void MathStampSite(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 = MathInstField(pInst, "file");
|
||||
if(pField) {
|
||||
PH7_MemObjRelease(pField);
|
||||
PH7_MemObjInit(pVm, pField);
|
||||
(void)ph7_value_string(pField, pTrace->pFile->zString, (int)pTrace->pFile->nByte);
|
||||
}
|
||||
pField = MathInstField(pInst, "line");
|
||||
if(pField) {
|
||||
(void)ph7_value_int64(pField, (ph7_int64)pTrace->nLine);
|
||||
}
|
||||
break;
|
||||
}
|
||||
SySetRelease(&sTrace);
|
||||
}
|
||||
/* Throw a real MathException object; temporary scalars are always released
|
||||
* and the instance reference dropped on every error path. */
|
||||
static int MathThrow(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; /* a handler exception takes precedence */
|
||||
}
|
||||
pClass = PH7_VmExtractClass(pVm, MCLS_EXC, sizeof(MCLS_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);
|
||||
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]);
|
||||
}
|
||||
}
|
||||
MathStampSite(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;
|
||||
}
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Method callbacks: MathException */
|
||||
/* ------------------------------------------------------------------ */
|
||||
@@ -206,7 +85,7 @@ static int MathOperand(ph7_context *pCtx, ph7_value *pArg, int *pIsInt,
|
||||
ph7_int64 *pInt, double *pReal) {
|
||||
if(pArg == 0 || (pArg->nType & MEMOBJ_NULL) ||
|
||||
(!ph7_value_is_int(pArg) && !ph7_value_is_float(pArg))) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "Expected an int or float argument");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "Expected an int or float argument");
|
||||
}
|
||||
*pIsInt = ph7_value_is_int(pArg) ? 1 : 0;
|
||||
if(*pIsInt) {
|
||||
@@ -225,7 +104,7 @@ static int MathNumber(ph7_context *pCtx, ph7_value *pArg, double *pOut) {
|
||||
}
|
||||
static int MathInteger(ph7_context *pCtx, ph7_value *pArg, ph7_int64 *pOut) {
|
||||
if(pArg == 0 || (pArg->nType & MEMOBJ_NULL) || !ph7_value_is_int(pArg)) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "Expected an integer argument");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "Expected an integer argument");
|
||||
}
|
||||
*pOut = ph7_value_to_int64(pArg);
|
||||
return PH7_OK;
|
||||
@@ -238,7 +117,7 @@ static int MathUnaryFloat(ph7_context *pCtx, int nArg, ph7_value **apArg,
|
||||
double x;
|
||||
int rc;
|
||||
if(nArg < 1) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "Expected one numeric argument");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "Expected one numeric argument");
|
||||
}
|
||||
rc = MathNumber(pCtx, apArg[0], &x);
|
||||
if(rc != PH7_OK) {
|
||||
@@ -251,7 +130,7 @@ static int MathBinaryFloat(ph7_context *pCtx, int nArg, ph7_value **apArg,
|
||||
double x, y;
|
||||
int rc;
|
||||
if(nArg < 2) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "Expected two numeric arguments");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "Expected two numeric arguments");
|
||||
}
|
||||
rc = MathNumber(pCtx, apArg[0], &x);
|
||||
if(rc != PH7_OK) {
|
||||
@@ -305,7 +184,7 @@ static int MathAbs(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
double x;
|
||||
int bIsInt, rc;
|
||||
if(nArg < 1) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "Abs() expects a value");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "Abs() expects a value");
|
||||
}
|
||||
rc = MathOperand(pCtx, apArg[0], &bIsInt, &iVal, &x);
|
||||
if(rc != PH7_OK) {
|
||||
@@ -314,7 +193,7 @@ static int MathAbs(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
if(bIsInt) {
|
||||
/* -SMALLEST_INT64 is undefined; check before negating. */
|
||||
if(iVal == SMALLEST_INT64) {
|
||||
return MathThrow(pCtx, MATH_ERROR_OVERFLOW, "Abs() overflows for the smallest integer");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_OVERFLOW, "Abs() overflows for the smallest integer");
|
||||
}
|
||||
return ph7_result_int64(pCtx, iVal < 0 ? -iVal : iVal);
|
||||
}
|
||||
@@ -325,7 +204,7 @@ static int MathSign(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
double x;
|
||||
int bIsInt, rc;
|
||||
if(nArg < 1) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "Sign() expects a value");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "Sign() expects a value");
|
||||
}
|
||||
rc = MathOperand(pCtx, apArg[0], &bIsInt, &iVal, &x);
|
||||
if(rc != PH7_OK) {
|
||||
@@ -335,7 +214,7 @@ static int MathSign(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
return ph7_result_int(pCtx, iVal > 0 ? 1 : (iVal < 0 ? -1 : 0));
|
||||
}
|
||||
if(isnan(x)) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARITHMETIC, "Sign() is undefined for NaN");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARITHMETIC, "Sign() is undefined for NaN");
|
||||
}
|
||||
return ph7_result_int(pCtx, x > 0.0 ? 1 : (x < 0.0 ? -1 : 0));
|
||||
}
|
||||
@@ -344,7 +223,7 @@ static int MathLog(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
double a, b;
|
||||
int bIsInt, bHasBase, rc;
|
||||
if(nArg < 1) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "Log() expects a value");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "Log() expects a value");
|
||||
}
|
||||
rc = MathOperand(pCtx, apArg[0], &bIsInt, &iVal, &a);
|
||||
if(rc != PH7_OK) {
|
||||
@@ -380,7 +259,7 @@ static int MathExtreme(ph7_context *pCtx, int nArg, ph7_value **apArg, int bWant
|
||||
double a, b;
|
||||
int ba, bb, rc;
|
||||
if(nArg != 2) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT,
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT,
|
||||
bWantMax ? "Max() expects exactly two arguments" : "Min() expects exactly two arguments");
|
||||
}
|
||||
rc = MathOperand(pCtx, apArg[0], &ba, &ia, &a);
|
||||
@@ -420,7 +299,7 @@ static int MathClamp(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
double v, dMin, dMax;
|
||||
int bAllInt = 1, bIsInt, rc;
|
||||
if(nArg != 3) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "Clamp() expects exactly three arguments");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "Clamp() expects exactly three arguments");
|
||||
}
|
||||
rc = MathOperand(pCtx, apArg[0], &bIsInt, &iv, &v);
|
||||
if(rc != PH7_OK) return rc;
|
||||
@@ -436,7 +315,7 @@ static int MathClamp(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
* bounds a NaN makes '>' false, which is the .NET Math.Clamp(double)
|
||||
* behavior: the comparisons below then simply fall through to value. */
|
||||
if(bAllInt ? (iMin > iMax) : (dMin > dMax)) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "Clamp() requires min <= max");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "Clamp() requires min <= max");
|
||||
}
|
||||
if(bAllInt) {
|
||||
if(iv < iMin) return ph7_result_int64(pCtx, iMin);
|
||||
@@ -477,7 +356,7 @@ static int MathMagnitude(ph7_context *pCtx, int nArg, ph7_value **apArg, int bWa
|
||||
double x, y, ax, ay;
|
||||
int rc;
|
||||
if(nArg != 2) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT,
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT,
|
||||
bWantMax ? "MaxMagnitude() expects exactly two arguments" :
|
||||
"MinMagnitude() expects exactly two arguments");
|
||||
}
|
||||
@@ -503,7 +382,7 @@ static int MathILogB(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
double x;
|
||||
int rc;
|
||||
if(nArg < 1) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "ILogB() expects a value");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "ILogB() expects a value");
|
||||
}
|
||||
rc = MathNumber(pCtx, apArg[0], &x);
|
||||
if(rc != PH7_OK) {
|
||||
@@ -518,14 +397,14 @@ static int MathScaleB(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
double x;
|
||||
int rc;
|
||||
if(nArg < 2) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "ScaleB() expects a value and an exponent");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "ScaleB() expects a value and an exponent");
|
||||
}
|
||||
rc = MathNumber(pCtx, apArg[0], &x);
|
||||
if(rc != PH7_OK) return rc;
|
||||
rc = MathInteger(pCtx, apArg[1], &n);
|
||||
if(rc != PH7_OK) return rc;
|
||||
if(n < (-2147483647 - 1) || n > 2147483647) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT_OUT_OF_RANGE,
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT_OUT_OF_RANGE,
|
||||
"ScaleB() exponent is outside the int32 range");
|
||||
}
|
||||
return ph7_result_double(pCtx, scalbn(x, (int)n));
|
||||
@@ -534,7 +413,7 @@ static int MathFusedMultiplyAdd(ph7_context *pCtx, int nArg, ph7_value **apArg)
|
||||
double x, y, z;
|
||||
int rc;
|
||||
if(nArg < 3) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "FusedMultiplyAdd() expects three values");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "FusedMultiplyAdd() expects three values");
|
||||
}
|
||||
rc = MathNumber(pCtx, apArg[0], &x);
|
||||
if(rc != PH7_OK) return rc;
|
||||
@@ -551,18 +430,18 @@ static int MathFloorDiv(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
ph7_int64 a, b, q, r;
|
||||
int rc;
|
||||
if(nArg < 2) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "FloorDiv() expects two integers");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "FloorDiv() expects two integers");
|
||||
}
|
||||
rc = MathInteger(pCtx, apArg[0], &a);
|
||||
if(rc != PH7_OK) return rc;
|
||||
rc = MathInteger(pCtx, apArg[1], &b);
|
||||
if(rc != PH7_OK) return rc;
|
||||
if(b == 0) {
|
||||
return MathThrow(pCtx, MATH_ERROR_DIVIDE_BY_ZERO, "Division by zero");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_DIVIDE_BY_ZERO, "Division by zero");
|
||||
}
|
||||
if(b == -1) {
|
||||
if(a == SMALLEST_INT64) {
|
||||
return MathThrow(pCtx, MATH_ERROR_OVERFLOW, "FloorDiv() overflows for the smallest integer");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_OVERFLOW, "FloorDiv() overflows for the smallest integer");
|
||||
}
|
||||
return ph7_result_int64(pCtx, -a);
|
||||
}
|
||||
@@ -577,14 +456,14 @@ static int MathFloorMod(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
ph7_int64 a, b, r;
|
||||
int rc;
|
||||
if(nArg < 2) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "FloorMod() expects two integers");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "FloorMod() expects two integers");
|
||||
}
|
||||
rc = MathInteger(pCtx, apArg[0], &a);
|
||||
if(rc != PH7_OK) return rc;
|
||||
rc = MathInteger(pCtx, apArg[1], &b);
|
||||
if(rc != PH7_OK) return rc;
|
||||
if(b == 0) {
|
||||
return MathThrow(pCtx, MATH_ERROR_DIVIDE_BY_ZERO, "Division by zero");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_DIVIDE_BY_ZERO, "Division by zero");
|
||||
}
|
||||
if(b == -1) {
|
||||
return ph7_result_int64(pCtx, 0);
|
||||
@@ -635,7 +514,7 @@ static int MathRound(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
double value, scaled, result, scale, intPart;
|
||||
int bIsInt, bNegative, rc;
|
||||
if(nArg < 1) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT, "Round() expects a value");
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT, "Round() expects a value");
|
||||
}
|
||||
rc = MathOperand(pCtx, apArg[0], &bIsInt, &iDummy, &value);
|
||||
if(rc != PH7_OK) return rc;
|
||||
@@ -644,11 +523,11 @@ static int MathRound(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
rc = MathInteger(pCtx, nArg > 2 ? apArg[2] : 0, &iMode);
|
||||
if(rc != PH7_OK) return rc;
|
||||
if(iDigits < 0 || iDigits > 15) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT_OUT_OF_RANGE,
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT_OUT_OF_RANGE,
|
||||
"Round() digits must be between 0 and 15");
|
||||
}
|
||||
if(iMode < 0 || iMode > 4) {
|
||||
return MathThrow(pCtx, MATH_ERROR_ARGUMENT,
|
||||
return PH7_VmThrowNativeException(pCtx, MCLS_EXC, MATH_ERROR_ARGUMENT,
|
||||
"Round() mode is not a known MidpointRounding value");
|
||||
}
|
||||
if(isnan(value) || isinf(value) || fabs(value) >= MATH_TWO_POW_52) {
|
||||
|
||||
@@ -0,0 +1,830 @@
|
||||
/**
|
||||
* @PROJECT AerScript Interpreter
|
||||
* @COPYRIGHT See COPYING in the top level directory
|
||||
* @FILE modules/system/system.c
|
||||
* @DESCRIPTION Static System class module for AerScript Interpreter
|
||||
* @DEVELOPERS Rafal Kupiec <belliash@codingworkshop.eu.org>
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef __WINNT__
|
||||
#include <windows.h>
|
||||
#include <sysinfoapi.h>
|
||||
#include <processthreadsapi.h>
|
||||
#include <winnt.h>
|
||||
#include <wchar.h>
|
||||
#else
|
||||
#include <errno.h>
|
||||
#include <pwd.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/utsname.h>
|
||||
extern char **environ;
|
||||
#endif
|
||||
#include "system.h"
|
||||
|
||||
#define SCLS_SYSTEM "System"
|
||||
#define SCLS_EXC "SystemException"
|
||||
|
||||
#ifdef __WINNT__
|
||||
#define SYS_NEW_LINE "\r\n"
|
||||
#else
|
||||
#define SYS_NEW_LINE "\n"
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Platform helpers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Throw the two SystemException flavours used all over this module. Both
|
||||
* helpers return the exact status of the core-owned throw routine. */
|
||||
static int SystemArgError(ph7_context *pCtx, const char *zMsg) {
|
||||
return PH7_VmThrowNativeException(pCtx, SCLS_EXC, SYS_ERROR_ARGUMENT, zMsg);
|
||||
}
|
||||
static int SystemIoError(ph7_context *pCtx, const char *zMsg) {
|
||||
return PH7_VmThrowNativeException(pCtx, SCLS_EXC, SYS_ERROR_IO, zMsg);
|
||||
}
|
||||
|
||||
/* Validate a variable name shared by GetEnvironmentVariable() and
|
||||
* SetEnvironmentVariable(): non-empty, no '=' and no embedded NUL. */
|
||||
static int SystemEnvName(ph7_context *pCtx, ph7_value *pArg, const char **pzName) {
|
||||
const char *zData;
|
||||
int nByte = 0;
|
||||
if(pArg == 0 || (pArg->nType & MEMOBJ_NULL) || !ph7_value_is_string(pArg)) {
|
||||
return SystemArgError(pCtx, "Environment variable name must be a string");
|
||||
}
|
||||
zData = ph7_value_to_string(pArg, &nByte);
|
||||
if(zData == 0 || nByte < 1 || memchr(zData, '=', (size_t)nByte) != 0 ||
|
||||
memchr(zData, '\0', (size_t)nByte) != 0) {
|
||||
return SystemArgError(pCtx, "Invalid environment variable name");
|
||||
}
|
||||
*pzName = zData;
|
||||
return PH7_OK;
|
||||
}
|
||||
|
||||
/* Compilation-target architecture of the running process: 64-bit spellings
|
||||
* are checked before their 32-bit counterparts, as required by the contract. */
|
||||
static const char *SystemProcessArch(void) {
|
||||
#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
|
||||
return "x64";
|
||||
#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
|
||||
return "arm64";
|
||||
#elif defined(__i386__) || defined(_M_IX86)
|
||||
return "x86";
|
||||
#elif defined(__arm__) || defined(_M_ARM)
|
||||
return "arm";
|
||||
#else
|
||||
return "Unknown";
|
||||
#endif
|
||||
}
|
||||
#ifndef __WINNT__
|
||||
/* Map uname(2)'s machine field onto the four contract spellings. */
|
||||
static const char *SystemArchFromMachine(const char *zMachine) {
|
||||
if(zMachine == 0) {
|
||||
return "Unknown";
|
||||
}
|
||||
if(strcmp(zMachine, "x86_64") == 0 || strcmp(zMachine, "amd64") == 0 ||
|
||||
strcmp(zMachine, "AMD64") == 0) {
|
||||
return "x64";
|
||||
}
|
||||
if(strcmp(zMachine, "aarch64") == 0 || strcmp(zMachine, "arm64") == 0) {
|
||||
return "arm64";
|
||||
}
|
||||
if(strcmp(zMachine, "i386") == 0 || strcmp(zMachine, "i486") == 0 ||
|
||||
strcmp(zMachine, "i586") == 0 || strcmp(zMachine, "i686") == 0 ||
|
||||
strcmp(zMachine, "x86") == 0) {
|
||||
return "x86";
|
||||
}
|
||||
if(strncmp(zMachine, "arm", 3) == 0) {
|
||||
return "arm";
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
#endif
|
||||
/* Architecture of the host, not of the compiled interpreter. */
|
||||
static const char *SystemOSArch(void) {
|
||||
#ifdef __WINNT__
|
||||
SYSTEM_INFO sInfo;
|
||||
GetNativeSystemInfo(&sInfo);
|
||||
switch(sInfo.wProcessorArchitecture) {
|
||||
case PROCESSOR_ARCHITECTURE_AMD64: return "x64";
|
||||
#ifdef PROCESSOR_ARCHITECTURE_ARM64
|
||||
case PROCESSOR_ARCHITECTURE_ARM64: return "arm64";
|
||||
#endif
|
||||
case PROCESSOR_ARCHITECTURE_INTEL: return "x86";
|
||||
case PROCESSOR_ARCHITECTURE_ARM: return "arm";
|
||||
default: return "Unknown";
|
||||
}
|
||||
#else
|
||||
struct utsname sInfo;
|
||||
if(uname(&sInfo) != 0) {
|
||||
return "Unknown";
|
||||
}
|
||||
return SystemArchFromMachine(sInfo.machine);
|
||||
#endif
|
||||
}
|
||||
static int SystemProcessorCount(void) {
|
||||
sxu64 nCount = 0;
|
||||
#ifdef __WINNT__
|
||||
#ifdef ALL_PROCESSOR_GROUPS
|
||||
nCount = (sxu64)GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
|
||||
#else
|
||||
{
|
||||
SYSTEM_INFO sInfo;
|
||||
GetSystemInfo(&sInfo);
|
||||
nCount = (sxu64)sInfo.dwNumberOfProcessors;
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
{
|
||||
/* sysconf() signals failure with -1; casting that to sxu64 would make
|
||||
* the final > 0 test succeed and yield a negative processor count. */
|
||||
long nRaw = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
nCount = nRaw > 0 ? (sxu64)nRaw : 0;
|
||||
}
|
||||
#endif
|
||||
return nCount > 0 ? (int)nCount : 1;
|
||||
}
|
||||
static ph7_int64 SystemTickCount(void) {
|
||||
#ifdef __WINNT__
|
||||
return (ph7_int64)GetTickCount64();
|
||||
#else
|
||||
struct timespec sNow;
|
||||
if(clock_gettime(CLOCK_MONOTONIC, &sNow) != 0) {
|
||||
return 0;
|
||||
}
|
||||
return (ph7_int64)sNow.tv_sec * 1000 + (ph7_int64)(sNow.tv_nsec / 1000000L);
|
||||
#endif
|
||||
}
|
||||
/* Sleep in milliseconds with EINTR resumption; iMs is always >= 0 here. */
|
||||
static void SystemSleepMs(ph7_int64 iMs) {
|
||||
#ifdef __WINNT__
|
||||
Sleep((DWORD)iMs);
|
||||
#else
|
||||
struct timespec sReq, sRem;
|
||||
sReq.tv_sec = (time_t)(iMs / 1000);
|
||||
sReq.tv_nsec = (long)((iMs % 1000) * 1000000);
|
||||
while(nanosleep(&sReq, &sRem) != 0) {
|
||||
if(errno == EINTR) {
|
||||
sReq = sRem;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#ifdef __WINNT__
|
||||
/* UTF-16 <-> UTF-8 conversion. Buffers are heap allocated and owned by the
|
||||
* caller; the VFS does not expose wide-string environment helpers. */
|
||||
static char *SystemUtf8FromWide(const WCHAR *zWide, int nWide) {
|
||||
char *zOut;
|
||||
int nByte;
|
||||
if(nWide < 0) {
|
||||
nWide = (int)wcslen(zWide);
|
||||
}
|
||||
if(nWide == 0) {
|
||||
/* An empty environment value is a valid value, not a failure. */
|
||||
char *zEmpty = (char *)malloc(1);
|
||||
if(zEmpty) {
|
||||
zEmpty[0] = '\0';
|
||||
}
|
||||
return zEmpty;
|
||||
}
|
||||
nByte = WideCharToMultiByte(CP_UTF8, 0, zWide, nWide, 0, 0, 0, 0);
|
||||
if(nByte < 1) {
|
||||
return 0;
|
||||
}
|
||||
zOut = (char *)malloc((size_t)nByte + 1);
|
||||
if(zOut == 0) {
|
||||
return 0;
|
||||
}
|
||||
if(WideCharToMultiByte(CP_UTF8, 0, zWide, nWide, zOut, nByte, 0, 0) < 1) {
|
||||
free(zOut);
|
||||
return 0;
|
||||
}
|
||||
zOut[nByte] = '\0';
|
||||
return zOut;
|
||||
}
|
||||
static WCHAR *SystemWideFromUtf8(const char *zUtf8) {
|
||||
WCHAR *zOut;
|
||||
int nByte, nWide;
|
||||
if(zUtf8 == 0) {
|
||||
return 0;
|
||||
}
|
||||
nByte = (int)strlen(zUtf8);
|
||||
if(nByte == 0) {
|
||||
/* Keep an empty value distinct from an absent variable: the caller
|
||||
* passes this NUL-only buffer to SetEnvironmentVariableW(). */
|
||||
zOut = (WCHAR *)malloc(sizeof(WCHAR));
|
||||
if(zOut) {
|
||||
zOut[0] = L'\0';
|
||||
}
|
||||
return zOut;
|
||||
}
|
||||
nWide = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, 0, 0);
|
||||
if(nWide < 1) {
|
||||
return 0;
|
||||
}
|
||||
zOut = (WCHAR *)malloc(((size_t)nWide + 1) * sizeof(WCHAR));
|
||||
if(zOut == 0) {
|
||||
return 0;
|
||||
}
|
||||
if(MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, zOut, nWide) < 1) {
|
||||
free(zOut);
|
||||
return 0;
|
||||
}
|
||||
zOut[nWide] = L'\0';
|
||||
return zOut;
|
||||
}
|
||||
static int SystemIsElevated(void) {
|
||||
HANDLE hToken = 0;
|
||||
TOKEN_ELEVATION sElevation;
|
||||
DWORD nSize = sizeof(sElevation);
|
||||
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
|
||||
return 0;
|
||||
}
|
||||
if(!GetTokenInformation(hToken, TokenElevation, &sElevation, sizeof(sElevation), &nSize)) {
|
||||
CloseHandle(hToken);
|
||||
return 0;
|
||||
}
|
||||
CloseHandle(hToken);
|
||||
return sElevation.TokenIsElevated ? 1 : 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Methods available under every SAPI */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static int System_GetEnvironmentVariable(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
#ifndef __WINNT__
|
||||
const ph7_vfs *pVfs;
|
||||
#endif
|
||||
const char *zName;
|
||||
int rc;
|
||||
rc = SystemEnvName(pCtx, nArg > 0 ? apArg[0] : 0, &zName);
|
||||
if(rc != PH7_OK) {
|
||||
return rc;
|
||||
}
|
||||
/* Absent variables (and a missing VFS) keep the NULL fallback result. */
|
||||
(void)ph7_result_null(pCtx);
|
||||
#ifdef __WINNT__
|
||||
{
|
||||
/* WinVfs_Getenv() maps GetEnvironmentVariableA() == 0 to "not found",
|
||||
* which is also returned for a variable set to "". Query the wide API
|
||||
* directly: only ERROR_ENVVAR_NOT_FOUND yields NULL, "" stays "". */
|
||||
WCHAR *zWideName = SystemWideFromUtf8(zName);
|
||||
DWORD nSize, nErr;
|
||||
if(zWideName == 0) {
|
||||
return SystemIoError(pCtx, "Unable to convert the variable name");
|
||||
}
|
||||
nSize = GetEnvironmentVariableW(zWideName, 0, 0);
|
||||
nErr = GetLastError();
|
||||
if(nSize == 0) {
|
||||
free(zWideName);
|
||||
if(nErr == ERROR_ENVVAR_NOT_FOUND) {
|
||||
return PH7_OK;
|
||||
}
|
||||
return SystemIoError(pCtx, "Unable to read the environment variable");
|
||||
}
|
||||
{
|
||||
WCHAR *zBuf = (WCHAR *)malloc(((size_t)nSize + 1) * sizeof(WCHAR));
|
||||
if(zBuf == 0) {
|
||||
free(zWideName);
|
||||
return PH7_ABORT;
|
||||
}
|
||||
(void)GetEnvironmentVariableW(zWideName, zBuf, nSize);
|
||||
free(zWideName);
|
||||
{
|
||||
char *zUtf = SystemUtf8FromWide(zBuf, -1);
|
||||
free(zBuf);
|
||||
if(zUtf == 0) {
|
||||
return SystemIoError(pCtx, "Unable to convert the environment variable value");
|
||||
}
|
||||
rc = ph7_result_string(pCtx, zUtf, -1);
|
||||
free(zUtf);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
pVfs = pCtx->pVm->pEngine->pVfs;
|
||||
if(pVfs == 0 || pVfs->xGetenv == 0) {
|
||||
return PH7_OK;
|
||||
}
|
||||
(void)pVfs->xGetenv(zName, pCtx);
|
||||
return PH7_OK;
|
||||
#endif
|
||||
}
|
||||
static int System_GetOSPlatform(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
#if defined(__WINNT__)
|
||||
return ph7_result_string(pCtx, "Windows", sizeof("Windows") - 1);
|
||||
#elif defined(__APPLE__)
|
||||
return ph7_result_string(pCtx, "macOS", sizeof("macOS") - 1);
|
||||
#elif defined(__FreeBSD__)
|
||||
return ph7_result_string(pCtx, "FreeBSD", sizeof("FreeBSD") - 1);
|
||||
#elif defined(__OpenBSD__)
|
||||
return ph7_result_string(pCtx, "OpenBSD", sizeof("OpenBSD") - 1);
|
||||
#elif defined(__NetBSD__)
|
||||
return ph7_result_string(pCtx, "NetBSD", sizeof("NetBSD") - 1);
|
||||
#elif defined(__linux__)
|
||||
return ph7_result_string(pCtx, "Linux", sizeof("Linux") - 1);
|
||||
#else
|
||||
return ph7_result_string(pCtx, "Unknown", sizeof("Unknown") - 1);
|
||||
#endif
|
||||
}
|
||||
static int System_GetOSArchitecture(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
return ph7_result_string(pCtx, SystemOSArch(), -1);
|
||||
}
|
||||
static int System_GetProcessArchitecture(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
return ph7_result_string(pCtx, SystemProcessArch(), -1);
|
||||
}
|
||||
static int System_GetProcessorCount(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
return ph7_result_int(pCtx, SystemProcessorCount());
|
||||
}
|
||||
static int System_GetTickCount(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
return ph7_result_int64(pCtx, SystemTickCount());
|
||||
}
|
||||
/* nWhich: 0 = current, 1 = peak, 2 = configured limit. */
|
||||
static int SystemMemory(ph7_context *pCtx, int nWhich) {
|
||||
const SyMemHeap *pHeap = pCtx->pVm->sAllocator.pHeap;
|
||||
ph7_int64 iValue = 0;
|
||||
if(pHeap != 0) {
|
||||
if(nWhich == 0) {
|
||||
iValue = (ph7_int64)pHeap->nSize;
|
||||
} else if(nWhich == 1) {
|
||||
iValue = (ph7_int64)pHeap->nPeak;
|
||||
} else {
|
||||
iValue = (ph7_int64)pHeap->nLimit;
|
||||
}
|
||||
}
|
||||
return ph7_result_int64(pCtx, iValue);
|
||||
}
|
||||
static int System_GetMemoryUsage(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
return SystemMemory(pCtx, 0);
|
||||
}
|
||||
static int System_GetPeakMemoryUsage(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
return SystemMemory(pCtx, 1);
|
||||
}
|
||||
static int System_GetMemoryLimit(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
return SystemMemory(pCtx, 2);
|
||||
}
|
||||
static int System_Sleep(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
ph7_int64 iMs;
|
||||
if(nArg < 1 || apArg[0] == 0 || (apArg[0]->nType & MEMOBJ_NULL) ||
|
||||
!ph7_value_is_int(apArg[0])) {
|
||||
return SystemArgError(pCtx, "Sleep() expects an integer number of milliseconds");
|
||||
}
|
||||
iMs = ph7_value_to_int64(apArg[0]);
|
||||
if(iMs < 0) {
|
||||
return SystemArgError(pCtx, "Sleep() rejects a negative delay");
|
||||
}
|
||||
if(iMs > 0) {
|
||||
SystemSleepMs(iMs);
|
||||
}
|
||||
return PH7_OK;
|
||||
}
|
||||
static int System_Exit(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
sxi32 iStatus = 0;
|
||||
if(nArg > 0) {
|
||||
if(apArg[0] == 0 || (apArg[0]->nType & MEMOBJ_NULL) ||
|
||||
!ph7_value_is_int(apArg[0])) {
|
||||
return SystemArgError(pCtx, "Exit() expects an integer status code");
|
||||
}
|
||||
iStatus = ph7_value_to_int(apArg[0]);
|
||||
}
|
||||
/* Under a future FastCGI SAPI this must end the current request, not the
|
||||
* process; the VM's existing PH7_ABORT path owns shutdown handlers and
|
||||
* destructors in both cases. */
|
||||
pCtx->pVm->iExitStatus = iStatus;
|
||||
return PH7_ABORT;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Methods registered only under the "cli" SAPI */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static int System_GetEnvironmentVariables(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
ph7_value *pArray;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
pArray = ph7_context_new_array(pCtx);
|
||||
if(pArray == 0) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
#ifdef __WINNT__
|
||||
{
|
||||
LPWCH pEnv = GetEnvironmentStringsW();
|
||||
LPWCH pCur;
|
||||
if(pEnv == 0) {
|
||||
return SystemIoError(pCtx, "Unable to read the environment block");
|
||||
}
|
||||
for(pCur = pEnv ; *pCur != 0 ; ) {
|
||||
WCHAR *zEq = wcschr(pCur, L'=');
|
||||
if(zEq != 0 && zEq != pCur) {
|
||||
ph7_value *pKey = ph7_context_new_scalar(pCtx);
|
||||
ph7_value *pVal = ph7_context_new_scalar(pCtx);
|
||||
char *zName = SystemUtf8FromWide(pCur, (int)(zEq - pCur));
|
||||
char *zValue = SystemUtf8FromWide(zEq + 1, -1);
|
||||
if(pKey == 0 || pVal == 0 || zName == 0 || zValue == 0) {
|
||||
free(zName);
|
||||
free(zValue);
|
||||
FreeEnvironmentStringsW(pEnv);
|
||||
return PH7_ABORT;
|
||||
}
|
||||
(void)ph7_value_string(pKey, zName, -1);
|
||||
(void)ph7_value_string(pVal, zValue, -1);
|
||||
if(ph7_array_add_elem(pArray, pKey, pVal) != PH7_OK) {
|
||||
free(zName);
|
||||
free(zValue);
|
||||
FreeEnvironmentStringsW(pEnv);
|
||||
return PH7_ABORT;
|
||||
}
|
||||
free(zName);
|
||||
free(zValue);
|
||||
}
|
||||
pCur += wcslen(pCur) + 1;
|
||||
}
|
||||
FreeEnvironmentStringsW(pEnv);
|
||||
}
|
||||
#else
|
||||
{
|
||||
char **pEnv;
|
||||
for(pEnv = environ ; pEnv != 0 && *pEnv != 0 ; ++pEnv) {
|
||||
const char *zEntry = *pEnv;
|
||||
const char *zEq = strchr(zEntry, '=');
|
||||
ph7_value *pKey, *pVal;
|
||||
if(zEq == 0 || zEq == zEntry) {
|
||||
continue;
|
||||
}
|
||||
pKey = ph7_context_new_scalar(pCtx);
|
||||
pVal = ph7_context_new_scalar(pCtx);
|
||||
if(pKey == 0 || pVal == 0) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
(void)ph7_value_string(pKey, zEntry, (int)(zEq - zEntry));
|
||||
(void)ph7_value_string(pVal, zEq + 1, -1);
|
||||
if(ph7_array_add_elem(pArray, pKey, pVal) != PH7_OK) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* Stamp the primitive element type: the declared string[] result must
|
||||
* match exactly, else VmCheckReturn() rejects the array on return. */
|
||||
pArray->nType = MEMOBJ_STRING | MEMOBJ_HASHMAP;
|
||||
return ph7_result_value(pCtx, pArray);
|
||||
}
|
||||
static int System_SetEnvironmentVariable(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
#ifndef __WINNT__
|
||||
const ph7_vfs *pVfs;
|
||||
#endif
|
||||
const char *zName;
|
||||
/* A NULL (or missing) value removes the variable. A NULL literal passed
|
||||
* through a MIXED formal arrives as MEMOBJ_MIXED | MEMOBJ_VOID. */
|
||||
int bRemove = (nArg < 2 || apArg[1] == 0 ||
|
||||
(apArg[1]->nType & (MEMOBJ_NULL | MEMOBJ_VOID))) ? 1 : 0;
|
||||
int rc;
|
||||
rc = SystemEnvName(pCtx, nArg > 0 ? apArg[0] : 0, &zName);
|
||||
if(rc != PH7_OK) {
|
||||
return rc;
|
||||
}
|
||||
if(!bRemove && !ph7_value_is_string(apArg[1])) {
|
||||
return SystemArgError(pCtx, "Environment variable value must be a string or NULL");
|
||||
}
|
||||
#ifdef __WINNT__
|
||||
{
|
||||
/* Keep both Windows paths wide: the VFS xSetenv() uses the ANSI API
|
||||
* and would store raw UTF-8 bytes that the W-based enumeration then
|
||||
* decodes as UTF-16, breaking non-ASCII round trips. */
|
||||
WCHAR *zWideName = SystemWideFromUtf8(zName);
|
||||
WCHAR *zWideValue = 0;
|
||||
BOOL bOk;
|
||||
if(zWideName == 0) {
|
||||
return SystemIoError(pCtx, "Unable to convert the variable name");
|
||||
}
|
||||
if(!bRemove) {
|
||||
zWideValue = SystemWideFromUtf8(ph7_value_to_string(apArg[1], 0));
|
||||
if(zWideValue == 0) {
|
||||
free(zWideName);
|
||||
return SystemIoError(pCtx, "Unable to convert the environment variable value");
|
||||
}
|
||||
}
|
||||
/* A NULL value pointer removes the variable. */
|
||||
bOk = SetEnvironmentVariableW(zWideName, zWideValue);
|
||||
free(zWideValue);
|
||||
free(zWideName);
|
||||
if(!bOk) {
|
||||
return SystemIoError(pCtx, bRemove ?
|
||||
"Unable to remove the environment variable" :
|
||||
"Unable to set the environment variable");
|
||||
}
|
||||
return PH7_OK;
|
||||
}
|
||||
#else
|
||||
if(bRemove) {
|
||||
if(unsetenv(zName) != 0) {
|
||||
return SystemIoError(pCtx, "Unable to remove the environment variable");
|
||||
}
|
||||
return PH7_OK;
|
||||
}
|
||||
pVfs = pCtx->pVm->pEngine->pVfs;
|
||||
if(pVfs == 0 || pVfs->xSetenv == 0) {
|
||||
return SystemIoError(pCtx, "Environment writes are not available in the underlying VFS");
|
||||
}
|
||||
if(pVfs->xSetenv(zName, ph7_value_to_string(apArg[1], 0)) != PH7_OK) {
|
||||
return SystemIoError(pCtx, "Unable to set the environment variable");
|
||||
}
|
||||
return PH7_OK;
|
||||
#endif
|
||||
}
|
||||
static int System_GetProcessId(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
const ph7_vfs *pVfs;
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
pVfs = pCtx->pVm->pEngine->pVfs;
|
||||
return ph7_result_int64(pCtx, pVfs != 0 && pVfs->xProcessId != 0 ?
|
||||
(ph7_int64)pVfs->xProcessId() : 0);
|
||||
}
|
||||
static int System_GetUserName(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
#ifdef __WINNT__
|
||||
{
|
||||
WCHAR zBuf[256];
|
||||
DWORD nSize = (DWORD)SX_ARRAYSIZE(zBuf);
|
||||
char *zUtf;
|
||||
int rc;
|
||||
if(!GetUserNameW(zBuf, &nSize)) {
|
||||
return SystemIoError(pCtx, "Unable to determine the current user name");
|
||||
}
|
||||
zUtf = SystemUtf8FromWide(zBuf, (int)nSize - 1);
|
||||
if(zUtf == 0) {
|
||||
return SystemIoError(pCtx, "Unable to convert the current user name");
|
||||
}
|
||||
rc = ph7_result_string(pCtx, zUtf, -1);
|
||||
free(zUtf);
|
||||
return rc;
|
||||
}
|
||||
#else
|
||||
{
|
||||
/* The VFS xUsername() helper uses the real UID; the contract asks for
|
||||
* the effective user, so query getpwuid(geteuid()) directly. */
|
||||
struct passwd *pPwd = getpwuid(geteuid());
|
||||
if(pPwd == 0 || pPwd->pw_name == 0) {
|
||||
return SystemIoError(pCtx, "Unable to determine the current user name");
|
||||
}
|
||||
return ph7_result_string(pCtx, pPwd->pw_name, -1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
static int System_IsPrivilegedProcess(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
#ifdef __WINNT__
|
||||
return ph7_result_bool(pCtx, SystemIsElevated());
|
||||
#else
|
||||
return ph7_result_bool(pCtx, geteuid() == 0 ? 1 : 0);
|
||||
#endif
|
||||
}
|
||||
static int System_GetMachineName(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
#ifdef __WINNT__
|
||||
{
|
||||
WCHAR zBuf[256];
|
||||
DWORD nSize = (DWORD)SX_ARRAYSIZE(zBuf);
|
||||
char *zUtf;
|
||||
int rc;
|
||||
if(!GetComputerNameExW(ComputerNameDnsHostname, zBuf, &nSize)) {
|
||||
return SystemIoError(pCtx, "Unable to determine the machine name");
|
||||
}
|
||||
zUtf = SystemUtf8FromWide(zBuf, (int)nSize);
|
||||
if(zUtf == 0) {
|
||||
return SystemIoError(pCtx, "Unable to convert the machine name");
|
||||
}
|
||||
rc = ph7_result_string(pCtx, zUtf, -1);
|
||||
free(zUtf);
|
||||
return rc;
|
||||
}
|
||||
#else
|
||||
{
|
||||
char zBuf[256];
|
||||
if(gethostname(zBuf, sizeof(zBuf) - 1) != 0) {
|
||||
return SystemIoError(pCtx, "Unable to determine the machine name");
|
||||
}
|
||||
zBuf[sizeof(zBuf) - 1] = '\0';
|
||||
return ph7_result_string(pCtx, zBuf, -1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
static int System_GetOSVersion(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
#ifdef __WINNT__
|
||||
{
|
||||
typedef LONG (WINAPI *RtlGetVersionFunc)(PRTL_OSVERSIONINFOW);
|
||||
HMODULE hNtdll = GetModuleHandleW(L"ntdll.dll");
|
||||
RtlGetVersionFunc xGetVersion;
|
||||
RTL_OSVERSIONINFOW sInfo;
|
||||
char zBuf[64];
|
||||
if(hNtdll == 0) {
|
||||
return SystemIoError(pCtx, "Unable to query the operating system version");
|
||||
}
|
||||
/* RtlGetVersion() bypasses the compatibility layer's fake version. */
|
||||
xGetVersion = (RtlGetVersionFunc)GetProcAddress(hNtdll, "RtlGetVersion");
|
||||
if(xGetVersion == 0) {
|
||||
return SystemIoError(pCtx, "Unable to query the operating system version");
|
||||
}
|
||||
SyZero(&sInfo, sizeof(sInfo));
|
||||
sInfo.dwOSVersionInfoSize = sizeof(sInfo);
|
||||
if(xGetVersion(&sInfo) != 0) {
|
||||
return SystemIoError(pCtx, "Unable to query the operating system version");
|
||||
}
|
||||
(void)snprintf(zBuf, sizeof(zBuf), "%lu.%lu.%lu",
|
||||
(unsigned long)sInfo.dwMajorVersion, (unsigned long)sInfo.dwMinorVersion,
|
||||
(unsigned long)sInfo.dwBuildNumber);
|
||||
return ph7_result_string(pCtx, zBuf, -1);
|
||||
}
|
||||
#else
|
||||
{
|
||||
struct utsname sInfo;
|
||||
if(uname(&sInfo) != 0) {
|
||||
return SystemIoError(pCtx, "Unable to query the operating system version");
|
||||
}
|
||||
return ph7_result_string(pCtx, sInfo.release, -1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Class registration */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static int SystemConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
SXUNUSED(pCtx);
|
||||
SXUNUSED(nArg);
|
||||
SXUNUSED(apArg);
|
||||
return PH7_ABORT; /* final class: no public construction */
|
||||
}
|
||||
static int SystemExceptionConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
ph7_value *pResult = ph7_context_new_scalar(pCtx);
|
||||
sxi32 rc;
|
||||
SXUNUSED(nArg);
|
||||
if(pResult == 0) {
|
||||
return PH7_ABORT;
|
||||
}
|
||||
/* apArg[1] is filled by the VM from the registered default snapshot. */
|
||||
rc = ph7_context_parent(pCtx, "__construct", 2, apArg, pResult);
|
||||
return rc;
|
||||
}
|
||||
static sxi32 SystemRegisterClasses(ph7_vm *pVm) {
|
||||
#define STY(T) { PH7_TYPE_##T, 0, 0 }
|
||||
#define SYSC(N) { N, 0, PH7_MEMBER_PUBLIC, 0, 0 }
|
||||
#define SYSM(NAME,RET,PARAMS,NPARAM,FUNC) \
|
||||
{ NAME, STY(RET), PARAMS, NPARAM, FUNC, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
|
||||
static ph7_constant_def aConst[] = {
|
||||
SYSC("ERROR_NONE"), SYSC("ERROR_ARGUMENT"), SYSC("ERROR_IO"), SYSC("NEW_LINE")
|
||||
};
|
||||
static const int aConstValue[] = { SYS_ERROR_NONE, SYS_ERROR_ARGUMENT, SYS_ERROR_IO };
|
||||
static ph7_param_def aExcCtor[] = {
|
||||
{ "message", STY(STRING), 0, 0, 0, 0 },
|
||||
{ "code", STY(INT), 0, 0, 0, 0 }
|
||||
};
|
||||
static const ph7_param_def aNameParam[] = { { "name", STY(STRING), 0, 0, 0, 0 } };
|
||||
/* MIXED, not STRING: a STRING formal casts NULL to "" before the callback,
|
||||
* but NULL is the "remove variable" sentinel here. */
|
||||
static const ph7_param_def aSetParam[] = {
|
||||
{ "name", STY(STRING), 0, 0, 0, 0 },
|
||||
{ "value", STY(MIXED), 0, 0, 0, 0 }
|
||||
};
|
||||
static const ph7_param_def aIntParam[] = { { "milliseconds", STY(INT), 0, 0, 0, 0 } };
|
||||
/* MIXED so a wrong-typed status reaches System_Exit() and raises
|
||||
* SystemException(ERROR_ARGUMENT) instead of a VM overload error. */
|
||||
static ph7_param_def aExitParam[] = { { "code", STY(MIXED), 0, 0, 0, 0 } };
|
||||
static const ph7_method_def aExcMethod[] = {
|
||||
{ "__construct", STY(VOID), aExcCtor, 2, SystemExceptionConstruct, 0, 0 }
|
||||
};
|
||||
static const ph7_method_def aMethodCommon[] = {
|
||||
{ "__construct", STY(VOID), 0, 0, SystemConstruct, 0, PH7_MEMBER_PRIVATE },
|
||||
/* MIXED, not STRING: a STRING return would cast the NULL sentinel for
|
||||
* an absent variable into an empty string (VmCheckReturn() cast). */
|
||||
SYSM("GetEnvironmentVariable", MIXED, aNameParam, 1, System_GetEnvironmentVariable),
|
||||
SYSM("GetOSPlatform", STRING, 0, 0, System_GetOSPlatform),
|
||||
SYSM("GetOSArchitecture", STRING, 0, 0, System_GetOSArchitecture),
|
||||
SYSM("GetProcessArchitecture", STRING, 0, 0, System_GetProcessArchitecture),
|
||||
SYSM("GetProcessorCount", INT, 0, 0, System_GetProcessorCount),
|
||||
SYSM("GetTickCount", INT, 0, 0, System_GetTickCount),
|
||||
SYSM("GetMemoryUsage", INT, 0, 0, System_GetMemoryUsage),
|
||||
SYSM("GetPeakMemoryUsage", INT, 0, 0, System_GetPeakMemoryUsage),
|
||||
SYSM("GetMemoryLimit", INT, 0, 0, System_GetMemoryLimit),
|
||||
SYSM("Sleep", VOID, aIntParam, 1, System_Sleep),
|
||||
SYSM("Exit", VOID, aExitParam, 1, System_Exit)
|
||||
};
|
||||
static const ph7_method_def aMethodCli[] = {
|
||||
{ "GetEnvironmentVariables", { PH7_TYPE_STRING, 0, 1 }, 0, 0,
|
||||
System_GetEnvironmentVariables, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
|
||||
SYSM("SetEnvironmentVariable", VOID, aSetParam, 2, System_SetEnvironmentVariable),
|
||||
SYSM("GetProcessId", INT, 0, 0, System_GetProcessId),
|
||||
SYSM("GetUserName", STRING, 0, 0, System_GetUserName),
|
||||
SYSM("IsPrivilegedProcess", BOOL, 0, 0, System_IsPrivilegedProcess),
|
||||
SYSM("GetMachineName", STRING, 0, 0, System_GetMachineName),
|
||||
SYSM("GetOSVersion", STRING, 0, 0, System_GetOSVersion)
|
||||
};
|
||||
static const char *const aExcBase[] = { "Exception" };
|
||||
ph7_method_def aMethod[SX_ARRAYSIZE(aMethodCommon) + SX_ARRAYSIZE(aMethodCli)];
|
||||
ph7_class_def sExc = {
|
||||
sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, SCLS_EXC,
|
||||
0, 0, 0, 0, aExcMethod, SX_ARRAYSIZE(aExcMethod), 0,
|
||||
aExcBase, 1, 0, 0, 0, 0
|
||||
};
|
||||
ph7_class_def sSystem = {
|
||||
sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, SCLS_SYSTEM,
|
||||
0, 0, aConst, SX_ARRAYSIZE(aConst), aMethod, 0,
|
||||
PH7_DECL_FINAL | PH7_DECL_VIRTUAL, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
ph7_value *apConst[SX_ARRAYSIZE(aConst)];
|
||||
ph7_value *apDefault[2];
|
||||
sxu32 n;
|
||||
sxi32 rc = PH7_OK;
|
||||
SyZero(apConst, sizeof(apConst));
|
||||
SyZero(apDefault, sizeof(apDefault));
|
||||
/* Constant and default snapshots; ph7_create_class() deep-copies them. */
|
||||
for(n = 0 ; n < SX_ARRAYSIZE(apConst) ; ++n) {
|
||||
apConst[n] = ph7_new_scalar(pVm);
|
||||
if(apConst[n] == 0) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Cleanup;
|
||||
}
|
||||
if(n < SX_ARRAYSIZE(aConstValue)) {
|
||||
ph7_value_int(apConst[n], aConstValue[n]);
|
||||
} else {
|
||||
(void)ph7_value_string(apConst[n], SYS_NEW_LINE, -1);
|
||||
}
|
||||
aConst[n].pValue = apConst[n];
|
||||
}
|
||||
/* SystemException("message") gets ERROR_NONE from this snapshot. */
|
||||
apDefault[0] = ph7_new_scalar(pVm);
|
||||
if(apDefault[0] == 0) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Cleanup;
|
||||
}
|
||||
ph7_value_int(apDefault[0], SYS_ERROR_NONE);
|
||||
aExcCtor[1].pDefault = apDefault[0];
|
||||
/* Exit()'s optional status code defaults to zero. */
|
||||
apDefault[1] = ph7_new_scalar(pVm);
|
||||
if(apDefault[1] == 0) {
|
||||
rc = PH7_NOMEM;
|
||||
goto Cleanup;
|
||||
}
|
||||
ph7_value_int(apDefault[1], 0);
|
||||
aExitParam[0].pDefault = apDefault[1];
|
||||
/* Per-SAPI method table: only the CLI SAPI sees the process/environment
|
||||
* helpers; every other SAPI gets the common, portable surface. */
|
||||
memcpy(aMethod, aMethodCommon, sizeof(aMethodCommon));
|
||||
sSystem.nMethod = SX_ARRAYSIZE(aMethodCommon);
|
||||
if(PH7_VmSapiIs(pVm, "cli")) {
|
||||
memcpy(aMethod + SX_ARRAYSIZE(aMethodCommon), aMethodCli, sizeof(aMethodCli));
|
||||
sSystem.nMethod += SX_ARRAYSIZE(aMethodCli);
|
||||
}
|
||||
rc = ph7_create_class(pVm, &sExc);
|
||||
if(rc == PH7_OK) {
|
||||
rc = ph7_create_class(pVm, &sSystem);
|
||||
}
|
||||
Cleanup:
|
||||
aExcCtor[1].pDefault = 0;
|
||||
aExitParam[0].pDefault = 0;
|
||||
for(n = 0 ; n < SX_ARRAYSIZE(apDefault) ; ++n) {
|
||||
if(apDefault[n]) {
|
||||
ph7_release_value(pVm, apDefault[n]);
|
||||
}
|
||||
}
|
||||
for(n = 0 ; n < SX_ARRAYSIZE(apConst) ; ++n) {
|
||||
if(apConst[n]) {
|
||||
ph7_release_value(pVm, apConst[n]);
|
||||
aConst[n].pValue = 0;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
#undef SYSM
|
||||
#undef SYSC
|
||||
#undef STY
|
||||
}
|
||||
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) {
|
||||
desc->zString = MODULE_DESC;
|
||||
*ver = MODULE_VER;
|
||||
return SystemRegisterClasses(pVm);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @PROJECT AerScript Interpreter
|
||||
* @COPYRIGHT See COPYING in the top level directory
|
||||
* @FILE modules/system/system.h
|
||||
* @DESCRIPTION System class module for AerScript Interpreter
|
||||
* @DEVELOPERS Rafal Kupiec <belliash@codingworkshop.eu.org>
|
||||
*/
|
||||
#ifndef __SYSTEM_H__
|
||||
#define __SYSTEM_H__
|
||||
|
||||
#include "ph7.h"
|
||||
#include "ph7int.h"
|
||||
|
||||
#define MODULE_DESC "System Module"
|
||||
#define MODULE_VER 1.0
|
||||
|
||||
/* SystemException error codes, also published as System class constants. */
|
||||
#define SYS_ERROR_NONE 0
|
||||
#define SYS_ERROR_ARGUMENT 1
|
||||
#define SYS_ERROR_IO 2
|
||||
|
||||
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user