Created
August 24, 2016 21:01
-
-
Save mattpodwysocki/63b8f3125348fd606326b9dd6189855f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JsErrorCode Stringify(JsValueRef value, ChakraHost* self) | |
{ | |
JsValueType type; | |
IfFailRet(JsGetValueType(value, &type)); | |
switch (type) | |
{ | |
case JsUndefined: | |
OutputDebugStringW(L"undefined"); | |
break; | |
case JsNull: | |
OutputDebugStringW(L"null"); | |
break; | |
case JsBoolean: | |
bool bResult; | |
IfFailRet(JsBooleanToBool(value, &bResult)); | |
OutputDebugStringW(bResult ? L"true" : L"false"); | |
break; | |
case JsString: | |
const wchar_t* szResult; | |
size_t sResult; | |
IfFailRet(JsStringToPointer(value, &szResult, &sResult)); | |
OutputDebugStringW(szResult); | |
break; | |
case JsObject: | |
JsValueRef props; | |
IfFailRet(JsGetOwnPropertyNames(value, &props)); | |
JsPropertyIdRef lengthId; | |
JsValueRef lengthProp; | |
int lengthValue; | |
IfFailRet(JsGetPropertyIdFromName(L"length", &lengthId)); | |
IfFailRet(JsGetProperty(props, lengthId, &lengthProp)); | |
IfFailRet(JsNumberToInt(lengthProp, &lengthValue)); | |
OutputDebugStringW(L"{ "); | |
for (int i = 0; i < lengthValue; i++) | |
{ | |
JsPropertyIdRef propId; | |
JsValueRef index, indexResult, prop; | |
const wchar_t* szProp; | |
size_t sProp; | |
IfFailRet(JsIntToNumber(i, &index)); | |
IfFailRet(JsGetIndexedProperty(props, index, &indexResult)); | |
IfFailRet(JsStringToPointer(indexResult, &szProp, &sProp)); | |
IfFailRet(JsGetPropertyIdFromName(szProp, &propId)); | |
IfFailRet(JsGetProperty(value, propId, &prop)); | |
OutputDebugStringW(szProp); | |
OutputDebugStringW(L": "); | |
IfFailRet(Stringify(prop, self)); | |
OutputDebugStringW(L" "); | |
} | |
OutputDebugStringW(L"}"); | |
break; | |
case JsNumber: | |
case JsArray: | |
case JsTypedArray: | |
JsValueRef resultJSON; | |
const wchar_t* szJson; | |
size_t sJson; | |
IfFailRet(self->JsonStringify(value, &resultJSON)); | |
IfFailRet(JsStringToPointer(resultJSON, &szJson, &sJson)); | |
OutputDebugStringW(szJson); | |
break; | |
case JsFunction: | |
case JsError: | |
case JsSymbol: | |
case JsArrayBuffer: | |
JsValueRef resultString; | |
const wchar_t* szStr; | |
size_t sStr; | |
IfFailRet(JsConvertValueToString(value, &resultString)); | |
IfFailRet(JsStringToPointer(resultString, &szStr, &sStr)); | |
OutputDebugStringW(szStr); | |
break; | |
} | |
return JsNoError; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment