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
+25 -11
View File
@@ -8,6 +8,7 @@
*/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "xml.h"
/*
@@ -826,7 +827,7 @@ static int XmlReaderParse(ph7_context *pCtx, int nArg, ph7_value **apArg) {
pEngine->nErrLine, pEngine->nErrCol);
}
if(pEngine->bAbort) {
return PH7_ABORT; /* e.g. exit() from OnDoctype rejects the document */
return PH7_ABORT; /* e.g. System::Exit() from OnDoctype rejects the document */
}
return PH7_OK; /* void */
}
@@ -1018,9 +1019,9 @@ static int XmlReaderClearHandler(ph7_context *pCtx, int nArg, ph7_value **apArg)
#define XNM(N) { PH7_TYPE_NAMED, N, 0 }
#define XP(N,T) { N, XTY(T), 0, 0, 0, 0 }
/* The pValue pointers are filled in per VM registration and cleared again
* afterwards: these tables are static and must never retain a stale value. */
static ph7_constant_def aExceptionConst[] = {
/* Immutable template: each registration copies it to a local array and fills
* those pValue pointers there, so no static object ever holds a VM value. */
static const ph7_constant_def tExceptionConst[] = {
{ "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_NO_MEMORY", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_SYNTAX", 0, PH7_MEMBER_PUBLIC, 0, 0 },
@@ -1064,10 +1065,10 @@ static sxi32 XmlRegisterClasses(ph7_vm *pVm) {
static const ph7_param_def aReaderInt[] = { XP("index", INT) };
static const ph7_param_def aReaderString[] = { XP("name", STRING) };
static const ph7_param_def aReaderPrefix[] = { XP("prefix", STRING) };
static ph7_param_def aCtor[] = { XP("namespaces", BOOL) };
static const ph7_param_def tCtor[] = { XP("namespaces", BOOL) };
static const ph7_param_def aParse[] = { XP("data", MIXED) };
static const ph7_param_def aSetHandler[] = { { "handler", XNM("IXmlHandler"), 0, 0, 0, 0 } };
static ph7_param_def aExcCtor[] = {
static const ph7_param_def tExcCtor[] = {
XP("message", STRING), XP("code", INT), XP("inputLine", INT), XP("inputColumn", INT)
};
static const ph7_param_def aExcDesc[] = { XP("code", INT) };
@@ -1131,16 +1132,16 @@ static sxi32 XmlRegisterClasses(ph7_vm *pVm) {
{ "inputColumn", XTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 }
};
static const char *const aExcBase[] = { "Exception" };
static const ph7_method_def aExcMethod[] = {
{ "__construct", XTY(VOID), aExcCtor, 4, XmlExceptionConstruct, 0, 0 },
static const ph7_method_def tExcMethod[] = {
{ "__construct", XTY(VOID), tExcCtor, 4, XmlExceptionConstruct, 0, 0 },
{ "GetInputLine", XTY(INT), 0, 0, XmlExceptionField, (void *)"inputLine", PH7_MEMBER_PUBLIC },
{ "GetInputColumn", XTY(INT), 0, 0, XmlExceptionField, (void *)"inputColumn", PH7_MEMBER_PUBLIC },
{ "GetDescription", XTY(STRING), aExcDesc, 1, XmlExceptionGetDescription, 0,
PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
};
/* XmlReader methods */
static const ph7_method_def aReaderMethod[] = {
{ "__construct", XTY(VOID), aCtor, 1, XmlReaderConstruct, 0, 0 },
static const ph7_method_def tReaderMethod[] = {
{ "__construct", XTY(VOID), tCtor, 1, XmlReaderConstruct, 0, 0 },
{ "Parse", XTY(VOID), aParse, 1, XmlReaderParse, 0, PH7_MEMBER_PUBLIC },
{ "Close", XTY(VOID), 0, 0, XmlReaderClose, 0, PH7_MEMBER_PUBLIC },
{ "__destruct", XTY(VOID), 0, 0, XmlReaderDestruct, 0, 0 },
@@ -1164,6 +1165,12 @@ static sxi32 XmlRegisterClasses(ph7_vm *pVm) {
static const ph7_field_def aReaderField[] = {
{ "resource", XTY(RESOURCE), 0, PH7_MEMBER_PRIVATE, 0, 0 }
};
/* Mutable registration data lives on the stack; the templates stay const. */
ph7_constant_def aExceptionConst[SX_ARRAYSIZE(tExceptionConst)];
ph7_param_def aCtor[SX_ARRAYSIZE(tCtor)];
ph7_param_def aExcCtor[SX_ARRAYSIZE(tExcCtor)];
ph7_method_def aExcMethod[SX_ARRAYSIZE(tExcMethod)];
ph7_method_def aReaderMethod[SX_ARRAYSIZE(tReaderMethod)];
ph7_class_def sIface = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION,
"IXmlHandler", 0, 0, 0, 0, aIfaceMethod, SX_ARRAYSIZE(aIfaceMethod),
PH7_DECL_INTERFACE, 0, 0, 0, 0, 0, 0 };
@@ -1181,6 +1188,13 @@ static sxi32 XmlRegisterClasses(ph7_vm *pVm) {
ph7_value *apErrVal[SX_ARRAYSIZE(aExceptionConst)];
sxu32 n;
sxi32 rc;
memcpy(aExceptionConst, tExceptionConst, sizeof(tExceptionConst));
memcpy(aCtor, tCtor, sizeof(tCtor));
memcpy(aExcCtor, tExcCtor, sizeof(tExcCtor));
memcpy(aExcMethod, tExcMethod, sizeof(tExcMethod));
memcpy(aReaderMethod, tReaderMethod, sizeof(tReaderMethod));
aExcMethod[0].aParam = aExcCtor;
aReaderMethod[0].aParam = aCtor;
for(n = 0 ; n < SX_ARRAYSIZE(aDefault) ; ++n) {
aDefault[n] = ph7_new_scalar(pVm);
if(aDefault[n] == 0) {
@@ -1219,7 +1233,7 @@ static sxi32 XmlRegisterClasses(ph7_vm *pVm) {
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sReader);
for(n = 0 ; n < SX_ARRAYSIZE(aExceptionConst) ; ++n) {
ph7_release_value(pVm, apErrVal[n]);
aExceptionConst[n].pValue = 0; /* static table: no dangling pointer across VMs */
aExceptionConst[n].pValue = 0; /* local copy: no dangling pointer across VMs */
}
aCtor[0].pDefault = 0;
aExcCtor[2].pDefault = 0;