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
+1 -1
View File
@@ -3074,7 +3074,7 @@ static int PH7_builtin_nl2br(ph7_context *pCtx, int nArg, ph7_value **apArg) {
* The format string is composed of zero or more directives: ordinary characters * The format string is composed of zero or more directives: ordinary characters
* (excluding %) that are copied directly to the result, and conversion * (excluding %) that are copied directly to the result, and conversion
* specifications, each of which results in fetching its own parameter. * specifications, each of which results in fetching its own parameter.
* This applies to both sprintf() and printf(). * This applies to sprintf().
* Each conversion specification consists of a percent sign (%), followed by one * Each conversion specification consists of a percent sign (%), followed by one
* or more of these elements, in order: * or more of these elements, in order:
* An optional sign specifier that forces a sign (- or +) to be used on a number. * An optional sign specifier that forces a sign (- or +) to be used on a number.
+32 -7
View File
@@ -5,6 +5,7 @@
* @DESCRIPTION Native declarations for the built-in classes * @DESCRIPTION Native declarations for the built-in classes
* @DEVELOPERS Rafal Kupiec <belliash@codingworkshop.eu.org> * @DEVELOPERS Rafal Kupiec <belliash@codingworkshop.eu.org>
*/ */
#include <string.h>
#include "ph7int.h" #include "ph7int.h"
/* Calls use the ordinary language dispatcher, including its exception result. */ /* Calls use the ordinary language dispatcher, including its exception result. */
@@ -621,38 +622,44 @@ PH7_PRIVATE sxi32 PH7_VmInstallBuiltinClasses(ph7_vm *pVm) {
{ .zName = "__toString", .sResult = { PH7_TYPE_STRING, 0, 0 }, .xFunc = StdConvert, .pUserData = (void *)&aConversion[3] }, { .zName = "__toString", .sResult = { PH7_TYPE_STRING, 0, 0 }, .xFunc = StdConvert, .pUserData = (void *)&aConversion[3] },
{ .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = StdConstruct, .iFlags = PH7_MEMBER_PRIVATE } { .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = StdConstruct, .iFlags = PH7_MEMBER_PRIVATE }
}; };
static ph7_constant_def aIoConst[] = { static const ph7_constant_def tIoConst[] = {
{ .zName = "ERROR_NONE", .iFlags = PH7_MEMBER_PUBLIC }, { .zName = "ERROR_NONE", .iFlags = PH7_MEMBER_PUBLIC },
{ .zName = "ERROR_ARGUMENT", .iFlags = PH7_MEMBER_PUBLIC }, { .zName = "ERROR_ARGUMENT", .iFlags = PH7_MEMBER_PUBLIC },
{ .zName = "ERROR_IO", .iFlags = PH7_MEMBER_PUBLIC } { .zName = "ERROR_IO", .iFlags = PH7_MEMBER_PUBLIC }
}; };
static ph7_param_def aIoParam[] = { static const ph7_param_def tIoParam[] = {
{ .zName = "message", .sType = { PH7_TYPE_STRING, 0, 0 } }, { .zName = "message", .sType = { PH7_TYPE_STRING, 0, 0 } },
{ .zName = "code", .sType = { PH7_TYPE_INT, 0, 0 } } { .zName = "code", .sType = { PH7_TYPE_INT, 0, 0 } }
}; };
static const ph7_param_def aIoDescParam[] = { static const ph7_param_def aIoDescParam[] = {
{ .zName = "code", .sType = { PH7_TYPE_INT, 0, 0 } } { .zName = "code", .sType = { PH7_TYPE_INT, 0, 0 } }
}; };
static ph7_param_def aPrintLineParam[] = { static const ph7_param_def tPrintLineParam[] = {
{ .zName = "value", .sType = { PH7_TYPE_MIXED, 0, 0 } } { .zName = "value", .sType = { PH7_TYPE_MIXED, 0, 0 } }
}; };
static const ph7_method_def aIoMethod[] = { static const ph7_method_def tIoMethod[] = {
{ .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aIoParam, .nParam = 2, .xFunc = IOExceptionConstruct }, { .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = tIoParam, .nParam = 2, .xFunc = IOExceptionConstruct },
{ .zName = "GetDescription", .sResult = { PH7_TYPE_STRING, 0, 0 }, .aParam = aIoDescParam, .nParam = 1, .xFunc = IOExceptionDescription, { .zName = "GetDescription", .sResult = { PH7_TYPE_STRING, 0, 0 }, .aParam = aIoDescParam, .nParam = 1, .xFunc = IOExceptionDescription,
.iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC } .iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
}; };
static const ph7_field_def aTextWriterField[] = { static const ph7_field_def aTextWriterField[] = {
{ .zName = "handle", .sType = { PH7_TYPE_RESOURCE, 0, 0 }, .iFlags = PH7_MEMBER_PRIVATE } { .zName = "handle", .sType = { PH7_TYPE_RESOURCE, 0, 0 }, .iFlags = PH7_MEMBER_PRIVATE }
}; };
static const ph7_method_def aTextWriterMethod[] = { static const ph7_method_def tTextWriterMethod[] = {
{ .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = TextWriterConstruct, .iFlags = PH7_MEMBER_PRIVATE }, { .zName = "__construct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = TextWriterConstruct, .iFlags = PH7_MEMBER_PRIVATE },
{ .zName = "__destruct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterDestruct }, { .zName = "__destruct", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterDestruct },
{ .zName = "__clone", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterClone, .iFlags = PH7_MEMBER_PRIVATE }, { .zName = "__clone", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterClone, .iFlags = PH7_MEMBER_PRIVATE },
{ .zName = "Print", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = TextWriterPrint, .iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_VARIADIC }, { .zName = "Print", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aValueParam, .nParam = 1, .xFunc = TextWriterPrint, .iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_VARIADIC },
{ .zName = "PrintLine", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = aPrintLineParam, .nParam = 1, .xFunc = TextWriterPrintLine, .iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_VARIADIC }, { .zName = "PrintLine", .sResult = { PH7_TYPE_VOID, 0, 0 }, .aParam = tPrintLineParam, .nParam = 1, .xFunc = TextWriterPrintLine, .iFlags = PH7_MEMBER_PUBLIC | PH7_MEMBER_VARIADIC },
{ .zName = "Flush", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterFlush } { .zName = "Flush", .sResult = { PH7_TYPE_VOID, 0, 0 }, .xFunc = TextWriterFlush }
}; };
static const char *const aIoBase[] = { "Exception" }; static const char *const aIoBase[] = { "Exception" };
/* Mutable registration data lives on the stack; the templates stay const. */
ph7_constant_def aIoConst[SX_ARRAYSIZE(tIoConst)];
ph7_param_def aIoParam[SX_ARRAYSIZE(tIoParam)];
ph7_param_def aPrintLineParam[SX_ARRAYSIZE(tPrintLineParam)];
ph7_method_def aIoMethod[SX_ARRAYSIZE(tIoMethod)];
ph7_method_def aTextWriterMethod[SX_ARRAYSIZE(tTextWriterMethod)];
ph7_class_def aClass[] = { ph7_class_def aClass[] = {
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "Exception", .aField = aExceptionField, .nField = SX_ARRAYSIZE(aExceptionField), .aMethod = aExceptionMethod, .nMethod = SX_ARRAYSIZE(aExceptionMethod) }, { .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "Exception", .aField = aExceptionField, .nField = SX_ARRAYSIZE(aExceptionField), .aMethod = aExceptionMethod, .nMethod = SX_ARRAYSIZE(aExceptionMethod) },
{ .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "IOException", .aConstant = aIoConst, .nConstant = SX_ARRAYSIZE(aIoConst), .aMethod = aIoMethod, .nMethod = SX_ARRAYSIZE(aIoMethod), .azBase = aIoBase, .nBase = 1 }, { .nSize = sizeof(ph7_class_def), .nVersion = PH7_CLASS_DEF_VERSION, .zName = "IOException", .aConstant = aIoConst, .nConstant = SX_ARRAYSIZE(aIoConst), .aMethod = aIoMethod, .nMethod = SX_ARRAYSIZE(aIoMethod), .azBase = aIoBase, .nBase = 1 },
@@ -666,6 +673,21 @@ PH7_PRIVATE sxi32 PH7_VmInstallBuiltinClasses(ph7_vm *pVm) {
ph7_value *apIoValue[SX_ARRAYSIZE(aIoConst)] = { 0 }; ph7_value *apIoValue[SX_ARRAYSIZE(aIoConst)] = { 0 };
sxi32 rc = PH7_NOMEM; sxi32 rc = PH7_NOMEM;
sxu32 n; sxu32 n;
memcpy(aIoConst, tIoConst, sizeof(tIoConst));
memcpy(aIoParam, tIoParam, sizeof(tIoParam));
memcpy(aPrintLineParam, tPrintLineParam, sizeof(tPrintLineParam));
memcpy(aIoMethod, tIoMethod, sizeof(tIoMethod));
memcpy(aTextWriterMethod, tTextWriterMethod, sizeof(tTextWriterMethod));
for(n = 0; n < SX_ARRAYSIZE(aIoMethod); ++n) {
if(aIoMethod[n].xFunc == IOExceptionConstruct) {
aIoMethod[n].aParam = aIoParam;
}
}
for(n = 0; n < SX_ARRAYSIZE(aTextWriterMethod); ++n) {
if(aTextWriterMethod[n].xFunc == TextWriterPrintLine) {
aTextWriterMethod[n].aParam = aPrintLineParam;
}
}
for(n = 0; n < SX_ARRAYSIZE(apValue); n++) { for(n = 0; n < SX_ARRAYSIZE(apValue); n++) {
apValue[n] = ph7_new_scalar(pVm); apValue[n] = ph7_new_scalar(pVm);
if(apValue[n] == 0) { if(apValue[n] == 0) {
@@ -705,6 +727,9 @@ PH7_PRIVATE sxi32 PH7_VmInstallBuiltinClasses(ph7_vm *pVm) {
} }
} }
Cleanup: Cleanup:
aIoParam[0].pDefault = 0;
aIoParam[1].pDefault = 0;
aPrintLineParam[0].pDefault = 0;
for(n = 0; n < SX_ARRAYSIZE(apIoValue); n++) { for(n = 0; n < SX_ARRAYSIZE(apIoValue); n++) {
if(apIoValue[n]) { if(apIoValue[n]) {
ph7_release_value(pVm, apIoValue[n]); ph7_release_value(pVm, apIoValue[n]);
+1 -1
View File
@@ -1583,7 +1583,7 @@ PH7_PRIVATE void PH7_HashmapExtractNodeKey(ph7_hashmap_node *pNode, ph7_value *p
} }
/* /*
* Store the address of nodes value in the given container. * Store the address of nodes value in the given container.
* Refer to the [vfprintf(),vprintf(),vsprintf()] implementations * Refer to the [vfprintf(),vsprintf()] implementations
* defined in 'builtin.c' for more information. * defined in 'builtin.c' for more information.
*/ */
PH7_PRIVATE int PH7_HashmapValuesToSet(ph7_hashmap *pMap, SySet *pOut) { PH7_PRIVATE int PH7_HashmapValuesToSet(ph7_hashmap *pMap, SySet *pOut) {
+4 -4
View File
@@ -2510,8 +2510,6 @@ PH7_PRIVATE sxi32 PH7_VmInit(
PH7_MemObjInit(pVm, &pVm->sPendingTransfer); PH7_MemObjInit(pVm, &pVm->sPendingTransfer);
/* Default assertion flags */ /* Default assertion flags */
pVm->iAssertFlags = PH7_ASSERT_WARNING; /* Issue a warning for each failed assertion */ pVm->iAssertFlags = PH7_ASSERT_WARNING; /* Issue a warning for each failed assertion */
/* JSON return status */
pVm->json_rc = JSON_ERROR_NONE;
/* PRNG context */ /* PRNG context */
SyRandomnessInit(&pVm->sPrng, 0, 0); SyRandomnessInit(&pVm->sPrng, 0, 0);
/* Install the null constant */ /* Install the null constant */
@@ -2711,7 +2709,9 @@ PH7_PRIVATE sxi32 PH7_VmMakeReady(
/* Zero the reference table */ /* Zero the reference table */
SyZero(pVm->apRefObj, sizeof(VmRefObj *) * pVm->nRefSize); SyZero(pVm->apRefObj, sizeof(VmRefObj *) * pVm->nRefSize);
} }
/* Register special functions first [i.e: print, json_encode(), func_get_args(), etc.] */ /* Register special functions first [i.e: module_loaded(), function_exists(),
* class_exists(), get_class(), rand(), var_dump(), print_r(), get_include_path(),
* debug_backtrace(), ...] */
rc = VmRegisterSpecialFunction(&(*pVm)); rc = VmRegisterSpecialFunction(&(*pVm));
if(rc != SXRET_OK) { if(rc != SXRET_OK) {
/* Don't worry about freeing memory, everything will be released shortly */ /* Don't worry about freeing memory, everything will be released shortly */
@@ -3670,7 +3670,7 @@ PH7_PRIVATE sxi32 PH7_VmConfigure(
n = (sxu32)SyStrlen(zValue); n = (sxu32)SyStrlen(zValue);
nText = SyBlobLength(&pVm->sArgv); nText = SyBlobLength(&pVm->sArgv);
nEntries = SyBlobLength(&pVm->sArgvEntries); nEntries = SyBlobLength(&pVm->sArgvEntries);
/* Keep exact entries separately from getopt's legacy flat text. */ /* Keep exact argument entries separately from the flat $argv text. */
rc = SyBlobAppend(&pVm->sArgvEntries, zValue, n + 1); rc = SyBlobAppend(&pVm->sArgvEntries, zValue, n + 1);
if(rc == SXRET_OK && n > 0) { if(rc == SXRET_OK && n > 0) {
if(nText > 0) { if(nText > 0) {
+1 -2
View File
@@ -1248,7 +1248,7 @@ struct ph7_vm {
SyHash hSuper; /* Superglobals hashtable */ SyHash hSuper; /* Superglobals hashtable */
SyHash hDBAL; /* DBAL installed drivers */ SyHash hDBAL; /* DBAL installed drivers */
SyBlob sConsumer; /* Default VM consumer [i.e Redirect all VM output to this blob] */ SyBlob sConsumer; /* Default VM consumer [i.e Redirect all VM output to this blob] */
SyBlob sArgv; /* $argv[] collector [refer to the [getopt()] implementation for more information] */ SyBlob sArgv; /* $argv[] collector: space-separated argument text */
SyBlob sArgvEntries; /* VM-owned argument list, each entry terminated by NUL */ SyBlob sArgvEntries; /* VM-owned argument list, each entry terminated by NUL */
SySet aModules; /* Set of loaded modules */ SySet aModules; /* Set of loaded modules */
SySet aModuleHandles; /* Libraries retained after initialization attempts */ SySet aModuleHandles; /* Libraries retained after initialization attempts */
@@ -1281,7 +1281,6 @@ struct ph7_vm {
ph7_value sPendingTransfer; ph7_value sPendingTransfer;
int nExceptDepth; /* Exception depth */ int nExceptDepth; /* Exception depth */
int closure_cnt; /* Loaded closures counter */ int closure_cnt; /* Loaded closures counter */
int json_rc; /* JSON return status [refer to json_encode()/json_decode()] */
ph7_output_consumer sVmConsumer; /* Registered output consumer callback */ ph7_output_consumer sVmConsumer; /* Registered output consumer callback */
int iAssertFlags; /* Assertion flags */ int iAssertFlags; /* Assertion flags */
ph7_value sAssertCallback; /* Callback to call on failed assertions */ ph7_value sAssertCallback; /* Callback to call on failed assertions */
+15 -5
View File
@@ -249,16 +249,16 @@ static int CharConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
return PH7_ABORT; /* final class: no public construction */ return PH7_ABORT; /* final class: no public construction */
} }
static sxi32 CharRegisterClasses(ph7_vm *pVm) { static sxi32 CharRegisterClasses(ph7_vm *pVm) {
static ph7_constant_def aExcConst[] = { static const ph7_constant_def tExcConst[] = {
{ "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_ARGUMENT", 0, PH7_MEMBER_PUBLIC, 0, 0 } { "ERROR_ARGUMENT", 0, PH7_MEMBER_PUBLIC, 0, 0 }
}; };
static const int aExcValue[] = { CHAR_ERROR_NONE, CHAR_ERROR_ARGUMENT }; static const int aExcValue[] = { CHAR_ERROR_NONE, CHAR_ERROR_ARGUMENT };
static ph7_constant_def aCharConst[] = { static const ph7_constant_def tCharConst[] = {
{ "MIN_VALUE", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "MIN_VALUE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "MAX_VALUE", 0, PH7_MEMBER_PUBLIC, 0, 0 } { "MAX_VALUE", 0, PH7_MEMBER_PUBLIC, 0, 0 }
}; };
static ph7_param_def aExcCtor[] = { static const ph7_param_def tExcCtor[] = {
{ "message", CTY(STRING), 0, 0, 0, 0 }, { "message", CTY(STRING), 0, 0, 0, 0 },
{ "code", CTY(INT), 0, 0, 0, 0 } { "code", CTY(INT), 0, 0, 0, 0 }
}; };
@@ -267,8 +267,8 @@ static sxi32 CharRegisterClasses(ph7_vm *pVm) {
}; };
#define SMV(NAME,RET,FUNC) \ #define SMV(NAME,RET,FUNC) \
{ NAME, CTY(RET), 0, 0, FUNC, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC | PH7_MEMBER_VARIADIC } { NAME, CTY(RET), 0, 0, FUNC, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC | PH7_MEMBER_VARIADIC }
static const ph7_method_def aExcMethod[] = { static const ph7_method_def tExcMethod[] = {
{ "__construct", CTY(VOID), aExcCtor, 2, CharExceptionConstruct, 0, 0 }, { "__construct", CTY(VOID), tExcCtor, 2, CharExceptionConstruct, 0, 0 },
{ "GetDescription", CTY(STRING), aExcDesc, 1, CharExceptionDescription, 0, { "GetDescription", CTY(STRING), aExcDesc, 1, CharExceptionDescription, 0,
PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC } PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
}; };
@@ -305,6 +305,11 @@ static sxi32 CharRegisterClasses(ph7_vm *pVm) {
}; };
#undef SMV #undef SMV
static const char *const aExcBase[] = { "Exception" }; static const char *const aExcBase[] = { "Exception" };
/* Mutable registration data lives on the stack; the templates stay const. */
ph7_constant_def aExcConst[SX_ARRAYSIZE(tExcConst)];
ph7_constant_def aCharConst[SX_ARRAYSIZE(tCharConst)];
ph7_param_def aExcCtor[SX_ARRAYSIZE(tExcCtor)];
ph7_method_def aExcMethod[SX_ARRAYSIZE(tExcMethod)];
ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, CCLS_EXC, ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, CCLS_EXC,
0, 0, aExcConst, SX_ARRAYSIZE(aExcConst), 0, 0, aExcConst, SX_ARRAYSIZE(aExcConst),
aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 }; aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 };
@@ -319,6 +324,11 @@ static sxi32 CharRegisterClasses(ph7_vm *pVm) {
sxi32 rc = PH7_OK; sxi32 rc = PH7_OK;
SyZero(apExc, sizeof(apExc)); SyZero(apExc, sizeof(apExc));
SyZero(apChar, sizeof(apChar)); SyZero(apChar, sizeof(apChar));
memcpy(aExcConst, tExcConst, sizeof(tExcConst));
memcpy(aCharConst, tCharConst, sizeof(tCharConst));
memcpy(aExcCtor, tExcCtor, sizeof(tExcCtor));
memcpy(aExcMethod, tExcMethod, sizeof(tExcMethod));
aExcMethod[0].aParam = aExcCtor;
for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) { for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) {
apExc[n] = ph7_new_scalar(pVm); apExc[n] = ph7_new_scalar(pVm);
if(apExc[n] == 0) { rc = PH7_NOMEM; goto Cleanup; } if(apExc[n] == 0) { rc = PH7_NOMEM; goto Cleanup; }
+8 -5
View File
@@ -149,8 +149,8 @@ static void ConsoleEmit(ph7_context *pCtx, const char *zData) {
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Writer backends */ /* Writer backends */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Standard output reuses the VM output consumer, so output buffers and /* Standard output reuses the VM output consumer, so host buffers see
* host consumers observe these bytes exactly as they observe print(). */ * Console/TextWriter bytes through the same path as ordinary script output. */
static int ConsoleOutWrite(void *pUserData, const void *pData, unsigned int nByte) { static int ConsoleOutWrite(void *pUserData, const void *pData, unsigned int nByte) {
ph7_vm *pVm = (ph7_vm *)pUserData; ph7_vm *pVm = (ph7_vm *)pUserData;
SyString sData; SyString sData;
@@ -220,8 +220,8 @@ static int ConsolePrintImpl(ph7_context *pCtx, int nArg, ph7_value **apArg, int
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Console method callbacks */ /* Console method callbacks */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Keep the legacy print() contract: the call is an expression whose value is /* Keep the Console::Print()/TextWriter::Print() contract: the call is an
* NULL, which matters for default-parameter sentinels. */ * expression whose value is NULL, which matters for default-parameter sentinels. */
static int ConsoleNullResult(ph7_context *pCtx, int rc) { static int ConsoleNullResult(ph7_context *pCtx, int rc) {
return rc == SXRET_OK ? ph7_result_null(pCtx) : rc; return rc == SXRET_OK ? ph7_result_null(pCtx) : rc;
} }
@@ -568,7 +568,7 @@ static int Console_ResetColor(ph7_context *pCtx, int nArg, ph7_value **apArg) {
static sxi32 ConsoleRegister(ph7_vm *pVm) { static sxi32 ConsoleRegister(ph7_vm *pVm) {
#define CTY(T) { PH7_TYPE_##T, 0, 0 } #define CTY(T) { PH7_TYPE_##T, 0, 0 }
#define CONST_(N,V) { N, 0, PH7_MEMBER_PUBLIC, 0, 0 } #define CONST_(N,V) { N, 0, PH7_MEMBER_PUBLIC, 0, 0 }
static ph7_constant_def aConst[] = { static const ph7_constant_def tConst[] = {
CONST_("COLOR_BLACK", 0), CONST_("COLOR_DARK_BLUE", 1), CONST_("COLOR_BLACK", 0), CONST_("COLOR_DARK_BLUE", 1),
CONST_("COLOR_DARK_GREEN", 2), CONST_("COLOR_DARK_CYAN", 3), CONST_("COLOR_DARK_GREEN", 2), CONST_("COLOR_DARK_CYAN", 3),
CONST_("COLOR_DARK_RED", 4), CONST_("COLOR_DARK_MAGENTA", 5), CONST_("COLOR_DARK_RED", 4), CONST_("COLOR_DARK_MAGENTA", 5),
@@ -617,6 +617,8 @@ static sxi32 ConsoleRegister(ph7_vm *pVm) {
SM("GetBackgroundColor", INT, 0, 0, Console_GetBackgroundColor), SM("GetBackgroundColor", INT, 0, 0, Console_GetBackgroundColor),
SM("ResetColor", VOID, 0, 0, Console_ResetColor) SM("ResetColor", VOID, 0, 0, Console_ResetColor)
}; };
/* Mutable registration data lives on the stack; the template stays const. */
ph7_constant_def aConst[SX_ARRAYSIZE(tConst)];
ph7_class_def sClass = { ph7_class_def sClass = {
sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, "Console", sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, "Console",
0, 0, aConst, SX_ARRAYSIZE(aConst), 0, 0, aConst, SX_ARRAYSIZE(aConst),
@@ -627,6 +629,7 @@ static sxi32 ConsoleRegister(ph7_vm *pVm) {
sxu32 n; sxu32 n;
sxi32 rc = PH7_OK; sxi32 rc = PH7_OK;
SyZero(apValue, sizeof(apValue)); SyZero(apValue, sizeof(apValue));
memcpy(aConst, tConst, sizeof(tConst));
for(n = 0 ; n < SX_ARRAYSIZE(apValue) ; ++n) { for(n = 0 ; n < SX_ARRAYSIZE(apValue) ; ++n) {
apValue[n] = ph7_new_scalar(pVm); apValue[n] = ph7_new_scalar(pVm);
if(apValue[n] == 0) { if(apValue[n] == 0) {
+76 -23
View File
@@ -2792,7 +2792,7 @@ static int JsonSerializeFn(ph7_context *pCtx, int nArg, ph7_value **apArg) {
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Native class registration */ /* Native class registration */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
static ph7_constant_def aNodeTypeConst[] = { static const ph7_constant_def tNodeTypeConst[] = {
{ "OBJECT", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "OBJECT", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ARRAY", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ARRAY", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "STRING", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "STRING", 0, PH7_MEMBER_PUBLIC, 0, 0 },
@@ -2804,7 +2804,7 @@ static const int aNodeTypeValue[] = {
JSON_NODE_OBJECT, JSON_NODE_ARRAY, JSON_NODE_STRING, JSON_NODE_OBJECT, JSON_NODE_ARRAY, JSON_NODE_STRING,
JSON_NODE_NUMBER, JSON_NODE_BOOLEAN, JSON_NODE_NULL_NODE JSON_NODE_NUMBER, JSON_NODE_BOOLEAN, JSON_NODE_NULL_NODE
}; };
static ph7_constant_def aExceptionConst[] = { static const ph7_constant_def tExceptionConst[] = {
{ "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_DEPTH", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ERROR_DEPTH", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_STATE_MISMATCH", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ERROR_STATE_MISMATCH", 0, PH7_MEMBER_PUBLIC, 0, 0 },
@@ -2825,7 +2825,7 @@ static const int aExceptionValue[] = {
JSON_ERROR_INDEX_OUT_OF_RANGE, JSON_ERROR_DUPLICATE_KEY, JSON_ERROR_INVALID_OPERATION, JSON_ERROR_INDEX_OUT_OF_RANGE, JSON_ERROR_DUPLICATE_KEY, JSON_ERROR_INVALID_OPERATION,
JSON_ERROR_INVALID_OPTION, JSON_ERROR_READONLY_OPTION JSON_ERROR_INVALID_OPTION, JSON_ERROR_READONLY_OPTION
}; };
static ph7_constant_def aOptionsConst[] = { static const ph7_constant_def tOptionsConst[] = {
{ "KEEP_LAST", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "KEEP_LAST", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "KEEP_FIRST", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "KEEP_FIRST", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "REJECT", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "REJECT", 0, PH7_MEMBER_PUBLIC, 0, 0 },
@@ -2833,7 +2833,7 @@ static ph7_constant_def aOptionsConst[] = {
static const int aOptionsValue[] = { JSON_DUP_KEEP_LAST, JSON_DUP_KEEP_FIRST, JSON_DUP_REJECT }; static const int aOptionsValue[] = { JSON_DUP_KEEP_LAST, JSON_DUP_KEEP_FIRST, JSON_DUP_REJECT };
static sxi32 JsonRegisterClasses(ph7_vm *pVm) { static sxi32 JsonRegisterClasses(ph7_vm *pVm) {
static ph7_param_def aExcCtor[] = { static const ph7_param_def tExcCtor[] = {
JP("message", STRING), JP("code", INT), JPN("inputLine", JTY(INT)), JP("message", STRING), JP("code", INT), JPN("inputLine", JTY(INT)),
JPN("inputColumn", JTY(INT)), JPN("path", JTY(STRING)) JPN("inputColumn", JTY(INT)), JPN("path", JTY(STRING))
}; };
@@ -2852,22 +2852,22 @@ static sxi32 JsonRegisterClasses(ph7_vm *pVm) {
static const ph7_param_def aNodeCtorFloat[] = { JP("value", FLOAT) }; static const ph7_param_def aNodeCtorFloat[] = { JP("value", FLOAT) };
static const ph7_param_def aNodeCtorRaw[] = { JP("rawNumber", STRING) }; static const ph7_param_def aNodeCtorRaw[] = { JP("rawNumber", STRING) };
static const ph7_param_def aNodeCtorBool[] = { JP("value", BOOL) }; static const ph7_param_def aNodeCtorBool[] = { JP("value", BOOL) };
static ph7_param_def aToJson[] = { JPO("options", JNM("JsonOptions"), 0) }; static const ph7_param_def tToJson[] = { JPO("options", JNM("JsonOptions"), 0) };
static const ph7_param_def aSetBool[] = { JP("value", BOOL) }; static const ph7_param_def aSetBool[] = { JP("value", BOOL) };
static const ph7_param_def aSetInt[] = { JP("value", INT) }; static const ph7_param_def aSetInt[] = { JP("value", INT) };
static const ph7_param_def aSetStr[] = { JP("value", STRING) }; static const ph7_param_def aSetStr[] = { JP("value", STRING) };
static ph7_param_def aParse[] = { static const ph7_param_def tParse[] = {
JP("text", STRING), JPO("options", JNM("JsonOptions"), 0) JP("text", STRING), JPO("options", JNM("JsonOptions"), 0)
}; };
static ph7_param_def aDocParse[] = { static const ph7_param_def tDocParse[] = {
JP("text", STRING), JPO("options", JNM("JsonOptions"), 0) JP("text", STRING), JPO("options", JNM("JsonOptions"), 0)
}; };
static ph7_param_def aDocToJson[] = { JPO("options", JNM("JsonOptions"), 0) }; static const ph7_param_def tDocToJson[] = { JPO("options", JNM("JsonOptions"), 0) };
static ph7_param_def aSerialize[] = { static const ph7_param_def tSerialize[] = {
JPN("node", JNM("JsonNode")), JPO("options", JNM("JsonOptions"), 0) JPN("node", JNM("JsonNode")), JPO("options", JNM("JsonOptions"), 0)
}; };
static const ph7_method_def aExcMethod[] = { static const ph7_method_def tExcMethod[] = {
{ "__construct", JTY(VOID), aExcCtor, 5, JsonExceptionConstruct, 0, 0 }, { "__construct", JTY(VOID), tExcCtor, 5, JsonExceptionConstruct, 0, 0 },
{ "GetInputLine", JTY(INT), 0, 0, JsonExceptionField, (void *)"inputLine", PH7_MEMBER_PUBLIC }, { "GetInputLine", JTY(INT), 0, 0, JsonExceptionField, (void *)"inputLine", PH7_MEMBER_PUBLIC },
{ "GetInputColumn", JTY(INT), 0, 0, JsonExceptionField, (void *)"inputColumn", PH7_MEMBER_PUBLIC }, { "GetInputColumn", JTY(INT), 0, 0, JsonExceptionField, (void *)"inputColumn", PH7_MEMBER_PUBLIC },
{ "GetPath", JTY(STRING), 0, 0, JsonExceptionField, (void *)"path", PH7_MEMBER_PUBLIC }, { "GetPath", JTY(STRING), 0, 0, JsonExceptionField, (void *)"path", PH7_MEMBER_PUBLIC },
@@ -2880,7 +2880,7 @@ static sxi32 JsonRegisterClasses(ph7_vm *pVm) {
{ "path", JTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "path", JTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
}; };
static const char *const aExcBase[] = { "Exception" }; static const char *const aExcBase[] = { "Exception" };
static const ph7_method_def aNodeMethod[] = { static const ph7_method_def tNodeMethod[] = {
{ "GetNodeType", JTY(INT), 0, 0, JsonNodeGetIntField, (void *)"kind", PH7_MEMBER_PUBLIC }, { "GetNodeType", JTY(INT), 0, 0, JsonNodeGetIntField, (void *)"kind", PH7_MEMBER_PUBLIC },
{ "GetPath", JTY(STRING), 0, 0, JsonNodeGetStrField, (void *)"path", PH7_MEMBER_PUBLIC }, { "GetPath", JTY(STRING), 0, 0, JsonNodeGetStrField, (void *)"path", PH7_MEMBER_PUBLIC },
{ "GetDepth", JTY(INT), 0, 0, JsonNodeGetDepth, 0, PH7_MEMBER_PUBLIC }, { "GetDepth", JTY(INT), 0, 0, JsonNodeGetDepth, 0, PH7_MEMBER_PUBLIC },
@@ -2902,10 +2902,10 @@ static sxi32 JsonRegisterClasses(ph7_vm *pVm) {
{ "GetBool", JTY(BOOL), 0, 0, JsonNodeGetBool, 0, PH7_MEMBER_PUBLIC }, { "GetBool", JTY(BOOL), 0, 0, JsonNodeGetBool, 0, PH7_MEMBER_PUBLIC },
{ "GetRawNumber", JTY(STRING), 0, 0, JsonNodeGetRawNumber, 0, PH7_MEMBER_PUBLIC }, { "GetRawNumber", JTY(STRING), 0, 0, JsonNodeGetRawNumber, 0, PH7_MEMBER_PUBLIC },
{ "IsNull", JTY(BOOL), 0, 0, JsonNodeIsNull, 0, PH7_MEMBER_PUBLIC }, { "IsNull", JTY(BOOL), 0, 0, JsonNodeIsNull, 0, PH7_MEMBER_PUBLIC },
{ "ToJson", JTY(STRING), aToJson, 1, JsonNodeToJson, 0, PH7_MEMBER_PUBLIC }, { "ToJson", JTY(STRING), tToJson, 1, JsonNodeToJson, 0, PH7_MEMBER_PUBLIC },
{ "__toString", JTY(STRING), 0, 0, JsonNodeToString, 0, 0 }, { "__toString", JTY(STRING), 0, 0, JsonNodeToString, 0, 0 },
}; };
static ph7_field_def aNodeField[] = { static const ph7_field_def tNodeField[] = {
{ "kind", JTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "kind", JTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
{ "path", JTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "path", JTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
{ "text", JTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "text", JTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
@@ -2938,7 +2938,7 @@ static sxi32 JsonRegisterClasses(ph7_vm *pVm) {
static const ph7_method_def aNullMethod[] = { static const ph7_method_def aNullMethod[] = {
{ "__construct", JTY(VOID), 0, 0, JsonNodeConstruct, (void *)(long)JSON_NODE_NULL_NODE, 0 } { "__construct", JTY(VOID), 0, 0, JsonNodeConstruct, (void *)(long)JSON_NODE_NULL_NODE, 0 }
}; };
static ph7_field_def aOptField[] = { static const ph7_field_def tOptField[] = {
{ "prettyPrint", JTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "prettyPrint", JTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 },
{ "unescapedUnicode", JTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "unescapedUnicode", JTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 },
{ "unescapedSlashes", JTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "unescapedSlashes", JTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 },
@@ -2990,21 +2990,37 @@ static sxi32 JsonRegisterClasses(ph7_vm *pVm) {
{ "GetIndent", JTY(STRING), 0, 0, JsonOptionsGet, (void *)"indent", PH7_MEMBER_PUBLIC }, { "GetIndent", JTY(STRING), 0, 0, JsonOptionsGet, (void *)"indent", PH7_MEMBER_PUBLIC },
{ "SetIndent", JTY(VOID), aSetStr, 1, JsonOptionsSetIndent, 0, PH7_MEMBER_PUBLIC }, { "SetIndent", JTY(VOID), aSetStr, 1, JsonOptionsSetIndent, 0, PH7_MEMBER_PUBLIC },
}; };
static const ph7_method_def aDocMethod[] = { static const ph7_method_def tDocMethod[] = {
{ "__construct", JTY(VOID), 0, 0, JsonDocumentConstruct, 0, 0 }, { "__construct", JTY(VOID), 0, 0, JsonDocumentConstruct, 0, 0 },
{ "Parse", JTY(VOID), aDocParse, 2, JsonDocumentParse, 0, PH7_MEMBER_PUBLIC }, { "Parse", JTY(VOID), tDocParse, 2, JsonDocumentParse, 0, PH7_MEMBER_PUBLIC },
{ "GetRoot", JNM("JsonNode"), 0, 0, JsonDocumentGetRoot, 0, PH7_MEMBER_PUBLIC }, { "GetRoot", JNM("JsonNode"), 0, 0, JsonDocumentGetRoot, 0, PH7_MEMBER_PUBLIC },
{ "SetRoot", JTY(VOID), aSerialize, 1, JsonDocumentSetRoot, 0, PH7_MEMBER_PUBLIC }, { "SetRoot", JTY(VOID), tSerialize, 1, JsonDocumentSetRoot, 0, PH7_MEMBER_PUBLIC },
{ "ToJson", JTY(STRING), aDocToJson, 1, JsonDocumentToJson, 0, PH7_MEMBER_PUBLIC }, { "ToJson", JTY(STRING), tDocToJson, 1, JsonDocumentToJson, 0, PH7_MEMBER_PUBLIC },
}; };
static const ph7_field_def aDocField[] = { static const ph7_field_def aDocField[] = {
{ "root", JNM("JsonNode"), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "root", JNM("JsonNode"), 0, PH7_MEMBER_PRIVATE, 0, 0 },
}; };
static const ph7_method_def aFacadeMethod[] = { static const ph7_method_def tFacadeMethod[] = {
{ "Parse", JNM("JsonDocument"), aParse, 2, JsonParse, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }, { "Parse", JNM("JsonDocument"), tParse, 2, JsonParse, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "ParseNode", JNM("JsonNode"), aParse, 2, JsonParseNode, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }, { "ParseNode", JNM("JsonNode"), tParse, 2, JsonParseNode, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Serialize", JTY(STRING), aSerialize, 2, JsonSerializeFn, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }, { "Serialize", JTY(STRING), tSerialize, 2, JsonSerializeFn, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
}; };
/* Mutable registration data lives on the stack; the templates stay const. */
ph7_constant_def aNodeTypeConst[SX_ARRAYSIZE(tNodeTypeConst)];
ph7_constant_def aExceptionConst[SX_ARRAYSIZE(tExceptionConst)];
ph7_constant_def aOptionsConst[SX_ARRAYSIZE(tOptionsConst)];
ph7_param_def aExcCtor[SX_ARRAYSIZE(tExcCtor)];
ph7_param_def aToJson[SX_ARRAYSIZE(tToJson)];
ph7_param_def aParse[SX_ARRAYSIZE(tParse)];
ph7_param_def aDocParse[SX_ARRAYSIZE(tDocParse)];
ph7_param_def aDocToJson[SX_ARRAYSIZE(tDocToJson)];
ph7_param_def aSerialize[SX_ARRAYSIZE(tSerialize)];
ph7_field_def aNodeField[SX_ARRAYSIZE(tNodeField)];
ph7_field_def aOptField[SX_ARRAYSIZE(tOptField)];
ph7_method_def aExcMethod[SX_ARRAYSIZE(tExcMethod)];
ph7_method_def aNodeMethod[SX_ARRAYSIZE(tNodeMethod)];
ph7_method_def aDocMethod[SX_ARRAYSIZE(tDocMethod)];
ph7_method_def aFacadeMethod[SX_ARRAYSIZE(tFacadeMethod)];
ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, JCLS_EXC, ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, JCLS_EXC,
aExcField, SX_ARRAYSIZE(aExcField), aExceptionConst, SX_ARRAYSIZE(aExceptionConst), aExcField, SX_ARRAYSIZE(aExcField), aExceptionConst, SX_ARRAYSIZE(aExceptionConst),
aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 }; aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 };
@@ -3049,6 +3065,43 @@ static sxi32 JsonRegisterClasses(ph7_vm *pVm) {
SyZero(apOptConst, sizeof(apOptConst)); SyZero(apOptConst, sizeof(apOptConst));
SyZero(apInit, sizeof(apInit)); SyZero(apInit, sizeof(apInit));
SyZero(apCtorDef, sizeof(apCtorDef)); SyZero(apCtorDef, sizeof(apCtorDef));
memcpy(aNodeTypeConst, tNodeTypeConst, sizeof(tNodeTypeConst));
memcpy(aExceptionConst, tExceptionConst, sizeof(tExceptionConst));
memcpy(aOptionsConst, tOptionsConst, sizeof(tOptionsConst));
memcpy(aExcCtor, tExcCtor, sizeof(tExcCtor));
memcpy(aToJson, tToJson, sizeof(tToJson));
memcpy(aParse, tParse, sizeof(tParse));
memcpy(aDocParse, tDocParse, sizeof(tDocParse));
memcpy(aDocToJson, tDocToJson, sizeof(tDocToJson));
memcpy(aSerialize, tSerialize, sizeof(tSerialize));
memcpy(aNodeField, tNodeField, sizeof(tNodeField));
memcpy(aOptField, tOptField, sizeof(tOptField));
memcpy(aExcMethod, tExcMethod, sizeof(tExcMethod));
memcpy(aNodeMethod, tNodeMethod, sizeof(tNodeMethod));
memcpy(aDocMethod, tDocMethod, sizeof(tDocMethod));
memcpy(aFacadeMethod, tFacadeMethod, sizeof(tFacadeMethod));
aExcMethod[0].aParam = aExcCtor;
for(n = 0 ; n < SX_ARRAYSIZE(aNodeMethod) ; ++n) {
if(aNodeMethod[n].xFunc == JsonNodeToJson) {
aNodeMethod[n].aParam = aToJson;
}
}
for(n = 0 ; n < SX_ARRAYSIZE(aDocMethod) ; ++n) {
if(aDocMethod[n].xFunc == JsonDocumentParse) {
aDocMethod[n].aParam = aDocParse;
} else if(aDocMethod[n].xFunc == JsonDocumentSetRoot) {
aDocMethod[n].aParam = aSerialize;
} else if(aDocMethod[n].xFunc == JsonDocumentToJson) {
aDocMethod[n].aParam = aDocToJson;
}
}
for(n = 0 ; n < SX_ARRAYSIZE(aFacadeMethod) ; ++n) {
if(aFacadeMethod[n].xFunc == JsonParse || aFacadeMethod[n].xFunc == JsonParseNode) {
aFacadeMethod[n].aParam = aParse;
} else if(aFacadeMethod[n].xFunc == JsonSerializeFn) {
aFacadeMethod[n].aParam = aSerialize;
}
}
/* Allocate constant and default snapshots. */ /* Allocate constant and default snapshots. */
for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) { for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) {
apExc[n] = ph7_new_scalar(pVm); apExc[n] = ph7_new_scalar(pVm);
+34 -10
View File
@@ -8,6 +8,7 @@
*/ */
#include <float.h> #include <float.h>
#include <math.h> #include <math.h>
#include <string.h>
#include "math.h" #include "math.h"
/* DBL_TRUE_MIN is C11; the module is compiled as C99 with GNU extensions. */ /* 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 */ /* Native class registration */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
static ph7_constant_def aMathConst[] = { static const ph7_constant_def tMathConst[] = {
{ "PI", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "PI", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "E", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "E", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "TAU", 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_PI, MATH_E, MATH_TAU, MATH_LN2, MATH_LN10,
MATH_LOG2E, MATH_LOG10E, MATH_SQRT2, MATH_SQRT1_2, MATH_EULER_GAMMA 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_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_ARGUMENT_OUT_OF_RANGE", 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 }, { "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 MATH_ERROR_OVERFLOW, MATH_ERROR_ARITHMETIC, MATH_ERROR_DIVIDE_BY_ZERO
}; };
static sxi32 MathRegisterClasses(ph7_vm *pVm) { static sxi32 MathRegisterClasses(ph7_vm *pVm) {
static ph7_param_def aExcCtor[] = { static const ph7_param_def tExcCtor[] = {
MP("message", STRING), MP("code", INT) MP("message", STRING), MP("code", INT)
}; };
static const ph7_param_def aExcDesc[] = { 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[] = { static const ph7_param_def aThree[] = {
MP("x", MIXED), MP("y", MIXED), MP("z", MIXED) 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) 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)) MP("value", MIXED), MPN("newBase", MTY(MIXED))
}; };
static const ph7_param_def aScaleB[] = { static const ph7_param_def aScaleB[] = {
MP("x", MIXED), MP("n", INT) MP("x", MIXED), MP("n", INT)
}; };
static const ph7_method_def aExcMethod[] = { static const ph7_method_def tExcMethod[] = {
{ "__construct", MTY(VOID), aExcCtor, 2, MathExceptionConstruct, 0, 0 }, { "__construct", MTY(VOID), tExcCtor, 2, MathExceptionConstruct, 0, 0 },
{ "GetDescription", MTY(STRING), aExcDesc, 1, MathExceptionDescription, 0, { "GetDescription", MTY(STRING), aExcDesc, 1, MathExceptionDescription, 0,
PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC } 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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "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 }, { "Truncate", MTY(FLOAT), aOne, 1, MathTruncate, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
}; };
static const char *const aExcBase[] = { "Exception" }; 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, ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, MCLS_EXC,
0, 0, aExceptionConst, SX_ARRAYSIZE(aExceptionConst), 0, 0, aExceptionConst, SX_ARRAYSIZE(aExceptionConst),
aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 }; 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(apMath, sizeof(apMath));
SyZero(apExc, sizeof(apExc)); SyZero(apExc, sizeof(apExc));
SyZero(apDefault, sizeof(apDefault)); 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 /* Constant and default snapshots. A fresh scalar is NULL, which is the
* exact default required for Log()'s optional newBase. */ * exact default required for Log()'s optional newBase. */
for(n = 0 ; n < SX_ARRAYSIZE(apMath) ; ++n) { for(n = 0 ; n < SX_ARRAYSIZE(apMath) ; ++n) {
+36 -13
View File
@@ -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) { static int SystemEnvName(ph7_context *pCtx, ph7_value *pArg, const char **pzName) {
const char *zData; const char *zData;
int nByte = 0; 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"); return SystemArgError(pCtx, "Environment variable name must be a string");
} }
zData = ph7_value_to_string(pArg, &nByte); 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) { static int System_Sleep(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_int64 iMs; 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])) { !ph7_value_is_int(apArg[0])) {
return SystemArgError(pCtx, "Sleep() expects an integer number of milliseconds"); 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) { static int System_Exit(ph7_context *pCtx, int nArg, ph7_value **apArg) {
sxi32 iStatus = 0; sxi32 iStatus = 0;
if(nArg > 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])) { !ph7_value_is_int(apArg[0])) {
return SystemArgError(pCtx, "Exit() expects an integer status code"); 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 SYSC(N) { N, 0, PH7_MEMBER_PUBLIC, 0, 0 }
#define SYSM(NAME,RET,PARAMS,NPARAM,FUNC) \ #define SYSM(NAME,RET,PARAMS,NPARAM,FUNC) \
{ NAME, STY(RET), PARAMS, NPARAM, FUNC, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC } { 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") 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 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 }, { "message", STY(STRING), 0, 0, 0, 0 },
{ "code", STY(INT), 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, /* 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[] = { 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 } { "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 /* MIXED so a wrong-typed status reaches System_Exit() and raises
* SystemException(ERROR_ARGUMENT) instead of a VM overload error. */ * SystemException(ERROR_ARGUMENT) instead of a VM overload error. */
static ph7_param_def aExitParam[] = { { "code", STY(MIXED), 0, 0, 0, 0 } }; static const ph7_param_def tExitParam[] = { { "code", STY(MIXED), 0, 0, 0, 0 } };
static const ph7_method_def aExcMethod[] = { static const ph7_method_def tExcMethod[] = {
{ "__construct", STY(VOID), aExcCtor, 2, SystemExceptionConstruct, 0, 0 } { "__construct", STY(VOID), tExcCtor, 2, SystemExceptionConstruct, 0, 0 }
}; };
static const ph7_method_def aMethodCommon[] = { static const ph7_method_def aMethodCommon[] = {
{ "__construct", STY(VOID), 0, 0, SystemConstruct, 0, PH7_MEMBER_PRIVATE }, { "__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("GetPeakMemoryUsage", INT, 0, 0, System_GetPeakMemoryUsage),
SYSM("GetMemoryLimit", INT, 0, 0, System_GetMemoryLimit), SYSM("GetMemoryLimit", INT, 0, 0, System_GetMemoryLimit),
SYSM("Sleep", VOID, aIntParam, 1, System_Sleep), 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[] = { static const ph7_method_def aMethodCli[] = {
{ "GetEnvironmentVariables", { PH7_TYPE_STRING, 0, 1 }, 0, 0, { "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) SYSM("GetOSVersion", STRING, 0, 0, System_GetOSVersion)
}; };
static const char *const aExcBase[] = { "Exception" }; 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_method_def aMethod[SX_ARRAYSIZE(aMethodCommon) + SX_ARRAYSIZE(aMethodCli)];
ph7_class_def sExc = { ph7_class_def sExc = {
sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, SCLS_EXC, sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, SCLS_EXC,
@@ -762,6 +774,11 @@ static sxi32 SystemRegisterClasses(ph7_vm *pVm) {
sxi32 rc = PH7_OK; sxi32 rc = PH7_OK;
SyZero(apConst, sizeof(apConst)); SyZero(apConst, sizeof(apConst));
SyZero(apDefault, sizeof(apDefault)); 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. */ /* Constant and default snapshots; ph7_create_class() deep-copies them. */
for(n = 0 ; n < SX_ARRAYSIZE(apConst) ; ++n) { for(n = 0 ; n < SX_ARRAYSIZE(apConst) ; ++n) {
apConst[n] = ph7_new_scalar(pVm); apConst[n] = ph7_new_scalar(pVm);
@@ -800,6 +817,12 @@ static sxi32 SystemRegisterClasses(ph7_vm *pVm) {
memcpy(aMethod + SX_ARRAYSIZE(aMethodCommon), aMethodCli, sizeof(aMethodCli)); memcpy(aMethod + SX_ARRAYSIZE(aMethodCommon), aMethodCli, sizeof(aMethodCli));
sSystem.nMethod += SX_ARRAYSIZE(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); rc = ph7_create_class(pVm, &sExc);
if(rc == PH7_OK) { if(rc == PH7_OK) {
rc = ph7_create_class(pVm, &sSystem); rc = ph7_create_class(pVm, &sSystem);
+25 -11
View File
@@ -8,6 +8,7 @@
*/ */
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "xml.h" #include "xml.h"
/* /*
@@ -826,7 +827,7 @@ static int XmlReaderParse(ph7_context *pCtx, int nArg, ph7_value **apArg) {
pEngine->nErrLine, pEngine->nErrCol); pEngine->nErrLine, pEngine->nErrCol);
} }
if(pEngine->bAbort) { 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 */ 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 XNM(N) { PH7_TYPE_NAMED, N, 0 }
#define XP(N,T) { N, XTY(T), 0, 0, 0, 0 } #define XP(N,T) { N, XTY(T), 0, 0, 0, 0 }
/* The pValue pointers are filled in per VM registration and cleared again /* Immutable template: each registration copies it to a local array and fills
* afterwards: these tables are static and must never retain a stale value. */ * those pValue pointers there, so no static object ever holds a VM value. */
static ph7_constant_def aExceptionConst[] = { static const ph7_constant_def tExceptionConst[] = {
{ "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_NO_MEMORY", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ERROR_NO_MEMORY", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_SYNTAX", 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 aReaderInt[] = { XP("index", INT) };
static const ph7_param_def aReaderString[] = { XP("name", STRING) }; static const ph7_param_def aReaderString[] = { XP("name", STRING) };
static const ph7_param_def aReaderPrefix[] = { XP("prefix", 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 aParse[] = { XP("data", MIXED) };
static const ph7_param_def aSetHandler[] = { { "handler", XNM("IXmlHandler"), 0, 0, 0, 0 } }; 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) XP("message", STRING), XP("code", INT), XP("inputLine", INT), XP("inputColumn", INT)
}; };
static const ph7_param_def aExcDesc[] = { XP("code", 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 } { "inputColumn", XTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 }
}; };
static const char *const aExcBase[] = { "Exception" }; static const char *const aExcBase[] = { "Exception" };
static const ph7_method_def aExcMethod[] = { static const ph7_method_def tExcMethod[] = {
{ "__construct", XTY(VOID), aExcCtor, 4, XmlExceptionConstruct, 0, 0 }, { "__construct", XTY(VOID), tExcCtor, 4, XmlExceptionConstruct, 0, 0 },
{ "GetInputLine", XTY(INT), 0, 0, XmlExceptionField, (void *)"inputLine", PH7_MEMBER_PUBLIC }, { "GetInputLine", XTY(INT), 0, 0, XmlExceptionField, (void *)"inputLine", PH7_MEMBER_PUBLIC },
{ "GetInputColumn", XTY(INT), 0, 0, XmlExceptionField, (void *)"inputColumn", PH7_MEMBER_PUBLIC }, { "GetInputColumn", XTY(INT), 0, 0, XmlExceptionField, (void *)"inputColumn", PH7_MEMBER_PUBLIC },
{ "GetDescription", XTY(STRING), aExcDesc, 1, XmlExceptionGetDescription, 0, { "GetDescription", XTY(STRING), aExcDesc, 1, XmlExceptionGetDescription, 0,
PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC } PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
}; };
/* XmlReader methods */ /* XmlReader methods */
static const ph7_method_def aReaderMethod[] = { static const ph7_method_def tReaderMethod[] = {
{ "__construct", XTY(VOID), aCtor, 1, XmlReaderConstruct, 0, 0 }, { "__construct", XTY(VOID), tCtor, 1, XmlReaderConstruct, 0, 0 },
{ "Parse", XTY(VOID), aParse, 1, XmlReaderParse, 0, PH7_MEMBER_PUBLIC }, { "Parse", XTY(VOID), aParse, 1, XmlReaderParse, 0, PH7_MEMBER_PUBLIC },
{ "Close", XTY(VOID), 0, 0, XmlReaderClose, 0, PH7_MEMBER_PUBLIC }, { "Close", XTY(VOID), 0, 0, XmlReaderClose, 0, PH7_MEMBER_PUBLIC },
{ "__destruct", XTY(VOID), 0, 0, XmlReaderDestruct, 0, 0 }, { "__destruct", XTY(VOID), 0, 0, XmlReaderDestruct, 0, 0 },
@@ -1164,6 +1165,12 @@ static sxi32 XmlRegisterClasses(ph7_vm *pVm) {
static const ph7_field_def aReaderField[] = { static const ph7_field_def aReaderField[] = {
{ "resource", XTY(RESOURCE), 0, PH7_MEMBER_PRIVATE, 0, 0 } { "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, ph7_class_def sIface = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION,
"IXmlHandler", 0, 0, 0, 0, aIfaceMethod, SX_ARRAYSIZE(aIfaceMethod), "IXmlHandler", 0, 0, 0, 0, aIfaceMethod, SX_ARRAYSIZE(aIfaceMethod),
PH7_DECL_INTERFACE, 0, 0, 0, 0, 0, 0 }; 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)]; ph7_value *apErrVal[SX_ARRAYSIZE(aExceptionConst)];
sxu32 n; sxu32 n;
sxi32 rc; 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) { for(n = 0 ; n < SX_ARRAYSIZE(aDefault) ; ++n) {
aDefault[n] = ph7_new_scalar(pVm); aDefault[n] = ph7_new_scalar(pVm);
if(aDefault[n] == 0) { if(aDefault[n] == 0) {
@@ -1219,7 +1233,7 @@ static sxi32 XmlRegisterClasses(ph7_vm *pVm) {
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sReader); if(rc == PH7_OK) rc = ph7_create_class(pVm, &sReader);
for(n = 0 ; n < SX_ARRAYSIZE(aExceptionConst) ; ++n) { for(n = 0 ; n < SX_ARRAYSIZE(aExceptionConst) ; ++n) {
ph7_release_value(pVm, apErrVal[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; aCtor[0].pDefault = 0;
aExcCtor[2].pDefault = 0; aExcCtor[2].pDefault = 0;
+78 -26
View File
@@ -5272,7 +5272,7 @@ static int YamlSerializeAllFn(ph7_context *pCtx, int nArg, ph7_value **apArg) {
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Native class registration */ /* Native class registration */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
static ph7_constant_def aNodeTypeConst[] = { static const ph7_constant_def tNodeTypeConst[] = {
{ "MAPPING", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "MAPPING", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "SEQUENCE", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "SEQUENCE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "STRING", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "STRING", 0, PH7_MEMBER_PUBLIC, 0, 0 },
@@ -5285,7 +5285,7 @@ static const int aNodeTypeValue[] = {
YAML_NODE_MAPPING, YAML_NODE_SEQUENCE, YAML_NODE_STRING, YAML_NODE_INT, YAML_NODE_MAPPING, YAML_NODE_SEQUENCE, YAML_NODE_STRING, YAML_NODE_INT,
YAML_NODE_FLOAT, YAML_NODE_BOOLEAN, YAML_NODE_NULL_NODE YAML_NODE_FLOAT, YAML_NODE_BOOLEAN, YAML_NODE_NULL_NODE
}; };
static ph7_constant_def aExceptionConst[] = { static const ph7_constant_def tExceptionConst[] = {
{ "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_DEPTH", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ERROR_DEPTH", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_SYNTAX", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ERROR_SYNTAX", 0, PH7_MEMBER_PUBLIC, 0, 0 },
@@ -5310,7 +5310,7 @@ static const int aExceptionValue[] = {
YAML_ERROR_READONLY_OPTION, YAML_ERROR_UNDEFINED_ALIAS, YAML_ERROR_ALIAS_LIMIT, YAML_ERROR_READONLY_OPTION, YAML_ERROR_UNDEFINED_ALIAS, YAML_ERROR_ALIAS_LIMIT,
YAML_ERROR_UNKNOWN_TAG, YAML_ERROR_UNSUPPORTED, YAML_ERROR_ALIAS_NOT_ALLOWED YAML_ERROR_UNKNOWN_TAG, YAML_ERROR_UNSUPPORTED, YAML_ERROR_ALIAS_NOT_ALLOWED
}; };
static ph7_constant_def aOptionsConst[] = { static const ph7_constant_def tOptionsConst[] = {
{ "KEEP_LAST", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "KEEP_LAST", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "KEEP_FIRST", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "KEEP_FIRST", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "REJECT", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "REJECT", 0, PH7_MEMBER_PUBLIC, 0, 0 },
@@ -5330,7 +5330,7 @@ static const int aOptionsValue[] = {
}; };
static sxi32 YamlRegisterClasses(ph7_vm *pVm) { static sxi32 YamlRegisterClasses(ph7_vm *pVm) {
static ph7_param_def aExcCtor[] = { static const ph7_param_def tExcCtor[] = {
YP("message", STRING), YP("code", INT), YPN("inputLine", YTY(INT)), YP("message", STRING), YP("code", INT), YPN("inputLine", YTY(INT)),
YPN("inputColumn", YTY(INT)), YPN("path", YTY(STRING)) YPN("inputColumn", YTY(INT)), YPN("path", YTY(STRING))
}; };
@@ -5348,34 +5348,34 @@ static sxi32 YamlRegisterClasses(ph7_vm *pVm) {
static const ph7_param_def aNodeCtorInt[] = { YP("value", INT) }; static const ph7_param_def aNodeCtorInt[] = { YP("value", INT) };
static const ph7_param_def aNodeCtorFloat[] = { YP("value", FLOAT) }; static const ph7_param_def aNodeCtorFloat[] = { YP("value", FLOAT) };
static const ph7_param_def aNodeCtorBool[] = { YP("value", BOOL) }; static const ph7_param_def aNodeCtorBool[] = { YP("value", BOOL) };
static ph7_param_def aToYaml[] = { YPO("options", YNM(YCLS_OPTIONS), 0) }; static const ph7_param_def tToYaml[] = { YPO("options", YNM(YCLS_OPTIONS), 0) };
static const ph7_param_def aSetBool[] = { YP("value", BOOL) }; static const ph7_param_def aSetBool[] = { YP("value", BOOL) };
static const ph7_param_def aSetInt[] = { YP("value", INT) }; static const ph7_param_def aSetInt[] = { YP("value", INT) };
static ph7_param_def aParse[] = { static const ph7_param_def tParse[] = {
YP("text", STRING), YPO("options", YNM(YCLS_OPTIONS), 0) YP("text", STRING), YPO("options", YNM(YCLS_OPTIONS), 0)
}; };
static ph7_param_def aSetRoot[] = { YPN("node", YNM(YCLS_NODE)) }; static const ph7_param_def aSetRoot[] = { YPN("node", YNM(YCLS_NODE)) };
static ph7_param_def aSerialize[] = { static const ph7_param_def tSerialize[] = {
YPN("node", YNM(YCLS_NODE)), YPO("options", YNM(YCLS_OPTIONS), 0) YPN("node", YNM(YCLS_NODE)), YPO("options", YNM(YCLS_OPTIONS), 0)
}; };
static ph7_param_def aSerializeAll[] = { static const ph7_param_def tSerializeAll[] = {
YPN("documents", YAR(YCLS_DOC)), YPO("options", YNM(YCLS_OPTIONS), 0) YPN("documents", YAR(YCLS_DOC)), YPO("options", YNM(YCLS_OPTIONS), 0)
}; };
static const ph7_method_def aExcMethod[] = { static const ph7_method_def tExcMethod[] = {
{ "__construct", YTY(VOID), aExcCtor, 5, YamlExceptionConstruct, 0, 0 }, { "__construct", YTY(VOID), tExcCtor, 5, YamlExceptionConstruct, 0, 0 },
{ "GetInputLine", YTY(INT), 0, 0, YamlExceptionField, (void *)"inputLine", PH7_MEMBER_PUBLIC }, { "GetInputLine", YTY(INT), 0, 0, YamlExceptionField, (void *)"inputLine", PH7_MEMBER_PUBLIC },
{ "GetInputColumn", YTY(INT), 0, 0, YamlExceptionField, (void *)"inputColumn", PH7_MEMBER_PUBLIC }, { "GetInputColumn", YTY(INT), 0, 0, YamlExceptionField, (void *)"inputColumn", PH7_MEMBER_PUBLIC },
{ "GetPath", YTY(STRING), 0, 0, YamlExceptionField, (void *)"path", PH7_MEMBER_PUBLIC }, { "GetPath", YTY(STRING), 0, 0, YamlExceptionField, (void *)"path", PH7_MEMBER_PUBLIC },
{ "GetDescription", YTY(STRING), aExcDesc, 1, YamlExceptionDescription, 0, { "GetDescription", YTY(STRING), aExcDesc, 1, YamlExceptionDescription, 0,
PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC } PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
}; };
static ph7_field_def aExcField[] = { static const ph7_field_def aExcField[] = {
{ "inputLine", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "inputLine", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
{ "inputColumn", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "inputColumn", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
{ "path", YTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "path", YTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
}; };
static const char *const aExcBase[] = { "Exception" }; static const char *const aExcBase[] = { "Exception" };
static const ph7_method_def aNodeMethod[] = { static const ph7_method_def tNodeMethod[] = {
{ "GetNodeType", YTY(INT), 0, 0, YamlNodeGetIntField, (void *)"kind", PH7_MEMBER_PUBLIC }, { "GetNodeType", YTY(INT), 0, 0, YamlNodeGetIntField, (void *)"kind", PH7_MEMBER_PUBLIC },
{ "GetPath", YTY(STRING), 0, 0, YamlNodeGetStrField, (void *)"path", PH7_MEMBER_PUBLIC }, { "GetPath", YTY(STRING), 0, 0, YamlNodeGetStrField, (void *)"path", PH7_MEMBER_PUBLIC },
{ "GetDepth", YTY(INT), 0, 0, YamlNodeGetDepth, 0, PH7_MEMBER_PUBLIC }, { "GetDepth", YTY(INT), 0, 0, YamlNodeGetDepth, 0, PH7_MEMBER_PUBLIC },
@@ -5398,10 +5398,10 @@ static sxi32 YamlRegisterClasses(ph7_vm *pVm) {
{ "GetBool", YTY(BOOL), 0, 0, YamlNodeGetBool, 0, PH7_MEMBER_PUBLIC }, { "GetBool", YTY(BOOL), 0, 0, YamlNodeGetBool, 0, PH7_MEMBER_PUBLIC },
{ "GetRawValue", YTY(STRING), 0, 0, YamlNodeGetRawValue, 0, PH7_MEMBER_PUBLIC }, { "GetRawValue", YTY(STRING), 0, 0, YamlNodeGetRawValue, 0, PH7_MEMBER_PUBLIC },
{ "IsNull", YTY(BOOL), 0, 0, YamlNodeIsNull, 0, PH7_MEMBER_PUBLIC }, { "IsNull", YTY(BOOL), 0, 0, YamlNodeIsNull, 0, PH7_MEMBER_PUBLIC },
{ "ToYaml", YTY(STRING), aToYaml, 1, YamlNodeToYaml, 0, PH7_MEMBER_PUBLIC }, { "ToYaml", YTY(STRING), tToYaml, 1, YamlNodeToYaml, 0, PH7_MEMBER_PUBLIC },
{ "__toString", YTY(STRING), 0, 0, YamlNodeToString, 0, 0 }, { "__toString", YTY(STRING), 0, 0, YamlNodeToString, 0, 0 },
}; };
static ph7_field_def aNodeField[] = { static const ph7_field_def tNodeField[] = {
{ "kind", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "kind", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
{ "path", YTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "path", YTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
{ "text", YTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "text", YTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
@@ -5436,7 +5436,7 @@ static sxi32 YamlRegisterClasses(ph7_vm *pVm) {
static const ph7_method_def aNullMethod[] = { static const ph7_method_def aNullMethod[] = {
{ "__construct", YTY(VOID), 0, 0, YamlNodeConstruct, (void *)(long)YAML_NODE_NULL_NODE, 0 } { "__construct", YTY(VOID), 0, 0, YamlNodeConstruct, (void *)(long)YAML_NODE_NULL_NODE, 0 }
}; };
static ph7_field_def aOptField[] = { static const ph7_field_def tOptField[] = {
{ "allowUnicode", YTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "allowUnicode", YTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 },
{ "allowAliases", YTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "allowAliases", YTY(BOOL), 0, PH7_MEMBER_PRIVATE, 0, 0 },
{ "maxAliasExpansion", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "maxAliasExpansion", YTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
@@ -5491,23 +5491,38 @@ static sxi32 YamlRegisterClasses(ph7_vm *pVm) {
{ "GetIndent", YTY(INT), 0, 0, YamlOptionsGet, (void *)"indent", PH7_MEMBER_PUBLIC }, { "GetIndent", YTY(INT), 0, 0, YamlOptionsGet, (void *)"indent", PH7_MEMBER_PUBLIC },
{ "SetIndent", YTY(VOID), aSetInt, 1, YamlOptionsSetInt, (void *)"indent", PH7_MEMBER_PUBLIC }, { "SetIndent", YTY(VOID), aSetInt, 1, YamlOptionsSetInt, (void *)"indent", PH7_MEMBER_PUBLIC },
}; };
static const ph7_method_def aDocMethod[] = { static const ph7_method_def tDocMethod[] = {
{ "__construct", YTY(VOID), 0, 0, YamlDocumentConstruct, 0, 0 }, { "__construct", YTY(VOID), 0, 0, YamlDocumentConstruct, 0, 0 },
{ "Parse", YTY(VOID), aParse, 2, YamlDocumentParse, 0, PH7_MEMBER_PUBLIC }, { "Parse", YTY(VOID), tParse, 2, YamlDocumentParse, 0, PH7_MEMBER_PUBLIC },
{ "GetRoot", YNM(YCLS_NODE), 0, 0, YamlDocumentGetRoot, 0, PH7_MEMBER_PUBLIC }, { "GetRoot", YNM(YCLS_NODE), 0, 0, YamlDocumentGetRoot, 0, PH7_MEMBER_PUBLIC },
{ "SetRoot", YTY(VOID), aSetRoot, 1, YamlDocumentSetRoot, 0, PH7_MEMBER_PUBLIC }, { "SetRoot", YTY(VOID), aSetRoot, 1, YamlDocumentSetRoot, 0, PH7_MEMBER_PUBLIC },
{ "ToYaml", YTY(STRING), aToYaml, 1, YamlDocumentToYaml, 0, PH7_MEMBER_PUBLIC }, { "ToYaml", YTY(STRING), tToYaml, 1, YamlDocumentToYaml, 0, PH7_MEMBER_PUBLIC },
}; };
static ph7_field_def aDocField[] = { static const ph7_field_def aDocField[] = {
{ "root", YNM(YCLS_NODE), 0, PH7_MEMBER_PRIVATE, 0, 0 }, { "root", YNM(YCLS_NODE), 0, PH7_MEMBER_PRIVATE, 0, 0 },
}; };
static const ph7_method_def aFacadeMethod[] = { static const ph7_method_def tFacadeMethod[] = {
{ "Parse", YNM(YCLS_DOC), aParse, 2, YamlParse, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }, { "Parse", YNM(YCLS_DOC), tParse, 2, YamlParse, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "ParseAll", YAR(YCLS_DOC), aParse, 2, YamlParseAll, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }, { "ParseAll", YAR(YCLS_DOC), tParse, 2, YamlParseAll, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "ParseNode", YNM(YCLS_NODE), aParse, 2, YamlParseNode, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }, { "ParseNode", YNM(YCLS_NODE), tParse, 2, YamlParseNode, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "Serialize", YTY(STRING), aSerialize, 2, YamlSerializeFn, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }, { "Serialize", YTY(STRING), tSerialize, 2, YamlSerializeFn, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
{ "SerializeAll", YTY(STRING), aSerializeAll, 2, YamlSerializeAllFn, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }, { "SerializeAll", YTY(STRING), tSerializeAll, 2, YamlSerializeAllFn, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC },
}; };
/* Mutable registration data lives on the stack; the templates stay const. */
ph7_constant_def aNodeTypeConst[SX_ARRAYSIZE(tNodeTypeConst)];
ph7_constant_def aExceptionConst[SX_ARRAYSIZE(tExceptionConst)];
ph7_constant_def aOptionsConst[SX_ARRAYSIZE(tOptionsConst)];
ph7_param_def aExcCtor[SX_ARRAYSIZE(tExcCtor)];
ph7_param_def aToYaml[SX_ARRAYSIZE(tToYaml)];
ph7_param_def aParse[SX_ARRAYSIZE(tParse)];
ph7_param_def aSerialize[SX_ARRAYSIZE(tSerialize)];
ph7_param_def aSerializeAll[SX_ARRAYSIZE(tSerializeAll)];
ph7_field_def aNodeField[SX_ARRAYSIZE(tNodeField)];
ph7_field_def aOptField[SX_ARRAYSIZE(tOptField)];
ph7_method_def aExcMethod[SX_ARRAYSIZE(tExcMethod)];
ph7_method_def aNodeMethod[SX_ARRAYSIZE(tNodeMethod)];
ph7_method_def aDocMethod[SX_ARRAYSIZE(tDocMethod)];
ph7_method_def aFacadeMethod[SX_ARRAYSIZE(tFacadeMethod)];
ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_EXC, ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, YCLS_EXC,
aExcField, SX_ARRAYSIZE(aExcField), aExceptionConst, SX_ARRAYSIZE(aExceptionConst), aExcField, SX_ARRAYSIZE(aExcField), aExceptionConst, SX_ARRAYSIZE(aExceptionConst),
aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 }; aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 };
@@ -5554,6 +5569,43 @@ static sxi32 YamlRegisterClasses(ph7_vm *pVm) {
SyZero(apOptConst, sizeof(apOptConst)); SyZero(apOptConst, sizeof(apOptConst));
SyZero(apInit, sizeof(apInit)); SyZero(apInit, sizeof(apInit));
SyZero(apCtorDef, sizeof(apCtorDef)); SyZero(apCtorDef, sizeof(apCtorDef));
memcpy(aNodeTypeConst, tNodeTypeConst, sizeof(tNodeTypeConst));
memcpy(aExceptionConst, tExceptionConst, sizeof(tExceptionConst));
memcpy(aOptionsConst, tOptionsConst, sizeof(tOptionsConst));
memcpy(aExcCtor, tExcCtor, sizeof(tExcCtor));
memcpy(aToYaml, tToYaml, sizeof(tToYaml));
memcpy(aParse, tParse, sizeof(tParse));
memcpy(aSerialize, tSerialize, sizeof(tSerialize));
memcpy(aSerializeAll, tSerializeAll, sizeof(tSerializeAll));
memcpy(aNodeField, tNodeField, sizeof(tNodeField));
memcpy(aOptField, tOptField, sizeof(tOptField));
memcpy(aExcMethod, tExcMethod, sizeof(tExcMethod));
memcpy(aNodeMethod, tNodeMethod, sizeof(tNodeMethod));
memcpy(aDocMethod, tDocMethod, sizeof(tDocMethod));
memcpy(aFacadeMethod, tFacadeMethod, sizeof(tFacadeMethod));
aExcMethod[0].aParam = aExcCtor;
for(n = 0 ; n < SX_ARRAYSIZE(aNodeMethod) ; ++n) {
if(aNodeMethod[n].xFunc == YamlNodeToYaml) {
aNodeMethod[n].aParam = aToYaml;
}
}
for(n = 0 ; n < SX_ARRAYSIZE(aDocMethod) ; ++n) {
if(aDocMethod[n].xFunc == YamlDocumentParse) {
aDocMethod[n].aParam = aParse;
} else if(aDocMethod[n].xFunc == YamlDocumentToYaml) {
aDocMethod[n].aParam = aToYaml;
}
}
for(n = 0 ; n < SX_ARRAYSIZE(aFacadeMethod) ; ++n) {
if(aFacadeMethod[n].xFunc == YamlParse || aFacadeMethod[n].xFunc == YamlParseAll ||
aFacadeMethod[n].xFunc == YamlParseNode) {
aFacadeMethod[n].aParam = aParse;
} else if(aFacadeMethod[n].xFunc == YamlSerializeFn) {
aFacadeMethod[n].aParam = aSerialize;
} else if(aFacadeMethod[n].xFunc == YamlSerializeAllFn) {
aFacadeMethod[n].aParam = aSerializeAll;
}
}
/* Allocate constant and default snapshots. */ /* Allocate constant and default snapshots. */
for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) { for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) {
apExc[n] = ph7_new_scalar(pVm); apExc[n] = ph7_new_scalar(pVm);