Make native class registration use const templates with stack copies, tighten System argument validation and drop unused json_rc
Build / AerScript (push) Successful in 45s

This commit is contained in:
2026-11-24 15:44:06 +01:00
parent 1db842fe0f
commit 0802b294b4
12 changed files with 311 additions and 108 deletions
+34 -10
View File
@@ -8,6 +8,7 @@
*/
#include <float.h>
#include <math.h>
#include <string.h>
#include "math.h"
/* DBL_TRUE_MIN is C11; the module is compiled as C99 with GNU extensions. */
@@ -557,7 +558,7 @@ static int MathRound(ph7_context *pCtx, int nArg, ph7_value **apArg) {
/* ------------------------------------------------------------------ */
/* Native class registration */
/* ------------------------------------------------------------------ */
static ph7_constant_def aMathConst[] = {
static const ph7_constant_def tMathConst[] = {
{ "PI", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "E", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "TAU", 0, PH7_MEMBER_PUBLIC, 0, 0 },
@@ -578,7 +579,7 @@ static const double aMathValue[] = {
MATH_PI, MATH_E, MATH_TAU, MATH_LN2, MATH_LN10,
MATH_LOG2E, MATH_LOG10E, MATH_SQRT2, MATH_SQRT1_2, MATH_EULER_GAMMA
};
static ph7_constant_def aExceptionConst[] = {
static const ph7_constant_def tExceptionConst[] = {
{ "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_ARGUMENT_OUT_OF_RANGE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_ARGUMENT", 0, PH7_MEMBER_PUBLIC, 0, 0 },
@@ -591,7 +592,7 @@ static const int aExceptionValue[] = {
MATH_ERROR_OVERFLOW, MATH_ERROR_ARITHMETIC, MATH_ERROR_DIVIDE_BY_ZERO
};
static sxi32 MathRegisterClasses(ph7_vm *pVm) {
static ph7_param_def aExcCtor[] = {
static const ph7_param_def tExcCtor[] = {
MP("message", STRING), MP("code", INT)
};
static const ph7_param_def aExcDesc[] = { MP("code", INT) };
@@ -600,21 +601,21 @@ static sxi32 MathRegisterClasses(ph7_vm *pVm) {
static const ph7_param_def aThree[] = {
MP("x", MIXED), MP("y", MIXED), MP("z", MIXED)
};
static ph7_param_def aRound[] = {
static const ph7_param_def tRound[] = {
MP("value", MIXED), MP("digits", INT), MP("mode", INT)
};
static ph7_param_def aLog[] = {
static const ph7_param_def tLog[] = {
MP("value", MIXED), MPN("newBase", MTY(MIXED))
};
static const ph7_param_def aScaleB[] = {
MP("x", MIXED), MP("n", INT)
};
static const ph7_method_def aExcMethod[] = {
{ "__construct", MTY(VOID), aExcCtor, 2, MathExceptionConstruct, 0, 0 },
static const ph7_method_def tExcMethod[] = {
{ "__construct", MTY(VOID), tExcCtor, 2, MathExceptionConstruct, 0, 0 },
{ "GetDescription", MTY(STRING), aExcDesc, 1, MathExceptionDescription, 0,
PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
};
static const ph7_method_def aMathMethod[] = {
static const ph7_method_def tMathMethod[] = {
{ "Abs", MTY(MIXED), aOne, 1, MathAbs, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Acos", MTY(FLOAT), aOne, 1, MathAcos, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Acosh", MTY(FLOAT), aOne, 1, MathAcosh, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
@@ -639,7 +640,7 @@ static sxi32 MathRegisterClasses(ph7_vm *pVm) {
{ "Hypot", MTY(FLOAT), aTwo, 2, MathHypot, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "IEEERemainder", MTY(FLOAT), aTwo, 2, MathIEEEremainder, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "ILogB", MTY(INT), aOne, 1, MathILogB, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Log", MTY(FLOAT), aLog, 2, MathLog, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Log", MTY(FLOAT), tLog, 2, MathLog, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Log10", MTY(FLOAT), aOne, 1, MathLog10, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Log2", MTY(FLOAT), aOne, 1, MathLog2, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Max", MTY(MIXED), aTwo, 2, MathMax, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
@@ -648,7 +649,7 @@ static sxi32 MathRegisterClasses(ph7_vm *pVm) {
{ "MinMagnitude", MTY(FLOAT), aTwo, 2, MathMinMagnitude, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Pow", MTY(FLOAT), aTwo, 2, MathPow, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Remainder", MTY(FLOAT), aTwo, 2, MathRemainder, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Round", MTY(FLOAT), aRound, 3, MathRound, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Round", MTY(FLOAT), tRound, 3, MathRound, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "ScaleB", MTY(FLOAT), aScaleB, 2, MathScaleB, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Sign", MTY(INT), aOne, 1, MathSign, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Sin", MTY(FLOAT), aOne, 1, MathSin, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
@@ -661,6 +662,14 @@ static sxi32 MathRegisterClasses(ph7_vm *pVm) {
{ "Truncate", MTY(FLOAT), aOne, 1, MathTruncate, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
};
static const char *const aExcBase[] = { "Exception" };
/* Mutable registration data lives on the stack; the templates stay const. */
ph7_constant_def aMathConst[SX_ARRAYSIZE(tMathConst)];
ph7_constant_def aExceptionConst[SX_ARRAYSIZE(tExceptionConst)];
ph7_param_def aExcCtor[SX_ARRAYSIZE(tExcCtor)];
ph7_param_def aRound[SX_ARRAYSIZE(tRound)];
ph7_param_def aLog[SX_ARRAYSIZE(tLog)];
ph7_method_def aExcMethod[SX_ARRAYSIZE(tExcMethod)];
ph7_method_def aMathMethod[SX_ARRAYSIZE(tMathMethod)];
ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, MCLS_EXC,
0, 0, aExceptionConst, SX_ARRAYSIZE(aExceptionConst),
aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 };
@@ -676,6 +685,21 @@ static sxi32 MathRegisterClasses(ph7_vm *pVm) {
SyZero(apMath, sizeof(apMath));
SyZero(apExc, sizeof(apExc));
SyZero(apDefault, sizeof(apDefault));
memcpy(aMathConst, tMathConst, sizeof(tMathConst));
memcpy(aExceptionConst, tExceptionConst, sizeof(tExceptionConst));
memcpy(aExcCtor, tExcCtor, sizeof(tExcCtor));
memcpy(aRound, tRound, sizeof(tRound));
memcpy(aLog, tLog, sizeof(tLog));
memcpy(aExcMethod, tExcMethod, sizeof(tExcMethod));
memcpy(aMathMethod, tMathMethod, sizeof(tMathMethod));
aExcMethod[0].aParam = aExcCtor;
for(n = 0 ; n < SX_ARRAYSIZE(aMathMethod) ; ++n) {
if(aMathMethod[n].xFunc == MathLog) {
aMathMethod[n].aParam = aLog;
} else if(aMathMethod[n].xFunc == MathRound) {
aMathMethod[n].aParam = aRound;
}
}
/* Constant and default snapshots. A fresh scalar is NULL, which is the
* exact default required for Log()'s optional newBase. */
for(n = 0 ; n < SX_ARRAYSIZE(apMath) ; ++n) {