JsErrorCode StringifyJsObject(JsValueRef value, USHORT depth, std::set<JsValueRef> seen) { std::pair<std::set<JsValueRef>::iterator, bool> ret; ret = seen.insert(value); if (!ret.second) { OutputDebugStringW(DEFAULT_RECURSIVE_VALUE); return JsNoError; } if (depth > DEFAULT_MAX_DEPTH) { OutputDebugStringW(DEFAULT_TRUNCATED_VALUE); return JsNoError; } 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(StringifyJsValue(prop, depth + 1, seen)); if (i != lengthValue - 1) { OutputDebugStringW(L", "); } else { OutputDebugStringW(L" "); } } OutputDebugStringW(L"}"); return JsNoError; }
JsErrorCode CreateHostContext(JsRuntimeHandle runtime, int argc, wchar_t *argv [], int argumentsStart, JsContextRef *context) { JsCreateContext(runtime, context); JsSetCurrentContext(*context); JsValueRef hostObject; JsCreateObject(&hostObject); ghostObject = hostObject; JsValueRef globalObject; JsGetGlobalObject(&globalObject); JsPropertyIdRef hostPropertyId; JsGetPropertyIdFromName(L"app", &hostPropertyId); // app.XXXXX JsSetProperty(globalObject, hostPropertyId, hostObject, true); //IfFailRet(DefineHostCallback(hostObject, L"msgbox", msgbox, nullptr)); JsSetCurrentContext(JS_INVALID_REFERENCE); return JsNoError; }
JsErrorCode ChakraHost::SetGlobalVariable(const wchar_t* szPropertyName, JsValueRef value) { JsPropertyIdRef globalVarId; IfFailRet(JsGetPropertyIdFromName(szPropertyName, &globalVarId)); IfFailRet(JsSetProperty(globalObject, globalVarId, value, true)); return JsNoError; }
JsErrorCode ChakraHost::InitJson() { JsPropertyIdRef jsonPropertyId; IfFailRet(JsGetPropertyIdFromName(L"JSON", &jsonPropertyId)); JsValueRef jsonObject; IfFailRet(JsGetProperty(globalObject, jsonPropertyId, &jsonObject)); JsPropertyIdRef jsonParseId; IfFailRet(JsGetPropertyIdFromName(L"parse", &jsonParseId)); IfFailRet(JsGetProperty(jsonObject, jsonParseId, &jsonParseObject)); JsPropertyIdRef jsonStringifyId; IfFailRet(JsGetPropertyIdFromName(L"stringify", &jsonStringifyId)); IfFailRet(JsGetProperty(jsonObject, jsonStringifyId, &jsonStringifyObject)); return JsNoError; }
JsErrorCode DefineHostCallback(JsValueRef globalObject, const wchar_t *callbackName, JsNativeFunction callback, void *callbackState) { JsPropertyIdRef propertyId; IfFailRet(JsGetPropertyIdFromName(callbackName, &propertyId)); JsValueRef function; IfFailRet(JsCreateFunction(callback, callbackState, &function)); IfFailRet(JsSetProperty(globalObject, propertyId, function, true)); return JsNoError; }
JsErrorCode StringifyJsArray(JsValueRef value, USHORT depth, std::set<JsValueRef> seen) { std::pair<std::set<JsValueRef>::iterator, bool> ret; ret = seen.insert(value); if (!ret.second) { OutputDebugStringW(DEFAULT_RECURSIVE_VALUE); return JsNoError; } if (depth > DEFAULT_MAX_DEPTH) { OutputDebugStringW(DEFAULT_TRUNCATED_VALUE); return JsNoError; } JsPropertyIdRef lengthId; JsValueRef lengthProp; int lengthValue; IfFailRet(JsGetPropertyIdFromName(L"length", &lengthId)); IfFailRet(JsGetProperty(value, lengthId, &lengthProp)); IfFailRet(JsNumberToInt(lengthProp, &lengthValue)); OutputDebugStringW(L"[ "); for (int i = 0; i < lengthValue; i++) { if (i == DEFAULT_ARRAY_MAX_LENGTH) { OutputDebugStringW(DEFAULT_TRUNCATED_VALUE); break; } JsValueRef index, indexResult; IfFailRet(JsIntToNumber(i, &index)); IfFailRet(JsGetIndexedProperty(value, index, &indexResult)); IfFailRet(StringifyJsValue(indexResult, depth, seen)); if (i != lengthValue - 1) { OutputDebugStringW(L", "); } else { OutputDebugStringW(L" "); } } OutputDebugStringW(L"]"); return JsNoError; }
JsErrorCode ChakraHost::InitConsole() { JsPropertyIdRef consolePropertyId; IfFailRet(JsGetPropertyIdFromName(L"console", &consolePropertyId)); JsValueRef consoleObject; IfFailRet(JsCreateObject(&consoleObject)); IfFailRet(JsSetProperty(globalObject, consolePropertyId, consoleObject, true)); IfFailRet(DefineHostCallback(consoleObject, L"info", ConsoleInfo, nullptr)); IfFailRet(DefineHostCallback(consoleObject, L"log", ConsoleLog, nullptr)); IfFailRet(DefineHostCallback(consoleObject, L"warn", ConsoleWarn, nullptr)); IfFailRet(DefineHostCallback(consoleObject, L"error", ConsoleError, nullptr)); return JsNoError; }
JsErrorCode StringifyJsFunction(JsValueRef value) { JsPropertyIdRef nameId; JsValueRef nameObj; const wchar_t* szName; size_t sName; IfFailRet(JsGetPropertyIdFromName(L"name", &nameId)); IfFailRet(JsGetProperty(value, nameId, &nameObj)); IfFailRet(JsStringToPointer(nameObj, &szName, &sName)); OutputDebugStringW(L"[Function"); if (sName > 0) { wchar_t* szFn = NULL; if (_aswprintf(&szFn, L": %s", szName) < 0) { return JsErrorFatal; } OutputDebugStringW(szFn); free(szFn); } OutputDebugStringW(L"]"); return JsNoError; }
BC_PLATFORMIMP_DLL core::bc_wstring bc_platform_script_error<core_platform::g_api_win32>::error_message() const { bc_chakra_call l_call; JsValueRef l_message_property; JsPropertyIdRef l_message_id; JsValueType l_message_type; l_call = JsGetPropertyIdFromName(L"message", &l_message_id); l_call = JsGetProperty(m_pack.m_js_error, l_message_id, &l_message_property); if(!l_call.successed()) { l_call.clear(); return L""; } JsGetValueType(l_message_property, &l_message_type); if(l_message_type != JsString) { return L""; } bcSIZE l_str_lenght; const bcWCHAR* l_str_data; l_call = JsStringToPointer(l_message_property, &l_str_data, &l_str_lenght); if(!l_call.successed()) { l_call.clear(); return L""; } return core::bc_wstring(l_str_data); }
JsErrorCode StringifyJsTypedArray(JsValueRef value) { JsTypedArrayType arrayType; JsValueRef arrayBuffer; UINT byteOffset, byteLength; IfFailRet(JsGetTypedArrayInfo(value, &arrayType, &arrayBuffer, &byteOffset, &byteLength)); switch (arrayType) { case JsArrayTypeInt8: OutputDebugStringW(L"Int8Array "); break; case JsArrayTypeUint8: OutputDebugStringW(L"Uint8Array "); break; case JsArrayTypeUint8Clamped: OutputDebugStringW(L"Uint8ClampedArray "); break; case JsArrayTypeInt16: OutputDebugStringW(L"Int16Array "); break; case JsArrayTypeUint16: OutputDebugStringW(L"Uint16Array "); break; case JsArrayTypeInt32: OutputDebugStringW(L"Int32Array "); break; case JsArrayTypeUint32: OutputDebugStringW(L"Uint32Array "); break; case JsArrayTypeFloat32: OutputDebugStringW(L"Float32Array "); break; case JsArrayTypeFloat64: OutputDebugStringW(L"Float64Array "); break; } JsPropertyIdRef lengthId; JsValueRef lengthProp; int lengthValue; IfFailRet(JsGetPropertyIdFromName(L"length", &lengthId)); IfFailRet(JsGetProperty(value, lengthId, &lengthProp)); IfFailRet(JsNumberToInt(lengthProp, &lengthValue)); OutputDebugStringW(L"[ "); for (int i = 0; i < lengthValue; i++) { if (i == DEFAULT_ARRAY_MAX_LENGTH) { OutputDebugStringW(DEFAULT_TRUNCATED_VALUE); break; } JsValueRef index, indexResult; IfFailRet(JsIntToNumber(i, &index)); IfFailRet(JsGetIndexedProperty(value, index, &indexResult)); IfFailRet(StringifyToString(indexResult)); if (i != lengthValue - 1) { OutputDebugStringW(L", "); } else { OutputDebugStringW(L" "); } } OutputDebugStringW(L"]"); return JsNoError; }