/** * @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 * Rafal Kupiec */ #include #include #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: the handler's return value is IGNORED (it is a * void method), so rejecting a document means throwing from OnDoctype. The * exception is propagated through XmlPropagate() and aborts token processing * before the remainder of the document 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 ]> 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) { ph7_xml_engine *pEngine; ph7_vm *pVm = pCtx->pVm; 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; SyXMLParserInit(&pEngine->sParser, &pVm->sAllocator, process_ns ? SXML_ENABLE_NAMESPACE : 0); SyBlobInit(&pEngine->sErr, &pVm->sAllocator); 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); SySetInit(&pEngine->sElemStack, &pVm->sAllocator, sizeof(ph7_xml_elem_snapshot)); pEngine->iErrCode = SXML_ERROR_NONE; pEngine->nMagic = XML_ENGINE_MAGIC; return pEngine; } /* Drop every open-element snapshot. Called on abort-recovery and at teardown * so a handler exception in the middle of a document never leaks a snapshot. */ static void XmlElemStackClear(ph7_xml_engine *pEngine) { ph7_xml_elem_snapshot *pSnap; while((pSnap = (ph7_xml_elem_snapshot *)SySetPop(&pEngine->sElemStack)) != 0) { PH7_MemObjRelease(&pSnap->sLocalName); PH7_MemObjRelease(&pSnap->sNsUri); PH7_MemObjRelease(&pSnap->sPrefix); } } static void VmReleaseXMLEngine(ph7_xml_engine *pEngine) { ph7_vm *pVm = pEngine->pVm; XmlElemStackClear(pEngine); SySetRelease(&pEngine->sElemStack); SyBlobRelease(&pEngine->sErr); SyXMLParserRelease(&pEngine->sParser); PH7_MemObjRelease(&pEngine->sReaderValue); PH7_MemObjRelease(&pEngine->sHandler); PH7_MemObjRelease(&pEngine->sAttrs); PH7_MemObjRelease(&pEngine->sLocalName); PH7_MemObjRelease(&pEngine->sNsUri); PH7_MemObjRelease(&pEngine->sPrefix); 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 default-namespace key 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; } /* Columns exposed by the typed API are 1-based, matching the line * numbering of the underlying token. */ pEngine->nCurCol = (sxu32)(z - p) + 1; } } /* The engine's QName fields always describe the innermost open element: * start/end events set them to that element, text/PI/DOCTYPE events simply * read them. The stack keeps deep snapshots so an end event can restore the * enclosing element for subsequent events. */ static void XmlElemRestoreSlot(ph7_xml_engine *pEngine, ph7_value *pSlot, ph7_value *pSrc) { PH7_MemObjRelease(pSlot); PH7_MemObjInit(pEngine->pVm, pSlot); (void)PH7_MemObjStore(pSrc, pSlot); } static int XmlElemPush(ph7_xml_engine *pEngine) { ph7_xml_elem_snapshot sSnap; PH7_MemObjInit(pEngine->pVm, &sSnap.sLocalName); PH7_MemObjInit(pEngine->pVm, &sSnap.sNsUri); PH7_MemObjInit(pEngine->pVm, &sSnap.sPrefix); if(PH7_MemObjSnapshot(&pEngine->sLocalName, &sSnap.sLocalName) != SXRET_OK || PH7_MemObjSnapshot(&pEngine->sNsUri, &sSnap.sNsUri) != SXRET_OK || PH7_MemObjSnapshot(&pEngine->sPrefix, &sSnap.sPrefix) != SXRET_OK || SySetPut(&pEngine->sElemStack, &sSnap) != SXRET_OK) { PH7_MemObjRelease(&sSnap.sLocalName); PH7_MemObjRelease(&sSnap.sNsUri); PH7_MemObjRelease(&sSnap.sPrefix); /* SXERR_ABORT alone is not enough: Parse() checks bAbort/bError to * decide the result. Without this flag an allocation failure here * would be reported as a successful (but truncated) document. */ pEngine->bAbort = TRUE; return SXERR_ABORT; } return SXRET_OK; } /* Drop the snapshot of the element that just closed and restore its parent. * When the root closes the stack becomes empty: keep the root's fields so the * getters still report the last element after Parse() returns. */ static void XmlElemEnd(ph7_xml_engine *pEngine) { ph7_xml_elem_snapshot *pSnap = (ph7_xml_elem_snapshot *)SySetPop(&pEngine->sElemStack); ph7_xml_elem_snapshot *pParent; if(pSnap == 0) { return; } pParent = (ph7_xml_elem_snapshot *)SySetPeek(&pEngine->sElemStack); if(pParent) { XmlElemRestoreSlot(pEngine, &pEngine->sLocalName, &pParent->sLocalName); XmlElemRestoreSlot(pEngine, &pEngine->sNsUri, &pParent->sNsUri); XmlElemRestoreSlot(pEngine, &pEngine->sPrefix, &pParent->sPrefix); } PH7_MemObjRelease(&pSnap->sLocalName); PH7_MemObjRelease(&pSnap->sNsUri); PH7_MemObjRelease(&pSnap->sPrefix); } /* ------------------------------------------------------------------ */ /* 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)); /* Only public methods are dispatchable from the SAX callbacks. A script * subclass may narrow an inherited method's visibility via PH7's internal * API; such a method must never be invoked from native code. */ if(pMeth == 0 || pMeth->iProtection != PH7_CLASS_PROT_PUBLIC) { return SXRET_OK; } PH7_MemObjInit(pEngine->pVm, &sResult); rc = PH7_VmCallClassMethod(pEngine->pVm, pThis, pMeth, &sResult, nArg, apArg); return XmlPropagate(pEngine, &sResult, rc); } /* Any non-OK result from a handler (including a deferred exception) aborts * SAX token processing. */ static sxi32 XmlNotify(ph7_xml_engine *pEngine, const char *zMethod, ph7_value **apArg, int nArg) { sxi32 rc = XmlCallHandler(pEngine, zMethod, apArg, nArg); if(rc != PH7_OK) { pEngine->bAbort = TRUE; } return rc; } /* ------------------------------------------------------------------ */ /* SAX values */ /* ------------------------------------------------------------------ */ static ph7_value *VmXMLValue(ph7_xml_engine *pEngine, SyXMLRawStr *pXML) { ph7_value *pValue = ph7_context_new_scalar(pEngine->pCtx); if(pValue == 0) { PH7_VmMemoryError(pEngine->pCtx->pVm); return 0; } 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); /* lib.c extracts the QName prefix independently of the namespace flag; * expose it only when namespace processing is actually enabled. */ if(pEngine->sParser.nFlags & SXML_ENABLE_NAMESPACE) { XmlSetString(pEngine, &pEngine->sPrefix, pPrefix ? pPrefix->zString : 0, pPrefix ? (int)pPrefix->nByte : 0); } else { XmlSetString(pEngine, &pEngine->sPrefix, "", 0); } if(XmlElemPush(pEngine) != SXRET_OK) { return SXERR_ABORT; } pEngine->bEmptyElement = bEmpty ? TRUE : FALSE; ++pEngine->nDepth; pTag = VmXMLValue(pEngine, pStart); /* 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, "OnStartElement", apArg, 3); ph7_context_release_value(pEngine->pCtx, pTag); ph7_context_release_value(pEngine->pCtx, pAttr); /* Attributes are meaningful only while OnStartElement runs. */ PH7_MemObjRelease(&pEngine->sAttrs); PH7_MemObjInit(pEngine->pVm, &pEngine->sAttrs); 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); if(pEngine->sParser.nFlags & SXML_ENABLE_NAMESPACE) { XmlSetString(pEngine, &pEngine->sPrefix, pPrefix ? pPrefix->zString : 0, pPrefix ? (int)pPrefix->nByte : 0); } else { XmlSetString(pEngine, &pEngine->sPrefix, "", 0); } pEngine->bEmptyElement = FALSE; if(pEngine->nDepth > 0) { --pEngine->nDepth; } pTag = VmXMLValue(pEngine, pEnd); if(pTag == 0) { return SXRET_OK; } apArg[0] = &pEngine->sReaderValue; apArg[1] = pTag; (void)XmlNotify(pEngine, "OnEndElement", apArg, 2); ph7_context_release_value(pEngine->pCtx, pTag); XmlElemEnd(pEngine); PH7_MemObjRelease(&pEngine->sAttrs); PH7_MemObjInit(pEngine->pVm, &pEngine->sAttrs); 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); /* LocalName/Prefix/NamespaceUri keep the innermost open element. */ pData = VmXMLValue(pEngine, pText); if(pData == 0) { return SXRET_OK; } apArg[0] = &pEngine->sReaderValue; apArg[1] = pData; (void)XmlNotify(pEngine, "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); pData = VmXMLValue(pEngine, pDataStr); if(pTarget == 0 || pData == 0) { return SXRET_OK; } apArg[0] = &pEngine->sReaderValue; apArg[1] = pTarget; apArg[2] = pData; (void)XmlNotify(pEngine, "OnProcessingInstruction", apArg, 3); ph7_context_release_value(pEngine->pCtx, pTarget); ph7_context_release_value(pEngine->pCtx, pData); return pEngine->bAbort ? SXERR_ABORT : SXRET_OK; } /* The default namespace is stored under a one-byte internal hash key. That * sentinel is an implementation detail: map it back to the empty prefix before * it reaches a typed OnStartNamespace/OnEndNamespace handler. lib.c sets the * sentinel after invoking the start callback, so on that path this is purely * defensive; the end callback reads the key straight from the hash entry and * really does receive it. */ static void XmlPublicNsPrefix(SyXMLRawStr *pOut, SyXMLRawStr *pIn) { *pOut = *pIn; if(pOut->nByte == SXML_DEFAULT_NS_KEY_LEN && pOut->zString != 0 && SyMemcmp(pOut->zString, SXML_DEFAULT_NS_KEY_STR, SXML_DEFAULT_NS_KEY_LEN) == 0) { pOut->zString = ""; pOut->nByte = 0; } } /* 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; SyXMLRawStr sPrefix; ph7_value *apArg[3]; ph7_value *pPrefix, *pUri; XmlPublicNsPrefix(&sPrefix, pPrefixStr); XmlTrackPos(pEngine, &sPrefix); pPrefix = VmXMLValue(pEngine, &sPrefix); pUri = VmXMLValue(pEngine, pUriStr); if(pPrefix == 0 || pUri == 0) { return SXRET_OK; } apArg[0] = &pEngine->sReaderValue; apArg[1] = pPrefix; apArg[2] = pUri; (void)XmlNotify(pEngine, "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; SyXMLRawStr sPrefix; ph7_value *apArg[2]; ph7_value *pPrefix; XmlPublicNsPrefix(&sPrefix, pPrefixStr); XmlTrackPos(pEngine, &sPrefix); pPrefix = VmXMLValue(pEngine, &sPrefix); if(pPrefix == 0) { return SXRET_OK; } apArg[0] = &pEngine->sReaderValue; apArg[1] = pPrefix; (void)XmlNotify(pEngine, "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); /* raw text is not NUL-terminated */ if(pText == 0) { return SXRET_OK; } apArg[0] = &pEngine->sReaderValue; apArg[1] = pText; (void)XmlNotify(pEngine, "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 */ /* ------------------------------------------------------------------ */ /* Fetch an inherited (protected/private) attribute slot straight off the * instance, exactly as the JSON module does. */ static ph7_value *XmlInstField(ph7_class_instance *pInst, const char *zName) { SyString s; if(pInst == 0) { return 0; } SyStringInitFromBuf(&s, zName, (sxu32)SyStrlen(zName)); return PH7_ClassInstanceFetchAttr(pInst, &s); } /* Stamp the inherited Exception file/line with the first script frame of the * active call stack. Native frames ([NATIVE], line 0) are skipped so the base * getters describe the place in the script that raised the error, not the * SAX input position (which lives in inputLine/inputColumn). */ static void XmlStampSite(ph7_vm *pVm, ph7_class_instance *pInst) { SySet sTrace; VmDebugTrace *pTrace; ph7_value *pField; if(VmExtractDebugTrace(pVm, &sTrace) != SXRET_OK) { return; } while(SySetGetNextEntry(&sTrace, (void **)&pTrace) == SXRET_OK) { if(pTrace->pFile == 0 || pTrace->nLine == 0) { continue; } if(pTrace->pFile->nByte == sizeof("[NATIVE]") - 1 && SyMemcmp(pTrace->pFile->zString, "[NATIVE]", sizeof("[NATIVE]") - 1) == 0) { continue; } pField = XmlInstField(pInst, "file"); if(pField) { PH7_MemObjRelease(pField); PH7_MemObjInit(pVm, pField); (void)ph7_value_string(pField, pTrace->pFile->zString, (int)pTrace->pFile->nByte); } pField = XmlInstField(pInst, "line"); if(pField) { (void)ph7_value_int64(pField, (ph7_int64)pTrace->nLine); } break; } SySetRelease(&sTrace); } /* Throw a real XmlException object with the given code/message and input * position. Temporary scalars are always released and the instance reference * dropped on every error path. */ static int XmlThrow(ph7_context *pCtx, int nErr, const char *zMsg, sxu32 nLine, sxu32 nCol) { ph7_vm *pVm = pCtx->pVm; ph7_class *pClass; ph7_class_instance *pInst; ph7_class_method *pCons; ph7_value *pObj, *apArg[4], *aTmp[4]; sxi32 rc; sxu32 n; 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) { PH7_VmMemoryError(pVm); return PH7_ABORT; } for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) { aTmp[n] = ph7_context_new_scalar(pCtx); if(aTmp[n] == 0) { while(n > 0) { --n; ph7_context_release_value(pCtx, aTmp[n]); } PH7_ClassInstanceUnref(pInst); PH7_VmMemoryError(pVm); return PH7_ABORT; } } pObj = ph7_context_new_scalar(pCtx); if(pObj == 0) { for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) { ph7_context_release_value(pCtx, aTmp[n]); } PH7_ClassInstanceUnref(pInst); PH7_VmMemoryError(pVm); return PH7_ABORT; } ph7_value_string(aTmp[0], zMsg, -1); ph7_value_int64(aTmp[1], nErr); ph7_value_int64(aTmp[2], (ph7_int64)nLine); ph7_value_int64(aTmp[3], (ph7_int64)nCol); for(n = 0 ; n < SX_ARRAYSIZE(apArg) ; ++n) { apArg[n] = aTmp[n]; } pCons = PH7_ClassExtractMethod(pClass, "__construct", sizeof("__construct") - 1); if(pCons) { rc = PH7_VmCallClassMethod(pVm, pInst, pCons, 0, 4, apArg); if(pInst->iRef < 1) { pInst->iRef = 1; } for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) { ph7_context_release_value(pCtx, aTmp[n]); } if(rc != PH7_OK) { ph7_context_release_value(pCtx, pObj); PH7_ClassInstanceUnref(pInst); return rc; } } else { for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) { ph7_context_release_value(pCtx, aTmp[n]); } } /* Base Exception fields describe the throwing script, not the XML input. */ XmlStampSite(pVm, pInst); if(pInst->iRef < 1) { pInst->iRef = 1; } PH7_MemObjRelease(pObj); pObj->x.pOther = pInst; MemObjSetType(pObj, MEMOBJ_OBJ); rc = ph7_result_value(pCtx, pObj); ph7_context_release_value(pCtx, pObj); return rc == PH7_OK ? PH7_EXCEPTION : rc; } static int XmlReentryGuard(ph7_context *pCtx) { return XmlThrow(pCtx, SXML_ERROR_INVALID_OPERATION, "XmlReader cannot be modified while Parse() is running", 0, 0); } /* ------------------------------------------------------------------ */ /* 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) { ph7_vm *pVm = pCtx->pVm; ph7_class *pBase; ph7_class_method *pCons; ph7_value *pThis = ph7_context_this(pCtx); sxi32 rc; SXUNUSED(nArg); /* Run the base constructor so message/code/file/line/trace/previous keep * ordinary Exception semantics for directly constructed instances. */ pBase = PH7_VmExtractClass(pVm, "Exception", sizeof("Exception") - 1, FALSE); if(pBase == 0) return PH7_ABORT; pCons = PH7_ClassExtractMethod(pBase, "__construct", sizeof("__construct") - 1); if(pCons == 0) return PH7_ABORT; rc = PH7_VmCallClassMethod(pVm, (ph7_class_instance *)pThis->x.pOther, pCons, 0, 2, apArg); if(rc != PH7_OK) return rc; /* XML-specific 1-based input position, independent of the script site. */ rc = ph7_context_attr_store(pCtx, pThis, 0, "inputLine", apArg[2]); if(rc != PH7_OK) return rc; rc = ph7_context_attr_store(pCtx, pThis, 0, "inputColumn", apArg[3]); 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); case SXML_ERROR_INVALID_OPERATION: return ph7_result_string(pCtx, "Invalid operation", -1); case SXML_ERROR_TYPE_MISMATCH: return ph7_result_string(pCtx, "Type mismatch", -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; } /* Every public accessor shares the same closed-reader diagnostic. */ static ph7_xml_engine *XmlReaderEngineOrThrow(ph7_context *pCtx) { ph7_xml_engine *pEngine = XmlReaderEngine(pCtx); if(pEngine == 0) { (void)XmlThrow(pCtx, SXML_ERROR_INVALID_OPERATION, "XmlReader is closed", 0, 0); return 0; } return pEngine; } static int XmlReaderCreate(ph7_context *pCtx, int process_ns) { ph7_xml_engine *pEngine = VmCreateXMLEngine(pCtx, process_ns); 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 XmlReaderConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) { /* A single bool argument defaults to FALSE; there is no separator knob. */ return XmlReaderCreate(pCtx, nArg > 0 && ph7_value_to_bool(apArg[0]) ? TRUE : FALSE); } static int XmlReaderCloseCtx(ph7_context *pCtx, int bDestruct) { 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)) { if(pEngine->iNest > 0) { /* sReaderValue still owns a reference; an explicit Close() is * rejected while Parse() is running, while __destruct simply * leaves the engine alive for the active Parse(). */ ph7_context_release_value(pCtx, pRes); return bDestruct ? PH7_OK : XmlReentryGuard(pCtx); } VmReleaseXMLEngine(pEngine); ph7_value_resource(pRes, 0); /* invalidate: future access throws */ (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, 0); } static int XmlReaderDestruct(ph7_context *pCtx, int nArg, ph7_value **apArg) { SXUNUSED(nArg); SXUNUSED(apArg); return XmlReaderCloseCtx(pCtx, 1); } static int XmlReaderParse(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_xml_engine *pEngine; SyXMLParser *pParser; const char *zData; int nByte; /* XTY(MIXED) keeps non-string arguments out of the VM's overload filter so * this callback can report a typed diagnostic for them. */ if(nArg < 1 || !ph7_value_is_string(apArg[0])) { return XmlThrow(pCtx, SXML_ERROR_TYPE_MISMATCH, "XmlReader::Parse() expects a string", 0, 0); } pEngine = XmlReaderEngineOrThrow(pCtx); if(pEngine == 0) { return PH7_EXCEPTION; } if(pEngine->iNest > 0) { return XmlThrow(pCtx, SXML_ERROR_INVALID_OPERATION, "Parse() cannot be called recursively", 0, 0); } pEngine->pCtx = pCtx; /* Reset all transient state, including after a previous abort. */ XmlElemStackClear(pEngine); 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) { /* Make the SAX message NUL-terminated for the typed scalar. */ SyBlobNullAppend(&pEngine->sErr); return XmlThrow(pCtx, pEngine->iErrCode, SyBlobData(&pEngine->sErr), pEngine->nErrLine, pEngine->nErrCol); } 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 = XmlReaderEngineOrThrow(pCtx); SXUNUSED(nArg); SXUNUSED(apArg); return pEngine ? ph7_result_int64(pCtx, pEngine->nCurLine) : PH7_EXCEPTION; } static int XmlReaderCurrentByteIndex(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_xml_engine *pEngine = XmlReaderEngineOrThrow(pCtx); SXUNUSED(nArg); SXUNUSED(apArg); return pEngine ? ph7_result_int64(pCtx, pEngine->iCurByte) : PH7_EXCEPTION; } static int XmlReaderCurrentColumn(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_xml_engine *pEngine = XmlReaderEngineOrThrow(pCtx); SXUNUSED(nArg); SXUNUSED(apArg); return pEngine ? ph7_result_int64(pCtx, pEngine->nCurCol) : PH7_EXCEPTION; } static int XmlReaderDepth(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_xml_engine *pEngine = XmlReaderEngineOrThrow(pCtx); SXUNUSED(nArg); SXUNUSED(apArg); return pEngine ? ph7_result_int64(pCtx, pEngine->nDepth) : PH7_EXCEPTION; } static int XmlReaderIsEmptyElement(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_xml_engine *pEngine = XmlReaderEngineOrThrow(pCtx); SXUNUSED(nArg); SXUNUSED(apArg); return pEngine ? ph7_result_bool(pCtx, pEngine->bEmptyElement) : PH7_EXCEPTION; } static int XmlReaderStringField(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_xml_engine *pEngine; SXUNUSED(nArg); SXUNUSED(apArg); pEngine = XmlReaderEngineOrThrow(pCtx); if(pEngine == 0) { return PH7_EXCEPTION; } 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 = XmlReaderEngineOrThrow(pCtx); SXUNUSED(nArg); SXUNUSED(apArg); if(pEngine == 0) { return PH7_EXCEPTION; } 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 = XmlReaderEngineOrThrow(pCtx); ph7_value *pName, *pValue, *pOut; struct XmlAttrWalk sWalk; SXUNUSED(nArg); if(pEngine == 0) { return PH7_EXCEPTION; } 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 = XmlReaderEngineOrThrow(pCtx); ph7_value *pValue; int nLen; const char *zName; SXUNUSED(nArg); if(pEngine == 0) { return PH7_EXCEPTION; } 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 = XmlReaderEngineOrThrow(pCtx); ph7_value *pValue; int nLen; const char *zName; SXUNUSED(nArg); if(pEngine == 0) { return PH7_EXCEPTION; } 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 = XmlReaderEngineOrThrow(pCtx); SyHashEntry *pEntry; const char *zPrefix; int nLen; SXUNUSED(nArg); if(pEngine == 0) { return PH7_EXCEPTION; } zPrefix = ph7_value_to_string(apArg[0], &nLen); if(ph7_context_status(pCtx) != PH7_OK) { return ph7_context_status(pCtx); } if(nLen == 0) { zPrefix = SXML_DEFAULT_NS_KEY_STR; nLen = SXML_DEFAULT_NS_KEY_LEN; } /* 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 = XmlReaderEngineOrThrow(pCtx); SXUNUSED(nArg); if(pEngine == 0) { return PH7_EXCEPTION; } if(pEngine->iNest > 0) { return XmlReentryGuard(pCtx); } PH7_MemObjRelease(&pEngine->sHandler); PH7_MemObjInit(pCtx->pVm, &pEngine->sHandler); PH7_MemObjStore(apArg[0], &pEngine->sHandler); return PH7_OK; } static int XmlReaderClearHandler(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_xml_engine *pEngine = XmlReaderEngineOrThrow(pCtx); SXUNUSED(nArg); SXUNUSED(apArg); if(pEngine == 0) { return PH7_EXCEPTION; } if(pEngine->iNest > 0) { return XmlReentryGuard(pCtx); } PH7_MemObjRelease(&pEngine->sHandler); PH7_MemObjInit(pCtx->pVm, &pEngine->sHandler); 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 }, { "ERROR_INVALID_OPERATION", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ERROR_TYPE_MISMATCH", 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, SXML_ERROR_INVALID_OPERATION, SXML_ERROR_TYPE_MISMATCH }; static sxi32 XmlRegisterClasses(ph7_vm *pVm) { static const ph7_param_def aReaderInt[] = { XP("index", INT) }; static const ph7_param_def aReaderString[] = { XP("name", STRING) }; static const ph7_param_def aReaderPrefix[] = { XP("prefix", STRING) }; static ph7_param_def aCtor[] = { XP("namespaces", BOOL) }; static const ph7_param_def aParse[] = { XP("data", MIXED) }; static const ph7_param_def aSetHandler[] = { { "handler", XNM("IXmlHandler"), 0, 0, 0, 0 } }; static ph7_param_def aExcCtor[] = { XP("message", STRING), XP("code", INT), XP("inputLine", INT), XP("inputColumn", 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" }; /* Declared private fields are required: PH7_VmContextAttr() resolves * attributes through the class table, so a dynamic attr_store() alone * would fail. The base Exception supplies message/code/file/line. */ static const ph7_field_def aExcField[] = { { "inputLine", 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 ph7_method_def aExcMethod[] = { { "__construct", XTY(VOID), aExcCtor, 4, XmlExceptionConstruct, 0, 0 }, { "GetInputLine", XTY(INT), 0, 0, XmlExceptionField, (void *)"inputLine", PH7_MEMBER_PUBLIC }, { "GetInputColumn", XTY(INT), 0, 0, XmlExceptionField, (void *)"inputColumn", PH7_MEMBER_PUBLIC }, { "GetDescription", XTY(STRING), aExcDesc, 1, XmlExceptionGetDescription, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC } }; /* XmlReader methods */ static const ph7_method_def aReaderMethod[] = { { "__construct", XTY(VOID), aCtor, 1, XmlReaderConstruct, 0, 0 }, { "Parse", XTY(VOID), aParse, 1, XmlReaderParse, 0, PH7_MEMBER_PUBLIC }, { "Close", XTY(VOID), 0, 0, XmlReaderClose, 0, PH7_MEMBER_PUBLIC }, { "__destruct", XTY(VOID), 0, 0, XmlReaderDestruct, 0, 0 }, { "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 } }; 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 *aDefault[SX_ARRAYSIZE(aExcCtor) + 1]; ph7_value *apErrVal[SX_ARRAYSIZE(aExceptionConst)]; sxu32 n; sxi32 rc; for(n = 0 ; n < SX_ARRAYSIZE(aDefault) ; ++n) { aDefault[n] = ph7_new_scalar(pVm); if(aDefault[n] == 0) { while(n > 0) { --n; ph7_release_value(pVm, aDefault[n]); } 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; } for(n = 0 ; n < SX_ARRAYSIZE(aDefault) ; ++n) { ph7_release_value(pVm, aDefault[n]); } return PH7_ABORT; } ph7_value_int(apErrVal[n], aExceptionConstValue[n]); aExceptionConst[n].pValue = apErrVal[n]; } ph7_value_bool(aDefault[0], 0); /* XmlReader(bool namespaces = false) */ ph7_value_int(aDefault[1], 0); /* inputLine = 0 */ ph7_value_int(aDefault[2], 0); /* inputColumn = 0 */ aCtor[0].pDefault = aDefault[0]; aExcCtor[2].pDefault = aDefault[1]; aExcCtor[3].pDefault = aDefault[2]; 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 */ } aCtor[0].pDefault = 0; aExcCtor[2].pDefault = 0; aExcCtor[3].pDefault = 0; for(n = 0 ; n < SX_ARRAYSIZE(aDefault) ; ++n) { ph7_release_value(pVm, aDefault[n]); } return rc; } PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) { desc->zString = MODULE_DESC; *ver = MODULE_VER; return XmlRegisterClasses(pVm); }