/** * @PROJECT AerScript Interpreter * @COPYRIGHT See COPYING in the top level directory * @FILE modules/char/char.c * @DESCRIPTION Static Char class module for AerScript Interpreter (0..255 contract) * @DEVELOPERS Rafal Kupiec */ #include #include "char.h" #define CCLS_CHAR "Char" #define CCLS_EXC "CharException" #define CTY(T) { PH7_TYPE_##T, 0, 0 } /* The engine's language char is a 64-bit integer (MEMOBJ_CHAR, x.iVal is an * sxi64), not a byte. PH7_MemObjToChar() (engine/memobj.c:612-620) stores the * integer unchanged: (char)128/233/255 give 128/233/255, but (char)256 gives * 256 and (char)-1 gives -1 (no clamp); ph7_value_to_int() shows only a 32-bit * view. Converting a one-byte string to char maps its first byte to 0..255, * so (char)chr(233) is 233. This scripted Char class adds its own contract * mask on top: CharOperand accepts only a char in 0..255 or a one-byte string * mapped as an unsigned byte, and rejects values outside that range. It does * not change the engine representation. Classifiers return FALSE for bytes * >= 0x80. */ typedef int (*CharPred)(ph7_int64); static const char *CharErrorDescription(int nErr) { switch(nErr) { case CHAR_ERROR_NONE: return "No error"; case CHAR_ERROR_ARGUMENT: return "Invalid argument"; default: return "Unknown error code"; } } /* ------------------------------------------------------------------ */ /* CharException */ /* ------------------------------------------------------------------ */ static ph7_value *CharInstField(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. */ static void CharStampSite(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 = CharInstField(pInst, "file"); if(pField) { PH7_MemObjRelease(pField); PH7_MemObjInit(pVm, pField); (void)ph7_value_string(pField, pTrace->pFile->zString, (int)pTrace->pFile->nByte); } pField = CharInstField(pInst, "line"); if(pField) { (void)ph7_value_int64(pField, (ph7_int64)pTrace->nLine); } break; } SySetRelease(&sTrace); } /* Throw a real CharException instance; every temporary is released on all * paths and the pending transfer is left for the VM unwinder. */ static int CharThrow(ph7_context *pCtx, int nErr, const char *zMsg) { ph7_vm *pVm = pCtx->pVm; ph7_class *pClass; ph7_class_instance *pInst; ph7_class_method *pCons; ph7_value *pObj, *apArg[2], *aTmp[2]; sxi32 rc; sxu32 n; if(pVm->iPendingTransfer) { return PH7_EXCEPTION; } pClass = PH7_VmExtractClass(pVm, CCLS_EXC, sizeof(CCLS_EXC) - 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_int(aTmp[1], nErr); apArg[0] = aTmp[0]; apArg[1] = aTmp[1]; pCons = PH7_ClassExtractMethod(pClass, "__construct", sizeof("__construct") - 1); if(pCons) { rc = PH7_VmCallClassMethod(pVm, pInst, pCons, 0, 2, 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]); } } CharStampSite(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 CharExceptionConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_value *pResult = ph7_context_new_scalar(pCtx); sxi32 rc; SXUNUSED(nArg); if(pResult == 0) { return PH7_ABORT; } /* apArg[1] is always filled by the VM from the registered pDefault. */ rc = ph7_context_parent(pCtx, "__construct", 2, apArg, pResult); return rc; } static int CharExceptionDescription(ph7_context *pCtx, int nArg, ph7_value **apArg) { int nErr = nArg > 0 ? ph7_value_to_int(apArg[0]) : -1; return ph7_result_string(pCtx, CharErrorDescription(nErr), -1); } /* ------------------------------------------------------------------ */ /* Argument conversion */ /* ------------------------------------------------------------------ */ /* Read a raw MIXED argument as an Aer char: either a char value in 0..255 or * a string of exactly one byte, interpreted as an unsigned byte. Every other * type (including NULL, void and int) is rejected with a CharException. */ static int CharOperand(ph7_context *pCtx, ph7_value *pArg, ph7_int64 *pOut) { const char *zData; sxu32 nByte; ph7_int64 iVal; if(pArg == 0) { return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Expected a char (0..255) or a single-byte string argument"); } if(ph7_value_is_char(pArg)) { iVal = ph7_value_to_int64(pArg); if(iVal < 0 || iVal > 255) { return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Char argument is outside the 0..255 range"); } *pOut = iVal; return PH7_OK; } if(ph7_value_is_string(pArg)) { nByte = SyBlobLength(&pArg->sBlob); if(nByte != 1) { return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Expected a string of exactly one byte"); } zData = (const char *)SyBlobData(&pArg->sBlob); *pOut = (ph7_int64)(unsigned char)zData[0]; return PH7_OK; } return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Expected a char (0..255) or a single-byte string argument"); } /* Set a full-range char result; ph7_value_char() clamps to 0..255. */ static int CharResultChar(ph7_context *pCtx, ph7_int64 iVal) { ph7_value *pRet = pCtx->pRet; (void)ph7_value_char(pRet, (int)iVal); pCtx->bResultSet = TRUE; return PH7_OK; } /* ------------------------------------------------------------------ */ /* Classification predicates */ /* ------------------------------------------------------------------ */ static int CharAscii(ph7_int64 c) { return c >= 0 && c <= 0x7F; } static int PredControl(ph7_int64 c) { return CharAscii(c) && ((c >= 0x00 && c <= 0x1F) || c == 0x7F); } static int PredDigit(ph7_int64 c) { return CharAscii(c) && c >= '0' && c <= '9'; } static int PredLetter(ph7_int64 c) { return CharAscii(c) && ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); } static int PredLetterOrDigit(ph7_int64 c) { return PredLetter(c) || PredDigit(c); } static int PredLower(ph7_int64 c) { return CharAscii(c) && c >= 'a' && c <= 'z'; } static int PredUpper(ph7_int64 c) { return CharAscii(c) && c >= 'A' && c <= 'Z'; } static int PredWhiteSpace(ph7_int64 c) { return CharAscii(c) && ((c >= 0x09 && c <= 0x0D) || c == 0x20); } static int PredSeparator(ph7_int64 c) { return CharAscii(c) && c == 0x20; } static int PredPunctuation(ph7_int64 c) { static const char zSet[] = "!\"#%&'()*,-./:;?@[\\]_{}"; return CharAscii(c) && c != 0 && strchr(zSet, (int)c) != 0; } static int PredSymbol(ph7_int64 c) { static const char zSet[] = "$+<=>^`|~"; return CharAscii(c) && c != 0 && strchr(zSet, (int)c) != 0; } static int PredGraphical(ph7_int64 c) { return CharAscii(c) && c >= 0x21 && c <= 0x7E; } static int PredPrintable(ph7_int64 c) { return CharAscii(c) && c >= 0x20 && c <= 0x7E; } static int PredAsciiDigit(ph7_int64 c) { return PredDigit(c); } static int PredAsciiLetter(ph7_int64 c) { return PredLetter(c); } static int PredAsciiLetterLower(ph7_int64 c) { return PredLower(c); } static int PredAsciiLetterUpper(ph7_int64 c) { return PredUpper(c); } static int PredAsciiLetterOrDigit(ph7_int64 c) { return PredLetterOrDigit(c); } static int PredHexLower(ph7_int64 c) { return CharAscii(c) && c >= 'a' && c <= 'f'; } static int PredHexUpper(ph7_int64 c) { return CharAscii(c) && c >= 'A' && c <= 'F'; } static int PredHexDigit(ph7_int64 c) { return PredDigit(c) || PredHexLower(c) || PredHexUpper(c); } static int CharClassify(ph7_context *pCtx, int nArg, ph7_value **apArg, CharPred xPred) { ph7_int64 c; if(nArg != 1) { return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Method expects exactly one argument"); } if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) { return PH7_EXCEPTION; } return ph7_result_bool(pCtx, xPred(c) ? 1 : 0); } #define CLS_FN(NAME, PRED) \ static int NAME(ph7_context *pCtx, int nArg, ph7_value **apArg) { \ return CharClassify(pCtx, nArg, apArg, PRED); \ } CLS_FN(CharIsControl, PredControl) CLS_FN(CharIsDigit, PredDigit) CLS_FN(CharIsNumber, PredDigit) CLS_FN(CharIsLetter, PredLetter) CLS_FN(CharIsLetterOrDigit, PredLetterOrDigit) CLS_FN(CharIsLower, PredLower) CLS_FN(CharIsUpper, PredUpper) CLS_FN(CharIsWhiteSpace, PredWhiteSpace) CLS_FN(CharIsSeparator, PredSeparator) CLS_FN(CharIsPunctuation, PredPunctuation) CLS_FN(CharIsSymbol, PredSymbol) CLS_FN(CharIsGraphical, PredGraphical) CLS_FN(CharIsPrintable, PredPrintable) CLS_FN(CharIsAscii, CharAscii) CLS_FN(CharIsAsciiDigit, PredAsciiDigit) CLS_FN(CharIsAsciiLetter, PredAsciiLetter) CLS_FN(CharIsAsciiLetterLower, PredAsciiLetterLower) CLS_FN(CharIsAsciiLetterUpper, PredAsciiLetterUpper) CLS_FN(CharIsAsciiLetterOrDigit, PredAsciiLetterOrDigit) CLS_FN(CharIsAsciiHexDigit, PredHexDigit) CLS_FN(CharIsAsciiHexDigitLower, PredHexLower) CLS_FN(CharIsAsciiHexDigitUpper, PredHexUpper) static int CharIsBetween(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_int64 c, zMin, zMax; if(nArg != 3) { return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "IsBetween expects exactly three arguments"); } if(CharOperand(pCtx, apArg[0], &c) != PH7_OK || CharOperand(pCtx, apArg[1], &zMin) != PH7_OK || CharOperand(pCtx, apArg[2], &zMax) != PH7_OK) { return PH7_EXCEPTION; } return ph7_result_bool(pCtx, zMin <= zMax && c >= zMin && c <= zMax ? 1 : 0); } /* ------------------------------------------------------------------ */ /* Conversions */ /* ------------------------------------------------------------------ */ static int CharCaseConvert(ph7_context *pCtx, int nArg, ph7_value **apArg, int bUpper) { ph7_int64 c; if(nArg != 1) { return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Method expects exactly one argument"); } if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) { return PH7_EXCEPTION; } if(bUpper && c >= 'a' && c <= 'z') { c -= ('a' - 'A'); } else if(!bUpper && c >= 'A' && c <= 'Z') { c += ('a' - 'A'); } return CharResultChar(pCtx, c); } static int CharToUpper(ph7_context *pCtx, int nArg, ph7_value **apArg) { return CharCaseConvert(pCtx, nArg, apArg, 1); } static int CharToLower(ph7_context *pCtx, int nArg, ph7_value **apArg) { return CharCaseConvert(pCtx, nArg, apArg, 0); } static int CharGetNumericValue(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_int64 c; if(nArg != 1) { return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "GetNumericValue expects exactly one argument"); } if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) { return PH7_EXCEPTION; } return ph7_result_double(pCtx, PredDigit(c) ? (double)(c - '0') : -1.0); } static int CharToString(ph7_context *pCtx, int nArg, ph7_value **apArg) { ph7_int64 c; char zByte[2]; if(nArg != 1) { return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "ToString expects exactly one argument"); } if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) { return PH7_EXCEPTION; } zByte[0] = (char)(unsigned char)(c & 0xFF); zByte[1] = 0; return ph7_result_string(pCtx, zByte, 1); } static int CharParse(ph7_context *pCtx, int nArg, ph7_value **apArg) { const char *zData; sxu32 nByte; if(nArg != 1) { return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Parse expects exactly one argument"); } if(apArg[0] == 0 || !ph7_value_is_string(apArg[0])) { return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Parse expects a single-byte string"); } nByte = SyBlobLength(&apArg[0]->sBlob); if(nByte != 1) { return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Parse expects a single-byte string"); } zData = (const char *)SyBlobData(&apArg[0]->sBlob); return CharResultChar(pCtx, (int)(unsigned char)zData[0]); } /* ------------------------------------------------------------------ */ /* Class registration */ /* ------------------------------------------------------------------ */ static int CharConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) { SXUNUSED(pCtx); SXUNUSED(nArg); SXUNUSED(apArg); return PH7_ABORT; /* final class: no public construction */ } static sxi32 CharRegisterClasses(ph7_vm *pVm) { static ph7_constant_def aExcConst[] = { { "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "ERROR_ARGUMENT", 0, PH7_MEMBER_PUBLIC, 0, 0 } }; static const int aExcValue[] = { CHAR_ERROR_NONE, CHAR_ERROR_ARGUMENT }; static ph7_constant_def aCharConst[] = { { "MIN_VALUE", 0, PH7_MEMBER_PUBLIC, 0, 0 }, { "MAX_VALUE", 0, PH7_MEMBER_PUBLIC, 0, 0 } }; static ph7_param_def aExcCtor[] = { { "message", CTY(STRING), 0, 0, 0, 0 }, { "code", CTY(INT), 0, 0, 0, 0 } }; static const ph7_param_def aExcDesc[] = { { "code", CTY(INT), 0, 0, 0, 0 } }; #define SMV(NAME,RET,FUNC) \ { NAME, CTY(RET), 0, 0, FUNC, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC | PH7_MEMBER_VARIADIC } static const ph7_method_def aExcMethod[] = { { "__construct", CTY(VOID), aExcCtor, 2, CharExceptionConstruct, 0, 0 }, { "GetDescription", CTY(STRING), aExcDesc, 1, CharExceptionDescription, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC } }; static const ph7_method_def aCharMethod[] = { { "__construct", CTY(VOID), 0, 0, CharConstruct, 0, PH7_MEMBER_PRIVATE }, SMV("IsControl", BOOL, CharIsControl), SMV("IsDigit", BOOL, CharIsDigit), SMV("IsNumber", BOOL, CharIsNumber), SMV("IsLetter", BOOL, CharIsLetter), SMV("IsLetterOrDigit", BOOL, CharIsLetterOrDigit), SMV("IsLower", BOOL, CharIsLower), SMV("IsUpper", BOOL, CharIsUpper), SMV("IsWhiteSpace", BOOL, CharIsWhiteSpace), SMV("IsSeparator", BOOL, CharIsSeparator), SMV("IsPunctuation", BOOL, CharIsPunctuation), SMV("IsSymbol", BOOL, CharIsSymbol), SMV("IsGraphical", BOOL, CharIsGraphical), SMV("IsPrintable", BOOL, CharIsPrintable), SMV("IsAscii", BOOL, CharIsAscii), SMV("IsAsciiDigit", BOOL, CharIsAsciiDigit), SMV("IsAsciiLetter", BOOL, CharIsAsciiLetter), SMV("IsAsciiLetterLower", BOOL, CharIsAsciiLetterLower), SMV("IsAsciiLetterUpper", BOOL, CharIsAsciiLetterUpper), SMV("IsAsciiLetterOrDigit", BOOL, CharIsAsciiLetterOrDigit), SMV("IsAsciiHexDigit", BOOL, CharIsAsciiHexDigit), SMV("IsAsciiHexDigitLower", BOOL, CharIsAsciiHexDigitLower), SMV("IsAsciiHexDigitUpper", BOOL, CharIsAsciiHexDigitUpper), SMV("IsBetween", BOOL, CharIsBetween), SMV("ToUpper", CHAR, CharToUpper), SMV("ToLower", CHAR, CharToLower), SMV("GetNumericValue", FLOAT, CharGetNumericValue), SMV("ToString", STRING, CharToString), SMV("Parse", CHAR, CharParse) }; #undef SMV static const char *const aExcBase[] = { "Exception" }; ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, CCLS_EXC, 0, 0, aExcConst, SX_ARRAYSIZE(aExcConst), aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 }; ph7_class_def sChar = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, CCLS_CHAR, 0, 0, aCharConst, SX_ARRAYSIZE(aCharConst), aCharMethod, SX_ARRAYSIZE(aCharMethod), PH7_DECL_FINAL | PH7_DECL_VIRTUAL, 0, 0, 0, 0, 0, 0 }; ph7_value *apExc[SX_ARRAYSIZE(aExcConst)]; ph7_value *apChar[SX_ARRAYSIZE(aCharConst)]; ph7_value *apDefault = 0; sxu32 n; sxi32 rc = PH7_OK; SyZero(apExc, sizeof(apExc)); SyZero(apChar, sizeof(apChar)); for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) { apExc[n] = ph7_new_scalar(pVm); if(apExc[n] == 0) { rc = PH7_NOMEM; goto Cleanup; } ph7_value_int(apExc[n], aExcValue[n]); aExcConst[n].pValue = apExc[n]; } for(n = 0 ; n < SX_ARRAYSIZE(apChar) ; ++n) { apChar[n] = ph7_new_scalar(pVm); if(apChar[n] == 0) { rc = PH7_NOMEM; goto Cleanup; } (void)ph7_value_char(apChar[n], n == 0 ? 0 : 255); aCharConst[n].pValue = apChar[n]; } /* CharException("message") gets ERROR_NONE from this snapshot. */ apDefault = ph7_new_scalar(pVm); if(apDefault == 0) { rc = PH7_NOMEM; goto Cleanup; } ph7_value_int(apDefault, CHAR_ERROR_NONE); aExcCtor[1].pDefault = apDefault; rc = ph7_create_class(pVm, &sExc); if(rc == PH7_OK) { rc = ph7_create_class(pVm, &sChar); } Cleanup: aExcCtor[1].pDefault = 0; if(apDefault) { ph7_release_value(pVm, apDefault); } for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) { if(apExc[n]) { ph7_release_value(pVm, apExc[n]); aExcConst[n].pValue = 0; } } for(n = 0 ; n < SX_ARRAYSIZE(apChar) ; ++n) { if(apChar[n]) { ph7_release_value(pVm, apChar[n]); aCharConst[n].pValue = 0; } } return rc; } PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) { desc->zString = MODULE_DESC; *ver = MODULE_VER; return CharRegisterClasses(pVm); }