| 798 | } |
| 799 | |
| 800 | void fxStringifyJSON(txMachine* the, txJSONStringifier* theStringifier) |
| 801 | { |
| 802 | txSlot* aSlot; |
| 803 | txInteger aFlag; |
| 804 | txSlot* instance; |
| 805 | |
| 806 | aSlot = fxGetInstance(the, mxThis); |
| 807 | theStringifier->offset = 0; |
| 808 | theStringifier->size = 1024; |
| 809 | theStringifier->buffer = c_malloc(1024); |
| 810 | if (!theStringifier->buffer) |
| 811 | fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT); |
| 812 | |
| 813 | if (mxArgc > 1) { |
| 814 | aSlot = mxArgv(1); |
| 815 | if (mxIsReference(aSlot)) { |
| 816 | if (fxIsCallable(the, aSlot)) |
| 817 | theStringifier->replacer = mxArgv(1); |
| 818 | else if (fxIsArray(the, fxGetInstance(the, aSlot))) |
| 819 | theStringifier->keys = fxToJSONKeys(the, aSlot); |
| 820 | } |
| 821 | } |
| 822 | if (mxArgc > 2) { |
| 823 | aSlot = mxArgv(2); |
| 824 | if (mxIsReference(aSlot)) { |
| 825 | txSlot* instance = fxGetInstance(the, aSlot); |
| 826 | if (mxIsNumber(instance)) { |
| 827 | fxToNumber(the, aSlot); |
| 828 | } |
| 829 | else if (mxIsString(instance)) { |
| 830 | fxToString(the, aSlot); |
| 831 | } |
| 832 | } |
| 833 | if ((aSlot->kind == XS_INTEGER_KIND) || (aSlot->kind == XS_NUMBER_KIND)) { |
| 834 | txInteger aCount = fxToInteger(the, aSlot), anIndex; |
| 835 | if (aCount < 0) |
| 836 | aCount = 0; |
| 837 | else if (aCount > 10) |
| 838 | aCount = 10; |
| 839 | for (anIndex = 0; anIndex < aCount; anIndex++) |
| 840 | theStringifier->indent[anIndex] = ' '; |
| 841 | theStringifier->indentLength = aCount; |
| 842 | } |
| 843 | else if (mxIsStringPrimitive(aSlot)) { |
| 844 | txInteger aCount = fxUnicodeLength(aSlot->value.string, C_NULL); |
| 845 | if (aCount > 10) { |
| 846 | aCount = fxUnicodeToUTF8Offset(aSlot->value.string, 10); |
| 847 | } |
| 848 | else { |
| 849 | aCount = (txInteger)c_strlen(aSlot->value.string); |
| 850 | } |
| 851 | c_memcpy(theStringifier->indent, aSlot->value.string, aCount); |
| 852 | theStringifier->indent[aCount] = 0; |
| 853 | theStringifier->indentLength = aCount; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | theStringifier->stack = the->stack; |
no test coverage detected