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:
+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) {
|
||||
|
||||
Reference in New Issue
Block a user