/** * @PROJECT PH7 Engine for the AerScript Interpreter * @COPYRIGHT See COPYING in the top level directory * @FILE engine/textwriter.c * @DESCRIPTION TextWriter core class support and its C API * @DEVELOPERS Rafal Kupiec */ #include "ph7int.h" /* A backend is stored as a resource in TextWriter->handle. The wrapper is * refcounted because cloning an instance copies the resource pointer: each * live instance owns one reference and releases it from its destructor. */ typedef struct text_writer_backend { ph7_text_writer_methods sMethods; void *pUserData; int nRef; } text_writer_backend; /* Adapter between PH7_InputFormat's consumer and a backend xWrite callback. */ typedef struct text_writer_sink { int (*xWrite)(void *, const void *, unsigned int); void *pUserData; } text_writer_sink; static int TextWriterSink(ph7_context *pCtx, const char *zData, int nLen, void *pUserData) { text_writer_sink *pSink = (text_writer_sink *)pUserData; SXUNUSED(pCtx); if(nLen > 0 && pSink->xWrite) { return pSink->xWrite(pSink->pUserData, zData, (unsigned int)nLen); } return SXRET_OK; } /* Fetch the backend wrapper owned by the current TextWriter instance. */ static text_writer_backend *TextWriterFetch(ph7_context *pCtx) { ph7_class_instance *pInst; ph7_value *pThis, *pField; SyString sName; pThis = ph7_context_this(pCtx); if(pThis == 0 || (pThis->nType & MEMOBJ_OBJ) == 0 || pThis->x.pOther == 0) { return 0; } pInst = (ph7_class_instance *)pThis->x.pOther; SyStringInitFromBuf(&sName, "handle", sizeof("handle") - 1); pField = PH7_ClassInstanceFetchAttr(pInst, &sName); if(pField == 0 || (pField->nType & MEMOBJ_RES) == 0 || pField->x.pOther == 0) { return 0; } return (text_writer_backend *)pField->x.pOther; } /* Single shared implementation of the Console/TextWriter print rule: with one * argument the value is written literally (a lone '%' is ordinary text), with * several the first argument is the format string. bLine appends the EOL. */ int ph7_text_writer_emit(ph7_context *pCtx, int (*xWrite)(void *, const void *, unsigned int), void *pUserData, int nArg, ph7_value **apArg, int bLine) { text_writer_sink sSink; const char *zFormat; int nByte, rc; /* Single rule for every caller (Console::Print and TextWriter::Print): * with several arguments the first one must be a string format. */ if(nArg > 1 && !ph7_value_is_string(apArg[0])) { return PH7_VmThrowIoException(pCtx, 1, "Format must be a string"); } if(nArg < 1) { rc = SXRET_OK; } else if(nArg == 1) { zFormat = ph7_value_to_string(apArg[0], &nByte); if(pCtx->pVm->iPendingTransfer) { return pCtx->pVm->iPendingTransfer; } rc = xWrite && nByte > 0 ? xWrite(pUserData, zFormat, (unsigned int)nByte) : SXRET_OK; } else { sSink.xWrite = xWrite; sSink.pUserData = pUserData; zFormat = ph7_value_to_string(apArg[0], &nByte); if(pCtx->pVm->iPendingTransfer) { return pCtx->pVm->iPendingTransfer; } rc = PH7_InputFormat(TextWriterSink, pCtx, zFormat, nByte, nArg, apArg, &sSink, FALSE); } if(rc == SXRET_OK && bLine) { #ifdef __WINNT__ static const char zEOL[] = "\r\n"; #else static const char zEOL[] = "\n"; #endif if(xWrite && xWrite(pUserData, zEOL, (unsigned int)(sizeof(zEOL) - 1)) != SXRET_OK) { rc = PH7_ABORT; } } return rc; } /* Create a TextWriter instance around a backend and store it in pResult. */ int ph7_text_writer_new(ph7_context *pCtx, const ph7_text_writer_methods *pMethods, void *pUserData, ph7_value *pResult) { ph7_vm *pVm = pCtx->pVm; text_writer_backend *pBackend; ph7_class *pClass; ph7_class_instance *pInst; ph7_value *pField; SyString sName; pClass = PH7_VmExtractClass(pVm, "TextWriter", sizeof("TextWriter") - 1, TRUE); if(pClass == 0) { return PH7_ABORT; } pBackend = (text_writer_backend *)SyMemBackendAlloc(&pVm->sAllocator, sizeof(text_writer_backend)); if(pBackend == 0) { PH7_VmMemoryError(pVm); return PH7_ABORT; } pBackend->sMethods = *pMethods; pBackend->pUserData = pUserData; pBackend->nRef = 1; pInst = PH7_NewClassInstance(pVm, pClass); if(pInst == 0) { SyMemBackendFree(&pVm->sAllocator, pBackend); PH7_VmMemoryError(pVm); return PH7_ABORT; } SyStringInitFromBuf(&sName, "handle", sizeof("handle") - 1); pField = PH7_ClassInstanceFetchAttr(pInst, &sName); if(pField == 0) { PH7_ClassInstanceUnref(pInst); SyMemBackendFree(&pVm->sAllocator, pBackend); PH7_VmMemoryError(pVm); return PH7_ABORT; } ph7_value_resource(pField, pBackend); PH7_MemObjRelease(pResult); pResult->x.pOther = pInst; MemObjSetType(pResult, MEMOBJ_OBJ); return PH7_OK; } /* ------------------------------------------------------------------ */ /* Native callbacks for the core TextWriter class */ /* ------------------------------------------------------------------ */ int TextWriterConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_class_instance *pInst; ph7_value *pField; SyString sName; ph7_value *pThis = ph7_context_this(pCtx); if(pThis == 0 || (pThis->nType & MEMOBJ_OBJ) == 0 || pThis->x.pOther == 0 || nArg < 1) { return PH7_ABORT; } pInst = (ph7_class_instance *)pThis->x.pOther; SyStringInitFromBuf(&sName, "handle", sizeof("handle") - 1); pField = PH7_ClassInstanceFetchAttr(pInst, &sName); if(pField == 0) { return PH7_ABORT; } return ph7_value_resource(pField, ph7_value_to_resource(apArg[0])) == PH7_OK ? PH7_OK : PH7_ABORT; } int TextWriterDestruct(ph7_context *pCtx, int nArg, ph7_value **apArg) { text_writer_backend *pBackend = TextWriterFetch(pCtx); ph7_vm *pVm = pCtx->pVm; SXUNUSED(nArg); SXUNUSED(apArg); if(pBackend == 0) { return PH7_OK; } ph7_class_instance *pInst; ph7_value *pField; SyString sName; /* Drop this instance's reference unconditionally: an explicit second * __destruct() must be a no-op instead of a use-after-free, and a clone * that outlives this instance must not keep a dangling handle. Each * instance owns its own field, so a co-owner's pointer is untouched. */ pInst = (ph7_class_instance *)ph7_context_this(pCtx)->x.pOther; SyStringInitFromBuf(&sName, "handle", sizeof("handle") - 1); pField = PH7_ClassInstanceFetchAttr(pInst, &sName); if(pField) { ph7_value_resource(pField, 0); } if(--pBackend->nRef <= 0) { if(pBackend->sMethods.xRelease) { pBackend->sMethods.xRelease(pBackend->pUserData); } SyMemBackendFree(&pVm->sAllocator, pBackend); } return PH7_OK; } int TextWriterClone(ph7_context *pCtx, int nArg, ph7_value **apArg) { text_writer_backend *pBackend = TextWriterFetch(pCtx); SXUNUSED(nArg); SXUNUSED(apArg); /* Clone copies the resource pointer; own a reference for the new instance. */ if(pBackend) { pBackend->nRef++; } return PH7_OK; } static int TextWriterEmitThis(ph7_context *pCtx, int nArg, ph7_value **apArg, int bLine) { text_writer_backend *pBackend = TextWriterFetch(pCtx); if(pBackend == 0 || pBackend->sMethods.xWrite == 0) { return PH7_ABORT; } return ph7_text_writer_emit(pCtx, pBackend->sMethods.xWrite, pBackend->pUserData, nArg, apArg, bLine); } int TextWriterPrint(ph7_context *pCtx, int nArg, ph7_value **apArg) { return TextWriterEmitThis(pCtx, nArg, apArg, 0); } int TextWriterPrintLine(ph7_context *pCtx, int nArg, ph7_value **apArg) { return TextWriterEmitThis(pCtx, nArg, apArg, 1); } int TextWriterFlush(ph7_context *pCtx, int nArg, ph7_value **apArg) { text_writer_backend *pBackend = TextWriterFetch(pCtx); SXUNUSED(nArg); SXUNUSED(apArg); if(pBackend == 0) { return PH7_OK; } if(pBackend->sMethods.xFlush) { return pBackend->sMethods.xFlush(pBackend->pUserData); } return SXRET_OK; }