1094 lines
44 KiB
C
1094 lines
44 KiB
C
/**
|
|
* @PROJECT AerScript Interpreter
|
|
* @COPYRIGHT See COPYING in the top level directory
|
|
* @FILE modules/xml/xml.c
|
|
* @DESCRIPTION Typed XML SAX module for AerScript Interpreter
|
|
* @DEVELOPERS Symisc Systems <devel@symisc.net>
|
|
* Rafal Kupiec <belliash@codingworkshop.eu.org>
|
|
*/
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include "xml.h"
|
|
|
|
/*
|
|
* Security note: the underlying SAX engine never resolves external entities
|
|
* and installs no entity handlers. OnDoctype therefore exists solely so a
|
|
* host can apply a DTD policy: returning PH7_ABORT from the callback stops
|
|
* the document before the remainder is parsed (see lib.c ProcessXML). The
|
|
* internal subset is only recognised when '[' is the LAST non-whitespace
|
|
* character on the DOCTYPE line (immediately before the line break): the
|
|
* lexer stops scanning that line at '>' and records the last non-space byte.
|
|
* An inline subset such as <!DOCTYPE root [<!ELEMENT ...>]> is truncated at
|
|
* the first '>' and rejected. This is a pre-existing engine limitation.
|
|
*/
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Engine lifetime */
|
|
/* ------------------------------------------------------------------ */
|
|
static ph7_xml_engine *VmCreateXMLEngine(ph7_context *pCtx, int process_ns, int ns_sep) {
|
|
ph7_xml_engine *pEngine;
|
|
ph7_vm *pVm = pCtx->pVm;
|
|
sxu32 n;
|
|
pEngine = (ph7_xml_engine *)SyMemBackendAlloc(&pVm->sAllocator, sizeof(ph7_xml_engine));
|
|
if(pEngine == 0) {
|
|
return 0;
|
|
}
|
|
SyZero(pEngine, sizeof(ph7_xml_engine));
|
|
pEngine->pVm = pVm;
|
|
pEngine->pCtx = 0;
|
|
pEngine->ns_sep = ns_sep;
|
|
SyXMLParserInit(&pEngine->sParser, &pVm->sAllocator, process_ns ? SXML_ENABLE_NAMESPACE : 0);
|
|
SyBlobInit(&pEngine->sErr, &pVm->sAllocator);
|
|
PH7_MemObjInit(pVm, &pEngine->sParserValue);
|
|
PH7_MemObjInit(pVm, &pEngine->sReaderValue);
|
|
PH7_MemObjInit(pVm, &pEngine->sHandler);
|
|
PH7_MemObjInit(pVm, &pEngine->sAttrs);
|
|
PH7_MemObjInit(pVm, &pEngine->sLocalName);
|
|
PH7_MemObjInit(pVm, &pEngine->sNsUri);
|
|
PH7_MemObjInit(pVm, &pEngine->sPrefix);
|
|
for(n = 0 ; n < SX_ARRAYSIZE(pEngine->aCB) ; ++n) {
|
|
PH7_MemObjInit(pVm, &pEngine->aCB[n]);
|
|
}
|
|
ph7_value_resource(&pEngine->sParserValue, pEngine);
|
|
pEngine->iErrCode = SXML_ERROR_NONE;
|
|
pEngine->nMagic = XML_ENGINE_MAGIC;
|
|
return pEngine;
|
|
}
|
|
static void VmReleaseXMLEngine(ph7_xml_engine *pEngine) {
|
|
ph7_vm *pVm = pEngine->pVm;
|
|
sxu32 n;
|
|
SyBlobRelease(&pEngine->sErr);
|
|
SyXMLParserRelease(&pEngine->sParser);
|
|
PH7_MemObjRelease(&pEngine->sParserValue);
|
|
PH7_MemObjRelease(&pEngine->sReaderValue);
|
|
PH7_MemObjRelease(&pEngine->sHandler);
|
|
PH7_MemObjRelease(&pEngine->sAttrs);
|
|
PH7_MemObjRelease(&pEngine->sLocalName);
|
|
PH7_MemObjRelease(&pEngine->sNsUri);
|
|
PH7_MemObjRelease(&pEngine->sPrefix);
|
|
for(n = 0 ; n < SX_ARRAYSIZE(pEngine->aCB) ; ++n) {
|
|
PH7_MemObjRelease(&pEngine->aCB[n]);
|
|
}
|
|
pEngine->nMagic = 0x2621;
|
|
SyMemBackendFree(&pVm->sAllocator, pEngine);
|
|
}
|
|
/* Replace an owned string slot with a snapshot of the SAX raw text. */
|
|
static void XmlSetString(ph7_xml_engine *pEngine, ph7_value *pSlot, const char *zString, int nByte) {
|
|
PH7_MemObjRelease(pSlot);
|
|
PH7_MemObjInit(pEngine->pVm, pSlot);
|
|
if(zString == 0) {
|
|
zString = "";
|
|
nByte = 0;
|
|
}
|
|
if(nByte < 0) {
|
|
nByte = 0;
|
|
}
|
|
ph7_value_string(pSlot, zString, nByte);
|
|
}
|
|
/* Capture line/byte-offset/column of the event's token. Both the engine and
|
|
* the raw string point into the same input buffer, so this is valid for the
|
|
* whole callback; we store plain integers so it also survives Parse(). */
|
|
static void XmlTrackPos(ph7_xml_engine *pEngine, SyXMLRawStr *pRaw) {
|
|
const char *zInput = (const char *)pEngine->sParser.sLex.sStream.zInput;
|
|
const char *zEnd = (const char *)pEngine->sParser.sLex.sStream.zEnd;
|
|
const char *z = pRaw ? pRaw->zString : 0;
|
|
pEngine->nCurLine = pRaw ? pRaw->nLine : 0;
|
|
pEngine->iCurByte = 0;
|
|
pEngine->nCurCol = 0;
|
|
/* Some SAX strings are static literals (e.g. the empty default-namespace
|
|
* prefix in lib.c), not slices of the input buffer. Never subtract or scan
|
|
* across unrelated allocations. */
|
|
if(zInput == 0 || zEnd == 0 || z == 0 || z < zInput || z > zEnd) {
|
|
return;
|
|
}
|
|
pEngine->iCurByte = (sxi64)(z - zInput);
|
|
{
|
|
const char *p = z;
|
|
while(p > zInput && p[-1] != '\n') {
|
|
--p;
|
|
}
|
|
pEngine->nCurCol = (sxu32)(z - p);
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Callback dispatch */
|
|
/* ------------------------------------------------------------------ */
|
|
static sxi32 XmlPropagate(ph7_xml_engine *pEngine, ph7_value *pResult, sxi32 rc) {
|
|
if(rc == PH7_EXCEPTION) {
|
|
PH7_VmDeferTransfer(pEngine->pVm, rc, pResult);
|
|
} else {
|
|
PH7_MemObjRelease(pResult);
|
|
}
|
|
return rc;
|
|
}
|
|
static sxi32 XmlCallHandler(ph7_xml_engine *pEngine, const char *zMethod,
|
|
ph7_value **apArg, int nArg) {
|
|
ph7_value sResult;
|
|
ph7_class_instance *pThis;
|
|
ph7_class_method *pMeth;
|
|
sxi32 rc;
|
|
if(!ph7_value_is_object(&pEngine->sHandler)) {
|
|
return SXRET_OK;
|
|
}
|
|
pThis = (ph7_class_instance *)pEngine->sHandler.x.pOther;
|
|
if(pThis == 0) {
|
|
return SXRET_OK;
|
|
}
|
|
pMeth = PH7_ClassExtractMethod(pThis->pClass, zMethod, (sxu32)SyStrlen(zMethod));
|
|
if(pMeth == 0) {
|
|
return SXRET_OK;
|
|
}
|
|
PH7_MemObjInit(pEngine->pVm, &sResult);
|
|
rc = PH7_VmCallClassMethod(pEngine->pVm, pThis, pMeth, &sResult, nArg, apArg);
|
|
return XmlPropagate(pEngine, &sResult, rc);
|
|
}
|
|
static sxi32 XmlCallCallback(ph7_xml_engine *pEngine, int nSlot, ph7_value **apArg, int nArg) {
|
|
ph7_value *pCallback = &pEngine->aCB[nSlot];
|
|
ph7_value sResult;
|
|
sxi32 rc;
|
|
if(!PH7_VmIsCallable(pEngine->pVm, pCallback, 0)) {
|
|
return SXRET_OK;
|
|
}
|
|
PH7_MemObjInit(pEngine->pVm, &sResult);
|
|
rc = PH7_VmCallUserFunction(pEngine->pVm, pCallback, nArg, apArg, &sResult);
|
|
return XmlPropagate(pEngine, &sResult, rc);
|
|
}
|
|
/* A typed IXmlHandler wins over a convenience callback; both receive the
|
|
* current XmlReader object as their first argument. */
|
|
static sxi32 XmlNotify(ph7_xml_engine *pEngine, int nSlot, const char *zMethod,
|
|
ph7_value **apArg, int nArg) {
|
|
sxi32 rc;
|
|
if(ph7_value_is_object(&pEngine->sHandler)) {
|
|
rc = XmlCallHandler(pEngine, zMethod, apArg, nArg);
|
|
} else {
|
|
rc = XmlCallCallback(pEngine, nSlot, apArg, nArg);
|
|
}
|
|
if(rc != PH7_OK) {
|
|
pEngine->bAbort = TRUE; /* PH7_ABORT / PH7_EXCEPTION from a user handler */
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* SAX values */
|
|
/* ------------------------------------------------------------------ */
|
|
static ph7_value *VmXMLValue(ph7_xml_engine *pEngine, SyXMLRawStr *pXML, SyXMLRawStr *pNsUri) {
|
|
ph7_value *pValue = ph7_context_new_scalar(pEngine->pCtx);
|
|
if(pValue == 0) {
|
|
PH7_VmMemoryError(pEngine->pCtx->pVm);
|
|
return 0;
|
|
}
|
|
if(pNsUri && pNsUri->nByte > 0) {
|
|
/* Dead in the typed API: every caller passes pNsUri == 0. Kept only so
|
|
* the reserved ns_sep field remains self-consistent. */
|
|
ph7_value_string_format(pValue, "%.*s%c", pNsUri->nByte, pNsUri->zString, pEngine->ns_sep);
|
|
}
|
|
ph7_value_string(pValue, pXML->zString, (int)pXML->nByte);
|
|
return pValue;
|
|
}
|
|
static ph7_value *VmXMLAttrValue(ph7_xml_engine *pEngine, SyXMLRawStr *aAttr, sxu32 nAttr) {
|
|
ph7_value *pArray = ph7_context_new_array(pEngine->pCtx);
|
|
if(pArray == 0) {
|
|
PH7_VmMemoryError(pEngine->pCtx->pVm);
|
|
return 0;
|
|
}
|
|
if(nAttr > 0) {
|
|
ph7_value *pKey = ph7_context_new_scalar(pEngine->pCtx);
|
|
ph7_value *pValue = ph7_context_new_scalar(pEngine->pCtx);
|
|
sxu32 n;
|
|
if(pKey == 0 || pValue == 0) {
|
|
PH7_VmMemoryError(pEngine->pCtx->pVm);
|
|
return 0;
|
|
}
|
|
for(n = 0 ; n < nAttr ; n += 2) {
|
|
ph7_value_reset_string_cursor(pKey);
|
|
ph7_value_reset_string_cursor(pValue);
|
|
ph7_value_string(pKey, aAttr[n].zString, (int)aAttr[n].nByte);
|
|
ph7_value_string(pValue, aAttr[n + 1].zString, (int)aAttr[n + 1].nByte);
|
|
ph7_array_add_elem(pArray, pKey, pValue);
|
|
}
|
|
ph7_context_release_value(pEngine->pCtx, pKey);
|
|
ph7_context_release_value(pEngine->pCtx, pValue);
|
|
}
|
|
return pArray;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* SAX event callbacks */
|
|
/* ------------------------------------------------------------------ */
|
|
static sxi32 VmXMLStartElementHandler(SyXMLRawStr *pStart, SyXMLRawStr *pNS,
|
|
SyXMLRawStr *pPrefix, sxu32 nAttr, SyXMLRawStr *aAttr, int bEmpty, void *pUserData) {
|
|
ph7_xml_engine *pEngine = (ph7_xml_engine *)pUserData;
|
|
ph7_value *apArg[3];
|
|
ph7_value *pTag, *pAttr;
|
|
XmlTrackPos(pEngine, pStart);
|
|
XmlSetString(pEngine, &pEngine->sLocalName, pStart->zString, (int)pStart->nByte);
|
|
XmlSetString(pEngine, &pEngine->sNsUri, pNS ? pNS->zString : 0, pNS ? (int)pNS->nByte : 0);
|
|
XmlSetString(pEngine, &pEngine->sPrefix, pPrefix ? pPrefix->zString : 0, pPrefix ? (int)pPrefix->nByte : 0);
|
|
pEngine->bEmptyElement = bEmpty ? TRUE : FALSE;
|
|
++pEngine->nDepth;
|
|
pTag = VmXMLValue(pEngine, pStart, 0); /* local name only; never glue URI + separator */
|
|
pAttr = VmXMLAttrValue(pEngine, aAttr, nAttr);
|
|
if(pTag == 0 || pAttr == 0) {
|
|
return SXRET_OK;
|
|
}
|
|
PH7_MemObjRelease(&pEngine->sAttrs);
|
|
PH7_MemObjInit(pEngine->pVm, &pEngine->sAttrs);
|
|
(void)PH7_MemObjSnapshot(pAttr, &pEngine->sAttrs);
|
|
apArg[0] = &pEngine->sReaderValue;
|
|
apArg[1] = pTag;
|
|
apArg[2] = pAttr;
|
|
(void)XmlNotify(pEngine, PH7_XML_START_TAG, "OnStartElement", apArg, 3);
|
|
ph7_context_release_value(pEngine->pCtx, pTag);
|
|
ph7_context_release_value(pEngine->pCtx, pAttr);
|
|
return pEngine->bAbort ? SXERR_ABORT : SXRET_OK;
|
|
}
|
|
static sxi32 VmXMLEndElementHandler(SyXMLRawStr *pEnd, SyXMLRawStr *pNS,
|
|
SyXMLRawStr *pPrefix, void *pUserData) {
|
|
ph7_xml_engine *pEngine = (ph7_xml_engine *)pUserData;
|
|
ph7_value *apArg[2];
|
|
ph7_value *pTag;
|
|
XmlTrackPos(pEngine, pEnd);
|
|
XmlSetString(pEngine, &pEngine->sLocalName, pEnd->zString, (int)pEnd->nByte);
|
|
XmlSetString(pEngine, &pEngine->sNsUri, pNS ? pNS->zString : 0, pNS ? (int)pNS->nByte : 0);
|
|
XmlSetString(pEngine, &pEngine->sPrefix, pPrefix ? pPrefix->zString : 0,
|
|
pPrefix ? (int)pPrefix->nByte : 0);
|
|
pEngine->bEmptyElement = FALSE;
|
|
if(pEngine->nDepth > 0) {
|
|
--pEngine->nDepth;
|
|
}
|
|
pTag = VmXMLValue(pEngine, pEnd, 0);
|
|
if(pTag == 0) {
|
|
return SXRET_OK;
|
|
}
|
|
apArg[0] = &pEngine->sReaderValue;
|
|
apArg[1] = pTag;
|
|
(void)XmlNotify(pEngine, PH7_XML_END_TAG, "OnEndElement", apArg, 2);
|
|
ph7_context_release_value(pEngine->pCtx, pTag);
|
|
return pEngine->bAbort ? SXERR_ABORT : SXRET_OK;
|
|
}
|
|
static sxi32 VmXMLTextHandler(SyXMLRawStr *pText, void *pUserData) {
|
|
ph7_xml_engine *pEngine = (ph7_xml_engine *)pUserData;
|
|
ph7_value *apArg[2];
|
|
ph7_value *pData;
|
|
XmlTrackPos(pEngine, pText);
|
|
pData = VmXMLValue(pEngine, pText, 0);
|
|
if(pData == 0) {
|
|
return SXRET_OK;
|
|
}
|
|
apArg[0] = &pEngine->sReaderValue;
|
|
apArg[1] = pData;
|
|
(void)XmlNotify(pEngine, PH7_XML_CDATA, "OnText", apArg, 2);
|
|
ph7_context_release_value(pEngine->pCtx, pData);
|
|
return pEngine->bAbort ? SXERR_ABORT : SXRET_OK;
|
|
}
|
|
static sxi32 VmXMLPIHandler(SyXMLRawStr *pTargetStr, SyXMLRawStr *pDataStr, void *pUserData) {
|
|
ph7_xml_engine *pEngine = (ph7_xml_engine *)pUserData;
|
|
ph7_value *apArg[3];
|
|
ph7_value *pTarget, *pData;
|
|
XmlTrackPos(pEngine, pTargetStr);
|
|
pTarget = VmXMLValue(pEngine, pTargetStr, 0);
|
|
pData = VmXMLValue(pEngine, pDataStr, 0);
|
|
if(pTarget == 0 || pData == 0) {
|
|
return SXRET_OK;
|
|
}
|
|
apArg[0] = &pEngine->sReaderValue;
|
|
apArg[1] = pTarget;
|
|
apArg[2] = pData;
|
|
(void)XmlNotify(pEngine, PH7_XML_PI, "OnProcessingInstruction", apArg, 3);
|
|
ph7_context_release_value(pEngine->pCtx, pTarget);
|
|
ph7_context_release_value(pEngine->pCtx, pData);
|
|
return pEngine->bAbort ? SXERR_ABORT : SXRET_OK;
|
|
}
|
|
/* lib.c forwards (prefix, uri) in that order. */
|
|
static sxi32 VmXMLNSStartHandler(SyXMLRawStr *pPrefixStr, SyXMLRawStr *pUriStr, void *pUserData) {
|
|
ph7_xml_engine *pEngine = (ph7_xml_engine *)pUserData;
|
|
ph7_value *apArg[3];
|
|
ph7_value *pPrefix, *pUri;
|
|
XmlTrackPos(pEngine, pPrefixStr);
|
|
pPrefix = VmXMLValue(pEngine, pPrefixStr, 0);
|
|
pUri = VmXMLValue(pEngine, pUriStr, 0);
|
|
if(pPrefix == 0 || pUri == 0) {
|
|
return SXRET_OK;
|
|
}
|
|
apArg[0] = &pEngine->sReaderValue;
|
|
apArg[1] = pPrefix;
|
|
apArg[2] = pUri;
|
|
(void)XmlNotify(pEngine, PH7_XML_NS_START, "OnStartNamespace", apArg, 3);
|
|
ph7_context_release_value(pEngine->pCtx, pPrefix);
|
|
ph7_context_release_value(pEngine->pCtx, pUri);
|
|
return pEngine->bAbort ? SXERR_ABORT : SXRET_OK;
|
|
}
|
|
static sxi32 VmXMLNSEndHandler(SyXMLRawStr *pPrefixStr, void *pUserData) {
|
|
ph7_xml_engine *pEngine = (ph7_xml_engine *)pUserData;
|
|
ph7_value *apArg[2];
|
|
ph7_value *pPrefix;
|
|
XmlTrackPos(pEngine, pPrefixStr);
|
|
pPrefix = VmXMLValue(pEngine, pPrefixStr, 0);
|
|
if(pPrefix == 0) {
|
|
return SXRET_OK;
|
|
}
|
|
apArg[0] = &pEngine->sReaderValue;
|
|
apArg[1] = pPrefix;
|
|
(void)XmlNotify(pEngine, PH7_XML_NS_END, "OnEndNamespace", apArg, 2);
|
|
ph7_context_release_value(pEngine->pCtx, pPrefix);
|
|
return pEngine->bAbort ? SXERR_ABORT : SXRET_OK;
|
|
}
|
|
static sxi32 VmXMLDoctypeHandler(SyXMLRawStr *pRaw, void *pUserData) {
|
|
ph7_xml_engine *pEngine = (ph7_xml_engine *)pUserData;
|
|
ph7_value *apArg[2];
|
|
ph7_value *pText;
|
|
XmlTrackPos(pEngine, pRaw);
|
|
pText = VmXMLValue(pEngine, pRaw, 0); /* raw text is not NUL-terminated */
|
|
if(pText == 0) {
|
|
return SXRET_OK;
|
|
}
|
|
apArg[0] = &pEngine->sReaderValue;
|
|
apArg[1] = pText;
|
|
(void)XmlNotify(pEngine, PH7_XML_DOCTYPE, "OnDoctype", apArg, 2);
|
|
ph7_context_release_value(pEngine->pCtx, pText);
|
|
return pEngine->bAbort ? SXERR_ABORT : SXRET_OK;
|
|
}
|
|
static sxi32 VmXMLErrorHandler(const char *zMessage, sxi32 iErrCode, SyToken *pToken, void *pUserData) {
|
|
ph7_xml_engine *pEngine = (ph7_xml_engine *)pUserData;
|
|
pEngine->iErrCode = iErrCode;
|
|
pEngine->bError = TRUE;
|
|
SyBlobReset(&pEngine->sErr);
|
|
if(zMessage) {
|
|
SyBlobAppend(&pEngine->sErr, zMessage, (sxu32)SyStrlen(zMessage));
|
|
}
|
|
if(pToken) {
|
|
SyXMLRawStr sRaw;
|
|
sRaw.zString = SyStringData(&pToken->sData);
|
|
sRaw.nByte = SyStringLength(&pToken->sData);
|
|
sRaw.nLine = pToken->nLine;
|
|
XmlTrackPos(pEngine, &sRaw);
|
|
pEngine->nErrLine = pEngine->nCurLine;
|
|
pEngine->nErrCol = pEngine->nCurCol;
|
|
}
|
|
if(ph7_value_is_object(&pEngine->sHandler)) {
|
|
ph7_value *apArg[4];
|
|
ph7_value *pMsg = ph7_context_new_scalar(pEngine->pCtx);
|
|
ph7_value *pCode = ph7_context_new_scalar(pEngine->pCtx);
|
|
ph7_value *pLine = ph7_context_new_scalar(pEngine->pCtx);
|
|
if(pMsg && pCode && pLine) {
|
|
ph7_value_string(pMsg, SyBlobData(&pEngine->sErr), (int)SyBlobLength(&pEngine->sErr));
|
|
ph7_value_int64(pCode, pEngine->iErrCode);
|
|
ph7_value_int64(pLine, pEngine->nErrLine);
|
|
apArg[0] = &pEngine->sReaderValue;
|
|
apArg[1] = pMsg;
|
|
apArg[2] = pCode;
|
|
apArg[3] = pLine;
|
|
(void)XmlCallHandler(pEngine, "OnError", apArg, 4);
|
|
}
|
|
if(pMsg) ph7_context_release_value(pEngine->pCtx, pMsg);
|
|
if(pCode) ph7_context_release_value(pEngine->pCtx, pCode);
|
|
if(pLine) ph7_context_release_value(pEngine->pCtx, pLine);
|
|
}
|
|
/* Abort XML processing immediately. */
|
|
return SXERR_ABORT;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* XmlException support */
|
|
/* ------------------------------------------------------------------ */
|
|
static int XmlThrowException(ph7_context *pCtx, ph7_xml_engine *pEngine) {
|
|
ph7_vm *pVm = pCtx->pVm;
|
|
ph7_class *pClass;
|
|
ph7_class_instance *pInst = 0;
|
|
ph7_class_method *pCons;
|
|
ph7_value *pObj, *apArg[4];
|
|
ph7_value *pMsg, *pCode, *pLine, *pCol;
|
|
sxi32 rc;
|
|
if(pVm->iPendingTransfer) {
|
|
return PH7_EXCEPTION; /* a handler exception takes precedence */
|
|
}
|
|
pClass = PH7_VmExtractClass(pVm, "XmlException", sizeof("XmlException") - 1, FALSE);
|
|
if(pClass == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
pInst = PH7_NewClassInstance(pVm, pClass);
|
|
if(pInst == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
pCons = PH7_ClassExtractMethod(pClass, "__construct", sizeof("__construct") - 1);
|
|
pMsg = ph7_context_new_scalar(pCtx);
|
|
pCode = ph7_context_new_scalar(pCtx);
|
|
pLine = ph7_context_new_scalar(pCtx);
|
|
pCol = ph7_context_new_scalar(pCtx);
|
|
pObj = ph7_context_new_scalar(pCtx);
|
|
if(pMsg == 0 || pCode == 0 || pLine == 0 || pCol == 0 || pObj == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
ph7_value_string(pMsg, SyBlobData(&pEngine->sErr), (int)SyBlobLength(&pEngine->sErr));
|
|
ph7_value_int64(pCode, pEngine->iErrCode);
|
|
ph7_value_int64(pLine, pEngine->nErrLine);
|
|
ph7_value_int64(pCol, pEngine->nErrCol);
|
|
apArg[0] = pMsg;
|
|
apArg[1] = pCode;
|
|
apArg[2] = pLine;
|
|
apArg[3] = pCol;
|
|
if(pCons) {
|
|
rc = PH7_VmCallClassMethod(pVm, pInst, pCons, 0, 4, apArg);
|
|
if(pInst->iRef < 1) {
|
|
pInst->iRef = 1;
|
|
}
|
|
if(rc != SXRET_OK) {
|
|
return rc;
|
|
}
|
|
}
|
|
PH7_MemObjRelease(pObj);
|
|
pObj->x.pOther = pInst;
|
|
MemObjSetType(pObj, MEMOBJ_OBJ);
|
|
(void)ph7_result_value(pCtx, pObj);
|
|
return PH7_EXCEPTION;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Native class method callbacks */
|
|
/* ------------------------------------------------------------------ */
|
|
static int XmlHandlerNoop(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(pCtx);
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return PH7_OK; /* void result */
|
|
}
|
|
static int XmlExceptionConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
static const char *const azField[] = { "xmlMessage", "xmlCode", "xmlLine", "xmlColumn" };
|
|
ph7_value *pThis = ph7_context_this(pCtx);
|
|
sxi32 rc;
|
|
sxu32 n;
|
|
SXUNUSED(nArg);
|
|
for(n = 0 ; n < SX_ARRAYSIZE(azField) ; ++n) {
|
|
rc = ph7_context_attr_store(pCtx, pThis, 0, azField[n], apArg[n]);
|
|
if(rc != PH7_OK) {
|
|
return rc;
|
|
}
|
|
}
|
|
/* Mirror the three fields inherited Exception::getMessage()/getCode()/
|
|
* getLine() read, so catch(Exception $e) observes the same values as the
|
|
* typed getters. apArg[2] is the line; apArg[3] is the column. */
|
|
rc = ph7_context_attr_store(pCtx, pThis, 0, "message", apArg[0]);
|
|
if(rc != PH7_OK) { return rc; }
|
|
rc = ph7_context_attr_store(pCtx, pThis, 0, "code", apArg[1]);
|
|
if(rc != PH7_OK) { return rc; }
|
|
rc = ph7_context_attr_store(pCtx, pThis, 0, "line", apArg[2]);
|
|
if(rc != PH7_OK) { return rc; }
|
|
return PH7_OK;
|
|
}
|
|
static int XmlExceptionField(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_value *pValue = ph7_context_new_scalar(pCtx);
|
|
sxi32 rc;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
if(pValue == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
rc = ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0,
|
|
(const char *)ph7_context_user_data(pCtx), pValue);
|
|
return rc == PH7_OK ? ph7_result_value(pCtx, pValue) : rc;
|
|
}
|
|
static int XmlExceptionGetDescription(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int nErr = nArg > 0 ? ph7_value_to_int(apArg[0]) : -1;
|
|
switch(nErr) {
|
|
case SXML_ERROR_NONE: return ph7_result_string(pCtx, "Not an error", -1);
|
|
case SXML_ERROR_NO_MEMORY: return ph7_result_string(pCtx, "Out of memory", -1);
|
|
case SXML_ERROR_SYNTAX: return ph7_result_string(pCtx, "Syntax error", -1);
|
|
case SXML_ERROR_INVALID_TOKEN: return ph7_result_string(pCtx, "Unexpected token", -1);
|
|
case SXML_ERROR_TAG_MISMATCH: return ph7_result_string(pCtx, "Tag mismatch", -1);
|
|
case SXML_ERROR_DUPLICATE_ATTRIBUTE: return ph7_result_string(pCtx, "Duplicate attribute", -1);
|
|
case SXML_ERROR_MISPLACED_XML_PI: return ph7_result_string(pCtx, "Misplaced processing instruction", -1);
|
|
case SXML_ERROR_INCORRECT_ENCODING: return ph7_result_string(pCtx, "Incorrect encoding", -1);
|
|
case SXML_ERROR_NO_ELEMENTS: return ph7_result_string(pCtx, "No element found", -1);
|
|
case SXML_ERROR_UNCLOSED_TOKEN: return ph7_result_string(pCtx, "Unclosed token", -1);
|
|
case SXML_ERROR_PARTIAL_CHAR: return ph7_result_string(pCtx, "Partial character", -1);
|
|
case SXML_ERROR_JUNK_AFTER_DOC_ELEMENT: return ph7_result_string(pCtx, "Junk after document element", -1);
|
|
case SXML_ERROR_PARAM_ENTITY_REF: return ph7_result_string(pCtx, "Parameter entity reference", -1);
|
|
case SXML_ERROR_UNDEFINED_ENTITY: return ph7_result_string(pCtx, "Undefined entity", -1);
|
|
case SXML_ERROR_RECURSIVE_ENTITY_REF: return ph7_result_string(pCtx, "Recursive entity reference", -1);
|
|
case SXML_ERROR_ASYNC_ENTITY: return ph7_result_string(pCtx, "Asynchronous entity", -1);
|
|
case SXML_ERROR_BAD_CHAR_REF: return ph7_result_string(pCtx, "Bad character reference", -1);
|
|
case SXML_ERROR_BINARY_ENTITY_REF: return ph7_result_string(pCtx, "Binary entity reference", -1);
|
|
case SXML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF: return ph7_result_string(pCtx, "External entity reference in attribute", -1);
|
|
case SXML_ERROR_UNKNOWN_ENCODING: return ph7_result_string(pCtx, "Unknown encoding", -1);
|
|
case SXML_ERROR_UNCLOSED_CDATA_SECTION: return ph7_result_string(pCtx, "Unclosed CDATA section", -1);
|
|
case SXML_ERROR_EXTERNAL_ENTITY_HANDLING: return ph7_result_string(pCtx, "External entity handling", -1);
|
|
default: return ph7_result_string(pCtx, "Unknown error code", -1);
|
|
}
|
|
}
|
|
|
|
/* Locate the engine payload of an XmlReader receiver. */
|
|
static ph7_xml_engine *XmlReaderEngine(ph7_context *pCtx) {
|
|
ph7_value *pRes = ph7_context_new_scalar(pCtx);
|
|
ph7_xml_engine *pEngine = 0;
|
|
if(pRes == 0) {
|
|
return 0;
|
|
}
|
|
if(ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0, "resource", pRes) == PH7_OK &&
|
|
ph7_value_is_resource(pRes)) {
|
|
pEngine = (ph7_xml_engine *)ph7_value_to_resource(pRes);
|
|
if(IS_INVALID_XML_ENGINE(pEngine)) {
|
|
pEngine = 0;
|
|
}
|
|
}
|
|
ph7_context_release_value(pCtx, pRes);
|
|
return pEngine;
|
|
}
|
|
static int XmlReaderCreate(ph7_context *pCtx, int process_ns, int ns_sep) {
|
|
ph7_xml_engine *pEngine = VmCreateXMLEngine(pCtx, process_ns, ns_sep);
|
|
ph7_value *pRes;
|
|
if(pEngine == 0) {
|
|
PH7_VmMemoryError(pCtx->pVm);
|
|
return PH7_ABORT;
|
|
}
|
|
pRes = ph7_context_new_scalar(pCtx);
|
|
if(pRes == 0) {
|
|
VmReleaseXMLEngine(pEngine);
|
|
return PH7_ABORT;
|
|
}
|
|
ph7_value_resource(pRes, pEngine);
|
|
{
|
|
sxi32 rc = ph7_context_attr_store(pCtx, ph7_context_this(pCtx), 0, "resource", pRes);
|
|
if(rc != PH7_OK) {
|
|
ph7_value_resource(pRes, 0);
|
|
VmReleaseXMLEngine(pEngine);
|
|
ph7_context_release_value(pCtx, pRes);
|
|
return rc;
|
|
}
|
|
ph7_context_release_value(pCtx, pRes);
|
|
return PH7_OK;
|
|
}
|
|
}
|
|
static int XmlReaderConstruct0(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return XmlReaderCreate(pCtx, FALSE, ':');
|
|
}
|
|
static int XmlReaderConstruct2(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
/* $separator is accepted for API compatibility but reserved/ignored: the
|
|
* typed getters forward the QName components verbatim instead of gluing
|
|
* URI + separator + local name. */
|
|
int ns_sep = ':';
|
|
if(nArg > 1 && ph7_value_is_string(apArg[1])) {
|
|
const char *zSep = ph7_value_to_string(apArg[1], 0);
|
|
if(zSep && zSep[0] != 0) {
|
|
ns_sep = zSep[0];
|
|
}
|
|
}
|
|
return XmlReaderCreate(pCtx, ph7_value_to_bool(apArg[0]) ? TRUE : FALSE, ns_sep);
|
|
}
|
|
static int XmlReaderCloseCtx(ph7_context *pCtx) {
|
|
ph7_value *pRes = ph7_context_new_scalar(pCtx);
|
|
ph7_xml_engine *pEngine;
|
|
if(pRes == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
if(ph7_context_attr_value(pCtx, ph7_context_this(pCtx), 0, "resource", pRes) == PH7_OK &&
|
|
ph7_value_is_resource(pRes)) {
|
|
pEngine = (ph7_xml_engine *)ph7_value_to_resource(pRes);
|
|
if(!IS_INVALID_XML_ENGINE(pEngine)) {
|
|
VmReleaseXMLEngine(pEngine);
|
|
ph7_value_resource(pRes, 0); /* invalidate: future access is harmless */
|
|
(void)ph7_context_attr_store(pCtx, ph7_context_this(pCtx), 0, "resource", pRes);
|
|
}
|
|
}
|
|
ph7_context_release_value(pCtx, pRes);
|
|
return PH7_OK;
|
|
}
|
|
static int XmlReaderClose(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return XmlReaderCloseCtx(pCtx);
|
|
}
|
|
static int XmlReaderDestruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return XmlReaderCloseCtx(pCtx);
|
|
}
|
|
static int XmlReaderParse(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine;
|
|
SyXMLParser *pParser;
|
|
const char *zData;
|
|
int nByte;
|
|
SXUNUSED(nArg); /* isFinal stays ignored, as in the underlying engine */
|
|
if(nArg < 1 || !ph7_value_is_string(apArg[0])) {
|
|
return PH7_ABORT;
|
|
}
|
|
pEngine = XmlReaderEngine(pCtx);
|
|
if(pEngine == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
if(pEngine->iNest > 0) {
|
|
PH7_VmThrowError(pCtx->pVm, PH7_CTX_WARNING,
|
|
"Recursive call to %s, PH7 is returning false", ph7_function_name(pCtx));
|
|
return PH7_ABORT;
|
|
}
|
|
pEngine->pCtx = pCtx;
|
|
/* Reset all transient state, including after a previous abort. */
|
|
pEngine->iErrCode = SXML_ERROR_NONE;
|
|
pEngine->bError = FALSE;
|
|
pEngine->bAbort = FALSE;
|
|
pEngine->nErrLine = 0;
|
|
pEngine->nErrCol = 0;
|
|
pEngine->nDepth = 0;
|
|
pEngine->bEmptyElement = FALSE;
|
|
pEngine->nCurLine = 0;
|
|
pEngine->iCurByte = 0;
|
|
pEngine->nCurCol = 0;
|
|
SyBlobReset(&pEngine->sErr);
|
|
PH7_MemObjRelease(&pEngine->sReaderValue);
|
|
PH7_MemObjInit(pCtx->pVm, &pEngine->sReaderValue);
|
|
PH7_MemObjStore(ph7_context_this(pCtx), &pEngine->sReaderValue);
|
|
pParser = &pEngine->sParser;
|
|
/* SyLexTokenizeInput() appends to sToken and ProcessXML() restarts its
|
|
* cursor at zero once exhausted. Drop tokens from any previous Parse() so
|
|
* a reused reader does not replay stale events (whose sData pointed into
|
|
* the previous, possibly already released, input buffer). SyToken only
|
|
* borrows the input buffer, so resetting the set owns/frees nothing. */
|
|
SySetReset(&pParser->sToken);
|
|
SyXMLParserSetEventHandler(pParser, pEngine,
|
|
VmXMLStartElementHandler, VmXMLTextHandler, VmXMLErrorHandler, 0,
|
|
VmXMLEndElementHandler, VmXMLPIHandler, 0, VmXMLDoctypeHandler,
|
|
VmXMLNSStartHandler, VmXMLNSEndHandler);
|
|
zData = ph7_value_to_string(apArg[0], &nByte);
|
|
pEngine->iNest++;
|
|
SyXMLProcess(pParser, zData, (sxu32)nByte);
|
|
pEngine->iNest--;
|
|
PH7_MemObjRelease(&pEngine->sReaderValue);
|
|
PH7_MemObjInit(pCtx->pVm, &pEngine->sReaderValue);
|
|
if(pCtx->pVm->iPendingTransfer) {
|
|
return PH7_EXCEPTION; /* exception from a user handler */
|
|
}
|
|
if(pEngine->bError) {
|
|
return XmlThrowException(pCtx, pEngine);
|
|
}
|
|
if(pEngine->bAbort) {
|
|
return PH7_ABORT; /* e.g. exit() from OnDoctype rejects the document */
|
|
}
|
|
return PH7_OK; /* void */
|
|
}
|
|
static int XmlReaderCurrentLine(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return pEngine ? ph7_result_int64(pCtx, pEngine->nCurLine) : PH7_ABORT;
|
|
}
|
|
static int XmlReaderCurrentByteIndex(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return pEngine ? ph7_result_int64(pCtx, pEngine->iCurByte) : PH7_ABORT;
|
|
}
|
|
static int XmlReaderCurrentColumn(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return pEngine ? ph7_result_int64(pCtx, pEngine->nCurCol) : PH7_ABORT;
|
|
}
|
|
static int XmlReaderDepth(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return pEngine ? ph7_result_int64(pCtx, pEngine->nDepth) : PH7_ABORT;
|
|
}
|
|
static int XmlReaderIsEmptyElement(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
return pEngine ? ph7_result_bool(pCtx, pEngine->bEmptyElement) : PH7_ABORT;
|
|
}
|
|
static int XmlReaderStringField(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine;
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
pEngine = XmlReaderEngine(pCtx);
|
|
if(pEngine == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
return ph7_result_value(pCtx,
|
|
(ph7_value *)((char *)pEngine + (size_t)(uintptr_t)ph7_context_user_data(pCtx)));
|
|
}
|
|
static int XmlReaderGetAttributeCount(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
SXUNUSED(nArg);
|
|
SXUNUSED(apArg);
|
|
if(pEngine == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
return ph7_result_int64(pCtx, ph7_value_is_array(&pEngine->sAttrs) ? ph7_array_count(&pEngine->sAttrs) : 0);
|
|
}
|
|
struct XmlAttrWalk {
|
|
int nTarget;
|
|
int nCur;
|
|
ph7_value *pName;
|
|
ph7_value *pValue;
|
|
};
|
|
static int XmlAttrWalker(ph7_value *pKey, ph7_value *pValue, void *pUserData) {
|
|
struct XmlAttrWalk *pWalk = (struct XmlAttrWalk *)pUserData;
|
|
if(pWalk->nCur == pWalk->nTarget) {
|
|
PH7_MemObjStore(pKey, pWalk->pName);
|
|
PH7_MemObjStore(pValue, pWalk->pValue);
|
|
return 1; /* stop */
|
|
}
|
|
++pWalk->nCur;
|
|
return 0;
|
|
}
|
|
static int XmlReaderAttributeAt(ph7_context *pCtx, int nArg, ph7_value **apArg, int wantName) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
ph7_value *pName, *pValue, *pOut;
|
|
struct XmlAttrWalk sWalk;
|
|
SXUNUSED(nArg);
|
|
if(pEngine == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
pName = ph7_context_new_scalar(pCtx);
|
|
pValue = ph7_context_new_scalar(pCtx);
|
|
pOut = wantName ? pName : pValue;
|
|
if(pName == 0 || pValue == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
sWalk.nTarget = ph7_value_to_int(apArg[0]);
|
|
sWalk.nCur = 0;
|
|
sWalk.pName = pName;
|
|
sWalk.pValue = pValue;
|
|
if(ph7_value_is_array(&pEngine->sAttrs) && sWalk.nTarget >= 0) {
|
|
(void)ph7_array_walk(&pEngine->sAttrs, XmlAttrWalker, &sWalk);
|
|
}
|
|
return ph7_result_value(pCtx, pOut);
|
|
}
|
|
static int XmlReaderGetAttributeName(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
return XmlReaderAttributeAt(pCtx, nArg, apArg, TRUE);
|
|
}
|
|
static int XmlReaderGetAttributeValue(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
return XmlReaderAttributeAt(pCtx, nArg, apArg, FALSE);
|
|
}
|
|
static int XmlReaderGetAttribute(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
ph7_value *pValue;
|
|
int nLen;
|
|
const char *zName;
|
|
SXUNUSED(nArg);
|
|
if(pEngine == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
zName = ph7_value_to_string(apArg[0], &nLen);
|
|
if(ph7_context_status(pCtx) != PH7_OK) {
|
|
return ph7_context_status(pCtx);
|
|
}
|
|
pValue = ph7_value_is_array(&pEngine->sAttrs) ? ph7_array_fetch(&pEngine->sAttrs, zName, nLen) : 0;
|
|
return pValue ? ph7_result_value(pCtx, pValue) : ph7_result_string(pCtx, "", 0);
|
|
}
|
|
static int XmlReaderHasAttribute(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
ph7_value *pValue;
|
|
int nLen;
|
|
const char *zName;
|
|
SXUNUSED(nArg);
|
|
if(pEngine == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
zName = ph7_value_to_string(apArg[0], &nLen);
|
|
if(ph7_context_status(pCtx) != PH7_OK) {
|
|
return ph7_context_status(pCtx);
|
|
}
|
|
pValue = ph7_value_is_array(&pEngine->sAttrs) ? ph7_array_fetch(&pEngine->sAttrs, zName, nLen) : 0;
|
|
return ph7_result_bool(pCtx, pValue != 0);
|
|
}
|
|
static int XmlReaderLookupNamespace(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
SyHashEntry *pEntry;
|
|
const char *zPrefix;
|
|
int nLen;
|
|
SXUNUSED(nArg);
|
|
if(pEngine == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
zPrefix = ph7_value_to_string(apArg[0], &nLen);
|
|
if(ph7_context_status(pCtx) != PH7_OK) {
|
|
return ph7_context_status(pCtx);
|
|
}
|
|
if(nLen == 0) {
|
|
zPrefix = "Default";
|
|
nLen = sizeof("Default") - 1;
|
|
}
|
|
/* Visibility is limited to the current element: lib.c removes the hash
|
|
* entry in XMLnsUnlink() when the element closes. */
|
|
pEntry = SyHashGet(&pEngine->sParser.hns, zPrefix, (sxu32)nLen);
|
|
if(pEntry && pEntry->pUserData) {
|
|
SyXMLRawStr *pUri = (SyXMLRawStr *)pEntry->pUserData;
|
|
return ph7_result_string(pCtx, pUri->zString, (int)pUri->nByte);
|
|
}
|
|
return ph7_result_string(pCtx, "", 0);
|
|
}
|
|
static int XmlReaderSetHandler(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
SXUNUSED(nArg);
|
|
if(pEngine == 0) {
|
|
return PH7_ABORT;
|
|
}
|
|
PH7_MemObjRelease(&pEngine->sHandler);
|
|
PH7_MemObjInit(pCtx->pVm, &pEngine->sHandler);
|
|
PH7_MemObjStore(apArg[0], &pEngine->sHandler);
|
|
return PH7_OK;
|
|
}
|
|
static int XmlReaderSetCallback(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
int nSlot = (int)(long)ph7_context_user_data(pCtx);
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
SXUNUSED(nArg);
|
|
if(pEngine == 0 || nSlot < 0 || nSlot >= XML_TOTAL_HANDLER) {
|
|
return PH7_ABORT;
|
|
}
|
|
PH7_MemObjRelease(&pEngine->aCB[nSlot]);
|
|
PH7_MemObjInit(pCtx->pVm, &pEngine->aCB[nSlot]);
|
|
PH7_MemObjStore(apArg[0], &pEngine->aCB[nSlot]);
|
|
return PH7_OK;
|
|
}
|
|
static int XmlReaderClearHandler(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
if(pEngine == 0) return PH7_ABORT;
|
|
PH7_MemObjRelease(&pEngine->sHandler);
|
|
PH7_MemObjInit(pCtx->pVm, &pEngine->sHandler);
|
|
return PH7_OK; /* void */
|
|
}
|
|
static int XmlReaderClearCallbacks(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
|
ph7_xml_engine *pEngine = XmlReaderEngine(pCtx);
|
|
sxu32 n;
|
|
SXUNUSED(nArg); SXUNUSED(apArg);
|
|
if(pEngine == 0) return PH7_ABORT;
|
|
for(n = 0 ; n < SX_ARRAYSIZE(pEngine->aCB) ; ++n) {
|
|
PH7_MemObjRelease(&pEngine->aCB[n]);
|
|
PH7_MemObjInit(pCtx->pVm, &pEngine->aCB[n]);
|
|
}
|
|
return PH7_OK; /* void */
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Native class registration */
|
|
/* ------------------------------------------------------------------ */
|
|
#define XTY(T) { PH7_TYPE_##T, 0, 0 }
|
|
#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[] = {
|
|
{ "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 },
|
|
{ "ERROR_NO_ELEMENTS", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_INVALID_TOKEN", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_UNCLOSED_TOKEN", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_PARTIAL_CHAR", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_TAG_MISMATCH", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_DUPLICATE_ATTRIBUTE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_JUNK_AFTER_DOC_ELEMENT", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_PARAM_ENTITY_REF", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_UNDEFINED_ENTITY", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_RECURSIVE_ENTITY_REF", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_ASYNC_ENTITY", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_BAD_CHAR_REF", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_BINARY_ENTITY_REF", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_MISPLACED_XML_PI", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_UNKNOWN_ENCODING", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_INCORRECT_ENCODING", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_UNCLOSED_CDATA_SECTION", 0, PH7_MEMBER_PUBLIC, 0, 0 },
|
|
{ "ERROR_EXTERNAL_ENTITY_HANDLING", 0, PH7_MEMBER_PUBLIC, 0, 0 }
|
|
};
|
|
/* Same order as aExceptionConst and enum xml_err_code. */
|
|
static const int aExceptionConstValue[] = {
|
|
SXML_ERROR_NONE, SXML_ERROR_NO_MEMORY, SXML_ERROR_SYNTAX, SXML_ERROR_NO_ELEMENTS,
|
|
SXML_ERROR_INVALID_TOKEN, SXML_ERROR_UNCLOSED_TOKEN, SXML_ERROR_PARTIAL_CHAR,
|
|
SXML_ERROR_TAG_MISMATCH, SXML_ERROR_DUPLICATE_ATTRIBUTE,
|
|
SXML_ERROR_JUNK_AFTER_DOC_ELEMENT, SXML_ERROR_PARAM_ENTITY_REF,
|
|
SXML_ERROR_UNDEFINED_ENTITY, SXML_ERROR_RECURSIVE_ENTITY_REF, SXML_ERROR_ASYNC_ENTITY,
|
|
SXML_ERROR_BAD_CHAR_REF, SXML_ERROR_BINARY_ENTITY_REF,
|
|
SXML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF, SXML_ERROR_MISPLACED_XML_PI,
|
|
SXML_ERROR_UNKNOWN_ENCODING, SXML_ERROR_INCORRECT_ENCODING,
|
|
SXML_ERROR_UNCLOSED_CDATA_SECTION, SXML_ERROR_EXTERNAL_ENTITY_HANDLING
|
|
};
|
|
|
|
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 aCtor2[] = {
|
|
XP("namespaces", BOOL), { "separator", XTY(STRING), 0, 0, 0, 0 }
|
|
};
|
|
static ph7_param_def aParse[] = {
|
|
XP("data", STRING), { "isFinal", XTY(BOOL), 0, 0, 0, 0 }
|
|
};
|
|
static const ph7_param_def aSetHandler[] = { { "handler", XNM("IXmlHandler"), 0, 0, 0, 0 } };
|
|
static const ph7_param_def aCallback[] = { XP("fn", CALLBACK) };
|
|
static const ph7_param_def aExcCtor[] = {
|
|
XP("message", STRING), XP("code", INT), XP("line", INT), XP("column", INT)
|
|
};
|
|
static const ph7_param_def aExcDesc[] = { XP("code", INT) };
|
|
/* IXmlHandler signatures */
|
|
static const ph7_param_def aOnStart[] = {
|
|
{ "reader", XNM("XmlReader"), 0, 0, 0, 0 },
|
|
XP("name", STRING), { "attributes", { PH7_TYPE_STRING, 0, 1 }, 0, 0, 0, 0 }
|
|
};
|
|
static const ph7_param_def aOnEnd[] = {
|
|
{ "reader", XNM("XmlReader"), 0, 0, 0, 0 }, XP("name", STRING)
|
|
};
|
|
static const ph7_param_def aOnText[] = {
|
|
{ "reader", XNM("XmlReader"), 0, 0, 0, 0 }, XP("text", STRING)
|
|
};
|
|
static const ph7_param_def aOnPI[] = {
|
|
{ "reader", XNM("XmlReader"), 0, 0, 0, 0 },
|
|
XP("target", STRING), XP("data", STRING)
|
|
};
|
|
static const ph7_param_def aOnNSStart[] = {
|
|
{ "reader", XNM("XmlReader"), 0, 0, 0, 0 },
|
|
XP("prefix", STRING), XP("uri", STRING)
|
|
};
|
|
static const ph7_param_def aOnNSEnd[] = {
|
|
{ "reader", XNM("XmlReader"), 0, 0, 0, 0 }, XP("prefix", STRING)
|
|
};
|
|
static const ph7_param_def aOnDoctype[] = {
|
|
{ "reader", XNM("XmlReader"), 0, 0, 0, 0 }, XP("raw", STRING)
|
|
};
|
|
static const ph7_param_def aOnError[] = {
|
|
{ "reader", XNM("XmlReader"), 0, 0, 0, 0 },
|
|
XP("message", STRING), XP("code", INT), XP("line", INT)
|
|
};
|
|
/* Interface: public nonstatic signatures, no callbacks. */
|
|
static const ph7_method_def aIfaceMethod[] = {
|
|
{ "OnStartElement", XTY(VOID), aOnStart, 3, 0, 0, 0 },
|
|
{ "OnEndElement", XTY(VOID), aOnEnd, 2, 0, 0, 0 },
|
|
{ "OnText", XTY(VOID), aOnText, 2, 0, 0, 0 },
|
|
{ "OnProcessingInstruction", XTY(VOID), aOnPI, 3, 0, 0, 0 },
|
|
{ "OnStartNamespace", XTY(VOID), aOnNSStart, 3, 0, 0, 0 },
|
|
{ "OnEndNamespace", XTY(VOID), aOnNSEnd, 2, 0, 0, 0 },
|
|
{ "OnDoctype", XTY(VOID), aOnDoctype, 2, 0, 0, 0 },
|
|
{ "OnError", XTY(VOID), aOnError, 4, 0, 0, 0 }
|
|
};
|
|
/* XmlHandler: concrete public non-final methods with empty bodies. */
|
|
static const ph7_method_def aHandlerMethod[] = {
|
|
{ "OnStartElement", XTY(VOID), aOnStart, 3, XmlHandlerNoop, 0, PH7_MEMBER_PUBLIC },
|
|
{ "OnEndElement", XTY(VOID), aOnEnd, 2, XmlHandlerNoop, 0, PH7_MEMBER_PUBLIC },
|
|
{ "OnText", XTY(VOID), aOnText, 2, XmlHandlerNoop, 0, PH7_MEMBER_PUBLIC },
|
|
{ "OnProcessingInstruction", XTY(VOID), aOnPI, 3, XmlHandlerNoop, 0, PH7_MEMBER_PUBLIC },
|
|
{ "OnStartNamespace", XTY(VOID), aOnNSStart, 3, XmlHandlerNoop, 0, PH7_MEMBER_PUBLIC },
|
|
{ "OnEndNamespace", XTY(VOID), aOnNSEnd, 2, XmlHandlerNoop, 0, PH7_MEMBER_PUBLIC },
|
|
{ "OnDoctype", XTY(VOID), aOnDoctype, 2, XmlHandlerNoop, 0, PH7_MEMBER_PUBLIC },
|
|
{ "OnError", XTY(VOID), aOnError, 4, XmlHandlerNoop, 0, PH7_MEMBER_PUBLIC }
|
|
};
|
|
static const char *const aHandlerIf[] = { "IXmlHandler" };
|
|
static const ph7_field_def aExcField[] = {
|
|
{ "xmlMessage", XTY(STRING), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "xmlCode", XTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "xmlLine", XTY(INT), 0, PH7_MEMBER_PRIVATE, 0, 0 },
|
|
{ "xmlColumn", 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 },
|
|
{ "GetMessage", XTY(STRING), 0, 0, XmlExceptionField, (void *)"xmlMessage", PH7_MEMBER_PUBLIC },
|
|
{ "GetCode", XTY(INT), 0, 0, XmlExceptionField, (void *)"xmlCode", PH7_MEMBER_PUBLIC },
|
|
{ "GetLine", XTY(INT), 0, 0, XmlExceptionField, (void *)"xmlLine", PH7_MEMBER_PUBLIC },
|
|
{ "GetColumn", XTY(INT), 0, 0, XmlExceptionField, (void *)"xmlColumn", 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), 0, 0, XmlReaderConstruct0, 0, 0 },
|
|
{ "__construct", XTY(VOID), aCtor2, 2, XmlReaderConstruct2, 0, 0 },
|
|
{ "Parse", XTY(VOID), aParse, 2, XmlReaderParse, 0, PH7_MEMBER_PUBLIC },
|
|
{ "Close", XTY(VOID), 0, 0, XmlReaderClose, 0, PH7_MEMBER_PUBLIC },
|
|
{ "__destruct", XTY(VOID), 0, 0, XmlReaderDestruct, 0, 0 },
|
|
{ "CurrentLine", XTY(INT), 0, 0, XmlReaderCurrentLine, 0, PH7_MEMBER_PUBLIC },
|
|
{ "CurrentByteIndex", XTY(INT), 0, 0, XmlReaderCurrentByteIndex, 0, PH7_MEMBER_PUBLIC },
|
|
{ "CurrentColumn", XTY(INT), 0, 0, XmlReaderCurrentColumn, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetAttributeCount", XTY(INT), 0, 0, XmlReaderGetAttributeCount, 0, PH7_MEMBER_PUBLIC },
|
|
{ "Depth", XTY(INT), 0, 0, XmlReaderDepth, 0, PH7_MEMBER_PUBLIC },
|
|
{ "IsEmptyElement", XTY(BOOL), 0, 0, XmlReaderIsEmptyElement, 0, PH7_MEMBER_PUBLIC },
|
|
{ "LocalName", XTY(STRING), 0, 0, XmlReaderStringField, (void *)(uintptr_t)offsetof(ph7_xml_engine, sLocalName), PH7_MEMBER_PUBLIC },
|
|
{ "NamespaceUri", XTY(STRING), 0, 0, XmlReaderStringField, (void *)(uintptr_t)offsetof(ph7_xml_engine, sNsUri), PH7_MEMBER_PUBLIC },
|
|
{ "Prefix", XTY(STRING), 0, 0, XmlReaderStringField, (void *)(uintptr_t)offsetof(ph7_xml_engine, sPrefix), PH7_MEMBER_PUBLIC },
|
|
{ "GetAttributeName", XTY(STRING), aReaderInt, 1, XmlReaderGetAttributeName, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetAttributeValue", XTY(STRING), aReaderInt, 1, XmlReaderGetAttributeValue, 0, PH7_MEMBER_PUBLIC },
|
|
{ "GetAttribute", XTY(STRING), aReaderString, 1, XmlReaderGetAttribute, 0, PH7_MEMBER_PUBLIC },
|
|
{ "HasAttribute", XTY(BOOL), aReaderString, 1, XmlReaderHasAttribute, 0, PH7_MEMBER_PUBLIC },
|
|
{ "LookupNamespace", XTY(STRING), aReaderPrefix, 1, XmlReaderLookupNamespace, 0, PH7_MEMBER_PUBLIC },
|
|
{ "SetHandler", XTY(VOID), aSetHandler, 1, XmlReaderSetHandler, 0, PH7_MEMBER_PUBLIC },
|
|
{ "ClearHandler", XTY(VOID), 0, 0, XmlReaderClearHandler, 0, PH7_MEMBER_PUBLIC },
|
|
{ "ClearCallbacks", XTY(VOID), 0, 0, XmlReaderClearCallbacks, 0, PH7_MEMBER_PUBLIC },
|
|
{ "OnStartElement", XTY(VOID), aCallback, 1, XmlReaderSetCallback, (void *)(long)PH7_XML_START_TAG, PH7_MEMBER_PUBLIC },
|
|
{ "OnEndElement", XTY(VOID), aCallback, 1, XmlReaderSetCallback, (void *)(long)PH7_XML_END_TAG, PH7_MEMBER_PUBLIC },
|
|
{ "OnText", XTY(VOID), aCallback, 1, XmlReaderSetCallback, (void *)(long)PH7_XML_CDATA, PH7_MEMBER_PUBLIC },
|
|
{ "OnProcessingInstruction", XTY(VOID), aCallback, 1, XmlReaderSetCallback, (void *)(long)PH7_XML_PI, PH7_MEMBER_PUBLIC },
|
|
{ "OnStartNamespace", XTY(VOID), aCallback, 1, XmlReaderSetCallback, (void *)(long)PH7_XML_NS_START, PH7_MEMBER_PUBLIC },
|
|
{ "OnEndNamespace", XTY(VOID), aCallback, 1, XmlReaderSetCallback, (void *)(long)PH7_XML_NS_END, PH7_MEMBER_PUBLIC },
|
|
{ "OnDoctype", XTY(VOID), aCallback, 1, XmlReaderSetCallback, (void *)(long)PH7_XML_DOCTYPE, PH7_MEMBER_PUBLIC }
|
|
};
|
|
static const ph7_field_def aReaderField[] = {
|
|
{ "resource", XTY(RESOURCE), 0, PH7_MEMBER_PRIVATE, 0, 0 }
|
|
};
|
|
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 };
|
|
ph7_class_def sHandler = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION,
|
|
"XmlHandler", 0, 0, 0, 0, aHandlerMethod, SX_ARRAYSIZE(aHandlerMethod),
|
|
0, 0, 0, aHandlerIf, 1, 0, 0 };
|
|
ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION,
|
|
"XmlException", aExcField, SX_ARRAYSIZE(aExcField),
|
|
aExceptionConst, SX_ARRAYSIZE(aExceptionConst),
|
|
aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 };
|
|
ph7_class_def sReader = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION,
|
|
"XmlReader", aReaderField, SX_ARRAYSIZE(aReaderField), 0, 0,
|
|
aReaderMethod, SX_ARRAYSIZE(aReaderMethod), 0, 0, 0, 0, 0, 0, 0 };
|
|
ph7_value *pSep = ph7_new_scalar(pVm);
|
|
ph7_value *pTrue = ph7_new_scalar(pVm);
|
|
ph7_value *apErrVal[SX_ARRAYSIZE(aExceptionConst)];
|
|
sxu32 n;
|
|
sxi32 rc;
|
|
if(pSep == 0 || pTrue == 0) {
|
|
if(pSep) ph7_release_value(pVm, pSep);
|
|
if(pTrue) ph7_release_value(pVm, pTrue);
|
|
return PH7_ABORT;
|
|
}
|
|
for(n = 0 ; n < SX_ARRAYSIZE(aExceptionConst) ; ++n) {
|
|
apErrVal[n] = ph7_new_scalar(pVm);
|
|
if(apErrVal[n] == 0) {
|
|
while(n > 0) {
|
|
--n;
|
|
ph7_release_value(pVm, apErrVal[n]);
|
|
aExceptionConst[n].pValue = 0;
|
|
}
|
|
ph7_release_value(pVm, pSep);
|
|
ph7_release_value(pVm, pTrue);
|
|
return PH7_ABORT;
|
|
}
|
|
ph7_value_int(apErrVal[n], aExceptionConstValue[n]);
|
|
aExceptionConst[n].pValue = apErrVal[n];
|
|
}
|
|
ph7_value_string(pSep, ":", 1);
|
|
ph7_value_bool(pTrue, 1);
|
|
aCtor2[1].pDefault = pSep;
|
|
aParse[1].pDefault = pTrue;
|
|
rc = ph7_create_class(pVm, &sIface);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sHandler);
|
|
if(rc == PH7_OK) rc = ph7_create_class(pVm, &sExc);
|
|
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 */
|
|
}
|
|
aCtor2[1].pDefault = 0;
|
|
aParse[1].pDefault = 0;
|
|
ph7_release_value(pVm, pSep);
|
|
ph7_release_value(pVm, pTrue);
|
|
return rc;
|
|
}
|
|
|
|
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) {
|
|
desc->zString = MODULE_DESC;
|
|
*ver = MODULE_VER;
|
|
return XmlRegisterClasses(pVm);
|
|
}
|