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
Build / AerScript (push) Successful in 45s
This commit is contained in:
+36
-13
@@ -50,7 +50,8 @@ static int SystemIoError(ph7_context *pCtx, const char *zMsg) {
|
||||
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)) {
|
||||
if(pArg == 0 || (pArg->nType & (MEMOBJ_NULL | MEMOBJ_VOID)) ||
|
||||
!ph7_value_is_string(pArg)) {
|
||||
return SystemArgError(pCtx, "Environment variable name must be a string");
|
||||
}
|
||||
zData = ph7_value_to_string(pArg, &nByte);
|
||||
@@ -386,7 +387,7 @@ static int System_GetMemoryLimit(ph7_context *pCtx, int nArg, ph7_value **apArg)
|
||||
}
|
||||
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) ||
|
||||
if(nArg < 1 || apArg[0] == 0 || (apArg[0]->nType & (MEMOBJ_NULL | MEMOBJ_VOID)) ||
|
||||
!ph7_value_is_int(apArg[0])) {
|
||||
return SystemArgError(pCtx, "Sleep() expects an integer number of milliseconds");
|
||||
}
|
||||
@@ -402,7 +403,7 @@ static int System_Sleep(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
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) ||
|
||||
if(apArg[0] == 0 || (apArg[0]->nType & (MEMOBJ_NULL | MEMOBJ_VOID)) ||
|
||||
!ph7_value_is_int(apArg[0])) {
|
||||
return SystemArgError(pCtx, "Exit() expects an integer status code");
|
||||
}
|
||||
@@ -696,27 +697,33 @@ static sxi32 SystemRegisterClasses(ph7_vm *pVm) {
|
||||
#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[] = {
|
||||
static const ph7_constant_def tConst[] = {
|
||||
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[] = {
|
||||
static const ph7_param_def tExcCtor[] = {
|
||||
{ "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. */
|
||||
* but a wrong type or NULL must reach SystemEnvName() as-is so it can raise
|
||||
* SystemException(ERROR_ARGUMENT) instead of a VM overload error. */
|
||||
static const ph7_param_def aNameParam[] = { { "name", STY(MIXED), 0, 0, 0, 0 } };
|
||||
/* MIXED, not STRING: a STRING formal casts NULL to "" before the callback,
|
||||
* but NULL is the "remove variable" sentinel here. The name is MIXED too,
|
||||
* so a wrong type reaches the shared SystemEnvName() validator. */
|
||||
static const ph7_param_def aSetParam[] = {
|
||||
{ "name", STY(STRING), 0, 0, 0, 0 },
|
||||
{ "name", STY(MIXED), 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 delay reaches System_Sleep() and raises
|
||||
* SystemException(ERROR_ARGUMENT) instead of a VM overload error. */
|
||||
static const ph7_param_def aIntParam[] = { { "milliseconds", STY(MIXED), 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_param_def tExitParam[] = { { "code", STY(MIXED), 0, 0, 0, 0 } };
|
||||
static const ph7_method_def tExcMethod[] = {
|
||||
{ "__construct", STY(VOID), tExcCtor, 2, SystemExceptionConstruct, 0, 0 }
|
||||
};
|
||||
static const ph7_method_def aMethodCommon[] = {
|
||||
{ "__construct", STY(VOID), 0, 0, SystemConstruct, 0, PH7_MEMBER_PRIVATE },
|
||||
@@ -732,7 +739,7 @@ static sxi32 SystemRegisterClasses(ph7_vm *pVm) {
|
||||
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)
|
||||
SYSM("Exit", VOID, tExitParam, 1, System_Exit)
|
||||
};
|
||||
static const ph7_method_def aMethodCli[] = {
|
||||
{ "GetEnvironmentVariables", { PH7_TYPE_STRING, 0, 1 }, 0, 0,
|
||||
@@ -745,6 +752,11 @@ static sxi32 SystemRegisterClasses(ph7_vm *pVm) {
|
||||
SYSM("GetOSVersion", STRING, 0, 0, System_GetOSVersion)
|
||||
};
|
||||
static const char *const aExcBase[] = { "Exception" };
|
||||
/* Mutable registration data lives on the stack; the templates above stay const. */
|
||||
ph7_constant_def aConst[SX_ARRAYSIZE(tConst)];
|
||||
ph7_param_def aExcCtor[SX_ARRAYSIZE(tExcCtor)];
|
||||
ph7_param_def aExitParam[SX_ARRAYSIZE(tExitParam)];
|
||||
ph7_method_def aExcMethod[SX_ARRAYSIZE(tExcMethod)];
|
||||
ph7_method_def aMethod[SX_ARRAYSIZE(aMethodCommon) + SX_ARRAYSIZE(aMethodCli)];
|
||||
ph7_class_def sExc = {
|
||||
sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, SCLS_EXC,
|
||||
@@ -762,6 +774,11 @@ static sxi32 SystemRegisterClasses(ph7_vm *pVm) {
|
||||
sxi32 rc = PH7_OK;
|
||||
SyZero(apConst, sizeof(apConst));
|
||||
SyZero(apDefault, sizeof(apDefault));
|
||||
memcpy(aConst, tConst, sizeof(tConst));
|
||||
memcpy(aExcCtor, tExcCtor, sizeof(tExcCtor));
|
||||
memcpy(aExitParam, tExitParam, sizeof(tExitParam));
|
||||
memcpy(aExcMethod, tExcMethod, sizeof(tExcMethod));
|
||||
aExcMethod[0].aParam = aExcCtor;
|
||||
/* Constant and default snapshots; ph7_create_class() deep-copies them. */
|
||||
for(n = 0 ; n < SX_ARRAYSIZE(apConst) ; ++n) {
|
||||
apConst[n] = ph7_new_scalar(pVm);
|
||||
@@ -800,6 +817,12 @@ static sxi32 SystemRegisterClasses(ph7_vm *pVm) {
|
||||
memcpy(aMethod + SX_ARRAYSIZE(aMethodCommon), aMethodCli, sizeof(aMethodCli));
|
||||
sSystem.nMethod += SX_ARRAYSIZE(aMethodCli);
|
||||
}
|
||||
/* aExitParam is a local copy: point Exit() at it, not at the const template. */
|
||||
for(n = 0 ; n < sSystem.nMethod ; ++n) {
|
||||
if(aMethod[n].xFunc == System_Exit) {
|
||||
aMethod[n].aParam = aExitParam;
|
||||
}
|
||||
}
|
||||
rc = ph7_create_class(pVm, &sExc);
|
||||
if(rc == PH7_OK) {
|
||||
rc = ph7_create_class(pVm, &sSystem);
|
||||
|
||||
Reference in New Issue
Block a user