Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created August 24, 2016 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/63b8f3125348fd606326b9dd6189855f to your computer and use it in GitHub Desktop.
Save mattpodwysocki/63b8f3125348fd606326b9dd6189855f to your computer and use it in GitHub Desktop.
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