MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / fxStringifyJSON

Function fxStringifyJSON

xs/sources/xsJSON.c:800–869  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

798}
799
800void 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;

Callers 1

fx_JSON_stringifyFunction · 0.85

Calls 13

fxGetInstanceFunction · 0.85
fxIsCallableFunction · 0.85
fxIsArrayFunction · 0.85
fxToJSONKeysFunction · 0.85
fxToNumberFunction · 0.85
fxToStringFunction · 0.85
fxToIntegerFunction · 0.85
fxUnicodeLengthFunction · 0.85
fxUnicodeToUTF8OffsetFunction · 0.85
fxNewObjectInstanceFunction · 0.85
fxNextSlotPropertyFunction · 0.85
fxStringifyJSONPropertyFunction · 0.85

Tested by

no test coverage detected